美文网首页
Kubernetes探索学习系列005----Kubernete

Kubernetes探索学习系列005----Kubernete

作者: StarDustMrsu | 来源:发表于2019-03-21 17:02 被阅读0次

1.Kubernetes的controller pattern

需要认识到Kubernetes操作Pod的逻辑,都是由控制器来完成的。

查看之前写过的nginx-deployment的YAML文件
[root@kubernetes01 ~]# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels: 
    app: nginx
spec:
  replicas: 2
  selector: 
    matchLabels:
      app: nginx
      
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.8.1
        ports:
        - containerPort: 80

Kubernetes中的kube-controller-manager组件执行了上面的Deployment定义的编排动作。
Deployment定义的template字段,这种字段在Kubernetes中叫做PodTemplate(Pod模版)。
而这个YAML文件描述的就是Deployment这样一个控制器,template以上的部分是对控制器的定义,template以下的部分属于对被控制的对象的定义。

2.Kubernetes Pod 的自动伸缩

Pod水平扩展和水平收缩的能力依赖于Kubernetes中一个非常重要的API对象,ReplicaSet也就是副本集。

2.1.Pod的水平扩展和伸缩

查看例子的YAML文件
[root@kubernetes01 ~]# cat nginx-replicaset.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset
  labels: 
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.8.0
上面这个例子中,ReplicaSet对象是由一个Pod模版和副本数目的多少定义组成的。

再看这个YAML文件
[root@kubernetes01 ~]# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels: 
    app: nginx
spec:
  replicas: 3
  selector: 
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.8.1
        ports:
        - containerPort: 80
Deployment控制器实际操作的是ReplicaSet对象,而不是Pod对象。
这个定义replicas=3的Deployment与他的ReplicaSet以及Pod的关系可以理解成是一种分层控制层层控制的关系,replicas的值决定是扩展还是收缩,当你去扩展或是收缩的时候还伴随着滚动更新。

2.2.Pod的滚动更新

1.首先修改镜像版本
kubectl edit deployment/nginx-deployment
spec:
      containers:
      - image: nginx:1.9.1
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
这块儿直接把image中nginx的版本改成了1.9.1然后保存退出,文件有了新的变动会触发更新。

2.查看滚动更新的效果
[root@kubernetes01 ~]# kubectl describe deployment nginx-deployment | tail -n 10
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  30m    deployment-controller  Scaled up replica set nginx-deployment-7f987f7889 to 3
  Normal  ScalingReplicaSet  8m55s  deployment-controller  Scaled up replica set nginx-deployment-6987cdb55b to 1
  Normal  ScalingReplicaSet  8m54s  deployment-controller  Scaled down replica set nginx-deployment-7f987f7889 to 2
  Normal  ScalingReplicaSet  8m54s  deployment-controller  Scaled up replica set nginx-deployment-6987cdb55b to 2
  Normal  ScalingReplicaSet  8m33s  deployment-controller  Scaled down replica set nginx-deployment-7f987f7889 to 1
  Normal  ScalingReplicaSet  8m33s  deployment-controller  Scaled up replica set nginx-deployment-6987cdb55b to 3
  Normal  ScalingReplicaSet  8m12s  deployment-controller  Scaled down replica set nginx-deployment-7f987f7889 to 0
首先通过Events输出的内容我们可以看到这个deployment滚动更新的Message。

[root@kubernetes01 ~]# kubectl get rs
NAME                               DESIRED   CURRENT   READY   AGE
nginx-deployment-5c689d88bb        0         0         0       14d
nginx-deployment-5cd6d46846        0         0         0       14d
nginx-deployment-67d57d6df9        0         0         0       9d
nginx-deployment-6987cdb55b        3         3         3       10m
nginx-deployment-7f987f7889        0         0         0       9d
nginx-replicaset                   0         0         0       37m
然后查看这个Deployment控制的ReplicaSet的最终状态,这里等待了一会儿所以已经是最终的状态了,配合前边Events的信息,nginx-deployment-6987cdb55b up to 1,旧的nginx-deployment-7f987f7889 down to 2,通过交替的逐一的升级和降级“滚动更新”,最终
nginx-deployment-6987cdb55b 的DESIRED,CURRENT,READY都变成了期望值3!

状态字段的含义:
DESIRED:用户期望的Pod副本个数
CURRENT:当前处于Running状态的Pod的个数
UP-TO-DATE:当前处于最新版本的Pod的个数
READY:处于健康检查正确状态的Pod的个数,Running状态的新版本

