1:控制器
-
控制器接介绍
1、自主式Pod,直接创建出来的Pod,pod删除了就没有了
2、控制器创建pod,通过控制器创建出来的pod,这种pod删除后,还会自动的重建
3、作用
1、pod控制器通过标签来管理一定数量的pod,创建多少的数量,如果pod出现了问题,策略重启和重建Pod
1、replicaSet(rs)
1、创建的数量的pod能够正常的运行
| [root@master rs] |
| apiVersion: apps/v1 |
| kind: ReplicaSet |
| metadata: |
| name: nginx-11 |
| namespace: dev |
| spec: |
| replicas: 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] |
| replicaset.apps/nginx-11 edited |
| [root@master rs] |
| NAME READY STATUS RESTARTS AGE |
| nginx-11-kvvpn 1/1 Running 0 4m39s |
| |
3、镜像版本的升级
| [root@master rs] |
| 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] |
| replicaset.apps/nginx-pod edited |
| [root@master rs] |
| NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR |
| nginx-pod 3 3 3 52s nxing nginx:1.17.1 app=nginx-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] |
| 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 ~] |
| 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 ~] |
| deployment.apps/nginx image updated |
| |
| [root@master ~] |
| NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR |
| nginx 3/3 3 3 5m29s nginx nginx:1.17.1 app=nginx-deploy |
| |
| |
| [root@master ~] |
| 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] |
| deployment.apps/nginx image updated |
| |
| [root@master deployment] |
| NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR |
| nginx 3/3 3 3 17m nginx nginx app=nginx-deploy |
| |
4、扩缩容
| |
| [root@master ~] |
| deployment.apps/nginx scaled |
| [root@master ~] |
| 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 |
| |
| |
5、版本回退
| 1、deploy镜像升级的话,会创建一个新的rs,里面再来创建pod,老的rs的pod删除掉 |
| |
| 2、留老rs的作用就是版本回退 |
| [root@master deployment] |
| Flag --record has been deprecated, --record will be removed in the future |
| deployment.apps/nginx created |
| |
| |
| [root@master deployment] |
| deployment.apps/nginx edited |
| |
| |
| [root@master deployment] |
| 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] |
| deployment.apps/nginx rolled back |
| |
| |
| [root@master deployment] |
| NAME DESIRED CURRENT READY AGE |
| nginx-d8d99f5bb 0 0 0 3m29s |
| nginx-f6b7dbf7 3 3 3 5m29s |
| [root@master deployment] |
| 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] |
| deployment.apps/nginx image updated |
| deployment.apps/nginx paused |
| |
| |
| [root@master deployment] |
| Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated... |
| |
| [root@master deployment] |
| NAME DESIRED CURRENT READY AGE |
| nginx-d8d99f5bb 1 1 1 11m |
| nginx-f6b7dbf7 3 3 3 13m |
| |
| |
| [root@master deployment] |
| <!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] |
| deployment.apps/nginx resumed |
| [root@master deployment] |
| 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] |
| deployment "nginx" successfully rolled out |
| [root@master deployment] |
| 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] |
| 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、安装检测软件
| |
| [root@master deployment] |
| |
| |
| [root@master deployment] |
| |
| |
| - --kubelet-insecure-tls |
| image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.3 |
| |
| |
| [root@master deployment] |
| NAME READY STATUS RESTARTS AGE |
| metrics-server-6779c94dff-tjv8f 1/1 Running 0 58s |
| |
| |
| [root@master deployment] |
| 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] |
| NAME CPU(cores) MEMORY(bytes) |
| nginx-d8d99f5bb-9gj4t 0m 1Mi |
| nginx-d8d99f5bb-fjhk6 0m 1Mi |
| nginx-d8d99f5bb-sll2t 0m 1Mi |
2、测试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 |
| |
| |
| [root@master deployment] |
| apiVersion: autoscaling/v2 |
| kind: HorizontalPodAutoscaler |
| metadata: |
| name: hpanginx |
| namespace: dev |
| spec: |
| scaleTargetRef: |
| apiVersion: apps/v1 |
| kind: deploy |
| name: nginx |
| minReplicas: 1 |
| maxReplicas: 10 |
| metrics: |
| - type: Resource |
| resource: |
| name: cpu |
| target: |
| type: Utilization |
| averageUtilization: 3 |
| |
| |
| [root@master deployment] |
| NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE |
| hpanginx Deployment/nginx 0%/30% 1 10 3 94s |
| |
| |
| [root@master deployment] |
| service/nginx exposed |
| [root@master deployment] |
| NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| nginx NodePort 10.96.20.47 <none> 80:30528/TCP 4s |
| |
| |
| [root@master deployment] |
| while `true` |
| do |
| curl 10.104.43.43:30528 &> /dev/null |
| done |
| |
| |
| [root@master deployment] |
| 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 ~] |
| 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 |
| |
| |
| [root@master ~] |
| 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,可以运用于监控使用,日志收集等作用 |
| |
| [root@master deployment] |
| 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 |
| |
| |
| [root@master deployment] |
| 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 |
| parallelism: 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] |
| 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] |
| NAME COMPLETIONS DURATION AGE |
| nginxjob 3/6 70s 70s |
| |
6、cronjob
| [root@master deployment] |
| 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"] |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库