k8s--pod控制器

q_7發表於2024-06-03

1:控制器

  1. 控制器接介紹

    1、自主式Pod,直接建立出來的Pod,pod刪除了就沒有了

    2、控制器建立pod,透過控制器建立出來的pod,這種pod刪除後,還會自動的重建

    3、作用

    1、pod控制器透過標籤來管理一定數量的pod,建立多少的數量,如果pod出現了問題,策略重啟和重建Pod

1、replicaSet(rs)

1、建立的數量的pod能夠正常的執行

[root@master rs]# cat rs-num.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-11
  namespace: dev
spec:
  replicas: 3   #數量為3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      name: nginx1
      labels:
        app: nginx-pod
    spec:
       containers:
       - name: nginx
         image: nginx:1.17.2

2、擴縮容

[root@master rs]# kubectl edit rs -n dev nginx-11  #使用edit來進行編輯即可
replicaset.apps/nginx-11 edited
[root@master rs]# kubectl get pod -n dev
NAME             READY   STATUS    RESTARTS   AGE
nginx-11-kvvpn   1/1     Running   0          4m39s

3、映象版本的升級

[root@master rs]# kubectl get rs -n dev -o wide
NAME        DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-pod   3         3         3       32s   nxing        nginx:1.17.2   app=nginx-pod
[root@master rs]# kubectl edit rs -n dev  nginx-pod 
replicaset.apps/nginx-pod edited
[root@master rs]# kubectl get rs -n dev -o wide
NAME        DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-pod   3         3         3       52s   nxing        nginx:1.17.1   app=nginx-pod


#刪除裡面的pod一個,之後再來建立的Pod就會是新的映象的構成的
 Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  18s   default-scheduler  Successfully assigned dev/nginx-pod-52hq5 to node1
  Normal  Pulling    18s   kubelet            Pulling image "nginx:1.17.1"
  Normal  Pulled     2s    kubelet            Successfully pulled image "nginx:1.17.1" in 16.375s (16.375s including waiting)
  Normal  Created    2s    kubelet            Created container nxing
  Normal  Started    2s    kubelet            Started container nxing

2、deployment(deploy)

img

1、支援很多的rs的功能

apiVersion: apps/v1 
kind: Deployment
metadata:
   name: nginx
   namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
       app: nginx-deploy
  template:
    metadata:
      name: nginxde
      labels:
        app: nginx-deploy
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.2

[root@master deployment]# kubectl get pod -n dev
NAME                   READY   STATUS    RESTARTS   AGE
nginx-f6b7dbf7-27zq5   1/1     Running   0          7s
nginx-f6b7dbf7-bm7vq   1/1     Running   0          7s
nginx-f6b7dbf7-z75d5   1/1     Running   0          7s
nginx-pod-52hq5        1/1     Running   0          10m
nginx-pod-wsr2l        1/1     Running   0          12m
nginx-pod-zmx5l        1/1     Running   0          12m

2、重建更新

重建更新

#一次更新所有的映象
apiVersion: apps/v1 
kind: Deployment
metadata:
   name: nginx
   namespace: dev
spec:
  replicas: 3
  strategy:
    type: Recreate
  selector:
    matchLabels:
       app: nginx-deploy
  template:
    metadata:
      name: nginxde
      labels:
        app: nginx-deploy
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.2

[root@master ~]# kubectl get pod -n dev
NAME                   READY   STATUS    RESTARTS   AGE
nginx-f6b7dbf7-b8z6q   1/1     Running   0          5s
nginx-f6b7dbf7-hvpww   1/1     Running   0          5s
nginx-f6b7dbf7-qrztz   1/1     Running   0          5s

#更新映象版本
[root@master ~]# kubectl set image -n dev deployment/nginx nginx=nginx:1.17.1
deployment.apps/nginx image updated

[root@master ~]# kubectl get deployments.apps  -n dev -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
nginx   3/3     3            3           5m29s   nginx        nginx:1.17.1   app=nginx-deploy

#裡面的pod裡面的映象也全部更新了
[root@master ~]# kubectl get pod -n dev
NAME                    READY   STATUS    RESTARTS   AGE
nginx-d8d99f5bb-7zk9w   1/1     Running   0          3m6s
nginx-d8d99f5bb-mmrfp   1/1     Running   0          3m6s
nginx-d8d99f5bb-p485m   1/1     Running   0          3m6s

3、滾動更新

#先更新一部分
apiVersion: apps/v1 
kind: Deployment
metadata:
   name: nginx
   namespace: dev
