常见命令
- 创建一个deployment
kubectl create deployment my-tomcat --image=tomcat:9.0.55
# my-tomcat 表示pod的名称,--image表示镜像的地址
- 查看一下deployment
kubectl get deployment
- 删除deployment
kubectl detele deployment my-tomcat
- 查看pod打印的日志
kubectl logs <pod名称>
- 使用exec可以在pod的容器中执行命令
kubectl exec <pod名称> -- env #使用env命令查看环境变量
kubectl exec <pod名称> -- ls / #查看容器的根目录下面内容
kubectl exec <pod名称> -- sh #进入pod容器内部并非执行bash命令,如果想退出容器可以使用exit命令
多副本
- 创建3个副本
[ops@master ~]$ kubectl create deployment mynginx2 --image=nginx:1.26-alpine3.20 --replicas=3
[ops@master ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx2-6f8df44986-c767h 1/1 Running 0 25s
mynginx2-6f8df44986-grn98 1/1 Running 0 25s
mynginx2-6f8df44986-lw69k 1/1 Running 0 25s
扩缩容
- 扩容到5个pod
[ops@master ~]$ kubectl scale --replicas=5 deployment mynginx2
deployment.apps/mynginx2 scaled
[ops@master ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx2-6f8df44986-25j5z 1/1 Running 0 4s
mynginx2-6f8df44986-c767h 1/1 Running 0 3m15s
mynginx2-6f8df44986-grn98 1/1 Running 0 3m15s
mynginx2-6f8df44986-gs2s7 1/1 Running 0 4s
mynginx2-6f8df44986-lw69k 1/1 Running 0 3m15s
- 缩到3个pod
[ops@master ~]$ kubectl scale --replicas=3 deployment mynginx2
deployment.apps/mynginx2 scaled
[ops@master ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx2-6f8df44986-grn98 1/1 Running 0 4m49s
mynginx2-6f8df44986-gs2s7 1/1 Running 0 98s
mynginx2-6f8df44986-lw69k 1/1 Running 0 4m49s
[ops@master ~]$
版本回滚
- 查看历史版本
[ops@master ~]$ kubectl rollout history deploy mynginx2
deployment.apps/mynginx2
REVISION CHANGE-CAUSE
1 <none>
- 回滚到上一个版本
[ops@master ~]$ kubectl rollout undo deploy mynginx
[ops@master ~]$ kubectl rollout undo deploy mynginx --to-revision=2