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>
- </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 target="_blank" href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a target="_blank" 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
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
- [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"]
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |