iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 5
1
Kubernetes

15 分鐘學習系列 - 第一次學 Kubernetes 就上手系列 第 5

Day 5 - 部署應用程式到 Kubernetes 叢集 - Part II - 使用配置檔案部署應用程式

前一篇筆記中介紹了使用命令部署應用程式到 Kubernetes 叢集中, 這篇筆記中, 將介紹使用配置檔案部署的方式.

Kubernetes 的配置檔有兩種格式, YAML 和 JSON 格式, 一般都慣用 YAML 語言來撰寫部署. 開始之前, 可能需要先簡單了解一下怎麼解讀 YAML 檔案內容, 有興趣可以參考一些網路上的文件 Introduction to YAML: Creating a Kubernetes deployment

YAML 基本上是一個一個的物件描述, 很適合用來宣告. 因此在 Kubernetes 的配置上很直覺方便. 以下是延續上一篇筆記的部署, 將應用程式的部署改為配置檔, 未來可以跟 CI/CD 與 DevOps 一起應用, 便可以體會到 Kubernetes 的強大管理能力了.

使用配置檔案部署應用程式

  1. 最初開始不太會寫 YMAL 的時候, 有一個既方便又偷懶的方式可以幫助我們寫出正確的 YAML 檔案.
    方法就是觀摩別人系統怎麼寫.
    我們可以用 kubectl get deployment aspnetapp-interactive-delopyment -o yaml 命令將正確的配置輸出, 以下是前一篇筆記用命令方式配置的應用程式, 我們可以將 deployment 的部分先行輸出, 再進行修改成簡化的版本即可.
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871NZCx1Gmx21.png
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: 2018-10-10T06:58:42Z
  generation: 1
  labels:
    run: aspnetapp-interactive-delopyment
  name: aspnetapp-interactive-delopyment
  namespace: default
  resourceVersion: "10577"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/aspnetapp-interactive-delopyment
  uid: ec2da991-cc59-11e8-9cba-00155d541009
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: aspnetapp-interactive-delopyment
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: aspnetapp-interactive-delopyment
    spec:
      containers:
      - image: aspnetapp:local
        imagePullPolicy: IfNotPresent
        name: aspnetapp-interactive-delopyment
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: 2018-10-10T06:58:46Z
    lastUpdateTime: 2018-10-10T06:58:46Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: 2018-10-10T06:58:42Z
    lastUpdateTime: 2018-10-10T06:58:46Z
    message: ReplicaSet "aspnetapp-interactive-delopyment-dbdd784f9" has successfully
      progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 3
  replicas: 3
  updatedReplicas: 3
  1. 除了 deployment 之外, 也可以使用命令 kubectl get svc aspnetapp-interactive-delopyment -o yaml 將 Service 配置輸出成 YMAL 格式
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2018-10-10T07:38:18Z
  labels:
    run: aspnetapp-interactive-delopyment
  name: aspnetapp-interactive-delopyment
  namespace: default
  resourceVersion: "13182"
  selfLink: /api/v1/namespaces/default/services/aspnetapp-interactive-delopyment
  uid: 74b57fea-cc5f-11e8-9cba-00155d541009
spec:
  clusterIP: 10.96.168.58
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30023
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: aspnetapp-interactive-delopyment
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer:
    ingress:
    - hostname: localhost
  1. 接下來我們在 application 專案目錄下, 用文字編輯器產生一個 YMAL 檔名的 deployment 配置檔
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871XGBotYwLuQ.png
  2. **aspnetapp-deployment.yml **檔案將主要的 deployment 與 service 宣告檔案內容如下:
    其中 Deployment 重點為 demployment name, replicas 數量, image 名稱, Service 重點為 service name, listen tcp port 80 以及 服務 type 為 NodePort
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetapp-deployment
  labels:
    app: aspnetapp
spec:
  replicas: 5
  template:
    metadata:
      name: aspnetapp
      labels:
        app: aspnetapp
    spec:
      containers:
      - name: aspnetapp
        `image: aspnetapp:local`
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: aspnetapp


---

apiVersion: v1
kind: Service
metadata:
  name: aspnetapp-service
spec:
  selector:
    app: aspnetapp
  ports:
    - port: 80
  type: NodePort
  1. 宣告式檔案完成後, 可以用以下的命令將應用程式配置到 Kubernetes 叢集上
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871gNcmg1fPri.png
  2. 查看 deployment 狀況
    Kubectl get deployments
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871Gkh0wm4kN0.png
  3. 查看 service 狀況
    Kubectl get service
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871FQ9pE0tNVN.png
  4. 查看 pod 狀況
    Kubectl get pods
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871QVzdbanQsC.png
  5. 修改 aspnetapp-deployment.yml 檔案, 將 replicas 修改成 2, 使用 kubectl apply -f .\aspnetapp-deployment.yml 檔案套用
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetapp-deployment
  labels:
    app: aspnetapp
spec:
  replicas: 2
  template:
    metadata:
      name: aspnetapp
      labels:
        app: aspnetapp
    spec:
      containers:
      - name: aspnetapp
        image: aspnetapp:local
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: aspnetapp


---

apiVersion: v1
kind: Service
metadata:
  name: aspnetapp-service
spec:
  selector:
    app: aspnetapp
  ports:
    - port: 80
  type: NodePort
  1. 再次查看 deployment 狀況
    Kubectl get deployments
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871zrTs9iJJ6D.png
  2. 再次查看 pod 狀況
    Kubectl get pods
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871bKail10RaK.png
  3. 開啟 Browser 連線到 http://localhost:32716 測試結果
    https://ithelp.ithome.com.tw/upload/images/20181017/20111871bu9leDzgJp.png

參考資料:


上一篇
Day 4 - 部署應用程式到 Kubernetes 叢集 - Part I - 手動建立 deployment 與 Service
下一篇
Day 6 - 淺談 Azure Kubernetes Service, Azure Container Registry, Azure Container Instance
系列文
15 分鐘學習系列 - 第一次學 Kubernetes 就上手30

1 則留言

0
ttn
iT邦新手 5 級 ‧ 2019-02-03 13:56:04

在第4步驟的 yml 設定檔中要把以下這句的 ` 拿掉,否則會出現 error parsing

`image: aspnetapp:local`

我要留言

立即登入留言