分享會大綱

  1. 實作前準備
  2. Pod 設定
  3. Secret 以及 ConfigMap 掛載
  4. Replication controller 設定
  5. Deployment 設定
  6. Service 設定
  7. Ingress 設定
  8. Ingress controller 設定
  9. Health check 設定
  10. Cronjob 設定

實作前準備

這邊教大家最簡單且方便的架設方式,安裝 Google 在 vscode 上開發的 cloud code

之後再安裝 skaffold

Pod 設定

  • apiVersion

    目前 k8s 中該元件的版本號,必須要依照 server 上 k8s 的版本來進行設定。

  • metadata

    用來擺放描述性資料的地方

  • kind

    用來決定此設定檔的類型

  • spec

    用來描述物件被生成的細節

apiVersion 的分法

  • 1.6之前:extensions/v1beta1
  • 1.6 ~ 1.9:apps/v1beta1
  • 1.9以後:apps/v1

spec 的細部寫法

  • spec.container.name

    container 名稱

  • spec.container.image

    container 的 image

  • spec.container.port

    container 的對外 port

              
                apiVersion: apps/v1
                kind: Pod
                metadata:
                  name: my-pod
                  labels:
                    app: my-Pod
                spec:
                  containers:
                  - name: my-Pod
                    image: demoImage
                    ports:
                    - containerPort: 3000
              
            

Secret 與 ConfigMap 掛載

這邊要用到 volumes 的觀念來進行掛載

  • containers.volumeMounts.name

    要掛載在 container 中的名稱

  • containers.volumeMounts.mountPath

    選擇要掛載在 container 中的哪個 path 上

  • containers.volumeMounts.subpath

    有時候我們要掛載的目錄中可能包含了多個子目錄,而這些子目錄恰巧又分別被多個不同的 container 使用,此時就可以透過 subPath 的方式來簡化 volume 的設定

  • spec.volumes.name

    從外部掛載進來的名稱

              
                spec:
                  containers:
                  - name: my-Pod
                    image: demoImage
                    ports:
                    - containerPort: 3000
                    volumeMounts:
                    - name: nginx-secret
                      mountPath: /etc/nginx/ssl
                    - name: pod-config
                      mountPath: /etc/nginx/conf.d
                      readOnly: true
                  volumes:
                    - name: pod-config
                      configMap:
                        name: pod-config
                    - name: nginx-secret
                      secret:
                        secretName: nginx-secret
              
            

Replication controller 設定

  • spec.replicas

    設定 Pod 的數量

  • spec.selector

    選擇 Pod 的條件

  • spec.template

    定義 Pod 的資訊,上述提到的 Pod 寫法都會擺在這

  • spec.template.metadata

    定義 Pod 的描述資訊

  • spec.template.spec

    定義 Pod 內的 container 資訊

              
                apiVersion: v1
                kind: ReplicationController
                metadata:
                  name: demoReplicationController
                spec:
                  replicas: 2
                  selector:
                    app: my-Pod
                  template:
                    metadata:
                      labels:
                        app: my-Pod
                    spec:
                      containers:
                      - name: my-pod
                        image: demoImage
                        ports:
                        - containerPort: 3000
              
            

Deployment 設定

strategy

k8s 為了確保在 rollout(滾動更新) 時可以按照想要的方式進行更新,只要是 Deployment 在 spec 中都會有 strategy 的設定值

  • strategy.type

    設定 rollout type,有 Recreate 以及 RollingUpdate 兩種,default 為 RollingUpdate

  • strategy.rollingUpdate.maxSurge

    在 rollout 的過程中最多可以比原本設定的 Pod 數量多出多少

  • strategy.rollingUpdate.maxUnavailable

    在 rollout 的過程中,可以允許多少個 Pod 無法使用,假如 MaxSurge 設定非 0,maxUnavailable 也不能設定非 0

              
                apiVersion: apps/v1
                kind: Deployment
                metadata:
                  name: demoDeployment
                spec:
                  replicas: 2
                  strategy:
                    type: RollingUpdate
                    rollingUpdate:
                      maxSurge: 1
                      maxUnavailable: 1
                  selector:
                    matchLabels:
                      app: my-deployment
                  template:
                    # . . . pod settings
              
            

