iT邦幫忙

2021 iThome 鐵人賽

DAY 7
3
DevOps

k8s 入門學習 30天系列 第 7

IT 鐵人賽 k8s 入門30天 -- day7 K8s YAML 設定檔

前言

這章節將要來學習 kubectl 執行的設定檔

理解每個設定的組成

還有細部內容

設定檔的格式

設定檔的格式是採用 yaml的格式

這種格式對於縮排要求很嚴格

因此要很注意每個元素所在的階層位置

可以使用 yaml-validator 做檢查避免錯誤

本文範例

本文以 nginx-deployment.yaml 這個檔案來解說

內容如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

開頭部份

apiVersion: apps/v1
kind: Deployment

這部份指定使用的 k8s api 版本

還有發佈的類型 以 nginx-deployment.yaml 來說

就是 Deployment

所有關於 kind 跟 apiVersion 的部份都可以透過 k8s api 官方文件去查詢

目前連結是採用 1.22 的版本

metadata

metadata:
  name: nginx-deployment
  labels:
    app: nginx

這部份是關於這個發佈元件的標記內容

舉例來說: 這個元件的名稱, 還有用來查詢的標記

specification

spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

這部份是用來指定要如何運行運行這個元件

也可以說是目標狀態

舉例來說: nginx-deployment.yaml 內

spec 指定要有一個 replicaSet

要採用 nginx: 1.16 作為 image

要注意的是, spec 的屬性會隨這 kind 不同而變

status

這部份由 k8s 自動產生

主要會分為目標狀態跟實際狀態

目標狀態是由 spec 指定的狀態來產生

實際運行時, k8s 會比較實際狀態與目標狀態的不同來做調整

而這個實際狀態就是存在 etcd 的狀態

整個 k8s 的運行狀態都會存在 etcd

設定檔儲存位置

一般來說, 會與要發佈的程式放在同一個資料夾

這樣會比較方便管理

template

spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

在 spec 內有一個特別的屬性 template

可以發現 template 有自己的 metadata 跟 spec

這邊的 template 是一個 Pod 的藍圖

符合前面所說的 Deploymenet 管理 ReplicaSet

ReplicaSet 管理 Pod

label 以及 selector

在 k8s 設定檔裡, label 就是用來標記元件

透過這些標記, 可以用來識別不同元件與建立連結

在 k8s 設定檔理, selector 可以用來根據 label 選擇要配對的元件

舉例來說:

在以下範例的 nginx-deployment.yaml 跟 nginx-service.yaml

nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 8080

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

nginx-service 透過 selector 對應到 metadata 是 app: nginx 的 Pod

驗證如下:

上圖說明 nginx-service 的 endpoints 是在 172.17.0.4:80880

IP 的部份是 172.17.0.4

而查詢 Pod 佈署的 IP 也是 172.17.0.4

因此有成功的把 service 接到 pod 的上面

其他

透過以下指令可以把當下 deployment 狀態轉出來成一個檔案

kubectl get deployment $deployemnt_name -o yaml > $outputfile_name

而這個轉換出來的檔案除了原本之前所講的設定檔內容外

還會有許多由 k8s 產生出來的設定

比如狀態等等

舉例來說: 透過以下指令

kubectl get deployment nginx-deployment -o yaml > nginx-deployment-result.yaml

產生出來的檔案如下

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.16","name":"nginx","ports":[{"containerPort":8080}]}]}}}}
  creationTimestamp: "2021-09-11T17:00:33Z"
  generation: 4
  labels:
    app: nginx
  name: nginx-deployment
  namespace: default
  resourceVersion: "109348"
  uid: 6a79a2f9-d8f3-43dc-b919-793839d4bf00
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.16
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 8080
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2021-09-11T17:01:42Z"
    lastUpdateTime: "2021-09-11T17:01:42Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2021-09-11T17:00:34Z"
    lastUpdateTime: "2021-09-13T09:46:42Z"
    message: ReplicaSet "nginx-deployment-f4b7bbcbc" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 4
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

這樣的檔案如果要用來做 apply, 必須要刪除 k8s 自動產生的部份

否則會報錯


上一篇
IT 鐵人賽 k8s 入門30天 -- day6 主要 Kubectl 指令 - K8s CLI
下一篇
IT 鐵人賽 k8s 入門30天 -- day8 Demo Project: MongoDB and MongoExpress
系列文
k8s 入門學習 30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言