pod控制器
pod控制器分类
RS(ReplicaSet)
[root@master ~]# vim pc-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
[root@master ~]# kubectl create -f pc-replicaset.yaml
replicaset.apps/pc-replicaset created
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 3 3 3 26s nginx nginx:1.17.1 app=nginx-pod
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 6 6 6 3m4s nginx nginx:1.17.1 app=nginx-pod
[root@master ~]# kubectl scale rs pc-replicaset --replicas=2 -n dev
replicaset.apps/pc-replicaset scaled
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 4m17s nginx nginx:1.17.1 app=nginx-pod
[root@master ~]# kubectl edit rs pc-replicaset -n dev
replicaset.apps/pc-replicaset edited
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 5m3s nginx nginx:1.17.2 app=nginx-pod
[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 pc-replicaset -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 6m14s nginx nginx:1.17.1 app=nginx-pod
[root@master ~]# kubectl delete -f pc-replicaset.yaml
replicaset.apps "pc-replicaset" deleted
[root@master ~]# kubectl get rs pc-replicaset -n dev -o wide
Error from server (NotFound): replicasets.apps "pc-replicaset" not found
Deployment
[root@master ~]# vim pc-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
[root@master ~]# kubectl create -f pc-deployment.yaml
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy pc-deployment -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 3/3 3 3 28s nginx nginx:1.17.1 app=nginx-pod
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 3 3 3 48s
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-5d89bdfbf9-6dfrr 1/1 Running 0 85s
pc-deployment-5d89bdfbf9-jl85p 1/1 Running 0 85s
pc-deployment-5d89bdfbf9-vmj99 1/1 Running 0 85s
[root@master ~]# vim pc-deployment.yaml
[root@master ~]# kubectl apply -f pc-deployment.yaml
deployment.apps/pc-deployment configured
[root@master ~]# kubectl set image deploy 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-6dfrr 1/1 Running 0 16m
pc-deployment-5d89bdfbf9-jl85p 1/1 Running 0 16m
pc-deployment-5d89bdfbf9-vmj99 1/1 Running 0 16m
pc-deployment-5d89bdfbf9-jl85p 1/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-6dfrr 1/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-vmj99 1/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-jl85p 0/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-vmj99 0/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-6dfrr 0/1 Terminating 0 17m
pc-deployment-5d89bdfbf9-6dfrr 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-6dfrr 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-jl85p 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-jl85p 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-vmj99 0/1 Terminating 0 18m
pc-deployment-5d89bdfbf9-vmj99 0/1 Terminating 0 18m
pc-deployment-675d469f8b-c6kcv 0/1 Pending 0 0s
pc-deployment-675d469f8b-c6kcv 0/1 Pending 0 0s
pc-deployment-675d469f8b-xhgkf 0/1 Pending 0 0s
pc-deployment-675d469f8b-fr8vm 0/1 Pending 0 0s
pc-deployment-675d469f8b-xhgkf 0/1 Pending 0 0s
pc-deployment-675d469f8b-fr8vm 0/1 Pending 0 0s
pc-deployment-675d469f8b-c6kcv 0/1 ContainerCreating 0 1s
pc-deployment-675d469f8b-xhgkf 0/1 ContainerCreating 0 2s
pc-deployment-675d469f8b-fr8vm 0/1 ContainerCreating 0 2s
pc-deployment-675d469f8b-xhgkf 1/1 Running 0 3s
pc-deployment-675d469f8b-c6kcv 1/1 Running 0 3s
pc-deployment-675d469f8b-fr8vm 1/1 Running 0 3s
[root@master ~]# vim pc-deployment.yaml
[root@master ~]# kubectl apply -f pc-deployment.yaml
deployment.apps/pc-deployment configured
[root@master ~]# kubectl set image deploy 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-5d89bdfbf9-ctm4h 1/1 Running 0 38s
pc-deployment-5d89bdfbf9-dnpn2 1/1 Running 0 40s
pc-deployment-5d89bdfbf9-v6m47 1/1 Running 0 37s
pc-deployment-7865c58bdf-77wgs 0/1 Pending 0 0s
pc-deployment-7865c58bdf-77wgs 0/1 Pending 0 0s
pc-deployment-7865c58bdf-77wgs 0/1 ContainerCreating 0 0s
pc-deployment-7865c58bdf-77wgs 1/1 Running 0 1s
pc-deployment-5d89bdfbf9-v6m47 1/1 Terminating 0 57s
pc-deployment-7865c58bdf-jgh5g 0/1 Pending 0 0s
pc-deployment-7865c58bdf-jgh5g 0/1 Pending 0 0s
pc-deployment-7865c58bdf-jgh5g 0/1 ContainerCreating 0 0s
pc-deployment-7865c58bdf-jgh5g 1/1 Running 0 1s
pc-deployment-5d89bdfbf9-v6m47 0/1 Terminating 0 58s
pc-deployment-5d89bdfbf9-ctm4h 1/1 Terminating 0 59s
pc-deployment-5d89bdfbf9-v6m47 0/1 Terminating 0 58s
pc-deployment-7865c58bdf-wjvc7 0/1 Pending 0 0s
pc-deployment-7865c58bdf-wjvc7 0/1 Pending 0 0s
pc-deployment-7865c58bdf-wjvc7 0/1 ContainerCreating 0 1s
pc-deployment-5d89bdfbf9-ctm4h 0/1 Terminating 0 60s
pc-deployment-7865c58bdf-wjvc7 1/1 Running 0 1s
pc-deployment-5d89bdfbf9-dnpn2 1/1 Terminating 0 62s
pc-deployment-5d89bdfbf9-dnpn2 0/1 Terminating 0 63s
pc-deployment-5d89bdfbf9-dnpn2 0/1 Terminating 0 64s
pc-deployment-5d89bdfbf9-dnpn2 0/1 Terminating 0 64s
pc-deployment-5d89bdfbf9-ctm4h 0/1 Terminating 0 70s
pc-deployment-5d89bdfbf9-ctm4h 0/1 Terminating 0 70s
pc-deployment-5d89bdfbf9-v6m47 0/1 Terminating 0 69s
pc-deployment-5d89bdfbf9-v6m47 0/1 Terminating 0 69s
[root@master ~]# kubectl create -f pc-deployment.yaml --record deployment.apps/pc-deployment created
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 0 0 0 59s
pc-deployment-675d469f8b 3 3 3 29s
taint1-766c47bf55 1 1 1 24h
taint2-84946958cf 1 1 1 24h
[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 kubectl create --filename=pc-deployment.yaml --record=true
2 kubectl create --filename=pc-deployment.yaml --record=true
[root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
2 kubectl create --filename=pc-deployment.yaml --record=true
3 kubectl create --filename=pc-deployment.yaml --record=true
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 3 3 3 18m
pc-deployment-675d469f8b 0 0 0 18m
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.3 -n dev && kubectl rollout pause deploy pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 3 3 3 21m
pc-deployment-675d469f8b 0 0 0 20m
pc-deployment-7865c58bdf 1 1 1 8s
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 0 0 0 23m
pc-deployment-675d469f8b 0 0 0 22m
pc-deployment-7865c58bdf 3 3 3 114s
HPA(Horizontal Pod Autoscaler)
# 安装git
[root@k8s-master01 ~]# yum install git -y
# 获取metrics-server, 注意使用的版本
[root@k8s-master01 ~]# git clone -b v0.3.6 https://github.com/kubernetes-incubator/metrics-server
# 修改deployment, 注意修改的是镜像和初始化参数
[root@k8s-master01 ~]# cd /root/metrics-server/deploy/1.8+/
[root@k8s-master01 1.8+]# vim metrics-server-deployment.yaml
按图中添加下面选项
hostNetwork: true
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
args:
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
# 安装metrics-server
[root@k8s-master01 1.8+]# kubectl apply -f ./
# 查看pod运行情况
[root@k8s-master01 1.8+]# kubectl get pod -n kube-system
metrics-server-6b976979db-2xwbj 1/1 Running 0 90s
# 使用kubectl top node 查看资源使用情况
[root@k8s-master01 1.8+]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master01 289m 14% 1582Mi 54%
k8s-node01 81m 4% 1195Mi 40%
k8s-node02 72m 3% 1211Mi 41%
[root@k8s-master01 1.8+]# kubectl top pod -n kube-system
NAME CPU(cores) MEMORY(bytes)
coredns-6955765f44-7ptsb 3m 9Mi
coredns-6955765f44-vcwr5 3m 8Mi
etcd-master 14m 145Mi
...
# 至此,metrics-server安装完成
# 创建deployment
[root@k8s-master01 1.8+]# kubectl run nginx --image=nginx:1.17.1 --requests=cpu=100m -n dev
# 创建service
[root@k8s-master01 1.8+]# kubectl expose deployment nginx --type=NodePort --port=80 -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 2m5s
NAME READY STATUS RESTARTS AGE
pod/nginx-778cb5fb7b-h9wnl 1/1 Running 0 2m5s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx NodePort 10.100.9.112 <none> 80:30644/TCP 66s
[root@master ~]# vim pc-hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: pc-hpa
namespace: dev
spec:
minReplicas: 1 #最小pod数量
maxReplicas: 10 #最大pod数量
targetCPUUtilizationPercentage: 3 # CPU使用率指标
scaleTargetRef: # 指定要控制的nginx信息
apiVersion: apps/v1
kind: Deployment
name: nginx
[root@master ~]# kubectl create -f pc-hpa.yaml
horizontalpodautoscaler.autoscaling/pc-hpa created
[root@master ~]# kubectl get hpa pc-hpa -n dev
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
pc-hpa Deployment/nginx 0%/3% 1 10 1 26s
[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
[root@k8s-master01 ~]# kubectl get deployment -n dev -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 11m
nginx 1/4 1 1 13m
nginx 1/4 1 1 13m
nginx 1/4 1 1 13m
nginx 1/4 4 1 13m
nginx 1/8 4 1 14m
nginx 1/8 4 1 14m
nginx 1/8 4 1 14m
nginx 1/8 8 1 14m
nginx 2/8 8 2 14m
nginx 3/8 8 3 14m
nginx 4/8 8 4 14m
nginx 5/8 8 5 14m
nginx 6/8 8 6 14m
nginx 7/8 8 7 14m
nginx 8/8 8 8 15m
nginx 8/1 8 8 20m
nginx 8/1 8 8 20m
nginx 1/1 1 1 20m
[root@k8s-master01 ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
nginx-7df9756ccc-bh8dr 1/1 Running 0 11m
nginx-7df9756ccc-cpgrv 0/1 Pending 0 0s
nginx-7df9756ccc-8zhwk 0/1 Pending 0 0s
nginx-7df9756ccc-rr9bn 0/1 Pending 0 0s
nginx-7df9756ccc-cpgrv 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-8zhwk 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-rr9bn 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-m9gsj 0/1 Pending 0 0s
nginx-7df9756ccc-g56qb 0/1 Pending 0 0s
nginx-7df9756ccc-sl9c6 0/1 Pending 0 0s
nginx-7df9756ccc-fgst7 0/1 Pending 0 0s
nginx-7df9756ccc-g56qb 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-m9gsj 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-sl9c6 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-fgst7 0/1 ContainerCreating 0 0s
nginx-7df9756ccc-8zhwk 1/1 Running 0 19s
nginx-7df9756ccc-rr9bn 1/1 Running 0 30s
nginx-7df9756ccc-m9gsj 1/1 Running 0 21s
nginx-7df9756ccc-cpgrv 1/1 Running 0 47s
nginx-7df9756ccc-sl9c6 1/1 Running 0 33s
nginx-7df9756ccc-g56qb 1/1 Running 0 48s
nginx-7df9756ccc-fgst7 1/1 Running 0 66s
nginx-7df9756ccc-fgst7 1/1 Terminating 0 6m50s
nginx-7df9756ccc-8zhwk 1/1 Terminating 0 7m5s
nginx-7df9756ccc-cpgrv 1/1 Terminating 0 7m5s
nginx-7df9756ccc-g56qb 1/1 Terminating 0 6m50s
nginx-7df9756ccc-rr9bn 1/1 Terminating 0 7m5s
nginx-7df9756ccc-m9gsj 1/1 Terminating 0 6m50s
nginx-7df9756ccc-sl9c6 1/1 Terminating 0 6m50s
DS(DaemoSet)
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
# 创建daemonset
[root@k8s-master01 ~]# kubectl create -f pc-daemonset.yaml
daemonset.apps/pc-daemonset created
# 查看daemonset
[root@k8s-master01 ~]# kubectl get ds -n dev -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES
pc-daemonset 2 2 2 2 2 24s nginx nginx:1.17.1
# 查看pod,发现在每个Node上都运行一个pod
[root@k8s-master01 ~]# kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE
pc-daemonset-9bck8 1/1 Running 0 37s 10.244.1.43 node1
pc-daemonset-k224w 1/1 Running 0 37s 10.244.2.74 node2
# 删除daemonset
[root@k8s-master01 ~]# kubectl delete -f pc-daemonset.yaml
daemonset.apps "pc-daemonset" deleted
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"]
[root@master ~]# vim pc-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pc-job
namespace: dev
spec:
manualSelector: true
completions: 6
parallelism: 3
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"]
[root@master ~]# kubectl create -f pc-job.yaml
job.batch/pc-job created
[root@master ~]# kubectl get job -n dev -w
NAME COMPLETIONS DURATION AGE
pc-job 0/6 0s
pc-job 0/6 0s 0s
pc-job 1/6 29s 29s
pc-job 2/6 29s 29s
pc-job 3/6 29s 29s
[root@master ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-tqz4q 0/1 Pending 0 0s
pc-job-tqz4q 0/1 Pending 0 0s
pc-job-j9ccj 0/1 Pending 0 0s
pc-job-ggpk9 0/1 Pending 0 0s
pc-job-j9ccj 0/1 Pending 0 0s
pc-job-ggpk9 0/1 Pending 0 0s
pc-job-tqz4q 0/1 ContainerCreating 0 0s
pc-job-j9ccj 0/1 ContainerCreating 0 0s
pc-job-ggpk9 0/1 ContainerCreating 0 0s
pc-job-ggpk9 1/1 Running 0 2s
pc-job-j9ccj 1/1 Running 0 2s
pc-job-tqz4q 1/1 Running 0 2s
pc-job-j9ccj 0/1 Completed 0 29s
pc-job-8cmwb 0/1 Pending 0 0s
pc-job-8cmwb 0/1 Pending 0 0s
pc-job-tqz4q 0/1 Completed 0 29s
pc-job-fz4l5 0/1 Pending 0 0s
pc-job-fz4l5 0/1 Pending 0 0s
pc-job-ggpk9 0/1 Completed 0 29s
pc-job-jlzv6 0/1 Pending 0 0s
pc-job-jlzv6 0/1 Pending 0 0s
pc-job-8cmwb 0/1 ContainerCreating 0 0s
pc-job-fz4l5 0/1 ContainerCreating 0 0s
pc-job-jlzv6 0/1 ContainerCreating 0 0s
pc-job-fz4l5 1/1 Running 0 1s
pc-job-jlzv6 1/1 Running 0 2s
pc-job-8cmwb 1/1 Running 0 2s
pc-job-jlzv6 0/1 Completed 0 28s
pc-job-fz4l5 0/1 Completed 0 28s
pc-job-8cmwb 0/1 Completed 0 28s
# 创建job
[root@k8s-master01 ~]# kubectl create -f pc-job.yaml
job.batch/pc-job created
# 查看job
[root@k8s-master01 ~]# kubectl get job -n dev -o wide -w
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
pc-job 0/1 21s 21s counter busybox:1.30 app=counter-pod
pc-job 1/1 31s 79s counter busybox:1.30 app=counter-pod
# 通过观察pod状态可以看到,pod在运行完毕任务后,就会变成Completed状态
[root@k8s-master01 ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-rxg96 1/1 Running 0 29s
pc-job-rxg96 0/1 Completed 0 33s
# 接下来,调整下pod运行的总数量和并行数量 即:在spec下设置下面两个选项
# completions: 6 # 指定job需要成功运行Pods的次数为6
# parallelism: 3 # 指定job并发运行Pods的数量为3
# 然后重新运行job,观察效果,此时会发现,job会每次运行3个pod,总共执行了6个pod
[root@k8s-master01 ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-job-684ft 1/1 Running 0 5s
pc-job-jhj49 1/1 Running 0 5s
pc-job-pfcvh 1/1 Running 0 5s
pc-job-684ft 0/1 Completed 0 11s
pc-job-v7rhr 0/1 Pending 0 0s
pc-job-v7rhr 0/1 Pending 0 0s
pc-job-v7rhr 0/1 ContainerCreating 0 0s
pc-job-jhj49 0/1 Completed 0 11s
pc-job-fhwf7 0/1 Pending 0 0s
pc-job-fhwf7 0/1 Pending 0 0s
pc-job-pfcvh 0/1 Completed 0 11s
pc-job-5vg2j 0/1 Pending 0 0s
pc-job-fhwf7 0/1 ContainerCreating 0 0s
pc-job-5vg2j 0/1 Pending 0 0s
pc-job-5vg2j 0/1 ContainerCreating 0 0s
pc-job-fhwf7 1/1 Running 0 2s
pc-job-v7rhr 1/1 Running 0 2s
pc-job-5vg2j 1/1 Running 0 3s
pc-job-fhwf7 0/1 Completed 0 12s
pc-job-v7rhr 0/1 Completed 0 12s
pc-job-5vg2j 0/1 Completed 0 12s
# 删除job
[root@k8s-master01 ~]# kubectl delete -f pc-job.yaml
job.batch "pc-job" deleted
CJ(CronJob)
[root@master ~]# vim 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"]
[root@master ~]# kubectl create -f pc-cronjob.yaml
cronjob.batch/pc-cronjob created
[root@master ~]# kubectl get cronjob -n dev -w
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
pc-cronjob */1 * * * * False 0 <none> 0s
pc-cronjob */1 * * * * False 1 4s 33s
pc-cronjob */1 * * * * False 0 34s 63s
pc-cronjob */1 * * * * False 1 4s 93s
pc-cronjob */1 * * * * False 0 34s 2m3s
pc-cronjob */1 * * * * False 1 4s 2m33s
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-cronjob-1676823540-zp8b4 0/1 Pending 0 0s
pc-cronjob-1676823540-zp8b4 0/1 Pending 0 0s
pc-cronjob-1676823540-zp8b4 0/1 ContainerCreating 0 0s
pc-cronjob-1676823540-zp8b4 1/1 Running 0 1s
pc-cronjob-1676823540-zp8b4 0/1 Completed 0 28s
pc-cronjob-1676823600-rgzz4 0/1 Pending 0 0s
pc-cronjob-1676823600-rgzz4 0/1 Pending 0 0s
pc-cronjob-1676823600-rgzz4 0/1 ContainerCreating 0 0s
pc-cronjob-1676823600-rgzz4 1/1 Running 0 2s
pc-cronjob-1676823600-rgzz4 0/1 Completed 0 29s
pc-cronjob-1676823660-fwglx 0/1 Pending 0 0s
pc-cronjob-1676823660-fwglx 0/1 Pending 0 0s
pc-cronjob-1676823660-fwglx 0/1 ContainerCreating 0 0s
pc-cronjob-1676823660-fwglx 1/1 Running 0 2s