iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
0

本文章同時發佈於:

文章為自己的經驗與夥伴整理的內容,設計沒有標準答案,如有可以改進的地方,請告訴我,我會盡我所能的修改,謝謝大家~

大家好,今天要介紹 K8s 到底有哪些 config yaml 檔,他們又是何種用途。

K8s 的整體架構

圖片來源: Kubernetes, At A First Glance

K8s 有非常多的元件來建構豐富多樣的架構,但我們可已由高至低了解以下 4 個不同層級的重要元件,會比較好理解:

  • Cluster: 管理多個 Master, Worker Node,可以理解為多個 VM 如何變為一個「大 VM」的方式
  • Master Node: 實際的 VM,K8s 會由一個 Master Node 去管理底下多個 Worker Node
  • Worker Node: 實際的 VM,裡頭管理著一個一個 Pod
  • Pod: Pod 裡放著容器,容器通常為一個,但也可以多個

Pod、Deployment、Service yaml config 介紹

Pod 可以理解為一個或多個 docker 容器怎麼啟動在 K8s 中,而在 kompose 轉換中沒有出現此 config 檔案,為什麼呢?

因為 Pod 之上有 Deployment,他除了描述 Pod 的行為外,還會描述Pod在K8s上狀態發生變化時要對應的機制

而關於如何連線存取這些Pod,就要透過 Service 來設定


Service 與 Deployment 中有些 config 使用的欄位相同,分別是:

  • apiVersion: 為描述此元件使用的 K8s API 的版本
  • kind: 說明此元件屬於何種類型,有 Service、Deployment 等等
  • metadata: 說明此元件的相關資訊
  • metadata.labels: 可以將此元件分類在特定 label
  • metadata.annotations: 說明不需要分類的相關資訊
  • metadata.creationTimestamp: 此元件被創立的時間
  • metadata.name: 此元件的名稱

Deployment 的欄位介紹:

# server-service.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: server
  name: server
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: server
    spec:
      containers:
        - command:
            - go
            - run
            - cmd/main.go
          image: superj80820/digimon-service
          name: server
          ports:
            - containerPort: 6000
          resources: {}
      restartPolicy: Always
status: {}
  • spec.replicas: 此 Pod 會在 K8s 有幾個橫向擴展(Horizontal Pod Autoscaler),目前設定一個
  • spec.strategy: 可以設定狀態變化對應機制的策略,例如 image 降版要要維持幾個 Pod 之類,這邊使用預設值
  • spec.template.spec.containers: 設置容器
  • spec.template.spec.containers.command: 容器的啟動 command
  • spec.template.spec.containers.image: 容器使用的 image
  • spec.template.spec.containers.ports: 容器使用的 port
  • spec.template.spec.containers.restartPolicy: 容器是否無預期關閉後要重新啟動

Service 的欄位介紹:

# server-service.yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.16.0 (0c01309)
  creationTimestamp: null
  labels:
    io.kompose.service: server
  name: server
spec:
  ports:
    - name: "6000"
      port: 6000
      targetPort: 6000
  selector:
    io.kompose.service: server
status:
  loadBalancer: {}
  • spec.ports.ports: 說明對外可連入的 port 為何
  • spec.ports.targetPort: 說明對外連入的 port 對應到 Pod 的哪個 port
  • spec.ports.selector: 此規則要套用到哪個 label 上

參考


上一篇
DAY17 - Kompose!快速學習 Kubernetes 不是夢
下一篇
DAY19 - 了解 K8s 中的大門神!Ingress
系列文
被選召的 Gopher 們,從零開始探索 Golang, Istio, K8s 數碼微服務世界30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言