1. 程式人生 > >Pod控制器

Pod控制器

一、Pod控制器簡介

  • ReplicationController(目前已廢棄)
  • ReplicaSet(不建議直接使用):使用者期望的Pod副本數,標籤選擇器以便選擇自己管理控制的副本,Pod資源模板來完成Pod的新建,主要管理無狀態的Pod資源副本
  • Deployment:通過控制ReplicaSet之上來工作,支援滾動更新,回滾,基於宣告式建立,在Pod執行中可動態修改Pod引數
  • DaemonSet:以守護程序的方式來控制Pod,在叢集中每個節點執行一個Pod副本
  • Job:執行一次性的作業Pod
  • Cronjob:執行週期性的作業Pod
  • StatefulSet:管理有狀態Pod,每個Pod都要有可能是有狀態、資料的

二、ReplicaSet

  • 獲取幫助
# kubectl explain ReplicaSet
# kubectl explain ReplicaSet.spec
# kubectl explain ReplicaSet.spec.template
# kubectl explain ReplicaSet.spec.template.spec
  • 簡單定義
manifests]# vim rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        release: canary
        environment: qa
    spec:
      containers:
      - name: myapp-container
        image: dongfeimg/myapp:v1
        ports:
        - name: http
          containerPort: 80
# kubectl get rs
  • 手動擴容/縮容
# kubectl edit rs myapp
spec:
  replicas: 5  #可動態修改Pod副本
  • 手動升級image
# kubectl edit rs myapp
    spec:
      containers:
      - image: dongfeimg/myapp:v2
# kubectl get rs -o wide

注意:只有重建的Pod才是新版本的image

三、Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: dongfeimg/myapp:v1
        ports:
        - name: http
          containerPort: 80
# kubectl apply -f Deployment-damo.yaml
# kubectl get deploy
# kubectl get rs
# kubectl get pods
# kubectl describe deploy myapp-deploy
  • 檢視歷史記錄
# kubectl rollout history deployment myapp-deploy
  • 監控檢視pod狀態
# kubectl get pods -l app=myapp -w
  • 通過打補丁的方式擴容
# kubectl patch deployment myapp-deploy -p '{"spec":{"relicas":5}}'
  • 滾動更新指定超出一個pod
# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
  • 更新image版本(金絲雀釋出)
# kubectl set image deployment myapp-deploy myapp=dongfeimg/myapp:v2 && kubectl rollout pause deployment myapp-deploy  #丟出一隻金絲雀
# kubectl rollout status deployment myapp-deploy
# kubectl get pods -l app=myapp -w
# kubectl rollout resume deployment myapp-deploy  #所有的都開始更新
  • 回滾
# kubectl rollout history deployment myapp-deploy  #檢視歷史版本
# kubectl rollout undo deployment myapp-deploy --to-revision=1  #回滾到第一版

四、DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  namespace: default
spec:
  selector:
    matchLabels:
      app: filebeat
      release: stable
  template:
    metadata:
      labels:
        app: filebeat
        release: stable
    spec:
      containers:
      - name: filebeat
        image: dongfeimg/filebeat:5.6.5-alpine
        env:
        - name: REDIS_HOST
          value: redis.default.svc.cluster.local
        - name: REDIS_LOG_LEVEL
          value: info

filebeat + redis

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
      - name: redis
        image: redis:4.0-alpine
        ports:
        - name: redis
          containerPort: 6379
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  namespace: default
spec:
  selector:
    matchLabels:
      app: filebeat
      release: stable
  template:
    metadata:
      labels:
        app: filebeat
        release: stable
    spec:
      containers:
      - name: filebeat
        image: dongfeimg/filebeat:5.6.5-alpine
        env:
        - name: REDIS_HOST
          value: redis.default.svc.cluster.local
        - name: REDIS_LOG_LEVEL
          value: info
  • 更新image版本
# kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine