pod控制器详解
1.ReplicaSet(RS)
ReplicaSet的主要作用是保证一定数量的pod正常运行,它会持续监听这些Pod的运行状态,一旦Pod发生故障,就会重启或重建。同时它还支持对pod数量的扩缩容和镜像版本的升降级
它的资源清单文件有:
apiVersion: apps/v1 # 版本号
kind: ReplicaSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: rs
spec: # 详情描述
replicas: 3 # 副本数量
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
创建ReplicaSet
创建yaml文件
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
//创建rs
[root@master ~]# kubectl create -f pc-replicaset.yaml
replicaset.apps/pc-replicaset created
//查看rs
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 3 3 3 22s nginx nginx:1.17.1 app=nginx-pod
//查看控制器创建出来的pod
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pc-replicaset-6vmvt 1/1 Running 0 54s
pc-replicaset-fmb8f 1/1 Running 0 54s
pc-replicaset-snrk2 1/1 Running 0 54s
扩缩容
//修改rs的副本数量
[root@master ~]# vim pc-replicaset.yaml
spec:
replicas: 6
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
//查看pod
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-replicaset-6vmvt 1/1 Running 0 114m
pc-replicaset-cftnp 1/1 Running 0 10s
pc-replicaset-fjlm6 1/1 Running 0 10s
pc-replicaset-fmb8f 1/1 Running 0 114m
pc-replicaset-s2whj 1/1 Running 0 10s
pc-replicaset-snrk2 1/1 Running 0 114m
//直接使用命令配置
[root@master ~]# kubectl scale rs pc-replicaset --replicas=2 -n dev
replicaset.apps/pc-replicaset scaled
//查看pod
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-replicaset-6vmvt 0/1 Terminating 0 118m
pc-replicaset-cftnp 0/1 Terminating 0 4m17s
pc-replicaset-fjlm6 0/1 Terminating 0 4m17s
pc-replicaset-fmb8f 1/1 Running 0 118m
pc-replicaset-s2whj 0/1 Terminating 0 4m17s
pc-replicaset-snrk2 1/1 Running 0 118m
//稍等片刻,就只剩下2个了
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-replicaset-fmb8f 1/1 Running 0 119m
pc-replicaset-snrk2 1/1 Running 0 119m
镜像升级
//编辑rs的容器镜像
[root@master ~]# vim pc-replicaset.yaml
image: nginx:1.17.2
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
//再次查看,发现镜像版本已经改变
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-replicaset 2 2 2 140m nginx nginx:1.17.2
//同样也可使用命令完成
[root@master ~]# kubectl set image rs pc-replicaset nginx=nginx:1.17.1 -n dev
replicaset.apps/pc-replicaset image updated
//再次查看,发现镜像版本已经变更了
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-replicaset 2 2 2 145m nginx nginx:1.17.1
2.Deployment(Deploy)
Deployment主要功能有下面几个:
- 支持ReplicaSet的所有功能
- 支持发布的停止、继续
- 支持滚动升级和回滚版本
可配置的Deployment的资源清单文件有:
apiVersion: apps/v1 # 版本号
kind: Deployment # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: deploy
spec: # 详情描述
replicas: 3 # 副本数量
revisionHistoryLimit: 3 # 保留历史版本
paused: false # 暂停部署,默认是false
progressDeadlineSeconds: 600 # 部署超时时间(s),默认是600
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数
maxUnavailable: 30% # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
创建deployment
//创建yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
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
//创建deployment
[root@master ~]# kubectl create -f pc-deployment.yaml --record=true
deployment.apps/pc-deployment created
//查看deployment
UP-TO-DATE 最新版本的pod的数量
AVAILABLE 当前可用的pod的数量
[root@master ~]# kubectl get deploy pc-deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 3/3 3 3 15s
//查看rs
[root@k8s-master01 ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6696798b78 3 3 3 23s
//查看pod
[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1 Running 0 107s
pc-deployment-6696798b78-smpvp 1/1 Running 0 107s
pc-deployment-6696798b78-wvjd8 1/1 Running 0 107s
扩缩容
//变更副本数量为5个
[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
deployment.apps/pc-deployment scaled
//查看deployment
[root@master ~]# kubectl get deploy pc-deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 5/5 5 5 2m
//查看pod
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1 Running 0 4m19s
pc-deployment-6696798b78-jxmdq 1/1 Running 0 94s
pc-deployment-6696798b78-mktqv 1/1 Running 0 93s
pc-deployment-6696798b78-smpvp 1/1 Running 0 4m19s
pc-deployment-6696798b78-wvjd8 1/1 Running 0 4m19s
//deployment的副本数量
[root@master ~]# vim pc-deployment.yaml
spec:replicas: 4
[root@master ~]# kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
# 查看pod
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1 Running 0 5m23s
pc-deployment-6696798b78-jxmdq 1/1 Running 0 2m38s
pc-deployment-6696798b78-smpvp 1/1 Running 0 5m23s
pc-deployment-6696798b78-wvjd8 1/1 Running 0 5m23s
镜像更新
deployment支持两种更新策略:重建更新
和滚动更新
,可以通过strategy
指定策略类型,支持两个属性
滚动更新:
1.编辑pc-deployment.yaml,在spec节点下添加更新策略
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
2.创建deploy进行验证
//变更镜像
[root@k8s-master01 ~]# 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
至此,新版本的pod创建完毕,旧版本的pod销毁完毕
中间过程是滚动进行的,也就是边销毁边创建
版本回退
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能,下面具体来看.
kubectl rollout: 版本升级相关功能,支持下面的选项:
- status 显示当前升级状态
- history 显示 升级历史记录
- pause 暂停版本升级过程
- resume 继续已经暂停的版本升级过程
- restart 重启版本升级过程
- undo 回滚到上一级版本(可以使用--to-revision回滚到指定版本)
//查看当前升级版本的状态
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
//查看升级历史记录
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
//可以发现有三次版本记录,说明完成过两次升级
//版本回退
[root@master ~]# kubectl rollout undo deployment pc-deployment --to-revision1
deployment.apps/pc-deployment rolled back
//查看发现,通过nginx镜像版本可以发现到了第一版
[root@master ~]# kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES
pc-deployment 4/4 4 4 74m nginx nginx:1.17.1
//
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6696798b78 2 2 2 24h
pc-deployment-966bf7f44 0 0 0 24h
pc-deployment-c848d767 0 0 0 24h
金丝雀发布
Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作。
比如有一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布
//更新deployment的版本,并配置暂停deployment
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
//观察更新状态
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-deployment-5d89bdfbf9 3 3 3 19m nginx nginx:1.17.1
pc-deployment-675d469f8b 0 0 0 14m nginx nginx:1.17.2
pc-deployment-6c9f56fcfb 2 2 2 3m16s nginx nginx:1.17.4
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-5d89bdfbf9-rj8sq 1/1 Running 0 7m33s
pc-deployment-5d89bdfbf9-ttwgg 1/1 Running 0 7m35s
pc-deployment-5d89bdfbf9-v4wvc 1/1 Running 0 7m34s
pc-deployment-6c9f56fcfb-996rt 1/1 Running 0 3m31s
pc-deployment-6c9f56fcfb-j2gtj 1/1 Running 0 3m31s
//确保更新的pod没问题了,继续更新
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
//查看最后的更新情况
[root@master ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-deployment-5d89bdfbf9 0 0 0 21m nginx nginx:1.17.1
pc-deployment-675d469f8b 0 0 0 16m nginx nginx:1.17.2
pc-deployment-6c9f56fcfb 4 4 4 5m11s nginx nginx:1.17.4
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6c9f56fcfb-7bfwh 1/1 Running 0 37s
pc-deployment-6c9f56fcfb-996rt 1/1 Running 0 5m27s
pc-deployment-6c9f56fcfb-j2gtj 1/1 Running 0 5m27s
pc-deployment-6c9f56fcfb-rf84v 1/1 Running 0 37s
删除Deployment
//删除deployment,其下的rs和pod也将被删除
[root@master ~]# kubectl delete -f pc-deployment.yaml
deployment.apps "pc-deployment" deleted
3.Horizontal Pod Autoscaler(HPA)
Kubernetes期望可以实现通过监测Pod的使用情况,实现pod数量的自动调整,于是就产生了Horizontal Pod Autoscaler(HPA)这种控制器
1.安装metrics-server
安装metrics-server
//安装git
[root@master ~]# yum -y install git
//获取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
按如下yaml文件进行修改
//安装metrics-server
[root@master 1.8+]# kubectl apply -f ./
//查看pod运行情况
[root@master 1.8+]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
metrics-server-9c7dc6fdc-5589z 1/1 Running 0 9s
//使用kubectl top node查看资源使用情况
[root@master 1.8+]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master-01 170m 8% 1022Mi 35%
k8s-node-01 45m 2% 367Mi 12%
k8s-node-02 39m 1% 286Mi 9%
[root@master 1.8+]# kubectl top pod -n kube-system
NAME CPU(cores) MEMORY(bytes)
coredns-7d7778b79-dlvkp 3m 15Mi
coredns-7d7778b79-sdxkg 3m 15Mi
//至此,metrics-server安装完成
修改metrics-server-deployment.yaml文件
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
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
2. 准备创建deployment和servie
//创建deployment
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: dev
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
cpu: 10m
limits:
cpu: 10m
---
//创建service
kind: Service
apiVersion: v1
metadata:
name: nginx
namespace: dev
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: nginx
type: NodePort
//创建deployment和servie
[root@master ~]# kubectl create -f nginx.yaml
deployment.apps/nginx created
service/nginx created
//查看
[root@master ~]# kubectl get deployment,pod,svc -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 48s
NAME READY STATUS RESTARTS AGE
pod/nginx-d657f44c6-dltql 1/1 Running 0 48s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx NodePort 10.96.239.131 <none> 80:30080/TCP 48s
3. 部署HPA
创建pc-hpa.yaml文件
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
name: nginx
namespace: dev
spec:
maxReplicas: 10 # 最大pod数量
minReplicas: 1 # 最小pod数量
scaleTargetRef: # 指定要控制的nginx信息
kind: Deployment
name: nginx
apiVersion: apps/v1
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 3 # CPU使用率指标
//创建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
nginx Deployment/nginx 0%/3% 1 10 1 18s
4. 测试
hpa变化
[root@master ~]# kubectl get hpa -n dev -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx 0%/3% 1 10 1 7m19s
nginx Deployment/nginx 110%/3% 1 10 1 7m30s
nginx Deployment/nginx 110%/3% 1 10 4 7m45s
nginx Deployment/nginx 110%/3% 1 10 8 8m
nginx Deployment/nginx 110%/3% 1 10 10 8m15s
nginx Deployment/nginx 100%/3% 1 10 10 8m30s
nginx Deployment/nginx 0%/3% 1 10 10 9m31s
nginx Deployment/nginx 0%/3% 1 10 10 14m
nginx Deployment/nginx 0%/3% 1 10 10 15m
deployment变化
[root@master ~]# kubectl get deployment -n dev -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 10m
nginx 1/4 1 1 11m
nginx 1/4 1 1 11m
nginx 1/4 1 1 11m
nginx 1/4 4 1 11m
nginx 1/8 4 1 11m
nginx 1/8 4 1 11m
nginx 1/8 4 1 11m
nginx 1/8 8 1 11m
nginx 1/10 8 1 11m
nginx 1/10 8 1 11m
nginx 1/10 8 1 11m
nginx 1/10 10 1 11m
nginx 2/10 10 2 11m
nginx 3/10 10 3 11m
nginx 4/10 10 4 12m
nginx 5/10 10 5 12m
nginx 6/10 10 6 12m
nginx 7/10 10 7 12m
nginx 8/10 10 8 12m
nginx 9/10 10 9 12m
nginx 10/10 10 10 13m
nginx 10/1 10 10 18m
nginx 10/1 10 10 18m
nginx 1/1 1 1 18m
pod变化
[root@master ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
nginx-d657f44c6-dltql 1/1 Running 0 11m
nginx-d657f44c6-dbtbq 0/1 Pending 0 0s
nginx-d657f44c6-5hxf6 0/1 Pending 0 0s
nginx-d657f44c6-f8mw6 0/1 Pending 0 0s
nginx-d657f44c6-dbtbq 0/1 ContainerCreating 0 0s
nginx-d657f44c6-5hxf6 0/1 ContainerCreating 0 0s
nginx-d657f44c6-f8mw6 0/1 ContainerCreating 0 0s
nginx-d657f44c6-ctgtc 0/1 Pending 0 0s
nginx-d657f44c6-n6h6d 0/1 Pending 0 0s
nginx-d657f44c6-jhlp5 0/1 Pending 0 0s
nginx-d657f44c6-brdnf 0/1 Pending 0 0s
nginx-d657f44c6-ctgtc 0/1 ContainerCreating 0 0s
nginx-d657f44c6-n6h6d 0/1 ContainerCreating 0 0s
nginx-d657f44c6-jhlp5 0/1 ContainerCreating 0 0s
nginx-d657f44c6-brdnf 0/1 ContainerCreating 0 0s
nginx-d657f44c6-28jcf 0/1 Pending 0 0s
nginx-d657f44c6-7wff5 0/1 Pending 0 0s
nginx-d657f44c6-28jcf 0/1 ContainerCreating 0 0s
nginx-d657f44c6-7wff5 0/1 ContainerCreating 0 0s
nginx-d657f44c6-5hxf6 1/1 Running 0 38s
nginx-d657f44c6-jhlp5 1/1 Running 0 35s
nginx-d657f44c6-f8mw6 1/1 Running 0 64s
nginx-d657f44c6-ctgtc 1/1 Running 0 50s
nginx-d657f44c6-dbtbq 1/1 Running 0 72s
nginx-d657f44c6-28jcf 1/1 Running 0 52s
nginx-d657f44c6-brdnf 1/1 Running 0 84s
nginx-d657f44c6-n6h6d 1/1 Running 0 99s
nginx-d657f44c6-7wff5 1/1 Running 0 95s
nginx-d657f44c6-5hxf6 1/1 Terminating 0 7m46s
nginx-d657f44c6-28jcf 1/1 Terminating 0 7m16s
nginx-d657f44c6-brdnf 1/1 Terminating 0 7m31s
nginx-d657f44c6-n6h6d 1/1 Terminating 0 7m31s
nginx-d657f44c6-7wff5 1/1 Terminating 0 7m16s
nginx-d657f44c6-ctgtc 1/1 Terminating 0 7m31s
nginx-d657f44c6-jhlp5 1/1 Terminating 0 7m31s
nginx-d657f44c6-f8mw6 1/1 Terminating 0 7m46s
nginx-d657f44c6-dbtbq 1/1 Terminating 0 7m46s
nginx-d657f44c6-n6h6d 0/1 Terminating 0 7m32s
nginx-d657f44c6-7wff5 0/1 Terminating 0 7m17s
DaemonSet(DS)
DaemonSet类型的控制器可以保证在集群中的每一台(或指定)节点上都运行一个副本。一般适用于日志收集、节点监控等场景。也就是说,如果一个Pod提供的功能是节点级别的(每个节点都需要且只需要一个),那么这类Pod就适合使用DaemonSet类型的控制器创建
DaemonSet控制器的特点:
-
每当向集群中添加一个节点时,指定的 Pod 副本也将添加到该节点上
-
当节点从集群中移除时,Pod 也就被垃圾回收了
可配置的DaemonSet的资源清单文件
apiVersion: apps/v1 # 版本号
kind: DaemonSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: daemonset
spec: # 详情描述
revisionHistoryLimit: 3 # 保留历史版本
updateStrategy: # 更新策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxUnavailable: 1 # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
创建pc-daemonset.yaml文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: pc-daemonset
namespace: dev
spec:
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
创建pod观察效果
//创建daemonset
[root@master ~]# kubectl create -f pc-daemonset.yaml
daemonset.apps/pc-daemonset created
//查看daemonset
[root@master ~]# kubectl get ds -n dev -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
pc-daemonset 2 2 0 2 0 <none> 20s nginx nginx:1.17.1 app=nginx-pod
//查看pod,发现在每个Node上都运行一个pod
[root@master ~]# kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-daemonset-46jp5 1/1 Running 0 103s 10.244.1.7 k8s-node-01 <none> <none>
pc-daemonset-xqd5w 1/1 Running 0 103s 10.244.2.8 k8s-node-02 <none> <none>
//删除daemonset
[root@master ~]# kubectl delete -f pc-daemonset.yaml
daemonset.apps "pc-daemonset" deleted
Job
ob,主要用于负责批量处理(一次要处理指定数量任务)短暂的一次性(每个任务仅运行一次就结束)任务。Job特点如下:
-
当Job创建的pod执行成功结束时,Job将记录成功结束的pod数量
-
当成功结束的pod达到指定的数量时,Job将完成执行
可配置的Job资源清单文件有
apiVersion: batch/v1 # 版本号
kind: Job # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: job
spec: # 详情描述
completions: 1 # 指定job需要成功运行Pods的次数。默认值: 1
parallelism: 1 # 指定job在任一时刻应该并发运行Pods的数量。默认值: 1
activeDeadlineSeconds: 30 # 指定job可运行的时间期限,超过时间还未结束,系统将会尝试进行终止。
backoffLimit: 6 # 指定job失败后进行重试的次数。默认是6
manualSelector: true # 是否可以使用selector选择器选择pod,默认是false
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: counter-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [counter-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 2;done"]
创建pc-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
selector:
matchLabels:
app: counter-pod
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
创建pod观察效果
//创建job
[root@master ~]# kubectl create -f pc-job.yaml
job.batch/pc-job created
//查看job
[root@master ~]# kubectl get job -n dev -o wide -w
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-job 0/1 0s 0s counter busybox:1.30 app=counter-pod
pc-job 1/1 28s 28s counter busybox:1.30 app=counter-pod
//通过观察pod状态可以看到,pod在运行完毕任务后,就会变成Completed状态
[root@master ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-6wk89 1/1 Running 0 1s
pc-job-6wk89 0/1 Completed 0 28s
//直接更新是不行的,对于同一个Job,这是不可变参数
[root@master ~]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
//然后重新运行job,观察效果,此时会发现,job会每次运行3个pod,总共执行了3个pod
[root@master ~]# kubectl create -f pc-job.yaml
job.batch/pc-job created
[root@master ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
nginx-d657f44c6-dltql 1/1 Running 0 105m
pc-job-6r2hl 0/1 Pending 0 0s
pc-job-6r2hl 0/1 Pending 0 0s
pc-job-zk9qn 0/1 Pending 0 0s
pc-job-hzf49 0/1 Pending 0 0s
pc-job-hzf49 0/1 Pending 0 0s
pc-job-zk9qn 0/1 Pending 0 0s
pc-job-6r2hl 0/1 ContainerCreating 0 0s
pc-job-zk9qn 0/1 ContainerCreating 0 0s
pc-job-hzf49 0/1 ContainerCreating 0 0s
pc-job-hzf49 1/1 Running 0 3s
pc-job-6r2hl 1/1 Running 0 3s
pc-job-hzf49 0/1 Completed 0 29s
pc-job-6r2hl 0/1 Completed 0 29s
pc-job-zk9qn 1/1 Running 0 44s
pc-job-zk9qn 0/1 Completed 0 72s
//删除job
[root@master ~]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
关于job的重启策略,有以下说明:
1.如果指定为OnFailure,则job会在pod出现故障时重启容器,而不是创建pod,failed次数不变
2.如果指定为Never,则job会在pod出现故障时创建新的pod,并且故障pod不会消失,也不会重启,failed次数加1
3.如果指定为Always的话,就意味着一直重启,意味着job任务会重复去执行了,当然不对,所以不能设置为Always
CronJob(CJ)
CronJob控制器以Job控制器资源为其管控对象,并借助它管理pod资源对象,Job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但CronJob可以以类似于Linux操作系统的 周期性计划任务的方式 控制其运行 时间点 及 重复运行 的方式。也就是说,CronJob可以在特定的时间点(反复的)去运行job任务
可配置的CronJob的资源清单文件有
apiVersion: batch/v1beta1 # 版本号
kind: CronJob # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: cronjob
spec: # 详情描述
schedule: # cron格式的作业调度运行时间点,用于控制任务在什么时间执行
concurrencyPolicy: # 并发执行策略,用于定义前一次作业运行尚未完成时是否以及如何运行后一次的作业
failedJobHistoryLimit: # 为失败的任务执行保留的历史记录数,默认为1
successfulJobHistoryLimit: # 为成功的任务执行保留的历史记录数,默认为3
startingDeadlineSeconds: # 启动作业错误的超时时长
jobTemplate: # job控制器模板,用于为cronjob控制器生成job对象;下面其实就是job的定义
metadata:
spec:
completions: 1
parallelism: 1
activeDeadlineSeconds: 30
backoffLimit: 6
manualSelector: true
selector:
matchLabels:
app: counter-pod
matchExpressions: 规则
- {key: app, operator: In, values: [counter-pod]}
template:
metadata:
labels:
app: counter-pod
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 20;done"]
创建pc-cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: pc-cronjob
namespace: dev
labels:
controller: cronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
metadata:
spec:
template:
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox:1.30
command: ["bin/sh","-c","for i in 9 8 7 6 5 4 3 2 1; do echo $i;sleep 3;done"]
创建cronjob
//创建cronjob
[root@master ~]# kubectl create -f pc-job.yaml
job.batch/pc-job created
//查看cronjob
[root@master ~]# kubectl get cronjobs -n dev
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
pc-cronjob */1 * * * * False 0 <none> 2s
//查看job
[root@master ~]# kubectl get jobs -n dev
NAME COMPLETIONS DURATION AGE
pc-cronjob-27143584 1/1 27s 2m20s
pc-cronjob-27143585 1/1 28s 80s
pc-cronjob-27143586 0/1 20s 20s
//查看pod
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-d657f44c6-dltql 1/1 Running 0 127m
pc-cronjob-27143584-ntw6x 0/1 Completed 0 2m3s
pc-cronjob-27143585-66f9p 0/1 Completed 0 63s
pc-cronjob-27143586-sqnsj 1/1 Running 0 3s
//删除cronjob
[root@master ~]# kubectl delete -f pc-cronjob.yaml
cronjob.batch "pc-cronjob" deleted