一、kubernetes核心技术-Controller控制器:controller流程概述

|NO.Z.00014|——————————|CloudNative|_4s

|NO.Z.00014|——————————|CloudNative|_nginx_02

|NO.Z.00014|——————————|CloudNative|_4s_03

二、使用deployment部署应用(yaml)

### --- 使用deployment部署应用(yaml)
~~~ 就可以部署,但是只能做测试环境
[root@k8s-master ~]# kubectl create deployment web --image=nginx

~~~ 生成yaml文件
[root@k8s-master ~]# kubectl create deployment web --image=nginx --dry-run -o yaml


~~~   生成yaml文件并导出

[root@k8s-master ~]# kubectl create deployment web --image=nginx --dry-run -o yaml > web.yaml
[root@k8s-master ~]# vim web.yaml
spec: # 通过这两条做匹配,selector和labels标签来做匹配
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
### --- 通过yaml文件进行部署
[root@k8s-master ~]# kubectl apply -f web.yaml
deployment.apps/web configured

~~~ 查看部署的应用
[root@k8s-master ~]# kubectl get pods
### --- 对外发布部署

[root@k8s-master ~]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web1 -o yaml > web1.yaml
### --- 部署
### --- 通过浏览器来访问验证部署是否验证成功。

[root@k8s-master ~]# kubectl apply -f web1.yaml
[root@k8s-master ~]# kubectl get pods,svc

三、应用升级回滚和弹性伸缩

### --- 应用升级回滚和弹性伸缩
~~~ 指定nginx版本

[root@k8s-master ~]# vim web.yaml
spec:
containers:
- image: nginx:1.14 # 指定为1.14的版本
[root@k8s-master ~]# kubectl apply -f web.yaml
deployment.apps/web configured
[root@k8s-master ~]# kubectl get pods
web-66bf4959f5-dh6dm 0/1 ContainerCreating 0 23s
### --- 升级nginx到1.15版本
[root@k8s-master ~]# kubectl set image deployment web nginx=nginx:1.15
deployment.apps/web image updated

~~~ 再做升级的操作
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 16m
web-66bf4959f5-dh6dm 1/1 Running 0 3m34s
web-bbcf684cb-cj274 0/1 ContainerCreating 0 4s
~~~   node节点可以看到镜像的版本1.15下载成功
[root@k8s-node1 ~]# docker images
nginx 1.15 53f3fd8007f7 21 months ago 109MB

~~~ 查看升级后的状态:显示successfully是成功的
[root@k8s-master ~]# kubectl rollout status deployment web
deployment "web" successfully rolled out
### --- 把nginx1.15回滚到nginx1.14版本

[root@k8s-master ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none> # nginx1.14版本
3 <none> # 当前的版本:1.15版本
~~~   回滚到上一个版本
[root@k8s-master ~]# kubectl rollout undo deployment web
deployment.apps/web rolled back

~~~ 查看回滚的状态
[root@k8s-master ~]# kubectl rollout status deployment web
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
deployment "web" successfully rolled out
### --- 把nginx1.14回滚到指定的版本
~~~ 回滚到第三个版本

[root@k8s-master ~]# kubectl rollout undo deployment web --to-revision=3
deployment.apps/web rolled back
[root@k8s-master ~]# kubectl rollout status deployment web
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
deployment "web" successfully rolled out
### --- 弹性伸缩

[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 28m
web-bbcf684cb-w894j 1/1 Running 0 2m20s
~~~   创建10个pod

[root@k8s-master ~]# kubectl scale deployment web --replicas=10
deployment.apps/web scaled
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 31m
web-bbcf684cb-2g2wr 1/1 Running 0 2m54s
web-bbcf684cb-672j9 1/1 Running 0 2m54s
web-bbcf684cb-bjbrj 1/1 Running 0 2m54s
web-bbcf684cb-ctpv2 1/1 Running 0 2m54s
web-bbcf684cb-hr8j5 1/1 Running 0 2m54s
web-bbcf684cb-jbm8l 1/1 Running 0 2m54s
web-bbcf684cb-rn4pt 1/1 Running 0 2m54s
web-bbcf684cb-vr2pb 1/1 Running 0 2m54s
web-bbcf684cb-w894j 1/1 Running 0 5m50s
web-bbcf684cb-xgdfq 0/1 Running 0 2m54s











Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart

                                                                                                                                                   ——W.S.Landor