kubernetes/k8s中deployments yaml 示例
目录kubernetes/k8s中deployments yaml 示例deployments的创建deployments的更新deployments image的升级通过`kubectl set image`来进行升级通过`deployment.yaml`文件来更新imagedeployments的回滚deployment的删除kubernetes/k8s中deployments yaml 示例
目录
kubernetes/k8s中deployments yaml 示例
本文介绍kubernetes/k8s中deployments的生命周期管理,包含创建,更新,升级,回退,删除。 并给出示例
deployments的创建
下面我们将创建一个hello-server
的deployment
创建文件deployment.yaml
,内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-hello-server
spec:
selector:
matchLabels:
app: deploy-hello-server
template:
metadata:
labels:
app: deploy-hello-server
spec:
containers:
- image: wssiqi/hello-server:v1
name: hello-server
应用deployment并查看状态
$ kubectl apply -f deployment.yaml
deployment.apps/deploy-hello-server created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy-hello-server-79846bbbd6-z8p77 1/1 Running 0 4s
deployments的更新
下面我们尝试将deployment中replica的数量设置为2
更新deployment.yaml
内容如下,添加replicas: 2
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-hello-server
spec:
selector:
matchLabels:
app: deploy-hello-server
replicas: 2
template:
metadata:
labels:
app: deploy-hello-server
spec:
containers:
- image: wssiqi/hello-server:v1
name: hello-server
更新deployment
$ kubectl apply -f deployment.yaml
deployment.apps/deploy-hello-server configured
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy-hello-server-79846bbbd6-qnwtn 1/1 Running 0 15s
deploy-hello-server-79846bbbd6-z8p77 1/1 Running 0 8m10s
可以看到pod的数量已经更新到设置的值replicas: 2
deployments image的升级
演示如何进行rolling upgrade
首先确保hello-server
的deployment已经创建,参考上面的文档
通过kubectl set image
来进行升级
先检查deployment里面的image情况
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
deploy-hello-server 2/2 2 2 17m
$ kubectl describe deployments deploy-hello-server
Name: deploy-hello-server
Namespace: default
CreationTimestamp: Thu, 03 Jun 2021 03:19:59 -0400
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=deploy-hello-server
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=deploy-hello-server
Containers:
hello-server:
Image: wssiqi/hello-server:v1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: deploy-hello-server-79846bbbd6 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set deploy-hello-server-79846bbbd6 to 1
Normal ScalingReplicaSet 9m32s deployment-controller Scaled up replica set deploy-hello-server-79846bbbd6 to 2
可以看到这个deployment的image只有wssiqi/hello-server:v1
我们将这个image升级为 wssiqi/hello-server:v2
$ kubectl set image deployments/deploy-hello-server hello-server=wssiqi/hello-server:v2
deployment.apps/deploy-hello-server image updated
命令中的参数介绍如下
deployments/deploy-hello-server
: deployments/
为固定写法, deploy-hello-server
来自于 Name: deploy-hello-server
hello-server=wssiqi/hello-server:v2
: hello-server
是container名字,来自于hello-server:
, wssiqi/hello-server:v2
表示将containerhello-server
的image设置为v2版本
检查是否已经更新完成
$ kubectl describe deployments deploy-hello-server | grep Image
Image: wssiqi/hello-server:v2
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy-hello-server-557fb9bcb7-857kq 1/1 Running 0 3m48s
deploy-hello-server-557fb9bcb7-f7tch 1/1 Running 0 2m41s
通过上面命令发现deployment已经更新完成,并且新的pod已经启动 (NAME和之前的不同)
通过deployment.yaml
文件来更新image
通过deployment.yaml来将image更新为v3版本
更新deployment.yaml
文件内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-hello-server
spec:
selector:
matchLabels:
app: deploy-hello-server
replicas: 2
template:
metadata:
labels:
app: deploy-hello-server
spec:
containers:
- image: wssiqi/hello-server:v3
name: hello-server
应用新的deployment
$ kubectl apply -f deployment.yaml
deployment.apps/deploy-hello-server configured
$ kubectl get deployments -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deploy-hello-server 2/2 2 2 36m hello-server wssiqi/hello-server:v3 app=deploy-hello-server
可以看到deployments的IMAGE已经更新为v3版本
deployments的回滚
当我们发现新的image有问题,可以通过kubectl来进行回滚
首先查看更新的history
$ kubectl rollout history deployment deploy-hello-server
deployment.apps/deploy-hello-server
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
表示总共有三次更新(第一次为创建)
查看各个版本的内容
$ kubectl rollout history deployment deploy-hello-server --revision=1
deployment.apps/deploy-hello-server with revision #1
Pod Template:
Labels: app=deploy-hello-server
pod-template-hash=79846bbbd6
Containers:
hello-server:
Image: wssiqi/hello-server:v1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
上面的版本查看了revision=1
的内容
回滚到revision=1
$ kubectl rollout undo deployment deploy-hello-server --to-revision=1
deployment.apps/deploy-hello-server rolled back
查看当前状态
$ kubectl get deployments -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deploy-hello-server 2/2 2 2 50m hello-server wssiqi/hello-server:v1 app=deploy-hello-server
可以看到已经回滚到v1版本,并且READY
和AVAILABLE
都正常
deployment的删除
介绍如何删除deployment
查看deployment
$ kubectl get deployments -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deploy-hello-server 2/2 2 2 53m hello-server wssiqi/hello-server:v1 app=deploy-hello-server
删除deployment
$ kubectl delete deployments deploy-hello-server
deployment.apps "deploy-hello-server" deleted
deployment和pod都已经没了
$ kubectl get deployments
No resources found in default namespace.
[omc@localhost deployment-example]$ kubectl get pods
No resources found in default namespace.
参考文档:
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
更多推荐
所有评论(0)