我们还可以通过 kubectl rollout status deployment/nginx-deployment这条命令实时的查看Deployment对象的状态变化。应用的版本和ReplicaSet是一一对应的,这也是Kubernetes实现对多个应用版本描述的设计思想。

3.Kubernetes Deployment对应用的版本控制

1.首先通过命令直接这个Deployment的镜像修改成错误的
[root@kubernetes01 ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.99
deployment.extensions/nginx-deployment image updated

2.查看Deployment的状态
[root@kubernetes01 ~]# kubectl get rs
NAME                               DESIRED   CURRENT   READY   AGE
nginx-deployment-5476c5bdd         1         1         0       12s
nginx-deployment-5c689d88bb        0         0         0       15d
nginx-deployment-5cd6d46846        0         0         0       14d
nginx-deployment-67d57d6df9        0         0         0       10d
nginx-deployment-6987cdb55b        3         3         3       46m
nginx-deployment-7f987f7889        0         0         0       10d
nginx-replicaset                   0         0         0       73m
nginx-staticwebsite-648bc64544     2         2         2       9d
nginx-staticwebsite-8479f8997f     0         0         0       9d
tomcat-deployment-001-84d957dc97   2         2         2       6d1h
tomcat-test-6cc4d85cf6             2         2         2       6d18h
可以看到nginx-deployment-5476c5bdd这个有问题的Deployment的状态。

3.回退到上个版本
[root@kubernetes01 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment

4.回退之后查看Deployment的状态
[root@kubernetes01 ~]# kubectl get rs
NAME                               DESIRED   CURRENT   READY   AGE
nginx-deployment-5476c5bdd         0         0         0       90s
nginx-deployment-5c689d88bb        0         0         0       15d
nginx-deployment-5cd6d46846        0         0         0       14d
nginx-deployment-67d57d6df9        0         0         0       10d
nginx-deployment-6987cdb55b        3         3         3       47m
nginx-deployment-7f987f7889        0         0         0       10d
nginx-replicaset                   0         0         0       74m
nginx-staticwebsite-648bc64544     2         2         2       9d
nginx-staticwebsite-8479f8997f     0         0         0       9d
tomcat-deployment-001-84d957dc97   2         2         2       6d1h
tomcat-test-6cc4d85cf6             2         2         2       6d18h

5.查看这个Deployment的历史版本记录
[root@kubernetes01 ~]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
4         <none>
6         <none>
7         <none>

6.查看第一个版本的信息
[root@kubernetes01 ~]# kubectl rollout history deployment/nginx-deployment --revision=1
deployment.extensions/nginx-deployment with revision #1
Pod Template:
  Labels:       app=nginx
        pod-template-hash=5c689d88bb
  Containers:
   nginx:
    Image:      nginx:1.7.9
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

7.切换到第一个版本
[root@kubernetes01 ~]# kubectl rollout undo deployment/nginx-deployment --to-revision=1
deployment.extensions/nginx-deployment

8.查看Deployment的状态
[root@kubernetes01 ~]# kubectl get rs
NAME                               DESIRED   CURRENT   READY   AGE
nginx-deployment-5476c5bdd         0         0         0       19m
nginx-deployment-5c689d88bb        3         3         3       15d
nginx-deployment-5cd6d46846        0         0         0       14d
nginx-deployment-67d57d6df9        0         0         0       10d
nginx-deployment-6987cdb55b        0         0         0       65m
nginx-deployment-7f987f7889        0         0         0       10d
nginx-replicaset                   0         0         0       92m
nginx-staticwebsite-648bc64544     2         2         2       9d
nginx-staticwebsite-8479f8997f     0         0         0       9d
tomcat-deployment-001-84d957dc97   2         2         2       6d1h
tomcat-test-6cc4d85cf6             2         2         2       6d18h
这块儿可以看到副本集nginx-deployment-5c689d88bb已经变成了我们期望的状态。

4.总结

Deployment是一个俩层控制器,首先通过控制ReplicaSet来控制版本,ReplicaSet则控制Pod。我们可以通过使用kubectl rollout命令控制应用的版本。
PS:文中服务器使用的是国内某☁️的机器
欢迎大家留言讨论哦,欢迎大家和我一起学习Kubernetes~~~

相关文章

网友评论

      本文标题:Kubernetes探索学习系列005----Kubernete

      本文链接:https://www.haomeiwen.com/subject/kiecvqtx.html