kubernetes学习-应用程序的生命周期管理
kubernetes学习-应用程序的生命周期管理
- 一、应用的部署
- 1.1 通过命令进行部署
- 1.2 通过yaml进行部署
- 1.3 部署示例
- 1.3.1 部署deployment
- 1.3.2 部署service
- 1.3.3 访问nginx页面
- 二、应用的升级
- 2.1 通过set 升级
- 2.2 通过yaml升级
- 2.3 通过编辑升级
- 三、应用的回滚
- 四、应用的扩容与缩容
- 4.1 通过命令扩容和缩容
- 4.2 通过yaml扩容和缩容
- 五、应用的下线
一、应用的部署
应用程序的部署方式有两种,一种是通过命令直接进行部署,另一种是通过yaml文件进行部署。
1.1 通过命令进行部署
示例,具体参数和说明可自行查看官方说明或百度。
~]# kubectl create deployment {pod名称} --image=镜像名:版本 --replicas=副本数
1.2 通过yaml进行部署
可使用官方yaml或命令生成yaml。
生成yaml示例:
~]# kubectl create deployment {pod名称} --image={镜像名}:{镜像版本} -o yaml --dry-run=client > xxx.yaml
# --dry-run=client 表示本地尝试运行pod,主要用于测试是否正常
~]# kubectl get deployment {pod名称} -o yaml > xxxx.yaml # 导出现有pod配置到yaml
部署命令示例
~]# kubectl apply -f xxxx.ayml
1.3 部署示例
1.3.1 部署deployment
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3 # 设置Pod副本数量
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:1.16 # 你的容器镜像
ports:
- containerPort: 80
~]# kubectl apply -f deployment.yaml
~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-657d778b65-2v745 1/1 Running 0 27m
my-nginx-657d778b65-5bfc6 1/1 Running 0 27m
my-nginx-657d778b65-z7n6l 1/1 Running 0 27m
1.3.2 部署service
service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-nginx
ports:
- protocol: TCP
port: 8081
targetPort: 80
nodePort: 32222
type: NodePort
~]# kubectl apply -f service.yaml
~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8h
my-service NodePort 10.97.148.243 <none> 8081:32222/TCP 28m
1.3.3 访问nginx页面
访问k8s集群内任意一主机的32222端口
二、应用的升级
升级可以分为三种方式进行升级,在升级过程以3个副本数为例。当更新 Deployment 时,系统创建了一个新的 ReplicaSet,并将其副本数量扩展到 1,然后将旧的 ReplicaSet 缩减为 2。之后,系统继续按照相同的更新策略对新旧两个ReplicaSet 进行逐个调整。最后,新的ReplicaSet 运行了 3 个新版本 Pod 副本,旧的 ReplicaSet 副本数量则缩减为0。
2.1 通过set 升级
示例
~]# kubectl set image deployment {pod名称} {pod名称}=镜像名:版本
查看当前镜像版本,可以看到镜像当前版本为nginx:1.16
~]# kubectl describe pod {pod名称} | grep image
~]# kubectl describe pod my-nginx | grep image
Normal Pulled 44m kubelet Container image "nginx:1.16" already present on machine
Normal Pulled 44m kubelet Container image "nginx:1.16" already present on machine
Normal Pulled 44m kubelet Container image "nginx:1.16" already present on machine
可以看到当前镜像为nginx:1.16,通过set升级到1.17版本。
~]# kubectl set image deployment my-nginx my-nginx=nginx:1.17
~]# kubectl describe pod my-nginx | grep image # 查看镜像版本,升级成功
Normal Pulled 76s kubelet Container image "nginx:1.17" already present on machine
Normal Pulled 2m9s kubelet Container image "nginx:1.17" already present on machine
Normal Pulled 83s kubelet Container image "nginx:1.17" already present on machine
2.2 通过yaml升级
~]# kubectl apply -f xxxx.ayml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3 # 设置Pod副本数量
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:1.18 # 你的容器镜像
ports:
- containerPort: 80
~]# kubectl apply -f deployment.yaml
deployment.apps/my-nginx configured
~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-657d778b65-2v745 1/1 Running 0 27m
my-nginx-657d778b65-5bfc6 1/1 Running 0 27m
my-nginx-657d778b65-z7n6l 1/1 Running 0 27m
~]# kubectl describe pod my-nginx | grep image
Normal Pulled <invalid> kubelet Container image "nginx:1.18" already present on machine
Normal Pulled 5s kubelet Container image "nginx:1.18" already present on machine
Normal Pulled 12s kubelet Container image "nginx:1.18" already present on machine
2.3 通过编辑升级
kubectl edit deployment {pod名称} #使用系统编辑器打开
修改镜像版本为1.16,编辑方式同vim文件,可使用:wq保存退出,保存以后将会立即生效执行。
~]# kubectl edit deployment my-nginx
deployment.apps/my-nginx edited
~]# kubectl describe pod my-nginx | grep image
Normal Pulled 8s kubelet Container image "nginx:1.16" already present on machine
Normal Pulled <invalid> kubelet Container image "nginx:1.16" already present on machine
Normal Pulled 5s kubelet Container image "nginx:1.16" already present on machine
三、应用的回滚
应用回滚需要掌握的几个命令
~]# kubectl rollout history deployment {pod名称} # 查看历史发布版本
~]# kubectl get rs # 查看RS记录
~]# kubectl describe rs # 查看rs的详细信息
~]# kubectl describe rs | grep -E "revision:|Image" # 查看发布记录对应的镜像版本
可以看到,版本4对应1.16,版本3对应1.18,版本2对应1.17
~]# kubectl rollout history deployment my-nginx
deployment.apps/my-nginx
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
~]# kubectl describe rs | grep -E "revision:|Image"
deployment.kubernetes.io/revision: 4
Image: nginx:1.16
deployment.kubernetes.io/revision: 3
Image: nginx:1.18
deployment.kubernetes.io/revision: 2
Image: nginx:1.17
可以看到通过rollout undo可以回滚到指定的版本,以回滚到版本2为例。
~]# kubectl rollout undo deployment my-nginx --to-revision=2
deployment.apps/my-nginx rolled back
]# kubectl describe pod my-nginx | grep image
Normal Pulled 36s kubelet Container image "nginx:1.17" already present on machine
Normal Pulled <invalid> kubelet Container image "nginx:1.17" already present on machine
Normal Pulled 35s kubelet Container image "nginx:1.17" already present on machine
四、应用的扩容与缩容
应用的扩容与缩容即pod副本数的增加或者减少,可以通过yaml和命令行方式进行操作,更新后的副本数大于当前副本数则为扩容,反之则为缩容。
4.1 通过命令扩容和缩容
可以通过scale命令指定–replicas副本数
~]# kubectl scale deployment {pod名称} --replicas={副本数}
~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7c4f7df846-dcswr 1/1 Running 0 7m7s
my-nginx-7c4f7df846-ngdns 1/1 Running 0 7m8s
my-nginx-7c4f7df846-wn6zd 1/1 Running 0 7m5s
~]# kubectl scale deployment my-nginx --replicas=6 # 增加副本数
deployment.apps/my-nginx scaled
[root@node-02 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7c4f7df846-dcswr 1/1 Running 0 8m18s
my-nginx-7c4f7df846-g57g7 1/1 Running 0 3s
my-nginx-7c4f7df846-j2j25 1/1 Running 0 3s
my-nginx-7c4f7df846-ngdns 1/1 Running 0 8m19s
my-nginx-7c4f7df846-wn6zd 1/1 Running 0 8m16s
my-nginx-7c4f7df846-xh925 1/1 Running 0 3s
~]# kubectl scale deployment my-nginx --replicas=2 # 减少副本数
deployment.apps/my-nginx scaled
[root@node-02 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7c4f7df846-ngdns 1/1 Running 0 9m16s
my-nginx-7c4f7df846-wn6zd 1/1 Running 0 9m13s
4.2 通过yaml扩容和缩容
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 5 # 设置Pod副本数量
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:1.17 # 你的容器镜像
ports:
- containerPort: 80
~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7c4f7df846-ngdns 1/1 Running 0 9m16s
my-nginx-7c4f7df846-wn6zd 1/1 Running 0 9m13s
~]# kubectl apply -f deployment.yaml
deployment.apps/my-nginx configured
~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-7c4f7df846-c56b9 1/1 Running 0 5s
my-nginx-7c4f7df846-mbx92 1/1 Running 0 5s
my-nginx-7c4f7df846-mrbgl 1/1 Running 0 5s
my-nginx-7c4f7df846-ngdns 1/1 Running 0 13m
my-nginx-7c4f7df846-wn6zd 1/1 Running 0 13m
五、应用的下线
需要删除svc和deployment
~]# kubectl delete deployment {pod名称}
~]# kubectl delete svc {svc名称}
~]# kubectl delete deployment my-nginx
deployment.apps "my-nginx" deleted
~]# kubectl delete svc my-service
service "my-service" deleted
~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9h