k8s--pod控制器

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)

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

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)

每个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"]

热门相关:宠妻入骨:神秘老公有点坏   非人类基因统合体   傲世武皇   一世倾心:误惹腹黑师弟   傲娇小萌妃:殿下太腹黑