Service 設定

  • spec.ports.port

    指定 service 建立完的 clusterIP 中哪個 port 要對應到 targetPort

  • spec.ports.nodePort

    可以指定 Node 中哪個 Port 要對應到 targetPort ,沒設定的話 k8s 會隨機挑選一個 Port

  • spec.ports.targetPort

    指定要相對應 Pod 的 Port,targetPort 會跟 port 相同

  • spec.ports.protocol

    使用 TCP 或 UDP, default 是 TCP

              
                apiVersion: v1
                kind: Service
                metadata:
                  name: my-service
                  labels:
                    app: my-service
                spec:
                  ports:
                    - protocol: TCP
                      port: 3000
                      targetPort: 3000
                  selector:
                    app: my-Pod
              
            

Ingress 設定

  • spec.tls.secretName

    要提供給網站用的 tls secret 名稱

  • spec.rules.host

    網站的 domain name

  • spec.rules.http.paths.path

    經由哪個 path 來連接到 service

  • spec.rules.http.paths.backend.serviceName

    欲連接到的 service 名稱

  • spec.rules.http.paths.backend.servicePort

    欲連接到的 service 對外port

                
                  apiVersion: extensions/v1beta1
                  kind: Ingress
                  metadata:
                    name: my-ingress
                  spec:
                    tls:
                      - secretName: nginx-secret
                    rules:
                      - host: mysite.com.tw
                        http:
                          paths:
                            - path: /
                              backend:
                                serviceName: my-service
                                servicePort: 3000
                
              

Ingress controller 設定

點擊 ingress-nginx 中 deploy 頁面進行初始化安裝

這邊有一個一定要安裝的描述檔,不論是架在本地端或是雲端都需要

使用 ingress-nginx 好處

ingress-nginx 會自動幫我們建立好的 ingress 進行統整的動作

統整過後的 ingress 都會擺在 nginx.conf 內

kubectl exec -n ingress-nginx -ti podName -- /bin/sh

Health check 設定

由於 Health check 是要檢查 Pod 的健康狀態,因此 Health check 會寫在 Pod 或 Deployment 的設定檔內

livenessProbe 與 readinessProbe 的設定方式一樣

  • livenessProbe.httpGet.path

    設定 health check 要造訪的路徑

  • livenessProbe.httpGet.scheme

    設定要造訪的 scheme,預設為 HTTP 也可設定為 HTTPS

  • livenessProbe.httpGet.port

    設定要造訪的 port

  • livenessProbe.initialDelaySeconds

    設定 service 剛啟動時要間隔多久再啟動 health check

  • livenessProbe.periodSeconds

    每多久訪問一次,預設為 10s

  • livenessProbe.successThreshold

    設定訪問幾次而且都成功就代表 service 成功運行,預設為 1 次

  • livenessProbe.failureThreshold

    代表 service 回傳不如預期時,在 kubelet 放棄此 container 之前會在嘗試的次數,預設為3次

              
                # . . . container settings
                livenessProbe:
                  httpGet:
                    path: /healthz
                    scheme: HTTPS
                    port: 8080
                  initialDelaySeconds: 3
                  periodSeconds: 60
                  successThreshold: 3
                  failureThreshold: 5
                readinessProbe:
                  httpGet:
                    path: /healthz
                    scheme: HTTPS
                    port: 8080
                  initialDelaySeconds: 3
                  periodSeconds: 60
                  successThreshold: 3
                  failureThreshold: 5
                # . . . container settings
              
            

Cronjob 設定

              
                apiVersion: batch/v1beta1
                kind: Cronjob
                spec:
                  schedules: 需輸入符合 cron 格式的字串
                  jobTemplate:
                    spec:
                      template:
                         spec:
                            containers:
                              - name: cornjob 名稱
                                image: 排程映像檔
                                command: [‘node’,’index.js’]  -> node index.js  
                            restartPolicy: 重啟設定(always, onFailure, Never)
              
            

謝謝大家