kubernetes-handbook/introduction/201.md

86 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Kubernetes 201
## 扩展应用
通过修改Deployment中副本的数量replicas可以动态扩展或收缩应用
![扩容](../images/scale.png)
这些自动扩展的容器会自动加入到service中而收缩回收的容器也会自动从service中删除。
```sh
$ kubectl scale --replicas=3 deployment/nginx-app
$ kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-app 3 3 3 3 10m
```
## 滚动升级
滚动升级Rolling Update通过逐个容器替代升级的方式来实现无中断的服务升级
```
kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2
```
![update1](../images/update1.png)
![update2](../images/update2.png)
![update3](../images/update3.png)
![update4](../images/update4.png)
在滚动升级的过程中,如果发现了失败或者配置错误,还可以随时会滚回来:
```
kubectl rolling-update frontend-v1 frontend-v2 --rollback
```
需要注意的是rolling-update只针对ReplicationController不能直接用在deployment上。Deployment可以在spec中设置更新策略为RollingUpdate默认就是RollingUpdate
```yaml
spec:
replicas: 3
selector:
matchLabels:
run: nginx-app
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
```
而更新应用的话,就可以直接用`kubectl set`命令:
```sh
kubectl set image deployment/nginx-app nginx-app=nginx:1.9.1
```
滚动升级的过程可以用`rollout`命令查看:
```sh
$ kubectl rollout status deployment/nginx-app
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for rollout to finish: 2 of 3 updated replicas are available...
Waiting for rollout to finish: 2 of 3 updated replicas are available...
Waiting for rollout to finish: 2 of 3 updated replicas are available...
Waiting for rollout to finish: 2 of 3 updated replicas are available...
Waiting for rollout to finish: 2 of 3 updated replicas are available...
deployment "nginx-app" successfully rolled out
```
Deployment同样支持回滚
```sh
$ kubectl rollout history deployment/nginx-app
deployments "nginx-app"
REVISION CHANGE-CAUSE
1 <none>
2 <none>
$ kubernetes kubectl rollout undo deployment/nginx-app
deployment "nginx-app" rolled back
```