更新cilium文档、滚动更新文档

pull/296/head
gjmzj 2018-08-19 13:03:23 +08:00
parent f0bff405af
commit e467d0a2de
9 changed files with 170 additions and 53 deletions

View File

@ -32,7 +32,7 @@
![cilium_http_gsg](../pics/cilium_http_gsg.jpg) ![cilium_http_gsg](../pics/cilium_http_gsg.jpg)
根据文件[http-sw-app.yaml](..//roles/cilium/files/star_war_example/http-sw-app.yaml) 创建 `$ kubectl create -f http-sw-app.yaml` 后,验证如下: 根据文件[http-sw-app.yaml](../roles/cilium/files/star_war_example/http-sw-app.yaml) 创建 `$ kubectl create -f http-sw-app.yaml` 后,验证如下:
``` bash ``` bash
$ kubectl get pods,svc $ kubectl get pods,svc
@ -88,7 +88,7 @@ Ship landed # 成功着陆
![cilium_http_l3_l4_gsg](../pics/cilium_http_l3_l4_gsg.jpg) ![cilium_http_l3_l4_gsg](../pics/cilium_http_l3_l4_gsg.jpg)
根据文件[sw_l3_l4_policy.yaml](..//roles/cilium/files/star_war_example/sw_l3_l4_policy.yaml) 创建 `$ kubectl apply -f sw_l3_l4_policy.yaml` 后,验证如下: 根据文件[sw_l3_l4_policy.yaml](../roles/cilium/files/star_war_example/sw_l3_l4_policy.yaml) 创建 `$ kubectl apply -f sw_l3_l4_policy.yaml` 后,验证如下:
``` bash ``` bash
$ kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing $ kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing
@ -148,7 +148,7 @@ main.main()
![cilium_http_l3_l4_l7_gsg](../pics/cilium_http_l3_l4_l7_gsg.jpg) ![cilium_http_l3_l4_l7_gsg](../pics/cilium_http_l3_l4_l7_gsg.jpg)
限制L7 的安全策略,根据文件[sw_l3_l4_l7_policy.yaml](..//roles/cilium/files/star_war_example/sw_l3_l4_l7_policy.yaml) 创建 `$ kubectl apply -f sw_l3_l4_policy.yaml` 后,验证如下: 限制L7 的安全策略,根据文件[sw_l3_l4_l7_policy.yaml](../roles/cilium/files/star_war_example/sw_l3_l4_l7_policy.yaml) 创建 `$ kubectl apply -f sw_l3_l4_l7_policy.yaml` 后,验证如下:
``` bash ``` bash
$ kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing $ kubectl exec tiefighter -- curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing

View File

@ -11,85 +11,202 @@
## 4、演示 ## 4、演示
>使用kubectl更新一个已部署的应用程序并模拟回滚。为了方便分析将应用程序的pod副本数量设置为10。 >使用kubectl更新一个已部署的应用程序并模拟回滚。为了方便分析将应用程序的pod副本数量设置为10。
```javascript ``` bash
kubectl -n k8s-ecoysystem-apps scale deployment helloworldapi --replicas=10 $ kubectl run busy --image=busybox:1.28.4 sleep 36000000 --replicas=10
``` ```
### 4.1. 发布微服务 ### 4.1. 发布微服务
```javascript - 当前服务状态查看
查看部署列表 ``` bash
$ kubectl get deployments -n k8s-ecoysystem-apps # 查看部署列表
查看正在运行的pod root@kube-aio:~# kubectl get deploy busy
$ kubectl get pods -n k8s-ecoysystem-apps NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
通过pod描述查看应用程序的当前映像版本 busy 10 10 10 10 5m
$ kubectl describe pods -n k8s-ecoysystem-apps
# 查看正在运行的pod
root@kube-aio:~# kubectl get pod | grep busy
busy-794c95f5d7-56b6w 1/1 Running 0 5m
busy-794c95f5d7-8ddjr 1/1 Running 0 5m
busy-794c95f5d7-8zm8r 1/1 Running 0 5m
busy-794c95f5d7-9hjhp 1/1 Running 0 5m
busy-794c95f5d7-df2r2 1/1 Running 0 5m
busy-794c95f5d7-fsn94 1/1 Running 0 5m
busy-794c95f5d7-k4w8r 1/1 Running 0 5m
busy-794c95f5d7-lsmgb 1/1 Running 0 5m
busy-794c95f5d7-rg8kw 1/1 Running 0 5m
busy-794c95f5d7-xpxxt 1/1 Running 0 5m
# 通过pod描述查看应用程序的当前映像版本
root@kube-aio:~# kubectl describe pod busy-794c95f5d7-56b6w |grep Image
Image: busybox:1.28.4
Image ID: docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
``` ```
![](../../pics/prePublish.png) - 升级镜像版本到1.29
- 为了更清晰看到更新过程,可另开一个窗口使用`$ watch kubectl get deployment busy`实时查看变化
```javascript ``` bash
升级镜像版本到v2.3 $ kubectl set image deployments/busy busy=busybox:1.29
$ kubectl -n k8s-ecoysystem-apps set image deployments/helloworldapi helloworldapi=registry.wuling.com/justmine/helloworldapi:v2.3
``` ```
![](../../pics/postPublish.png)
### 4.2. 验证发布 ### 4.2. 验证发布
```javascript ``` bash
检查rollout状态 # 检查rollout状态
kubectl -n k8s-ecoysystem-apps rollout status deployments/helloworldapi root@kube-aio:~# kubectl rollout status deployments/busy
检查pod详情 deployment "busy" successfully rolled out
kubectl describe pods -n k8s-ecoysystem-apps
# 检查pod详情
root@kube-aio:~# kubectl describe pod busy-665cdb7b-44jnt |grep Image
Image: busybox:1.29
Image ID: docker-pullable://busybox@sha256:cb63aa0641a885f54de20f61d152187419e8f6b159ed11a251a09d115fdff9bd
``` ```
从上面可以看到镜像已经升级到1.29版本
![](../../pics/validatePublish.png)
从上图可以看到镜像已经升级到v2.3版本
### 4.3. 回滚发布 ### 4.3. 回滚发布
```javascript ``` bash
kubectl -n k8s-ecoysystem-apps rollout undo deployments/helloworldapi # 回滚发布
``` root@kube-aio:~# kubectl rollout undo deployments/busy
deployment.apps "busy"
![](../../pics/rollbackPublish.png) # 回滚完成
root@kube-aio:~# kubectl rollout status deployments/busy
deployment "busy" successfully rolled out
# 镜像又回退到1.28.4 版本
root@kube-aio:~# kubectl describe pod busy-794c95f5d7-4x9bn |grep Image
Image: busybox:1.28.4
Image ID: docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
```
到目前为止,整个滚动发布工作就圆满完成了!!! 到目前为止,整个滚动发布工作就圆满完成了!!!
**那么如果我们想回滚到指定版本呢答案是k8s完美支持并且还可以通过资源文件进行配置保留的历史版次量**。由于篇幅有限,感兴趣的朋友,可以自己下去实战,回滚命令如下: **那么如果我们想回滚到指定版本呢答案是k8s完美支持并且还可以通过资源文件进行配置保留的历史版次量**。由于篇幅有限,感兴趣的朋友,可以自己下去实战,回滚命令如下:
```javascript ```javascript
kubectl -n k8s-ecoysystem-apps rollout undo deployment/helloworldapi --to-revision=<版次> kubectl rollout undo deployment/busy --to-revision=<版次>
``` ```
## 5、原理 ## 5、原理
k8s精确地控制着整个发布过程分批次有序地进行着滚动更新直到把所有旧的副本全部更新到新版本。实际上k8s是通过两个参数来精确地控制着每次滚动的pod数量 k8s精确地控制着整个发布过程分批次有序地进行着滚动更新直到把所有旧的副本全部更新到新版本。实际上k8s是通过两个参数来精确地控制着每次滚动的pod数量
>* **`maxSurge` 滚动更新过程中运行操作期望副本数的最大pod数可以为绝对数值(eg5)但不能为0也可以为百分数(eg10%)。默认为25%。** >* **`maxSurge` 滚动更新过程中运行操作期望副本数的最大pod数可以为绝对数值(eg5)但不能为0也可以为百分数(eg10%)。**
>* **`maxUnavailable` 滚动更新过程中不可用的最大pod数可以为绝对数值(eg5)但不能为0也可以为百分数(eg10%)。默认为25%。** >* **`maxUnavailable` 滚动更新过程中不可用的最大pod数可以为绝对数值(eg5)但不能为0也可以为百分数(eg10%)。**
如果未指定这两个可选参数则k8s会使用默认配置 如果未指定这两个可选参数则k8s会使用默认配置
```javascript ``` bash
kubectl -n k8s-ecoysystem-apps get deployment helloworldapi -o yaml root@kube-aio:~# kubectl get deploy busy -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
creationTimestamp: 2018-08-19T02:42:56Z
generation: 3
labels:
run: busy
name: busy
namespace: default
resourceVersion: "199461"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/busy
uid: 93fde307-a359-11e8-a93b-525400c61543
spec:
progressDeadlineSeconds: 600
replicas: 10
revisionHistoryLimit: 10
selector:
matchLabels:
run: busy
strategy:
rollingUpdate:
maxSurge: 1 # 滚动更新中最多超过预期值的 pod数
maxUnavailable: 1 # 滚动更新中最多不可用的 pod数
type: RollingUpdate
...
``` ```
![](../../pics/publishDefaulConfig.png)
### 5.1. 浅析部署概况 ### 5.1. 浅析部署概况
![](../../pics/theory-dep-summary.png) ``` bash
# 初始状态
root@kube-aio:~# kubectl get deploy busy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
busy 10 10 10 10 1h
# 再做一遍回退
root@kube-aio:~# kubectl rollout undo deploy busy
deployment.apps "busy"
# 更新过程1
root@kube-aio:~# kubectl get deploy busy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
busy 10 11 2 9 1h
# 更新过程2
root@kube-aio:~# kubectl get deploy busy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
busy 10 11 4 9 1h
# 更新过程3
root@kube-aio:~# kubectl get deploy busy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
busy 10 11 6 9 1h
# 更新结束
root@kube-aio:~# kubectl get deploy busy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
busy 10 10 10 10 1h
```
>* `DESIRED`   最终期望处于READY状态的副本数   >* `DESIRED`   最终期望处于READY状态的副本数  
>* `CURRENT` 当前的副本总数 >* `CURRENT` 当前的副本总数
>* `UP-TO-DATE` 当前完成更新的副本数 >* `UP-TO-DATE` 当前完成更新的副本数
>* `AVAILABLE` 当前可用的副本数 >* `AVAILABLE` 当前可用的副本数
当前的副本总数 = 10 + 10 * 25% = 13所以CURRENT为13。 当前的副本总数10(DESIRED) + 1(maxSurge) = 11所以CURRENT为11
当前可用的副本数 = 10 - 10 * 25% = 8所以AVAILABLE为8。 当前可用的副本数10(DESIRED) - 1(maxUnavailable) = 9所以AVAILABLE为9
### 5.2. 浅析部署详情 ### 5.2. 浅析部署详情
```javascript
kubectl -n k8s-ecoysystem-apps describe deployment helloworldapi
```
![](../../pics/theory-dep-detail.png)
整个滚动过程是通过控制两个副本集来完成的新的副本集helloworldapi-6564f59f66旧的副本集helloworldapi-6f4959c8c7 。 ``` bash
root@kube-aio:~# kubectl describe deploy busy
Name: busy
Namespace: default
CreationTimestamp: Sun, 19 Aug 2018 12:27:19 +0800
Labels: run=busy
Annotations: deployment.kubernetes.io/revision=2
Selector: run=busy
Replicas: 10 desired | 10 updated | 10 total | 10 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=busy
Containers:
busy:
Image: busybox:1.29
Port: <none>
Host Port: <none>
Args:
sleep
3600000
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: busy-84cb46955d (10/10 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set busy-9669c8599 to 10
Normal ScalingReplicaSet 46s deployment-controller Scaled up replica set busy-84cb46955d to 1
Normal ScalingReplicaSet 46s deployment-controller Scaled down replica set busy-9669c8599 to 9
Normal ScalingReplicaSet 46s deployment-controller Scaled up replica set busy-84cb46955d to 2
Normal ScalingReplicaSet 43s deployment-controller Scaled down replica set busy-9669c8599 to 8
Normal ScalingReplicaSet 43s deployment-controller Scaled up replica set busy-84cb46955d to 3
Normal ScalingReplicaSet 43s deployment-controller Scaled down replica set busy-9669c8599 to 7
Normal ScalingReplicaSet 43s deployment-controller Scaled up replica set busy-84cb46955d to 4
Normal ScalingReplicaSet 40s deployment-controller Scaled down replica set busy-9669c8599 to 6
Normal ScalingReplicaSet 28s (x12 over 40s) deployment-controller (combined from similar events): Scaled down replica set busy-9669c8599 to 0
```
整个滚动过程是通过控制两个副本集来完成的新的副本集busy-84cb46955d旧的副本集busy-9669c8599 。
理想状态下的滚动过程: 理想状态下的滚动过程:
>1. 创建了一个新的副本集并为其分配3个新版本的pod使副本总数达到13一切正常。 >1. 创建新副本集并为其分配1个新版本的pod
>2. 通知旧副本集销毁2个旧版本的pod使可用副本总数保持到8一起正常。 >2. 通知旧副本集,销毁1个旧版本的pod
>3. 当两个副本销毁成功后通知新副本集再新增2个新版本的pod使副本总数达到13一切正常。 >3. 当旧副本销毁成功后通知新副本集再新增1个新版本的pod当新副本创建成功后通知旧副本再减少1个pod
>只要销毁成功新副本集就会创造新的pod一直循环直到旧的副本集pod数量为0。 >只要销毁成功新副本集就会创造新的pod一直循环直到旧的副本集pod数量为0。
### 5.4 总结 ### 5.4 总结
**`无论理想还是不理想k8s最终都会使应用程序全部更新到期望状态都会始终保持最大的副本总数和可用副本总数的不变性`** **`无论理想还是不理想k8s最终都会使应用程序全部更新到期望状态都会始终保持最大的副本总数和可用副本总数的不变性`**

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB