deployment:应用生命周期管理流程
应用程序 -> 部署 -> 升级 ->回滚 -> 删除
1 部署
deployment###
kubectl apply -f web1-deployp.yaml
kubectl create deployment web --image=nginx:1.16 --replicas=3
#web1-deploy.yaml
kind: Deployment
metadata:
annotations:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web1
namespace: web1
labels:
app: web1-app
project: web1-pj
spec:
replicas: 3
selector:
matchLabels:
app: web1-app
template:
metadata:
labels:
app: web1-app
spec:
containers:
- image: nginx:1.16
name: nginx
[root@k8s-master ~]# vi web1-deploy.yaml
[root@k8s-master ~]# kubectl apply -f web1-deploy.yaml
deployment.apps/web1 created
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-546f799d9-gjdll 1/1 Running 0 34s
web1-546f799d9-h8s9k 1/1 Running 0 34s
web1-546f799d9-rmlzk 1/1 Running 0 34s
service###
[root@k8s-master ~]# vi web1-service.yaml
apiVersion: v1
kind: Service
metadata:
name: web1
namespace: web1
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web1-app
type: NodePort
[root@k8s-master ~]# kubectl apply -f web1-service.yaml
service/web1 created
[root@k8s-master ~]# kubectl get pods,svc -n web1
NAME READY STATUS RESTARTS AGE
pod/web1-546f799d9-gjdll 1/1 Running 0 19m
pod/web1-546f799d9-h8s9k 1/1 Running 0 19m
pod/web1-546f799d9-rmlzk 1/1 Running 0 19m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web1 NodePort 10.97.204.112
[root@k8s-master ~]# kubectl logs web1-546f799d9-gjdll -n web1
[root@k8s-master ~]# kubectl logs web1-546f799d9-h8s9k -n web1
[root@k8s-master ~]# kubectl logs web1-546f799d9-rmlzk -n web1
10.5.0.5 - - [20/Feb/2022:08:01:57 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0
; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36" "-"
2022/02/20 08:01:57 [error] 7#7: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such fi
le or directory), client: 10.5.0.5, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "
10.5.0.5:31065", referrer: "http://10.5.0.5:31065/"
10.5.0.5 - - [20/Feb/2022:08:01:57 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://10.5.0.5:31065
/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.475
8.82 Safari/537.36" "-"
10.5.0.5 - - [20/Feb/2022:08:02:13 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36" "-"
2 应用升级
更新镜像的三种方式,
kubectl apply -f xxx.yaml
kubectl set image deployment/web nginx=nginx:1.17
kubectl edit deployment/web #系统编辑器
升级前版本号1.16
滚动升级
[root@k8s-master ~]# vi web1-deploy.yaml
…
spec:
containers:
- image: nginx:1.17
[root@k8s-master ~]# kubectl apply -f web1-deploy.yaml
deployment.apps/web1 configured
[root@k8s-master ~]# kubectl get pods,svc -n web1
NAME READY STATUS RESTARTS AGE
pod/web1-546f799d9-gjdll 1/1 Running 0 83m
pod/web1-546f799d9-h8s9k 1/1 Running 0 83m
pod/web1-778dcf88b-7lgjm 1/1 Running 0 25s
pod/web1-778dcf88b-tj86f 0/1 ContainerCreating 0 11s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web1 NodePort 10.97.204.112
[root@k8s-master ~]# kubectl get pods,svc -n web1
NAME READY STATUS RESTARTS AGE
pod/web1-546f799d9-gjdll 0/1 Terminating 0 83m
pod/web1-546f799d9-h8s9k 0/1 Terminating 0 83m
pod/web1-778dcf88b-7lgjm 1/1 Running 0 36s
pod/web1-778dcf88b-c7fw9 1/1 Running 0 7s
pod/web1-778dcf88b-tj86f 1/1 Running 0 22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web1 NodePort 10.97.204.112
[root@k8s-master ~]# kubectl get pods,svc -n web1
NAME READY STATUS RESTARTS AGE
pod/web1-778dcf88b-7lgjm 1/1 Running 0 57s
pod/web1-778dcf88b-c7fw9 1/1 Running 0 28s
pod/web1-778dcf88b-tj86f 1/1 Running 0 43s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web1 NodePort 10.97.204.112
[root@k8s-master ~]# kubectl set image deployment web1 nginx=nginx:1.18 -n web1
deployment.apps/web1 image updated
kubectl edit deployment web1 -n web1
…
滚动升级过程
[root@k8s-master ~]# kubectl get replicaset -n web1
NAME DESIRED CURRENT READY AGE
web1-546f799d9 0 0 0 100m -> nginx:1.16
web1-778dcf88b 0 0 0 17m -> nginx:1.17
web1-69f84f567 3 3 3 8m20s -> nginx:1.18
deployment -> replicaset
[root@k8s-master ~]# kubectl describe deploy web1 -n web1
Name: web1
Namespace: web1
CreationTimestamp: Sun, 20 Feb 2022 07:51:11 +0000
Labels: app=web1-app
project=web1-pj
Annotations: deployment.kubernetes.io/revision: 3
Selector: app=web1-app
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=web1-app
Containers:
nginx:
Image: nginx:1.18
OldReplicaSets: <none>
NewReplicaSet: web1-69f84f567 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 22m deployment-controller Scaled up replica set web1-778dcf88b to 1
Normal ScalingReplicaSet 21m deployment-controller Scaled down replica set web1-546f799d9 to 2
Normal ScalingReplicaSet 21m deployment-controller Scaled up replica set web1-778dcf88b to 2
Normal ScalingReplicaSet 21m deployment-controller Scaled down replica set web1-546f799d9 to 1
Normal ScalingReplicaSet 21m deployment-controller Scaled up replica set web1-778dcf88b to 3
Normal ScalingReplicaSet 21m deployment-controller Scaled down replica set web1-546f799d9 to 0
Normal ScalingReplicaSet 13m deployment-controller Scaled up replica set web1-69f84f567 to 1
Normal ScalingReplicaSet 13m deployment-controller Scaled down replica set web1-778dcf88b to 2
Normal ScalingReplicaSet 13m deployment-controller Scaled up replica set web1-69f84f567 to 2
Normal ScalingReplicaSet 12m (x3 over 12m) deployment-controller (combined from similar events): Scaled down replica set web1-778dcf88b to 0
3 水平扩缩容(启动多实例,挺高并发性)
修改yaml里的replicas值,再apply
kubectl scale deployment web1 --replicas=10
*replicas参数控制pod副本数
[root@k8s-master ~]# vi web1-deploy.yaml
spec:
replicas: 10
[root@k8s-master ~]# kubectl apply -f web1-deploy.yaml
deployment.apps/web1 configured
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-778dcf88b-2lqvr 1/1 Running 0 22s
web1-778dcf88b-2mglr 1/1 Running 0 21s
web1-778dcf88b-4fgqq 1/1 Running 0 26s
web1-778dcf88b-5d69b 1/1 Running 0 21s
web1-778dcf88b-6jqzk 1/1 Running 0 26s
web1-778dcf88b-7gnnv 1/1 Running 0 23s
web1-778dcf88b-f26ld 1/1 Running 0 26s
web1-778dcf88b-j5mhr 1/1 Running 0 24s
web1-778dcf88b-m78lc 1/1 Running 0 26s
web1-778dcf88b-nzrmc 1/1 Running 0 26s
[root@k8s-master ~]# kubectl scale deployment web1 --replicas=5 -n web1
deployment.apps/web1 scaled
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-778dcf88b-2lqvr 0/1 Terminating 0 2m36s
web1-778dcf88b-2mglr 0/1 Terminating 0 2m35s
web1-778dcf88b-4fgqq 1/1 Running 0 2m40s
web1-778dcf88b-5d69b 0/1 Terminating 0 2m35s
web1-778dcf88b-6jqzk 1/1 Running 0 2m40s
web1-778dcf88b-7gnnv 0/1 Terminating 0 2m37s
web1-778dcf88b-f26ld 1/1 Running 0 2m40s
web1-778dcf88b-j5mhr 0/1 Terminating 0 2m38s
web1-778dcf88b-m78lc 1/1 Running 0 2m40s
web1-778dcf88b-nzrmc 1/1 Running 0 2m40s
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-778dcf88b-4fgqq 1/1 Running 0 2m51s
web1-778dcf88b-6jqzk 1/1 Running 0 2m51s
web1-778dcf88b-f26ld 1/1 Running 0 2m51s
web1-778dcf88b-m78lc 1/1 Running 0 2m51s
web1-778dcf88b-nzrmc 1/1 Running 0 2m51s
4 回滚(项目升级失败回复到正常版本)
kubectl rollout history deployment/web #查看历史发布版本
kubectl rollout undo deployment/web #回滚上一个版本
kubectl rollout undo deployment/web --to-revision=2 #回滚到历史指定版本
[root@k8s-master ~]# kubectl get rs -n web1
NAME DESIRED CURRENT READY AGE
web1-546f799d9 0 0 0 135m
web1-69f84f567 0 0 0 44m
web1-778dcf88b 5 5 5 52m
[root@k8s-master ~]# kubectl rollout history deployment web1 -n web1
deployment.apps/web1
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
[root@k8s-master ~]# kubectl rollout undo deployment web1 -n web1
deployment.apps/web1 rolled back
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-69f84f567-2pflg 1/1 Running 0 9s
web1-69f84f567-5kcvt 1/1 Running 0 9s
web1-69f84f567-lv8t4 1/1 Running 0 11s
web1-69f84f567-xsjlm 1/1 Running 0 11s
web1-69f84f567-zqm6m 1/1 Running 0 11s
web1-778dcf88b-6jqzk 0/1 Terminating 0 15m
web1-778dcf88b-m78lc 0/1 Terminating 0 15m
web1-778dcf88b-nzrmc 0/1 Terminating 0 15m
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-69f84f567-2pflg 1/1 Running 0 15s
web1-69f84f567-5kcvt 1/1 Running 0 15s
web1-69f84f567-lv8t4 1/1 Running 0 17s
web1-69f84f567-xsjlm 1/1 Running 0 17s
web1-69f84f567-zqm6m 1/1 Running 0 17s
[root@k8s-master ~]# kubectl rollout history deployment web1 -n web1
deployment.apps/web1
REVISION CHANGE-CAUSE
1 <none>
4 <none>
5 <none>
[root@k8s-master ~]# kubectl get rs -n web1
NAME DESIRED CURRENT READY AGE
web1-546f799d9 0 0 0 140m
web1-69f84f567 5 5 5 48m
web1-778dcf88b 0 0 0 57m
5 删除
kubectl delete deployment/web
kubectl delete svc/web
*kubeclt deletel pod 删除pod并没有用,是由控制器管理的,控制器还会再启起来新的pod
[root@k8s-master ~]# kubectl delete pod web1-69f84f567-2pflg -n web1
pod "web1-69f84f567-2pflg" deleted
[root@k8s-master ~]# kubectl get pods -n web1
NAME READY STATUS RESTARTS AGE
web1-69f84f567-5kcvt 1/1 Running 0 19m
web1-69f84f567-bp6sr 1/1 Running 0 16s
web1-69f84f567-lv8t4 1/1 Running 0 19m
web1-69f84f567-xsjlm 1/1 Running 0 19m
web1-69f84f567-zqm6m 1/1 Running 0 19m
[root@k8s-master ~]#
滚动升级与回滚实现机制
标签:kubectl,生命周期,Running,web1,778dcf88b,deployment,k8s From: https://www.cnblogs.com/z20240722/p/18316061