spec:
  replicas: 3
  strategy:
    type: RollingUpdate   #滾動更新
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  selector:
    matchLabels:
       app: nginx-deploy
  template:
    metadata:
      name: nginxde
      labels:
        app: nginx-deploy
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.2


#更新映象
[root@master deployment]# kubectl set image -n dev deploy/nginx nginx=nginx:1.17.1
deployment.apps/nginx image updated

[root@master deployment]# kubectl get deployments.apps  -n dev -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES   SELECTOR
nginx   3/3     3            3           17m   nginx        nginx    app=nginx-deploy

4、擴縮容

#命令來進行編輯
[root@master ~]# kubectl scale -n dev deployment/nginx  --replicas=5
deployment.apps/nginx scaled
[root@master ~]# kubectl get pod -n dev 
NAME                   READY   STATUS    RESTARTS   AGE
nginx-f6b7dbf7-7vfhn   1/1     Running   0          10h
nginx-f6b7dbf7-d5chw   1/1     Running   0          5s
nginx-f6b7dbf7-fbflf   1/1     Running   0          5s
nginx-f6b7dbf7-hnnnz   1/1     Running   0          10h
nginx-f6b7dbf7-nssr7   1/1     Running   0          10h

#edit也可以進行編輯,用法與之前的rs一樣

5、版本回退

1、deploy映象升級的話,會建立一個新的rs,裡面再來建立pod,老的rs的pod刪除掉

2、留老rs的作用就是版本回退
[root@master deployment]# kubectl create -f deploy.yaml --record  #記錄
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx created

#編輯裡面的版本
[root@master deployment]# kubectl edit -n dev deployments.apps/nginx 
deployment.apps/nginx edited

#顯示升級的狀態
[root@master deployment]# kubectl rollout history  -n dev  deployment/nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deploy.yaml --record=true
2         kubectl create --filename=deploy.yaml --record=true

#回退到上一個版本,不指定的話,預設是回退到上一個版本
[root@master deployment]# kubectl rollout undo -n dev deployment/nginx  --to-revision 1
deployment.apps/nginx rolled back

#rs也會回退到上一個rs裡面
[root@master deployment]# kubectl get rs -n dev
NAME              DESIRED   CURRENT   READY   AGE
nginx-d8d99f5bb   0         0         0       3m29s
nginx-f6b7dbf7    3         3         3       5m29s
[root@master deployment]# kubectl get deployments.apps  -n dev -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
nginx   3/3     3            3           5m37s   nginx        nginx:1.17.2   app=nginx-deploy

6、金絲雀釋出

1、支援在更新的過程控制,暫停,繼續更新等操作
在更新的過程,先將一部分更新應用,測試一下,發個請求,好的話,就全部更新,不是好的話,就回退版本
#更新然後立刻暫停
[root@master deployment]# kubectl set  image -n dev deploy/nginx nginx=nginx:1.17.1 && kubectl rollout  pause deployment  -n dev nginx
deployment.apps/nginx image updated
deployment.apps/nginx paused

#檢視當前升級的狀態
[root@master deployment]# kubectl rollout  status deployment  -n dev
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...

[root@master deployment]# kubectl get rs -n dev 
NAME              DESIRED   CURRENT   READY   AGE
nginx-d8d99f5bb   1         1         1       11m
nginx-f6b7dbf7    3         3         3       13m

#傳送一個請求
[root@master deployment]# curl 10.244.1.46:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#持續更新
##取消暫停更新
[root@master deployment]# kubectl rollout resume -n dev deployment/nginx
deployment.apps/nginx resumed
[root@master deployment]# kubectl get pod -n dev
NAME                    READY   STATUS        RESTARTS   AGE
nginx-d8d99f5bb-9gj4t   1/1     Running       0          3s
nginx-d8d99f5bb-fjhk6   1/1     Running       0          3m50s
nginx-d8d99f5bb-sll2t   1/1     Running       0          2s
nginx-f6b7dbf7-fl8b7    0/1     Terminating   0          11m
[root@master deployment]# kubectl rollout status -n dev deployment/nginx 
deployment "nginx" successfully rolled out
[root@master deployment]# kubectl get pod -n dev
NAME                    READY   STATUS    RESTARTS   AGE
nginx-d8d99f5bb-9gj4t   1/1     Running   0          22s
nginx-d8d99f5bb-fjhk6   1/1     Running   0          4m9s
nginx-d8d99f5bb-sll2t   1/1     Running   0          21s
[root@master deployment]# kubectl get rs -n dev
NAME              DESIRED   CURRENT   READY   AGE
nginx-d8d99f5bb   3         3         3       15m
nginx-f6b7dbf7    0         0         0       17m

