k8s-升级和回滚app的版本

首先我们使用下面的命令查看一下当前的namespace中的deployment:

~> kubectl get deployments
No resources found.

可以看到目前并没有deployment,接下来我们先创建一个2个实例的book-store程序。

kubectl run book-store --replicas=2 --labels="run=dev" --image=docker.io/rollenholt/book-store:v1 --port=8080

这个命令创建了一个Deployment对象和一个关联的ReplicaSet对象。这个ReplicaSet有2个Pods,每一个pods都运行一个book-store程序
然后创建一个Service:

~> kubectl expose deployment book-store --type=NodePort --name=book-store-service
service "book-store-service" exposed
~> kubectl get deployments
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
book-store   2         2         2            2           2m
~>

可以看到只有一个book-store, 然后查看正在运行的Pods:

~> kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
book-store-2084161627-2pvmr   1/1       Running   0          2m
book-store-2084161627-72lpf   1/1       Running   0          2m

如果我们使用kubectl describe pods查看这些运行的Pods的详情的话:

~> kubectl describe pods
Name:		book-store-2084161627-2pvmr
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 11:45:02 +0800
Labels:		pod-template-hash=2084161627
		run=dev
Status:		Running
IP:		172.17.0.4
Controllers:	ReplicaSet/book-store-2084161627
Containers:
  book-store:
    Container ID:	docker://a9f2553e298708163a5fc1b950474b725711a4aa15dad430768a3fad8d0cb904
    Image:		docker.io/rollenholt/book-store:v1
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 11:45:03 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  3m		3m		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2084161627-2pvmr to minikube
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v1" already present on machine
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id a9f2553e2987; Security:[seccomp=unconfined]
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id a9f2553e2987
Name:		book-store-2084161627-72lpf
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 11:45:02 +0800
Labels:		pod-template-hash=2084161627
		run=dev
Status:		Running
IP:		172.17.0.5
Controllers:	ReplicaSet/book-store-2084161627
Containers:
  book-store:
    Container ID:	docker://6577d43652f00b2e659e723367508e5cba265bfba27b732f83c108d1a435781c
    Image:		docker.io/rollenholt/book-store:v1
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 11:45:03 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  3m		3m		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2084161627-72lpf to minikube
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v1" already present on machine
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id 6577d43652f0; Security:[seccomp=unconfined]
  3m		3m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id 6577d43652f0

在上面的命令输出中,重点关注下面的几行:

Containers:
  book-store:
    Container ID:	docker://a9f2553e298708163a5fc1b950474b725711a4aa15dad430768a3fad8d0cb904
    Image:		docker.io/rollenholt/book-store:v1

可以看到使用的image为docker.io/rollenholt/book-store:v1,版本为v1。接下来我们会把book-store程序升级到v2版本。

版本升级

为了升级程序使用的image的版本,我们需要使用set image命令:

    kubectl set image deployments/book-store book-store=docker.io/rollenholt/book-store:v2

上面的命令会通知Deployment为我们的程序使用不同的image并且发起rolling update,也就是滚动更新。 可以使用kubectl get pods来检查新pod的状态并且可以看到旧的pod正在处于terminating状态(有时候状态更新很快,可能执行命令的时候已经是Running状态了)
现在我们需要确认一下应用程序的升级是否正确, 首先确认我们的app运行没有问题:

~> kubectl get services
NAME                 CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
book-store-service   10.0.0.123   <nodes>       8080:30001/TCP   1m
kubernetes           10.0.0.1     <none>        443/TCP          1d
~>
~>
~> kubectl describe service book-store-service
Name:			book-store-service
Namespace:		default
Labels:			run=dev
Selector:		run=dev
Type:			NodePort
IP:			10.0.0.123
Port:			<unset>	8080/TCP
NodePort:		<unset>	30001/TCP
Endpoints:		172.17.0.5:8080,172.17.0.6:8080
Session Affinity:	None
No events.
~>
~>
~> kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/proxy/namespaces/kube-system/services/kube-dns
kubernetes-dashboard is running at https://192.168.99.100:8443/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
~>
~> curl http://192.168.99.100:30001/book/list
[]%

可以看出我们的app运行的一切正常。(为了简单我的book-store的v1和v2其实一模一样,就是build的指定了不同的标签而已,读者可以尝试修改自己的返回值,这样看着更加明显一些)
appa的升级状态确认也可以使用下面的命令:

~> kubectl rollout status deployments/book-store
deployment "book-store" successfully rolled out

