Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试进行重启,当根据重启策略无效,则会重新新建pod的资源。分为以下几类:
- ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级
- Deployment:通过控制ReplicaSet来控制Pod,并支持滚动升级、回退版本
- Horizontal Pod Autoscaler:可以根据集群负载自动水平调整Pod的数量,实现削峰填谷
- DaemonSet:在集群中的指定Node上运行且仅运行一个副本,一般用于守护进程类的任务
- Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
- Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
- StatefulSet:管理有状态应用,作为 Controller 为 Pod 提供唯一的标识。它可以保证部署和 scale 的顺序,
StatefulSet是为了解决有状态服务的问题
(对应Deployments和ReplicaSets是为无状态服务而设计)。
ReplicaSet
ReplicaSet的主要作用是保证一定数量的pod正常运行,它会持续监听这些Pod的运行状态,一旦Pod发生故障,就会重启或重建。同时它还支持对pod数量的扩缩容和镜像版本的升降级。
ReplicaSet的资源清单文件:
1 | apiVersion: apps/v1 # 版本号 |
需要新了解的配置项就是spec
下面几个选项:
replicas:指定副本数量,其实就是当前rs创建出来的pod的数量,默认为1
selector:选择器,它的作用是建立pod控制器和pod之间的关联关系,采用的Label Selector机制
在pod模板上定义label,在控制器上定义选择器,就可以表明当前控制器能管理哪些pod了
template:模板,就是当前控制器创建pod所使用的模板板
创建pc-replicaset.yaml文件:
1 | apiVersion: apps/v1 kind: ReplicaSet metadata: name: pc-replicaset namespace: dev spec: replicas: 3 selector: matchLabels: app: nginx-pod template: metadata: labels: app: nginx-pod spec: containers: - name: nginx image: nginx:1.17.1 |
1 | # 创建 |
两种方式实现扩缩容
1 | # 编辑rs的副本数量,修改spec:replicas: 6即可 [root@master ~]# kubectl edit rs pc-replicaset -n dev replicaset.apps/pc-replicaset edited # 当然也可以直接使用命令实现 # 使用scale命令, 后面--replicas=n直接指定目标数量即可 [root@master ~]# kubectl scale rs pc-replicaset --replicas=2 -n dev replicaset.apps/pc-replicaset scaled |
Deployment
这种控制器并不直接管理pod,而是通过管理ReplicaSet来简介管理Pod
在创建一个Deployment时,会自动创建一个ReplicaSet,然后由ReplicaSet去创建POD。在Deployment升级时,旧的ReplicaSet会保留,但pod都会升级到新的ReplicaSet上。
Deployment的资源清单文件:
1 | apiVersion: apps/v1 # 版本号 |
创建
创建pc-deployment.yaml:
1 | apiVersion: apps/v1 |
1 | [root@master ~]# kubectl create -f pc-deployment.yaml deployment.apps/nginx-deployment created [root@master ~]# kubectl get deploy nginx-deployment -n dev NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 8s [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE nginx-deployment-68ddf649b8-cnf8q 1/1 Running 0 25s nginx-deployment-68ddf649b8-dtwbc 1/1 Running 0 25s nginx-deployment-68ddf649b8-xwsqs 1/1 Running 0 26s |
扩容
1 | # 变更副本数量为5个 [root@master ~]# kubectl scale deploy nginx-deployment --replicas=5 -n dev deployment.apps/nginx-deployment scaled #查看 [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE nginx-deployment-68ddf649b8-bq8n2 1/1 Running 0 5s nginx-deployment-68ddf649b8-cnf8q 1/1 Running 0 24m nginx-deployment-68ddf649b8-dtwbc 1/1 Running 0 24m nginx-deployment-68ddf649b8-sspml 1/1 Running 0 4s nginx-deployment-68ddf649b8-xwsqs 1/1 Running 0 24m # 直接修改yaml文件,修改spec:replicas: 4即可 [root@master ~]# kubectl edit deploy pc-deployment -n dev deployment.apps/pc-deployment edited |
镜像更新
- 通过set 命令直接修改image的版本进行升级:
kubectl set image deployment/nginx-deployment nginx=nginx:1.17.2
- 使用
kubectl edit deployment nginx-deployment
来修改image的值。
deployment支持两种更新策略:重建更新
和滚动更新
,可以通过strategy
指定策略类型,支持两个属性:
1 | strategy:指定新的Pod替换旧的Pod的策略, 支持两个属性: |
- Recreate这种方式通常不用
1 | # 变更镜像 [root@master ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.2 -n dev deployment.apps/pc-deployment image updated # 观察升级过程 [root@master ~]# kubectl get pods -n dev -w NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-65qcw 1/1 Running 0 31s pc-deployment-5d89bdfbf9-w5nzv 1/1 Running 0 31s pc-deployment-5d89bdfbf9-xpt7w 1/1 Running 0 31s pc-deployment-5d89bdfbf9-xpt7w 1/1 Terminating 0 41s pc-deployment-5d89bdfbf9-65qcw 1/1 Terminating 0 41s pc-deployment-5d89bdfbf9-w5nzv 1/1 Terminating 0 41s pc-deployment-675d469f8b-grn8z 0/1 Pending 0 0s pc-deployment-675d469f8b-hbl4v 0/1 Pending 0 0s pc-deployment-675d469f8b-67nz2 0/1 Pending 0 0s pc-deployment-675d469f8b-grn8z 0/1 ContainerCreating 0 0s pc-deployment-675d469f8b-hbl4v 0/1 ContainerCreating 0 0s pc-deployment-675d469f8b-67nz2 0/1 ContainerCreating 0 0s pc-deployment-675d469f8b-grn8z 1/1 Running 0 1s pc-deployment-675d469f8b-67nz2 1/1 Running 0 1s pc-deployment-675d469f8b-hbl4v 1/1 Running 0 2s |
- rollingUpdate
1 | # 变更镜像 [root@master ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.3 -n dev deployment.apps/pc-deployment image updated # 观察升级过程 [root@master ~]# kubectl get pods -n dev -w NAME READY STATUS RESTARTS AGE pc-deployment-c848d767-8rbzt 1/1 Running 0 31m pc-deployment-c848d767-h4p68 1/1 Running 0 31m pc-deployment-c848d767-hlmz4 1/1 Running 0 31m pc-deployment-c848d767-rrqcn 1/1 Running 0 31m pc-deployment-966bf7f44-226rx 0/1 Pending 0 0s pc-deployment-966bf7f44-226rx 0/1 ContainerCreating 0 0s pc-deployment-966bf7f44-226rx 1/1 Running 0 1s pc-deployment-c848d767-h4p68 0/1 Terminating 0 34m pc-deployment-966bf7f44-cnd44 0/1 Pending 0 0s pc-deployment-966bf7f44-cnd44 0/1 ContainerCreating 0 0s pc-deployment-966bf7f44-cnd44 1/1 Running 0 2s pc-deployment-c848d767-hlmz4 0/1 Terminating 0 34m pc-deployment-966bf7f44-px48p 0/1 Pending 0 0s pc-deployment-966bf7f44-px48p 0/1 ContainerCreating 0 0s pc-deployment-966bf7f44-px48p 1/1 Running 0 0s pc-deployment-c848d767-8rbzt 0/1 Terminating 0 34m pc-deployment-966bf7f44-dkmqp 0/1 Pending 0 0s pc-deployment-966bf7f44-dkmqp 0/1 ContainerCreating 0 0s pc-deployment-966bf7f44-dkmqp 1/1 Running 0 2s pc-deployment-c848d767-rrqcn 0/1 Terminating 0 34m |
版本Rollback
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能,创建deployment时要增加–record参数,才能看到revision的变化。
kubectl rollout: 版本升级相关功能,支持下面的选项:
- status 显示当前升级状态
- history 显示 升级历史记录
- pause 暂停版本升级过程
- resume 继续已经暂停的版本升级过程
- restart 重启版本升级过程
- undo 回滚到上一级版本(可以使用–to-revision回滚到指定版本)
Horizontal Pod Autoscaler(HPA)
HPA可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod的数量的调整。
环境部署
安装metrics-server
metrics-server可以用来收集集群中的资源使用情况
1
# 安装git [root@master ~]# yum install git -y # 获取metrics-server [root@master ~]# git clone -b v0.3.6 https://github.com/kubernetes-incubator/metrics-server # 修改deployment, 注意修改的是镜像和初始化参数 [root@master ~]# cd /root/metrics-server/deploy/1.8+/ [root@master 1.8+]# vim metrics-server-deployment.yaml metadata: name: metrics-server namespace: kube-system labels: k8s-app: metrics-server spec: selector: matchLabels: k8s-app: metrics-server template: metadata: name: metrics-server labels: k8s-app: metrics-server spec: hostNetwork: true serviceAccountName: metrics-server volumes: # mount in tmp so we can safely use from-scratch images and/or read-only containers - name: tmp-dir emptyDir: {} containers: - name: metrics-server image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6 imagePullPolicy: Always args: - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP volumeMounts: - name: tmp-dir mountPath: /tmp # 安装metrics-server [root@master 1.8+]# kubectl apply -f ./ # 验证 [root@master ~]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% node1 144m 7% 377Mi 21% node2 186m 9% 351Mi 18%
deployment和servie
1
# 创建deployment [root@master ~]# kubectl run nginx --image=nginx:1.17.1 --requests=cpu=100m -n dev deployment.apps/nginx created # 创建service [root@master ~]# kubectl expose deploy nginx --port=80 --target-port=80 --type=NodePort -n dev service/nginx exposed # 查看 [root@master ~]# kubectl get deployment,pod,svc -n dev NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx 1/1 1 1 47s NAME READY STATUS RESTARTS AGE pod/nginx-7df9756ccc-bh8dr 1/1 Running 0 47s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/nginx NodePort 10.101.18.29 <none> 80:31830/TCP 35s
创建pc-hpa.yaml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: pc-hpa
namespace: dev
spec:
minReplicas: 1 #最小pod数量
maxReplicas: 10 #最大pod数量
targetCPUUtilizationPercentage: 3 # CPU使用率指标
scaleTargetRef: # 指定要控制的nginx信息
apiVersion: /v1
kind: Deployment
name: nginx1
# 创建hpa [root@master ~]# kubectl create -f pc-hpa.yaml horizontalpodautoscaler.autoscaling/pc-hpa created # 查看hpa [root@master ~]# kubectl get hpa -n dev NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE pc-hpa Deployment/nginx 0%/3% 1 10 1 62s
验证
对http://192.168.36.10:30668/
压测,查看变化
1 | [root@k8s-master01 ~]# kubectl get hpa -n dev -w NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE pc-hpa Deployment/nginx 0%/3% 1 10 1 4m11s pc-hpa Deployment/nginx 0%/3% 1 10 1 5m19s pc-hpa Deployment/nginx 22%/3% 1 10 1 6m50s pc-hpa Deployment/nginx 22%/3% 1 10 4 7m5s pc-hpa Deployment/nginx 22%/3% 1 10 8 7m21s pc-hpa Deployment/nginx 6%/3% 1 10 8 7m51s pc-hpa Deployment/nginx 0%/3% 1 10 8 9m6s pc-hpa Deployment/nginx 0%/3% 1 10 8 13m pc-hpa Deployment/nginx 0%/3% 1 10 1 14m |
剩下的控制器不做过多学习验证,用到的时候可以直接官方文档。