3、hpa

img

1、主要就是自動的擴縮容,獲取了每一個pod的利用率,與pod上面的hpa定義的指標進行比較,如果超過的話,就自動的擴容,當訪問量減少的話,會刪除增加的pod

2、透過監控pod負載均衡的情況,實現pod數量擴縮容

1、安裝檢測軟體

#安裝一個監控的軟體metries-server
[root@master deployment]# wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.6.3/components.yaml

#手動拉取這個國內的映象
[root@master deployment]# ctr image pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3

#修改這2行
        - --kubelet-insecure-tls
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3

#這個便是那個pod提供監控負載均衡的軟體
[root@master deployment]# kubectl get pod -n kube-system metrics-server-6779c94dff-tjv8f 
NAME                              READY   STATUS    RESTARTS   AGE
metrics-server-6779c94dff-tjv8f   1/1     Running   0          58s

#檢視資源的使用情況
[root@master deployment]# kubectl top node
NAME               CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master.novalocal   225m         3%     1061Mi          6%        
node1              51m          0%     617Mi           3%        
node2              47m          0%     576Mi           3%    

[root@master deployment]# kubectl top pod -n dev
NAME                    CPU(cores)   MEMORY(bytes)   
nginx-d8d99f5bb-9gj4t   0m           1Mi             
nginx-d8d99f5bb-fjhk6   0m           1Mi             
nginx-d8d99f5bb-sll2t   0m           1Mi  

2、測試hpa

#先建立一個deploy,再來建立一個hpa
apiVersion: apps/v1 
kind: Deployment
metadata:
   name: nginx
   namespace: dev
spec:
  replicas: 3
  strategy:
    type: RollingUpdate 
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  selector:
    matchLabels:
       app: nginx-deploy
  template:
    metadata:
      name: nginxde
      labels:
        app: nginx-deploy
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.2
        resources:   #必須要有資源的限定,才能做
          requests:
            cpu: 100m

#建立一個hpa控制器
[root@master deployment]# cat hpa.yaml 
apiVersion: autoscaling/v2 
kind: HorizontalPodAutoscaler 
metadata:
   name: hpanginx
   namespace: dev
spec:
  scaleTargetRef:  #控制的deploy控制器
    apiVersion: apps/v1
    kind: deploy
    name: nginx
  minReplicas: 1  #最小Pod數量
  maxReplicas: 10 #最大pod數量
  metrics:   #定義伸縮規則
  - type: Resource   #伸縮型別是資源
    resource: 
      name: cpu  #要伸縮的資源是cpu
      target:
        type: Utilization  #目標型別是利用率
        averageUtilization: 3   #cpu的平均利用率是3%

#檢視hpa資訊
[root@master deployment]# kubectl get hpa -n dev
NAME       REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpanginx   Deployment/nginx   0%/30%    1         10        3          94s

#暴露埠,進行壓力測試
[root@master deployment]# kubectl expose deployment nginx --type=NodePort --port=80 -n dev
service/nginx exposed
[root@master deployment]# kubectl get svc -n dev
NAME    TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
nginx   NodePort   10.96.20.47   <none>        80:30528/TCP   4s

#迴圈指令碼
[root@master deployment]# cat curl.sh 
while `true`
do
  curl 10.104.43.43:30528 &> /dev/null
done

#觀察hpa,pod
[root@master deployment]# kubectl get hpa -n dev -w
NAME       REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpanginx   Deployment/nginx   3%/3%     1         10        2          19s
hpanginx   Deployment/nginx   0%/3%     1         10        2          30s

hpanginx   Deployment/nginx   10%/3%    1         10        2          45s
hpanginx   Deployment/nginx   20%/3%    1         10        4          60s
hpanginx   Deployment/nginx   10%/3%    1         10        8          75s
hpanginx   Deployment/nginx   2%/3%     1         10        10         90s
hpanginx   Deployment/nginx   0%/3%     1         10        10         105s

