赞
踩
目录
四.Horizontal Pod Autoscaler(简写hpa)
(2)对刚才的service暴露出来的IP进行压测,总访问量为200000,每次访问1000次
(3)接下来减少访问量看看他们的变化如何,期望和运行的pod自动减少
(1)之前我们提到kubernetes的最小管理单元室pod,也讲到过有自主式pod和控制器管理pod(推荐)两种。pod控制器结余用户管理和pod中间,利用pod控制器来告知pod自己的期望需求,同时还包含有编排pod的多种策略。这里我们主要介绍ReplicaSet(无状态)、Deployment(无状态)、Horizontal Pod Autoscaler(通常无状态)、DaemonSet(守护型)、Job(批处理)、Cronjob(批处理)、StatefulSet(有状态)七个控制器。
(2)有/无状态
a.无状态Pod应用
是一种简单的 Pod,它不会存储任何持久化数据。当 Pod 被删除或重新启动时,容器内的数据也会被删除。
没有顺序要求且不用考虑在哪个node上运行,甚至可以自由扩/缩容。
通常用于部署无需持久化存储数据的应用程序,例如 Web 服务器、负载均衡器等。
b.有状态Pod应用
是一种可以存储持久化数据的 Pod。它们可以使用网络存储卷来存储数据,并且每个 Pod 都有一个唯一的标识符。这使得有状态 Pod 可以保持其身份和状态,并且在重新启动后可以保留数据
几乎和无状态呈相反特点,他需要考虑运行在哪个node,需要按照顺序来进行扩/缩容,且有状态应用pod相互独立(etcd,zooleeper元数据不同),具有唯一性和启动顺序性。
通常用于需要存储状态信息的应用程序,例如数据库、缓存服务器等。
(3)守护型
守护型 Pod 会自动在每个节点上创建和维护副本,以确保在整个集群中的每个节点上都能运行该 Pod,可以通过适当的配置进行扩展、升级和维护,适用于收集日志、执行后台任务,运行网络服务。
(4)批处理
批处理型 Pod 旨在执行一次性任务或定期任务,而不是持续运行的服务,适用于数据处理、定时任务和一次性理性任务执行。
保障pod的副本维持在期望数量,持续监听pod运行状态(故障后进行重启或重创键),进行pod扩/缩容,进行镜像的升级,主要是处理无状态pod应用。
截取edit rs的spec部分
- spec:
- replicas: 3
- selector:
- matchLabels:
- app: my-nginx
- template:
- metadata:
- creationTimestamp: null
- labels:
- app: my-nginx
- spec:
- containers:
- - image: nginx:1.17.1
- imagePullPolicy: IfNotPresent
- name: nginx-container
- ports:
- - containerPort: 80
- protocol: TCP
- resources: {}
- terminationMessagePath: /dev/termination-log
- #指定容器终止时日志消息的路径。在容器终止时,Kubernetes 将将容器的终止消息写入指定路径的文件中
- terminationMessagePolicy: File
- #定义容器终止消息的策略。File 表示将终止消息写入到文件中,其他选项还包括 FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
- dnsPolicy: ClusterFirst
- #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
- restartPolicy: Always
- schedulerName: default-scheduler
- #指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器
- securityContext: {}
- terminationGracePeriodSeconds: 30
- #指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止
- [root@k8s-master pod]# cat myrs.yaml
- apiVersion: apps/v1
- kind: ReplicaSet
- metadata:
- name: my-rs
- namespace: myns
- spec:
- replicas: 3
- selector: #选择器部分,使用matchLabels机制来定义label,定义选择器可以表明这个控制器能够管理哪些pod
- matchLabels:
- app: my-nginx
- template: #副本数量不足时,根据模版创建pod副本
- metadata:
- labels:
- app: my-nginx
- spec:
- containers:
- - name: nginx-container
- image: nginx
- ports:
- - containerPort: 80
-
- [root@k8s-master pod]# kubectl get rs -n myns
- NAME DESIRED CURRENT READY AGE
- my-rs 3 3 3 8s
- #DESIRED指期望副本数,CURRENT指当前副本数
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-rs-jb2cd 1/1 Running 0 12s
- my-rs-jsr84 1/1 Running 0 12s
- my-rs-sdk9l 1/1 Running 0 12s
a.同以前使用yaml文件创建自主pod一样,同样可以通过yaml文件配合delete进行删除
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-rs-2xk2p 1/1 Running 0 10m
- my-rs-rl9zw 1/1 Running 0 10m
- my-rs-zp6zs 1/1 Running 0 10m
- [root@k8s-master pod]# kubectl delete -f myrs.yaml
- replicaset.apps "my-rs" deleted
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-rs-2xk2p 0/1 Terminating 0 12m
- my-rs-rl9zw 0/1 Terminating 0 12m
- [root@k8s-master pod]# kubectl get pods -n myns
- No resources found in myns namespace.
b.仅删除rs而不删除其下管理的pod
- [root@k8s-master pod]# kubectl delete rs my-rs -n myns --cascade=orphan
- #较新的版本用--cascade=orphan来替代--cascade=false,都表示同一个意思
- replicaset.apps "my-rs" deleted
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-rs-blcgf 1/1 Running 0 107s
- my-rs-kkqfp 1/1 Running 0 107s
- my-rs-l7tqk 1/1 Running 0 107s
- [root@k8s-master pod]# kubectl get rs -n myns
- No resources found in myns namespace.
c.rs连同pod一起删除
- [root@k8s-master pod]# kubectl get pods -n myns -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- my-rs-6lfgd 1/1 Running 0 21s 10.244.169.132 k8s-node2 <none> <none>
- my-rs-cqtfg 1/1 Running 0 21s 10.244.36.70 k8s-node1 <none> <none>
- my-rs-rv6j5 1/1 Running 0 21s 10.244.36.69 k8s-node1 <none> <none>
- [root@k8s-master pod]# kubectl delete rs my-rs -n myns
- replicaset.apps "my-rs" deleted
- [root@k8s-master pod]# kubectl get pods -n myns
- No resources found in myns namespace.
- [root@k8s-master pod]# kubectl get rs -n myns
- No resources found in myns namespace.
直接通过“replicas=num”即可
- [root@k8s-master pod]# kubectl scale rs my-rs --replicas=6 -n myns
- replicaset.apps/my-rs scaled
- [root@k8s-master pod]# kubectl get rs -n myns
- NAME DESIRED CURRENT READY AGE
- my-rs 6 6 6 2m12s
-
- [root@k8s-master pod]# kubectl scale rs my-rs --replicas=2 -n myns
- replicaset.apps/my-rs scaled
- [root@k8s-master pod]# kubectl get rs -n myns
- NAME DESIRED CURRENT READY AGE
- my-rs 2 2 2 2m58s
通过edit去编辑RS,更改其中的replicas保存退出后直接查看即可。
- [root@k8s-master pod]# kubectl edit rs my-rs -n myns
- replicaset.apps/my-rs edited
- [root@k8s-master pod]# kubectl get rs -n myns
- NAME DESIRED CURRENT READY AGE
- my-rs 5 5 5 4m
“kubectl set image rs rs名称 pod内容器的名称=镜像名:具体版本”
- [root@k8s-master pod]# kubectl set image rs my-rs nginx-container=nginx:1.17.1 -n myns
- replicaset.apps/my-rs image updated
- [root@k8s-master pod]# kubectl get rs -n myns -o wide
- NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
- my-rs 3 3 3 6m35s nginx-container nginx:1.17.1 app=my-nginx
- [root@k8s-master pod]# kubectl edit rs my-rs -n myns
- replicaset.apps/my-rs edited
- [root@k8s-master pod]# kubectl get rs -n myns -o wide
- NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
- my-rs 3 3 3 3m48s nginx-container nginx:1.17.2 app=my-nginx
ReplicaSet修改镜像版本,对rs进行更改表面上rs的标识的版本号已经改变,但是pod的镜像版本仍然未变,修改过后需要自己手动删除原有pod(不删除rs),删除过后rs会自动重新创建pod
- [root@k8s-master pod]# kubectl set image rs my-rs nginx-container=nginx:1.17.2 -n myns
- replicaset.apps/my-rs image updated
-
- [root@k8s-master pod]# kubectl describe rs my-rs -n myns | grep Image #rs版本变化
- Image: nginx:1.17.2
- [root@k8s-master pod]# kubectl describe pod my-rs-mshs6 -n myns | grep Image #pod版本未变
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
-
- [root@k8s-master pod]# kubectl delete pod my-rs-6wl6h my-rs-d5rd5 my-rs-mshs6 -n myns #手动删除pod副本
- pod "my-rs-6wl6h" deleted
- pod "my-rs-d5rd5" deleted
- pod "my-rs-mshs6" deleted
-
- [root@k8s-master pod]# kubectl get pods -n myns #自动重新创建三个副本
- NAME READY STATUS RESTARTS AGE
- my-rs-bg6fc 1/1 Running 0 119s
- my-rs-czgdc 1/1 Running 0 119s
- my-rs-dwdbd 1/1 Running 0 119s
-
- [root@k8s-master pod]# kubectl describe pod my-rs-bg6fc -n myns | grep Image #再查看就成功更改了
- Image: nginx:1.17.2
- Image ID: docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
- [root@k8s-master pod]# kubectl describe rs my-rs -n myns | grep Image
- Image: nginx:1.17.2
-
在ReplicaSet的基础上进一步对pod进行编排服务,除了支持比ReplicaSet的所有功能(监控、扩/缩容、镜像版本管理)还可以进行发布管理、滚动升级和回滚版本。主要处理无状态应用。
截取edit deploy中的spec部分
- spec:
- progressDeadlineSeconds: 600 #部署超时时间,默认600s
- replicas: 3
- revisionHistoryLimit: 10 #保留历史修订版本数
- selector:
- matchLabels:
- app: my-nginx
- strategy: #策略
- rollingUpdate: #滚动更新
- maxSurge: 25% #最大额外可拥有的pod副本数量,整数/百分比
- maxUnavailable: 25% #最大不可用pod的最大值,整数/百分比
- type: RollingUpdate #镜像更新方式,支持RollingUpdate和Recreate,后面会详细介绍
- template: #模版
- metadata:
- creationTimestamp: null
- labels:
- app: my-nginx
- spec:
- containers:
- - image: nginx
- imagePullPolicy: Always
- name: nginx-pod
- resources: {}
- terminationMessagePath: /dev/termination-log #容器终止时日志消息路径位置
- terminationMessagePolicy: File
- #容器终止消息策略,File表示将终止消息写入到文件中,其他选项还包括FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
- dnsPolicy: ClusterFirst
- #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
- restartPolicy: Always
- schedulerName: default-scheduler #指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器
- securityContext: {}
- terminationGracePeriodSeconds: 30
- #指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止
- [root@k8s-master pod]# cat mydeploy.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: my-nginx
- namespace: myns
- labels:
- name: my-de
- spec:
- replicas: 3
- selector:
- matchLabels:
- app: my-nginx
- template:
- metadata:
- labels:
- app: my-nginx
- spec:
- containers:
- - name: nginx-pod
- image: nginx
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7b78cb5c86-b2tdh 1/1 Running 0 4m5s
- my-nginx-7b78cb5c86-qtcsr 1/1 Running 0 4m5s
- my-nginx-7b78cb5c86-v7xr9 1/1 Running 0 4m5s
- [root@k8s-master pod]# kubectl get deploy -n myns
- NAME READY UP-TO-DATE AVAILABLE AGE
- my-nginx 3/3 3 3 4m10s
方式同ReplicaSet的删除
- #只删deploy不删pod
- [root@k8s-master pod]# kubectl delete deployment my-nginx -n myns --cascade=orphan
- deployment.apps "my-nginx" deleted
- [root@k8s-master pod]# kubectl get deploy -n myns
- No resources found in myns namespace.
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7b78cb5c86-fqvcm 1/1 Running 0 2m9s
- my-nginx-7b78cb5c86-nf5gb 1/1 Running 0 2m9s
- my-nginx-7b78cb5c86-wpsbd 1/1 Running 0 2m9s
- #deploy和pod一起删除
- [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
- deployment.apps/my-nginx created
- [root@k8s-master pod]# kubectl delete -f mydeploy.yaml
- deployment.apps "my-nginx" deleted
-
- [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
- deployment.apps/my-nginx created
- [root@k8s-master pod]# kubectl delete deploy my-nginx -n myns
- deployment.apps "my-nginx" deleted
用法和ReplicaSet一致,支持scale和edit
更改方式和ReplicaSet一致,但这里在其基础上介绍Deployment可配置的两种镜像更新策略
该参数表示察觉到版本更改后会先杀死现在版本的pod再重新建立新版本pod
- [root@k8s-master pod]# cat mydeploy.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: my-nginx
- namespace: myns
- labels:
- name: my-de
- spec:
- replicas: 3
- selector:
- matchLabels:
- app: my-nginx
- strategy:
- type: Recreate #指定为Recreate方式
- template:
- metadata:
- labels:
- app: my-nginx
- spec:
- containers:
- - name: nginx-pod
- image: nginx
-
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns
- deployment.apps/my-nginx image updated
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-79df9bbf76-586sz 1/1 Running 0 14s
- my-nginx-79df9bbf76-9jq2n 1/1 Running 0 14s
- my-nginx-79df9bbf76-lw8f2 1/1 Running 0 14s
- [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep Image
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
-
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #再次更改后查看,原pod已无法找到,新建立了pod
- deployment.apps/my-nginx image updated
- [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep Image
- Error from server (NotFound): pods "my-nginx-79df9bbf76-586sz" not found
- Error from server (NotFound): pods "my-nginx-79df9bbf76-9jq2n" not found
- Error from server (NotFound): pods "my-nginx-79df9bbf76-lw8f2" not found
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7978cf754c-2xs4q 1/1 Running 0 11s
- my-nginx-7978cf754c-rs2vk 1/1 Running 0 11s
- my-nginx-7978cf754c-zlp4k 1/1 Running 0 11s
该参数表示察觉到版本更改后,删除一个旧版本pod再新建一个新版本pod,边删边建
- [root@k8s-master pod]# cat mydeploy.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: my-nginx
- namespace: myns
- labels:
- name: my-de
- spec:
- replicas: 3
- selector:
- matchLabels:
- app: my-nginx
- strategy:
- type: RollingUpdate #指定滚动更新策略
- rollingUpdate:
- maxSurge: 25%
- maxUnavailable: 25%
- template:
- metadata:
- labels:
- app: my-nginx
- spec:
- containers:
- - name: nginx-pod
- image: nginx
-
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns #更改版本
- deployment.apps/my-nginx image updated
-
- [root@k8s-master pod]# kubectl describe pod my-nginx-7b78cb5c86-5f56f my-nginx-7b78cb5c86-m27zq my-nginx-7b78cb5c86-vfrfx -n myns | grep Image
- #仔细观察,此处已经开始部分pod的更新了,新pod正在创建中
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-79df9bbf76-8szdt 0/1 ContainerCreating 0 2s
- my-nginx-79df9bbf76-9gxtz 1/1 Running 0 6s
- my-nginx-7b78cb5c86-5f56f 1/1 Running 0 94s
- my-nginx-7b78cb5c86-vfrfx 1/1 Running 0 94s
-
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-79df9bbf76-8szdt 1/1 Running 0 6s
- my-nginx-79df9bbf76-9d9jm 0/1 ContainerCreating 0 2s
- my-nginx-79df9bbf76-9gxtz 1/1 Running 0 10s
- my-nginx-7b78cb5c86-5f56f 1/1 Running 0 98s
-
- [root@k8s-master pod]# kubectl get pods -n myns #最后所有旧pod都被替换为新版本了
- NAME READY STATUS RESTARTS AGE
- my-nginx-79df9bbf76-8szdt 1/1 Running 0 10s
- my-nginx-79df9bbf76-9d9jm 1/1 Running 0 6s
- my-nginx-79df9bbf76-9gxtz 1/1 Running 0 14s
-
- [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-8szdt my-nginx-79df9bbf76-9d9jm my-nginx-79df9bbf76-9gxtz -n myns | grep Image
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
上面我们更改deploy中pod镜像版本后,可以看到存在一个pod数量为0的rs,理解为这是为deploy留下的回滚依据,更新完成只需要将不使用的rs的pod数降为0,回滚时又将pod数提升到期望数量即可,同时版本又跟着变化
- [root@k8s-master pod]# kubectl get rs -n myns
- NAME DESIRED CURRENT READY AGE
- my-nginx-79df9bbf76 3 3 3 6m35s
- my-nginx-7b78cb5c86 0 0 0 8m3s
- [root@k8s-master pod]# kubectl rollout status deploy my-nginx -n myns
- #显示当前升级状态
- [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns
- #显示当前升级历史记录
- [root@k8s-master pod]# kubectl rollout pause deploy my-nginx -n myns
- #暂停当前升级过程
- [root@k8s-master pod]# kubectl rollout resume deploy my-nginx -n myns
- #继续执行暂停的升级过程
- [root@k8s-master pod]# kubectl rollout restart deploy my-nginx -n myns
- #重新启动升级过程
- [root@k8s-master pod]# kubectl rollout undo deploy my-nginx --to-revision=history内查看到的号 -n myns
- #回退逮哪一级版本
这里我们对原来的nginx:1.17.1进行两次版本升级,然后最后回退到nginx:1.17.1
- [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns #查看版本历史
- deployment.apps/my-nginx
- REVISION CHANGE-CAUSE
- 1 <none>
- 2 <none>
-
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #进行两次版本升级
- deployment.apps/my-nginx image updated
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:latest -n myns
- deployment.apps/my-nginx image updated
-
- [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns #升级记录已多出两次
- deployment.apps/my-nginx
- REVISION CHANGE-CAUSE
- 1 <none>
- 2 <none>
- 3 <none>
- 4 <none>
-
-
- [root@k8s-master pod]# kubectl rollout undo deploy my-nginx --to-revision=2 -n myns #指定回到历史记录中的2,即nginx:1.17.1
- deployment.apps/my-nginx rolled back
-
- [root@k8s-master pod]# kubectl describe deploy my-nginx -n myns | grep Image
- Image: nginx:1.17.1
上面讲到deployment可以对pod的镜像版本进行滚动更新,但是在更新后的版本对于用户或者软件来说可能运行错误或不兼容,那么金丝雀就派上用处了,原理就是在更新镜像但只更新了部分pod的情况下暂停更新,待测试正常后再回复更新,利于运行更稳定。
下面有一个案例演示
- [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
- deployment.apps/my-nginx created
-
- [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns && kubectl rollout pause deploy my-nginx -n myns
- #更新操作成功开始后立即停止更新
- deployment.apps/my-nginx image updated
- deployment.apps/my-nginx paused
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7978cf754c-hjg2b 1/1 Running 0 10s
- my-nginx-7b78cb5c86-kzbw5 1/1 Running 0 103s
- my-nginx-7b78cb5c86-lwjxf 1/1 Running 0 103s
- my-nginx-7b78cb5c86-mbhr5 1/1 Running 0 103s
-
- [root@k8s-master pod]# kubectl describe pod my-nginx-7978cf754c-hjg2b my-nginx-7b78cb5c86-kzbw5 my-nginx-7b78cb5c86-lwjxf my-nginx-7b78cb5c86-mbhr5 -n myns | grep Image
- #可以看到目前新建了一个新版本的pod,但是还没有删除原有旧版本pod,说明已经停止更新,如果需要对pod进行相关的测试就可以现在做
- Image: nginx:1.17.2
- Image ID: docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
-
- [root@k8s-master pod]# kubectl rollout resume deploy my-nginx -n myns
- #测试结果满意之后就继续执行更新操作
- deployment.apps/my-nginx resumed
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7978cf754c-cwtkw 1/1 Running 0 7s
- my-nginx-7978cf754c-hjg2b 1/1 Running 0 2m16s
- my-nginx-7978cf754c-ktm5f 0/1 ContainerCreating 0 3s
- my-nginx-7b78cb5c86-lwjxf 1/1 Running 0 3m49s
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-nginx-7978cf754c-cwtkw 1/1 Running 0 10s
- my-nginx-7978cf754c-hjg2b 1/1 Running 0 2m19s
- my-nginx-7978cf754c-ktm5f 1/1 Running 0 6s
- [root@k8s-master pod]# kubectl describe pod my-nginx-7978cf754c-cwtkw my-nginx-7978cf754c-hjg2b my-nginx-7978cf754c-ktm5f -n myns | grep Image
- #更新完成
- Image: nginx:1.17.2
- Image ID: docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
- Image: nginx:1.17.2
- Image ID: docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
- Image: nginx:1.17.2
- Image ID: docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
HPA通过检测所有pod使用情况(使用效率),与定义的指标进行比对,来对pod的数量自动进行调整,超出cpu阈值就另起pod,低于阈值就缩减pod。
(1)下载
链接:百度网盘 请输入提取码 提取码:3oob
(2)更改其中的镜像地址和新增参数
新增参数“- --kubelet-insecure-tls”
更该镜像为registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.2“
(3)应用该文件并查看是否成功运行其pod
- [root@k8s-master pod]# kubectl apply -f components.yaml
- serviceaccount/metrics-server created
- clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
- clusterrole.rbac.authorization.k8s.io/system:metrics-server created
- rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
- clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
- clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
- service/metrics-server created
- deployment.apps/metrics-server created
- apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
-
- [root@k8s-master pod]# kubectl get pod -n kube-system | grep metrics
- metrics-server-f974958b-hlb6p 1/1 Running 0 69s
(1)以之前用到的deployment和为其创建的service(暴露端口用于外界测试)为例
- [root@k8s-master pod]# cat nginx-deployment.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- creationTimestamp: null
- labels:
- app: nginx-deploy
- name: nginx-deploy
- namespace: myns
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: nginx-deploy
- strategy: {}
- template:
- metadata:
- labels:
- app: nginx-deploy
- spec:
- containers:
- - image: nginx
- name: nginx
- ports:
- - containerPort: 80
- resources:
- requests:
- memory: "60M"
- cpu: "1"
- limits:
- memory: "61M"
- cpu: "1"
(2)暴露80端口服务
- [root@k8s-master pod]# kubectl expose deployment nginx-deploy --type=NodePort --port=80 -n myns
- [root@k8s-master pod]# kubectl get service -n myns #后面测试用这个IP进行测试
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- nginx-deploy NodePort 10.108.107.216 <none> 80:30446/TCP 20m
(3)创建hpa
这里使用的快捷命令将结果写入yaml并应用,快捷命令可指定的参数较少,可以后面根据需求又自行添加
- [root@k8s-master pod]# kubectl autoscale deployment nginx-deploy --min=1 --max=20 --cpu-percent=15 --dry-run=client -n myns -o yaml > myhpa.yaml
-
- [root@k8s-master pod]# cat myhpa.yaml
- apiVersion: autoscaling/v1
- kind: HorizontalPodAutoscaler
- metadata:
- creationTimestamp: null
- name: nginx-deploy
- namespace: myns
- spec:
- maxReplicas: 20
- minReplicas: 1
- scaleTargetRef:
- apiVersion: apps/v1
- kind: Deployment
- name: nginx-deploy
- targetCPUUtilizationPercentage: 15 #这里指定的是期望的cpu使用率,超过这个值就会自动扩容
- status:
- currentReplicas: 0
- desiredReplicas: 0
-
- [root@k8s-master pod]# kubectl apply -f myhpa.yaml
-
- [root@k8s-master pod]# kubectl get hpa -n myns
- NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 1 13m
(4)使用delete -f 文件或delete hpa 名称即可删除
yum install httpd-tools -y
- [root@k8s-master pod]# ab -c 1000 -n 250000 http://10.108.107.216/
- This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Licensed to The Apache Software Foundation, http://www.apache.org/
-
- Benchmarking 10.108.107.216 (be patient)
- Completed 25000 requests
- Completed 50000 requests
- Completed 75000 requests
- Completed 100000 requests
- Completed 125000 requests
- Completed 150000 requests
- Completed 175000 requests
- Completed 200000 requests
- Completed 225000 requests
- Completed 250000 requests
- Finished 250000 requests
-
-
- Server Software: nginx/1.25.3
- Server Hostname: 10.108.107.216
- Server Port: 80
-
- Document Path: /
- Document Length: 615 bytes
-
- Concurrency Level: 1000
- Time taken for tests: 30.574 seconds
- Complete requests: 250000
- Failed requests: 0
- Write errors: 0
- Total transferred: 212000000 bytes
- HTML transferred: 153750000 bytes
- Requests per second: 8176.76 [#/sec] (mean)
- Time per request: 122.298 [ms] (mean)
- Time per request: 0.122 [ms] (mean, across all concurrent requests)
- Transfer rate: 6771.38 [Kbytes/sec] received
-
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 83 543.2 13 15048
- Processing: 0 36 86.0 18 5601
- Waiting: 0 33 84.8 15 5600
- Total: 1 119 551.6 34 15096
-
- Percentage of the requests served within a certain time (ms)
- 50% 34
- 66% 42
- 75% 48
- 80% 52
- 90% 75
- 95% 273
- 98% 1042
- 99% 3027
- 100% 15096 (longest request)
- [root@k8s-master pod]# kubectl get hpa -n myns -w #随着cpu使用率降低,pod副本期望也回归初始值
- NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
- nginx-deploy Deployment/nginx-deploy 99%/15% 1 20 2 68s
- nginx-deploy Deployment/nginx-deploy 68%/15% 1 20 4 75s
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 90s
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 105s
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 2m
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 2m15s
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 7m
- nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 1 7m15s
注意:测试的话,运行一个busybox的pod也可以
- [root@k8s-master pod]# kubectl run -it mybusybox --image=busybox -- sh
- / # while true; do wget -q -O- http://10.108.107.216;done
- [root@k8s-master pod]# ab -c 1000 -n 30000 http://10.108.107.216/
- This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Licensed to The Apache Software Foundation, http://www.apache.org/
-
- Benchmarking 10.108.107.216 (be patient)
- Completed 3000 requests
- Completed 6000 requests
- Completed 9000 requests
- Completed 12000 requests
- Completed 15000 requests
- Completed 18000 requests
- Completed 21000 requests
- Completed 24000 requests
- Completed 27000 requests
- Completed 30000 requests
-
- [root@k8s-master pod]# kubectl get hpa -n myns
- NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
- nginx-deploy Deployment/nginx-deploy 4%/15% 1 20 2 7m50s
-
- [root@k8s-master pod]# kubectl get deploy -n myns
- NAME READY UP-TO-DATE AVAILABLE AGE
- nginx-deploy 2/2 2 2 21m
-
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- nginx-deploy-556cd7b4fd-c6r6p 1/1 Running 0 11m
- nginx-deploy-556cd7b4fd-n9bnk 1/1 Running 0 11m
(1)StatefulSet一般用来部署Zookeeper集群、MySQL集群等,他需要HeadLinessService,一般称其为无头服务。
(2)对于Deployment来说,每一个Pod名称没有顺序(随机字符串),但pod ip时变化的,对于StatefulSet来说,“主机名.无头service的名称.名称空间.svc.cluster.local”是其的唯一标识(可以简写为"主机名.无头service的名称"),可以将这个唯一标识称为dns记录,无头服务能够让这些名称唯一。
(3)无头服务重要性
无头服务不会分配 Cluster IP,它不提供负载均衡或代理功能。
无头服务创建的同时会为pod记录独有的dns记录,如上所述,这会作为其的唯一标识,而通过这个dns直接访问pod,就不会经过服务的负载均衡,利于指定应用程序与特定pod的通信。
无头服务的域名是pod内解析,而不是node上dns解析的,所以在主机上访问域名是访问不到的。
这个例子创建了一个pod副本数为3的statefulset,并为其创建了一个无头服务,最后通过所记录的dns去访问目标
- [root@k8s-master pod]# cat mystatefulset.yaml
- apiVersion: v1
- kind: Service #这里为statefulset创建无头服务
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- selector:
- app: mypod-nginx
- clusterIP: None #需要将clusterip设置为none,禁用负载均衡和代理功能
- ports:
- - name: http
- port: 80
- targetPort: 80
- protocol: TCP
-
- ---
-
- apiVersion: apps/v1
- kind: StatefulSet
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- replicas: 3
- serviceName: my-statefulset #这里就要指定无头服务的名称,引用无头服务
- template:
- metadata:
- labels:
- app: mypod-nginx
- spec:
- containers:
- - name: nginx
- image: nginx
- ports:
- - containerPort: 80
- selector:
- matchLabels:
- app: mypod-nginx
-
- [root@k8s-master pod]# kubectl get pods,service,statefulsets.apps -n myns
- NAME READY STATUS RESTARTS AGE
- pod/my-statefulset-0 1/1 Running 0 4m25s
- pod/my-statefulset-1 1/1 Running 0 4m20s
- pod/my-statefulset-2 1/1 Running 0 4m16s
-
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- service/my-statefulset ClusterIP None <none> 80/TCP 4m25s
-
- NAME READY AGE
- statefulset.apps/my-statefulset 3/3 4m25s
-
- [root@k8s-master pod]# kubectl exec -it my-statefulset-0 -n myns -- /bin/bash
- root@my-statefulset-0:/# curl my-statefulset-0.my-statefulset #进入pod后,我们就通过标识规则记录了的dns去访问服务
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- 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@my-statefulset-0:/# exit
功能和deployment的金丝雀发布相同,不过statefulset的金丝雀发布支持OnDelete和RollingUpdate,下方一一介绍
示例演示
a.创建并应用yaml文件
- [root@k8s-master pod]# cat mystatefulset.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- selector:
- app: mypod-nginx
- clusterIP: None
- ports:
- - name: http
- port: 80
- targetPort: 80
- protocol: TCP
-
- ---
-
- apiVersion: apps/v1
- kind: StatefulSet
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- replicas: 3
- serviceName: my-statefulset
- template:
- metadata:
- labels:
- app: mypod-nginx
- spec:
- containers:
- - name: nginx
- image: nginx
- ports:
- - containerPort: 80
- selector:
- matchLabels:
- app: mypod-nginx
- updateStrategy: #这部分为关键代码
- rollingUpdate: #滚动更新参数
- partition: 1 #这个参数表示从哪个分区(可以理解为索引值)开始更新,如此文件我们有三个pod,他们的索引是0、1、2,若是指定从1开始更新,那么0索引的pod的镜像不会被更新
- type: RollingUpdate #更新策略
-
- [root@k8s-master pod]# kubectl apply -f mystatefulset.yaml
- service/my-statefulset created
- statefulset.apps/my-statefulset created
b.更改镜像版本,上面是从索引值为1的pod开始更新,我们目前有三个副本,那么第一个副本就不会被更新
- [root@k8s-master pod]# kubectl set image statefulsets my-statefulset nginx=nginx:1.17.1 -n myns
- statefulset.apps/my-statefulset image updated
-
- [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- c.将partition更改(edit)为0,那么又会将第一个副本更新
-
- [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
注意:这个就需要自己进行手动删除了,删除一个更新一个
- [root@k8s-master pod]# cat mystatefulset.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- selector:
- app: mypod-nginx
- clusterIP: None
- ports:
- - name: http
- port: 80
- targetPort: 80
- protocol: TCP
-
- ---
-
- apiVersion: apps/v1
- kind: StatefulSet
- metadata:
- name: my-statefulset
- namespace: myns
- spec:
- replicas: 3
- serviceName: my-statefulset
- template:
- metadata:
- labels:
- app: mypod-nginx
- spec:
- containers:
- - name: nginx
- image: nginx
- ports:
- - containerPort: 80
- selector:
- matchLabels:
- app: mypod-nginx
- updateStrategy: #指定为OnDelete策略
- type: OnDelete
-
- [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image #更新前查看
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
-
- [root@k8s-master pod]# kubectl set image statefulsets my-statefulset nginx=nginx:1.17.1 -n myns
- statefulset.apps/my-statefulset image updated
-
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-statefulset-0 1/1 Running 0 2m29s
- my-statefulset-1 1/1 Running 0 2m24s
- my-statefulset-2 1/1 Running 0 2m18s
- [root@k8s-master pod]# kubectl delete pods my-statefulset-0 -n myns
- pod "my-statefulset-0" deleted
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-statefulset-0 0/1 ContainerCreating 0 3s
- my-statefulset-1 1/1 Running 0 2m40s
- my-statefulset-2 1/1 Running 0 2m34s
- [root@k8s-master pod]# kubectl get pods -n myns
- NAME READY STATUS RESTARTS AGE
- my-statefulset-0 1/1 Running 0 6s
- my-statefulset-1 1/1 Running 0 2m43s
- my-statefulset-2 1/1 Running 0 2m37s
-
- [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
- Image: nginx
- Image ID: docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
-
- [root@k8s-master pod]# kubectl delete pod my-statefulset-1 my-statefulset-2 -n myns
- pod "my-statefulset-1" deleted
- pod "my-statefulset-2" deleted
-
- [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
- Image: nginx:1.17.1
- Image ID: docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。