然后我们确认当前的image的版本:

~> kubectl describe pods
Name:		book-store-2165557340-9gb6f
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 11:51:30 +0800
Labels:		pod-template-hash=2165557340
		run=dev
Status:		Running
IP:		172.17.0.6
Controllers:	ReplicaSet/book-store-2165557340
Containers:
  book-store:
    Container ID:	docker://0c1eb84bde78c82b2e0e0f3edc78341ab39c281293ef6f6b0092c3325d3bef1f
    Image:		docker.io/rollenholt/book-store:v2
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 11:51:31 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  12m		12m		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2165557340-9gb6f to minikube
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v2" already present on machine
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id 0c1eb84bde78; Security:[seccomp=unconfined]
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id 0c1eb84bde78
Name:		book-store-2165557340-bfrqk
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 11:51:30 +0800
Labels:		pod-template-hash=2165557340
		run=dev
Status:		Running
IP:		172.17.0.5
Controllers:	ReplicaSet/book-store-2165557340
Containers:
  book-store:
    Container ID:	docker://9249bddb5a6bc02431a5bb8c0f6efd66dc42a7f80c11eb56cb68777f7fd55406
    Image:		docker.io/rollenholt/book-store:v2
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 11:51:31 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  12m		12m		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2165557340-bfrqk to minikube
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v2" already present on machine
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id 9249bddb5a6b; Security:[seccomp=unconfined]
  12m		12m		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id 9249bddb5a6b
~>

从上面的日志中的:

Containers:
  book-store:
    Container ID:	docker://0c1eb84bde78c82b2e0e0f3edc78341ab39c281293ef6f6b0092c3325d3bef1f
    Image:		docker.io/rollenholt/book-store:v2

可以看出我们的程序的image已经升级到v2版本了。

版本回滚

如果我们想回滚刀之前的版本,我们可以使用下面的命令:

kubectl rollout undo deployments/book-store

然后我们再次使用kubectl describe pods进行验证:

~> kubectl describe pods
Name:		book-store-2084161627-4t2pb
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 12:06:26 +0800
Labels:		pod-template-hash=2084161627
		run=dev
Status:		Running
IP:		172.17.0.4
Controllers:	ReplicaSet/book-store-2084161627
Containers:
  book-store:
    Container ID:	docker://074d64f999f8bd6bdd2d2ca94e557f3df9f0b42a3b7b7801bdba8088c2f95272
    Image:		docker.io/rollenholt/book-store:v1
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 12:06:26 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  17s		17s		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2084161627-4t2pb to minikube
  17s		17s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v1" already present on machine
  17s		17s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id 074d64f999f8; Security:[seccomp=unconfined]
  17s		17s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id 074d64f999f8
Name:		book-store-2084161627-r400c
Namespace:	default
Node:		minikube/192.168.99.100
Start Time:	Wed, 22 Mar 2017 12:06:26 +0800
Labels:		pod-template-hash=2084161627
		run=dev
Status:		Running
IP:		172.17.0.5
Controllers:	ReplicaSet/book-store-2084161627
Containers:
  book-store:
    Container ID:	docker://71d24b52c862dc068e6f1fa043b60838cae8c570daa3485273383e85f725ee19
    Image:		docker.io/rollenholt/book-store:v1
    Image ID:		docker://sha256:3b21f3ff3dca273da90fa2c35cf04ec23cfd6a1b4bf7aa2a492c3d83ec866705
    Port:		8080/TCP
    State:		Running
      Started:		Wed, 22 Mar 2017 12:06:27 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xwgxn (ro)
    Environment Variables:	<none>
Conditions:
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
Volumes:
  default-token-xwgxn:
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-xwgxn
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  17s		17s		1	{default-scheduler }					Normal		Scheduled	Successfully assigned book-store-2084161627-r400c to minikube
  17s		17s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Pulled		Container image "docker.io/rollenholt/book-store:v1" already present on machine
  16s		16s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Created		Created container with docker id 71d24b52c862; Security:[seccomp=unconfined]
  16s		16s		1	{kubelet minikube}	spec.containers{book-store}	Normal		Started		Started container with docker id 71d24b52c862

可以看到我们现在回到了v1版本,说明回滚成功了。

本文版权归作者所有,禁止一切形式的转载,复制等操作
赞赏

微信赞赏支付宝赞赏

2 replies to k8s-升级和回滚app的版本

发表评论

电子邮件地址不会被公开。 必填项已用*标注