[root@master ~]# 
[root@master ~]# kubectl get pod -n dev -w
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5bf8cbdf5d-bv24r   1/1     Running   0          3m43s
nginx-5bf8cbdf5d-v7vnx   1/1     Running   0          9m51s
nginx-5bf8cbdf5d-hfd9w   0/1     Pending   0          0s
nginx-5bf8cbdf5d-xmlr9   0/1     Pending   0          0s
nginx-5bf8cbdf5d-hfd9w   0/1     Pending   0          0s
nginx-5bf8cbdf5d-xmlr9   0/1     Pending   0          0s
nginx-5bf8cbdf5d-hfd9w   0/1     ContainerCreating   0          0s
nginx-5bf8cbdf5d-xmlr9   0/1     ContainerCreating   0          0s
nginx-5bf8cbdf5d-hfd9w   1/1     Running             0          1s
nginx-5bf8cbdf5d-xmlr9   1/1     Running             0          2s

#當訪問量減少時,pod會自動的減少,需要一定的時間
[root@master ~]# kubectl get pod -n dev
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5bf8cbdf5d-7258w   1/1     Running   0          2m6s
nginx-5bf8cbdf5d-bv24r   1/1     Running   0          6m27s
nginx-5bf8cbdf5d-h2hq9   1/1     Running   0          2m6s
nginx-5bf8cbdf5d-hfd9w   1/1     Running   0          2m21s
nginx-5bf8cbdf5d-ncjq4   1/1     Running   0          111s
nginx-5bf8cbdf5d-t2glk   1/1     Running   0          2m6s
nginx-5bf8cbdf5d-tlcn9   1/1     Running   0          2m6s
nginx-5bf8cbdf5d-v7vnx   1/1     Running   0          12m

4、daemonset(ds)

img

每個node節點上面都有一個pod,可以運用於監控使用,日誌收集等作用
#網路外掛的就是這種型別的,每一個node節點上面都有一個pod,提供網路服務
[root@master deployment]# cat daemonset.yaml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemon
  namespace: dev
spec:
  selector:  #匹配的標籤
    matchLabels:
      app: nginx-daemon
  template:
    metadata:
      name: nginx-dd
      labels:
        app: nginx-daemon
    spec:
      containers:
        - name: nginx
          image: nginx:1.17.1

#每個節點上面都有一個pod
[root@master deployment]# kubectl get pod -o wide -n dev
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
nginx-5bf8cbdf5d-v7vnx   1/1     Running   0          20m   10.244.2.41   node2   <none>           <none>
nginx-daemon-4v4gf       1/1     Running   0          16s   10.244.1.56   node1   <none>           <none>
nginx-daemon-vt25q       1/1     Running   0          16s   10.244.2.46   node2   <none>           <none>

5、job

1、用於負責處理一次性任務,一次性任務,結束後就沒有了

2、重啟策略不能設定為always,一但結束後,又要進行重啟,不符合
onfailure,
never

apiVersion: batch/v1
kind: Job
metadata:
  name: nginxjob
  namespace: dev
spec:
  manualSelector: true
  completions: 6  #一共有6個Pod
  parallelism: 3   #每輪執行3個(併發的執行)
  selector:
    matchLabels:
      app: job
  template:
    metadata:
      name: job1
      labels:
        app: job
    spec:
      restartPolicy: Never
      containers:
        - name: busybox
          image: busybox:1.30
          command: ["/bin/sh","-c","for i in 1 2 3 4 5 6 7;do echo $i;sleep 5;done"]

[root@master deployment]# kubectl get pod -n dev
NAME                     READY   STATUS      RESTARTS   AGE
nginx-5bf8cbdf5d-v7vnx   1/1     Running     0          30m
nginx-daemon-4v4gf       1/1     Running     0          10m
nginx-daemon-vt25q       1/1     Running     0          10m
nginxjob-2cpwh           0/1     Completed   0          63s
nginxjob-7xtdd           0/1     Completed   0          63s
nginxjob-c52fk           0/1     Completed   0          63s
nginxjob-k2r74           1/1     Running     0          25s
nginxjob-szlzd           1/1     Running     0          25s
nginxjob-zr8d2           1/1     Running     0          25s
[root@master deployment]# kubectl get job -n dev
NAME       COMPLETIONS   DURATION   AGE
nginxjob   3/6           70s        70s

6、cronjob

#週期性的執行job任務
[root@master deployment]# cat cronjob.yaml 
apiVersion: batch/v1
kind: CronJob
metadata:
  name: pcjob
  namespace: dev
  labels:
    app: job2
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    metadata:
      name: job2
      labels:
        app: job2
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: busybox
            image: busybox:1.30
            command: ["/bin/sh","-c","for i in 1 2 3 4 5 6 7;do echo $i;sleep 5;done"]

相關文章