Kubernetes 150 个操作练习 (中)
原文链接:Practice Enough With These 150 Questions for the CKAD Exam。Medium 上的一篇文章记录了 150 个 Kubernetes 的练习,自己练习了一遍,在原文基础上加了一些自己实践过程中的的提示,希望对需要熟悉 K8S 操作的同学有所帮助。本篇介绍 Pod 设计相关的 练习。三.Pod 设计篇这是原文中的第三部分,主要关于 ...
·
原文链接:Practice Enough With These 150 Questions for the CKAD Exam。
Medium 上的一篇文章记录了 150 个 Kubernetes 的练习,自己练习了一遍,在原文基础上加了一些自己实践过程中的的提示,希望对需要熟悉 K8S 操作的同学有所帮助。本篇介绍 Pod 设计相关的 练习。
三.Pod 设计篇
这是原文中的第三部分,主要关于 Pod 操作的,主要有下面一些内容:
- 关于 标签 Label、选择器 selector 和 注解 Annotations 的使用
- 了解 Deployment 如何进行滚动升级和回滚
- 理解 Job 和 CronJobs
36. 查看 Pod,展示标签信息
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
busybox 0/3 Completed 0 17h run=busybox
37. 创建五个 Nginx 的 Pod,两个带 env=prod 标签,三个带 env=dev 标签
$ kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod
kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod
pod/nginx-dev1 created
pod/nginx-dev2 created
pod/nginx-dev3 created
pod/nginx-prod1 created
pod/nginx-prod2 created
38. 检查上面 Pod 的创建情况
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 87s env=dev
nginx-dev2 1/1 Running 0 87s env=dev
nginx-dev3 1/1 Running 0 87s env=dev
nginx-prod1 1/1 Running 0 87s env=prod
nginx-prod2 1/1 Running 0 87s env=prod
39. 查看带有标签 env=dev 的 Pod
$ kubectl get pods -l env=dev
NAME READY STATUS RESTARTS AGE
nginx-dev1 1/1 Running 0 3m6s
nginx-dev2 1/1 Running 0 3m6s
nginx-dev3 1/1 Running 0 3m6s
40. 查看带有标签 env=dev 的 Pod 并展示其标签
$ kubectl get pods -l env=dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 3m42s env=dev
nginx-dev2 1/1 Running 0 3m42s env=dev
nginx-dev3 1/1 Running 0 3m42s env=dev
41. 查看带有标签 env=prod 的 Pod
$ kubectl get pods -l env=prod
NAME READY STATUS RESTARTS AGE
nginx-prod1 1/1 Running 0 6m21s
nginx-prod2 1/1 Running 0 6m21s
42. 查看带有标签 env=prod 的 Pod 并展示其标签
$ kubectl get pods -l env=prod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-prod1 1/1 Running 0 6m53s env=prod
nginx-prod2 1/1 Running 0 6m53s env=prod
43. 查看 Pod,并显示器其 env 标签
这时没有对应标签的 Pod 展示标签值为空
$ kubectl get pods -L env
NAME READY STATUS RESTARTS AGE ENV
my-app-v1-c9b7f9985-xnwkt 1/1 Running 0 11d
my-app-v2-77fc8c9499-9nxv2 1/1 Running 0 11d
nginx 1/1 Running 0 18h
nginx-dev1 1/1 Running 0 7m7s dev
nginx-dev2 1/1 Running 0 7m7s dev
nginx-dev3 1/1 Running 0 7m7s dev
nginx-prod1 1/1 Running 0 7m7s prod
nginx-prod2 1/1 Running 0 7m7s prod
44. 查看标签为 env 且值为 prod 或者 dev 的 Pod
$ kubectl get pods -l 'env in (dev,prod)'
NAME READY STATUS RESTARTS AGE
nginx-dev1 1/1 Running 0 9m3s
nginx-dev2 1/1 Running 0 9m3s
nginx-dev3 1/1 Running 0 9m3s
nginx-prod1 1/1 Running 0 9m3s
nginx-prod2 1/1 Running 0 9m3s
45. 查看标签为 env 且值为 prod 或者 dev 的 Pod,并展示其标签
$ kubectl get pods -l 'env in (dev,prod)' --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 9m21s env=dev
nginx-dev2 1/1 Running 0 9m21s env=dev
nginx-dev3 1/1 Running 0 9m21s env=dev
nginx-prod1 1/1 Running 0 9m21s env=prod
nginx-prod2 1/1 Running 0 9m21s env=prod
46. 修改某个 Pod 的标签为 env=uat,查看修改结果
$ kubectl label pod nginx-dev3 env=uat --overwrite
pod/nginx-dev3 labeled
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 11m env=dev
nginx-dev2 1/1 Running 0 11m env=dev
nginx-dev3 1/1 Running 0 11m env=uat
nginx-prod1 1/1 Running 0 11m env=prod
nginx-prod2 1/1 Running 0 11m env=prod
47. 删除 Pod 的标签并查看修改结果
$ kubectl label pod nginx-dev{1..3} env-
pod/nginx-dev1 labeled
pod/nginx-dev2 labeled
pod/nginx-dev3 labeled
# ubuntu @ VM-0-4-ubuntu in ~ [9:33:08]
$ kubectl label pod nginx-prod{1..2} env-
pod/nginx-prod1 labeled
pod/nginx-prod2 labeled
$ kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 13m <none>
nginx-dev2 1/1 Running 0 13m <none>
nginx-dev3 1/1 Running 0 13m <none>
nginx-prod1 1/1 Running 0 13m <none>
nginx-prod2 1/1 Running 0 13m <none>
48. 给多个 Pod 添加 app=nginx 标签并查看结果
$ kubectl label pod nginx-dev{1..3} app=nginx
pod/nginx-dev1 labeled
pod/nginx-dev2 labeled
pod/nginx-dev3 labeled
# ubuntu @ VM-0-4-ubuntu in ~ [9:34:29]
$ kubectl label pod nginx-prod{1..2} app=nginx
pod/nginx-prod1 labeled
pod/nginx-prod2 labeled
# ubuntu @ VM-0-4-ubuntu in ~ [9:34:34]
$ kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-dev1 1/1 Running 0 14m app=nginx
nginx-dev2 1/1 Running 0 14m app=nginx
nginx-dev3 1/1 Running 0 14m app=nginx
nginx-prod1 1/1 Running 0 14m app=nginx
nginx-prod2 1/1 Running 0 14m app=nginx
49. 获取所有 node 节点并展示其信息
$ kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
vm-0-2-ubuntu Ready <none> 19d v1.18.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=vm-0-2-ubuntu,kubernetes.io/os=linux
vm-0-4-ubuntu Ready master 19d v1.18.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=vm-0-4-ubuntu,kubernetes.io/os=linux,node-role.kubernetes.io/master=
50. 为 Node 打 nodeName=nginxNode 标签
$ kubectl label node vm-0-2-ubuntu nodeName=nginxnode
node/vm-0-2-ubuntu labeled
51. 创建 Pod,打标签 run=nginx,使用 nodeSelector 将 Pod 部署到带有 nodeName=nginxnode 的节点
- 创建用的 yaml 文件
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
# 达标欧倩
labels:
run: nginx
name: nginx
spec:
# 选择节点,
nodeSelector:
nodeName: nginxnode
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
- 创建 Pod
kubectl create -f pod.yaml
52. 查看 Pod 的 Node-Selector
$ kubectl describe po nginx | grep Node-Selectors
Node-Selectors: nodeName=nginxnode
53.查看刚创建的 Nginx Pod 的标签
$ kubectl describe po nginx | grep Labels
Labels: run=nginx
54. 为 Pod 添加注解 name=webapp
$ kubectl annotate pod nginx-dev{1..3} name=webapp
pod/nginx-dev1 annotated
pod/nginx-dev2 annotated
pod/nginx-dev3 annotated
$ kubectl annotate pod nginx-prod{1..2} name=webapp
pod/nginx-prod1 annotated
pod/nginx-prod2 annotated
55. 查看 Pod 的注解
$ kubectl describe po nginx-dev{1..3} | grep -i annotations
Annotations: name: webapp
Annotations: name: webapp
Annotations: name: webapp
$ kubectl describe po nginx-prod{1..2} | grep -i annotations
Annotations: name: webapp
Annotations: name: webapp
56. 移除 Pod 的注解,查看结果
# ubuntu @ VM-0-4-ubuntu in ~ [9:52:15]
$ kubectl annotate pod nginx-dev{1..3} name-
pod/nginx-dev1 annotated
pod/nginx-dev2 annotated
pod/nginx-dev3 annotated
# ubuntu @ VM-0-4-ubuntu in ~ [9:53:34]
$ kubectl annotate pod nginx-prod{1..2} name-
pod/nginx-prod1 annotated
pod/nginx-prod2 annotated
$ kubectl describe po nginx-dev{1..3} | grep -i annotations
57. 删除所有 Pod
kubectl delete po --all
58. 创建有 5 个 Nginx 副本的名为 webapp 的 Deployment
- yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webapp
name: webapp
spec:
replicas: 5
selector:
matchLabels:
app: webapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: webapp
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
- 创建 Deployment
$ kubectl create -f webapp.yaml
deployment.apps/webapp created
59. 查看上面创建的 Deployment
$ kubectl get deploy webapp --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webapp 5/5 5 5 113s app=webapp
60. 获取刚创建的 Deployment 的 yaml 文件
$ kubectl get deploy webapp -o yaml
61. 查看 Deployment 的所有 Pod
$ kubectl get deploy webapp --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webapp 5/5 5 5 3m13s app=webapp
# ubuntu @ VM-0-4-ubuntu in ~ [10:12:16]
$ kubectl get pods -l app=webapp
NAME READY STATUS RESTARTS AGE
webapp-59d9889648-b74bs 1/1 Running 0 3m17s
webapp-59d9889648-dk2w6 1/1 Running 0 3m17s
webapp-59d9889648-mm5v8 1/1 Running 0 3m17s
webapp-59d9889648-q4w5d 1/1 Running 0 3m16s
webapp-59d9889648-wq9vc 1/1 Running 0 3m16s
62. 水平扩展 webapp Deployment 的 Pod 数量到 20 个
$ kubectl scale deploy webapp --replicas=20
deployment.apps/webapp scaled
$ kubectl get po -l app=webapp
NAME READY STATUS RESTARTS AGE
webapp-59d9889648-2bzwh 1/1 Running 0 49s
webapp-59d9889648-6mgxk 1/1 Running 0 49s
webapp-59d9889648-8mk7c 1/1 Running 0 49s
webapp-59d9889648-b74bs 1/1 Running 0 5m35s
webapp-59d9889648-c5kwq 1/1 Running 0 49s
webapp-59d9889648-d98t6 1/1 Running 0 49s
webapp-59d9889648-dk2w6 1/1 Running 0 5m35s
webapp-59d9889648-fkdnk 1/1 Running 0 49s
webapp-59d9889648-lvqgj 1/1 Running 0 49s
webapp-59d9889648-mm5v8 1/1 Running 0 5m35s
webapp-59d9889648-mvtxb 1/1 Running 0 49s
webapp-59d9889648-pkmt6 1/1 Running 0 49s
webapp-59d9889648-q4w5d 1/1 Running 0 5m34s
webapp-59d9889648-qq245 1/1 Running 0 49s
webapp-59d9889648-skrbq 1/1 Running 0 49s
webapp-59d9889648-vgpdt 1/1 Running 0 49s
webapp-59d9889648-vncbd 1/1 Running 0 49s
webapp-59d9889648-wp48w 1/1 Running 0 49s
webapp-59d9889648-wq9vc 1/1 Running 0 5m34s
webapp-59d9889648-xc8g6 1/1 Running 0 49s
63. 查看 Deployment 的部署状态
$ kubectl rollout status deploy webapp
deployment "webapp" successfully rolled out
64. 查看 webapp Deployment 的 replicaset
$ kubectl get rs -l app=webapp
NAME DESIRED CURRENT READY AGE
webapp-59d9889648 20 20 20 6m45s
65. 获取 webapp Deployment 下 Pod 和 ReplicaSet 的 yaml 文件
kubectl get rs -l app=webapp -o yaml
kubectl get po -l app=webapp -o yaml
66. 删除 webapp Deployment 并查看 Pod 的删除情况
kubectl delete deploy webapp
kubectl get po -l app=webapp -w
67. 创建 webapp Deployment,指定镜像版本和端口
- yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webapp
name: webapp
spec:
replicas: 1
selector:
matchLabels:
app: webapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: webapp
spec:
containers:
- image: nginx:1.17.1
name: nginx
ports:
- containerPort: 80
resources: {}
status: {}
- 创建 Deployment
$ kubectl create -f webapp.yaml
deployment.apps/webapp created
68. 更新 Deployment 的镜像版本并查看
$ kubectl set image deploy/webapp nginx=nginx:1.17.4
deployment.apps/webapp image updated
# ubuntu @ VM-0-4-ubuntu in ~ [10:34:56]
$ kubectl describe deploy webapp | grep Image
Image: nginx:1.17.4
69. 检查更新历史并查看 Deployment、ReplicaSet、Pod 的信息
$ kubectl rollout history deploy webapp
deployment.apps/webapp
REVISION CHANGE-CAUSE
1 <none>
2 <none>
$ kubectl get deploy webapp --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webapp 1/1 1 1 103s app=webapp
$ kubectl get rs -l app=webapp
NAME DESIRED CURRENT READY AGE
webapp-67f449866c 0 0 0 108s
webapp-6894d66768 1 1 1 59s
$ kubectl get po -l app=webapp
NAME READY STATUS RESTARTS AGE
webapp-6894d66768-pvrvv 1/1 Running 0 63s
70. 撤销对 Deployment 镜像版本的更改,检查回滚结果
$ kubectl rollout undo deploy webapp
deployment.apps/webapp rolled back
$ kubectl describe deploy webapp | grep Image
Image: nginx:1.17.1
71. 设置镜像版本为 1.16.1 并检查升级记录
$ kubectl set image deploy/webapp nginx=nginx:1.16.1
deployment.apps/webapp image updated
# ubuntu @ VM-0-4-ubuntu in ~ [10:38:10]
$ kubectl describe deploy webapp | grep Image
Image: nginx:1.16.1
# ubuntu @ VM-0-4-ubuntu in ~ [10:38:14]
$ kubectl rollout history deploy webapp
deployment.apps/webapp
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
72. 将 Deployment 中镜像版本更新至之前的 1.17.1 版本
$ kubectl rollout undo deploy webapp --to-revision=3
deployment.apps/webapp rolled back
$ kubectl describe deploy webapp | grep Image
Image: nginx:1.17.1
$ kubectl rollout status deploy webapp
deployment "webapp" successfully rolled out
73. 将 Deployment 中镜像版本更新至错误的版本 1.100 并检查相关信息
$ kubectl set image deploy/webapp nginx=nginx:1.100
deployment.apps/webapp image updated
$ kubectl rollout status deploy webapp
Waiting for deployment "webapp" rollout to finish: 1 old replicas are pending termination...
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
webapp-67f449866c-87cgc 1/1 Running 0 108s
webapp-6b684475c5-qs2wq 0/1 ImagePullBackOff 0 22s
74. 撤销之前的错误操作并确认 Deployment 、Pod 已恢复正常
$ kubectl rollout undo deploy webapp
deployment.apps/webapp rolled back
# ubuntu @ VM-0-4-ubuntu in ~ [10:42:58]
$ kubectl rollout status deploy webapp
deployment "webapp" successfully rolled out
# ubuntu @ VM-0-4-ubuntu in ~ [10:43:03]
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubia-59d857b444-sfvxc 1/1 Running 0 36m
kubia-59d857b444-v72jc 1/1 Running 0 36m
kubia-59d857b444-zflsz 1/1 Running 0 36m
my-app-v1-c9b7f9985-qrwx6 1/1 Running 0 36m
my-app-v2-77fc8c9499-9wjcp 1/1 Running 0 36m
webapp-67f449866c-87cgc 1/1 Running 0 3m39s
75. 查看指定版本的更新记录
$ kubectl rollout history deploy webapp --revision=7
deployment.apps/webapp with revision #7
Pod Template:
Labels: app=webapp
pod-template-hash=67f449866c
Containers:
nginx:
Image: nginx:1.17.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
76. 暂停 Deployment 的部署
$ kubectl rollout pause deploy webapp
deployment.apps/webapp paused
77. 更新 Nginx 至最新版本,查看更新历史
因为已经暂停了,所以不会有任何的变化。
$ kubectl set image deploy/webapp nginx=nginx:latest
$ kubectl rollout history deploy webapp
deployment.apps/webapp
REVISION CHANGE-CAUSE
2 <none>
4 <none>
6 <none>
7 <none>
78. 恢复 Deployment 的部署
kubectl rollout resume deploy webapp
79. 检查滚动历史并确认其新版本
$ kubectl rollout history deploy webapp
deployment.apps/webapp
REVISION CHANGE-CAUSE
2 <none>
4 <none>
6 <none>
7 <none>
8 <none>
# ubuntu @ VM-0-4-ubuntu in ~ [10:54:15]
$ kubectl rollout history deploy webapp --revision=8
deployment.apps/webapp with revision #8
Pod Template:
Labels: app=webapp
pod-template-hash=5f5bf685db
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
80. 为 Deployment 设置自动伸缩,最小数值为 10,最大数为 20 以及CPU 使用率为 85%,验证 hpa 和 副本的创建
可以看到 Pod 数量从 1 变为 10。
$ kubectl autoscale deploy webapp --min=10 --max=20 --cpu-percent=85
horizontalpodautoscaler.autoscaling/webapp autoscaled
# ubuntu @ VM-0-4-ubuntu in ~ [10:57:13]
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
webapp Deployment/webapp <unknown>/85% 10 20 0 6s
$ kubectl get pod -l app=webapp
NAME READY STATUS RESTARTS AGE
webapp-5f5bf685db-dqh8t 1/1 Running 0 5m17s
$ kubectl get pod -l app=webapp
NAME READY STATUS RESTARTS AGE
webapp-5f5bf685db-2q5ld 1/1 Running 0 6s
webapp-5f5bf685db-545d4 1/1 Running 0 6s
webapp-5f5bf685db-6h9bl 1/1 Running 0 6s
webapp-5f5bf685db-7dzjm 1/1 Running 0 6s
webapp-5f5bf685db-8rl6x 1/1 Running 0 6s
webapp-5f5bf685db-dqh8t 1/1 Running 0 5m26s
webapp-5f5bf685db-fk2hm 1/1 Running 0 6s
webapp-5f5bf685db-hmvhx 1/1 Running 0 6s
webapp-5f5bf685db-j27lg 1/1 Running 0 6s
webapp-5f5bf685db-w88ns 1/1 Running 0 6s
- hpa 指的是 Horizontal Pod Autoscaler。
81. 删除刚创建的 Deployment 和 hpa
$ kubectl delete deploy webapp
deployment.apps "webapp" deleted
# ubuntu @ VM-0-4-ubuntu in ~ [11:02:08]
$ kubectl delete hpa webapp
horizontalpodautoscaler.autoscaling "webapp" deleted
82. 创建一个带有 node 镜像的 Job,并打印节点版本
kubectl create job nodeversion --image=node -- node -v
$ kubectl get job -w
NAME COMPLETIONS DURATION AGE
nodeversion 0/1 10s 10s
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nodeversion-b4rvl 0/1 ContainerCreating 0 37s
83. 查看上面创建的 Pod 的日志
$ kubectl logs nodeversion-b4rvl
v14.0.0
84. 基于 Job 打印 yaml 文件
kubectl create job hello-job --image=busybox --dry-run=client -o yaml -- echo "Hello I am from job"
- yaml 文件
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: hello-job
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- echo
- Hello I am from job
image: busybox
name: hello-job
resources: {}
restartPolicy: Never
status: {}
85. 基于上面输出的信息创建 Job
kubectl create -f hello-job.yaml
86. 查看 Job 与 Pod,打印其日志
$ kubectl get job
NAME COMPLETIONS DURATION AGE
hello-job 1/1 6s 40s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
hello-job-z2kms 0/1 Completed 0 45s
$ kubectl logs hello-job-z2kms
Hello I am from job
87. 删除刚创建 Job
$ kubectl delete job hello-job
job.batch "hello-job" deleted
88. 再次创建 Job,并将任务执行数设置为 10
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: hello-job
spec:
# 将执行数设置为 10
completions: 10
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- echo
- Hello I am from job
image: busybox
name: hello-job
resources: {}
restartPolicy: Never
status: {}
89. 查看新创建的 Job
$ kubectl get job -w
NAME COMPLETIONS DURATION AGE
hello-job 0/10 5s 5s
hello-job 1/10 7s 7s
hello-job 2/10 13s 13s
hello-job 3/10 19s 19s
hello-job 4/10 25s 25s
hello-job 5/10 31s 31s
hello-job 6/10 38s 38s
hello-job 7/10 44s 44s
hello-job 8/10 50s 50s
hello-job 9/10 56s 56s
hello-job 10/10 62s 62s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
hello-job-6mmp9 0/1 Completed 0 68s
hello-job-8s28x 0/1 Completed 0 43s
hello-job-dwvpf 0/1 Completed 0 49s
hello-job-gf5wz 0/1 Completed 0 37s
hello-job-hcb96 0/1 Completed 0 80s
hello-job-nqmvk 0/1 Completed 0 31s
hello-job-pczkz 0/1 Completed 0 74s
hello-job-pkf2d 0/1 Completed 0 87s
hello-job-tsj5n 0/1 Completed 0 62s
hello-job-z5cnx 0/1 Completed 0 56s
kubectl delete job hello-job
90. 再次创建 Job 并设置并行执行数为 10
- yaml 文件
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: hello-job
spec:
# 设置并行数
parallelism: 10
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- echo
- Hello I am from job
image: busybox
name: hello-job
resources: {}
restartPolicy: Never
status: {}
- 创建 Job
$ kubectl create -f hello-job.yaml
job.batch/hello-job created
91. 查看 job 的执行与 pod 情况,完成后删除 job
$ kubectl get job -w
NAME COMPLETIONS DURATION AGE
hello-job 0/1 of 10 1s 1s
hello-job 1/1 of 10 8s 8s
hello-job 2/1 of 10 8s 8s
hello-job 3/1 of 10 12s 12s
hello-job 4/1 of 10 16s 16s
hello-job 5/1 of 10 19s 19s
hello-job 6/1 of 10 23s 23s
hello-job 7/1 of 10 28s 28s
hello-job 8/1 of 10 31s 31s
hello-job 9/1 of 10 35s 35s
hello-job 10/1 of 10 39s 39s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
hello-job-2jsql 0/1 Completed 0 54s
hello-job-7j5z5 0/1 Completed 0 54s
hello-job-89xcz 0/1 Completed 0 54s
hello-job-cjqgh 0/1 Completed 0 54s
hello-job-dm4gd 0/1 Completed 0 54s
hello-job-fn9gc 0/1 Completed 0 54s
hello-job-qssm4 0/1 Completed 0 54s
hello-job-rf926 0/1 Completed 0 54s
hello-job-vww26 0/1 Completed 0 54s
hello-job-z4fc7 0/1 Completed 0 54s
kubectl delete job hello-job
92. 创建一个 CronJob,每分钟打印日期和 Hello from kubernetes cluster
$ kubectl create cronjob date-job --image=busybox --schedule="*/1 * * * *" -- bin/sh -c "date; echo Hello from kubernetes cluster"
cronjob.batch/date-job created
93. 获取 CronJob 的 yaml
kubectl get cj date-job -o yaml
94. 查看 CronJob 组件信息和打印日志
$ kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
date-job */1 * * * * False 0 27s 116s
$ kubectl get job
NAME COMPLETIONS DURATION AGE
date-job-1587871560 1/1 6s 12s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
date-job-1587871560-ndj9v 0/1 Completed 0 90s
$ kubectl logs date-job-1587871560-ndj9v
Sun Apr 26 03:26:14 UTC 2020
Hello from kubernetes cluster
95. 删除 CronJob,并确认与其关联的 Job 和 Pod 是否已删除
kubectl delete cj date-job
// 确认关联的 Pod 和 job 是否已经删除
kubectl get po
kubectl get job
以上就是 Pod Design 部分的练习,最后一篇介绍余下的 50 多个练习。
更多推荐
已为社区贡献5条内容
所有评论(0)