K8s YAML 設定

範例 (ReplicationController)

apiVersion: v1 kind: ReplicationController metadata: name: my-replication-controller spec: replicas: 3 selector: app: hello-pod-v1 template: metadata: labels: app: hello-pod-v1 spec: containers: - name: my-pod image: zxcvbnius/docker-demo ports: - containerPort: 3000

apiVersion

代表目前 Kubernetes 中該元件的版本號
apiVersion: v1

Kind

在這裡可以表明是哪個元件的設定檔
例:ReplicationController / Pod / Service
kind: ReplicationController

metadata

  • name
    • 指定這個元件的名稱,這裡的名稱就是 my-replication-controller
  • labels
    • Kubernetes 會透過 Label Selector 將Pod分群管理(功用是什麼、層級是什麼、服務的對象是哪些部門的人)
    • 可以透過 Selector,幫我們縮小要尋找的物件。
  • annotations
    • 類似 label
    • 通常是使用者任意自定義的附加資訊,提供外部進行查詢使用,(版本號,發布日期)
metadata: name: my-replication-controller labels: app: youbike-helper tier: backend

spec

依目前是 replicaControllerpod 來定義 pods / containers,以下範例為執行三個 pods
  • replicas
    • 定義Pod的數量
  • selector
    • 指定我們要選擇的 Pod 的條件(labels)
  • template
    • Pod 的 labels 以及 Pod 中要運行的 container
spec: replicas: 3 selector: app: hello-pod-v1 template: 以下都是 pod 的設定
  • spec.template.metadata
    • Pod 的 labels,不寫會發生 error
  • spec.template.spec
    • 定義 container
template: metadata: labels: app: hello-pod-v1 spec: containers: - name: my-pod image: zxcvbnius/docker-demo ports: - containerPort: 3000