创建应用
depoly-demoapp-v10.yaml
apiVersion: v1
kind: Namespace
metadata:
name: demoapp
---
apiVersion: v1
kind: Service
metadata:
labels:
app: demoappv10
name: demoappv10
namespace: demoapp
spec:
ports:
- name: http-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: demoapp
version: v1.0
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: demoappv10
version: v1.0
name: demoappv10
namespace: demoapp
spec:
replicas: 2
selector:
matchLabels:
app: demoapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: demoapp
version: v1.0
spec:
containers:
- image: ikubernetes/demoapp:v1.0
name: demoapp
env:
- name: PORT
value: "8080"
resources: {}
创建deployment资源
# kubectl apply -f depoly-demoapp-v10.yaml
namespace/demoapp created
deployment.apps/demoappv10 created
访问服务
# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoappv10-69f6cf9477-v9m6g, ServerIP: 172.20.154.216!
查看deployment资源
# kubectl get deploy -n demoapp
NAME READY UP-TO-DATE AVAILABLE AGE
demoappv10 2/2 2 2 104s
查看 Deployment 滚动更新状态
# kubectl rollout status deploy/demoappv10 -n demoapp
deployment "demoappv10" successfully rolled out
查看ReplicaSet
# kubectl get rs -n demoapp
NAME DESIRED CURRENT READY AGE
demoappv10-78b6586d58 2 2 2 4m59s
查看pod 标签
# kubectl get pods -n demoapp --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demoappv10-78b6586d58-64hq4 1/1 Running 0 6m48s app=demoapp,pod-template-hash=78b6586d58,version=v1.0
demoappv10-78b6586d58-f72jw 1/1 Running 0 6m48s app=demoapp,pod-template-hash=78b6586d58,version=v1.0
查看Deployment 修订历史
# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10
REVISION CHANGE-CAUSE
1 <none>
Deployment 添加注解
# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl apply -f depoly-demoapp-v10.yaml" -n demoapp
deployment.apps/demoappv10 annotated
确认Deployment 添加注解
# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10
REVISION CHANGE-CAUSE
1 kubectl apply -f depoly-demoapp-v10.yaml
应用更新
仅当 Deployment Pod 模板(即 .spec.template)发生改变时,例如模板的标签或容器镜像被更新, 才会触发 Deployment 更新。其他更新(如对 Deployment 执行扩缩容的操作)不会触发更新动作。
更新操作
# kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
deployment.apps/demoappv10 image updated
deployment/demoappv10表明 Deployment 的名称,demoapp表明需要进行更新的容器,ikubernetes/demoapp:v1.1则表示镜像的新版本以及它的标签。
查看滚动更新过程
# kubectl rollout status deploy demoappv10 -n demoapp
deployment "demoappv10" successfully rolled out
查看rs控制器
# kubectl get rs -n demoapp
NAME DESIRED CURRENT READY AGE
demoappv10-6d99f44649 2 2 2 62s
demoappv10-78b6586d58 0 0 0 85s
查看deployment日志
# kubectl describe deployments -n demoapp
Name: demoappv10
Namespace: demoapp
CreationTimestamp: Fri, 16 Jun 2023 15:34:50 +0800
Labels: app=demoappv10
version=v1.0
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=demoapp
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=demoapp
version=v1.0
Containers:
demoapp:
Image: ikubernetes/demoapp:v1.1
Port: <none>
Host Port: <none>
Environment:
PORT: 8080
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: demoappv10-6d99f44649 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m15s deployment-controller Scaled up replica set demoappv10-78b6586d58 to 2
Normal ScalingReplicaSet 2m52s deployment-controller Scaled up replica set demoappv10-6d99f44649 to 1
Normal ScalingReplicaSet 2m50s deployment-controller Scaled down replica set demoappv10-78b6586d58 to 1 from 2
Normal ScalingReplicaSet 2m50s deployment-controller Scaled up replica set demoappv10-6d99f44649 to 2 from 1
Normal ScalingReplicaSet 2m49s deployment-controller Scaled down replica set demoappv10-78b6586d58 to 0 from 1
访问服务
# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoappv10-6d99f44649-q8cbc, ServerIP: 172.20.44.209!
Deployment 添加注解
# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp" -n demoapp
deployment.apps/demoappv10 annotated
查看 Deployment 修订历史
# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10
REVISION CHANGE-CAUSE
1 kubectl apply -f depoly-demoapp-v10.yaml
2 kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
应用回滚
有时,你可能想要回滚 Deployment;例如,当 Deployment 不稳定时(例如进入反复崩溃状态)。 默认情况下,Deployment 的所有上线记录都保留在系统中,以便可以随时回滚 (你可以通过修改修订历史记录限制来更改这一约束)。
检查 Deployment 上线历史
# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10
REVISION CHANGE-CAUSE
1 kubectl apply -f depoly-demoapp-v10.yaml
2 kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
要查看修订历史的详细信息
# kubectl rollout history deployment/demoappv10 --revision=2 -n demoapp
deployment.apps/demoappv10 with revision #2
Pod Template:
Labels: app=demoapp
pod-template-hash=6d99f44649
version=v1.0
Annotations: kubernetes.io/change-cause: kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
Containers:
demoapp:
Image: ikubernetes/demoapp:v1.1
Port: <none>
Host Port: <none>
Environment:
PORT: 8080
Mounts: <none>
Volumes: <none>
回滚到上一个版本
执行操作
# kubectl rollout undo deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 rolled back
访问服务
# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoappv10-78b6586d58-c5n96, ServerIP: 172.20.89.151!
获取 Deployment 描述信息
# kubectl describe deployments -n demoapp
Name: demoappv10
Namespace: demoapp
CreationTimestamp: Fri, 16 Jun 2023 15:52:30 +0800
Labels: app=demoappv10
version=v1.0
Annotations: deployment.kubernetes.io/revision: 3
kubernetes.io/change-cause: kubectl apply -f depoly-demoapp-v10.yaml
Selector: app=demoapp
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=demoapp
version=v1.0
Containers:
demoapp:
Image: ikubernetes/demoapp:v1.0
Port: <none>
Host Port: <none>
Environment:
PORT: 8080
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: demoappv10-78b6586d58 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 108s deployment-controller Scaled up replica set demoappv10-78b6586d58 to 1 from 0
Normal ScalingReplicaSet 106s deployment-controller Scaled down replica set demoappv10-6d99f44649 to 1 from 2
Normal ScalingReplicaSet 106s deployment-controller Scaled up replica set demoappv10-78b6586d58 to 2 from 1
Normal ScalingReplicaSet 105s deployment-controller Scaled down replica set demoappv10-6d99f44649 to 0 from 1
Deployment 添加注解
# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl rollout undo deployment/demoappv10 -n demoapp" -n demoapp
deployment.apps/demoappv10 annotated
查看 Deployment 修订历史
# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10
REVISION CHANGE-CAUSE
2 kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
3 kubectl rollout undo deployment/demoappv10 -n demoapp
回滚到指定版本
# kubectl rollout undo deployment/demoappv10 --revision=2 -n demoapp
参考文档
https://kubernetes.io/zh-cn/docs/tutorials/kubernetes-basics/update/update-intro/
标签:kubectl,回滚,set,Deployment,demoapp,版本升级,deployment,pod,demoappv10 From: https://www.cnblogs.com/wangguishe/p/17483012.html