pod控制器deployment控制器实例

  • Post author:
  • Post category:其他



Deployment控制器简介:

Deployment控制器:建筑在replicaset之上对pod资源进行操作,支持流动,回滚,作用与无状态应用。
[root@k8s-master deployment]# vim deployment.yaml    编辑一个yaml文件。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - containerPort: 80
[root@k8s-master deployment]# kubectl apply -f deployment.yaml
在这里我们生成文件是使用apply,apply与create的区别在于,apply我们可以多次修改yaml配置文件修改完后,也可以重新
deployment.apps/myapp-deploy created
[root@k8s-master deployment]# 
[root@k8s-master deployment]# kubectl get deployment    查看创建的pod控制器
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy       2/2     2            2           6s
[root@k8s-master deployment]# kubectl get rs     名称后面的字符串是模板的哈希值,不会发生变化
NAME                         DESIRED   CURRENT   READY   AGE
myapp-deploy-756d67b77d      2         2         2       31s

[root@k8s-master deployment]# kubectl describe deploy myapp-deploy   查看我们创建控制器的详细信息
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Thu, 24 Dec 2020 15:53:56 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=myapp,release=canary
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate    这表示我们创建的deployment控制器支持的更新类型
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge  最多有几个不可用,指定超出副本数有几个
Pod Template:
  Labels:  app=myapp
           release=canary
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-756d67b77d (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  6m28s  deployment-controller  Scaled up replica set myapp-deploy-756d67b77d to 2
[root@k8s-master deployment]# 
#我们通过打补丁的方式修改yaml配置文件清单,通过path命令。
[root@k8s-master deployment]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
myapp-deploy-756d67b77d-dsx2d      1/1     Running   0          14m
myapp-deploy-756d67b77d-vz8ss      1/1     Running   0          14m
[root@k8s-master deployment]# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
deployment.apps/myapp-deploy patched
[root@k8s-master deployment]# kubectl get pods  请求pod得知我们名称后面表示模板的hash值,后面才是pod名称。
NAME                               READY   STATUS    RESTARTS   AGE
myapp-deploy-756d67b77d-5c8gd      1/1     Running   0          4s
myapp-deploy-756d67b77d-dsx2d      1/1     Running   0          15m
myapp-deploy-756d67b77d-vz8ss      1/1     Running   0          15m
myapp-deploy-756d67b77d-w9gwz      1/1     Running   0          4s
myapp-deploy-756d67b77d-zcpsv      1/1     Running   0          4s

#实例2:我们修改控制中滚动升级策略。将修该最多有0个不可用,指定超出副本1
[root@k8s-master deployment]# kubectl patch deployment  myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.apps/myapp-deploy patched
[root@k8s-master deployment]# 
[root@k8s-master deployment]# kubectl describe deploy myapp-deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Thu, 24 Dec 2020 15:53:56 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=myapp,release=canary
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  0 max unavailable, 1 max surge
Pod Template:
  Labels:  app=myapp
           release=canary
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-756d67b77d (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  46m   deployment-controller  Scaled up replica set myapp-deploy-756d67b77d to 5
[root@k8s-master deployment]# 


进行版本升级发布。

[root@k8s-master deployment]# kubectl set image deploy myapp-deploy myapp=ikubernetes/myapp:v2 
deployment.apps/myapp-deploy image updated
[root@k8s-master deployment]# kubectl rollout status deployment myapp-deploy    查看控制器的更新状态
deployment "myapp-deploy" successfully rolled out
[root@k8s-master deployment]# kubectl get pods
myapp-deploy-d74b56f6c-26x6x       1/1     Running   0          19m
myapp-deploy-d74b56f6c-2bbxd       1/1     Running   0          19m
myapp-deploy-d74b56f6c-9q4xf       1/1     Running   0          19m
myapp-deploy-d74b56f6c-svrq2       1/1     Running   0          19m
myapp-deploy-d74b56f6c-xrc6d       1/1     Running   0          19m

[root@k8s-master deployment]# kubectl describe pods myapp-deploy-d74b56f6c-26x6x    查看pod镜像版本已经升级到v2了。
Name:         myapp-deploy-d74b56f6c-26x6x
Namespace:    default
Priority:     0
Node:         k8s-node1/10.5.100.157
Start Time:   Fri, 25 Dec 2020 10:51:17 +0800
Labels:       app=myapp
              pod-template-hash=d74b56f6c
              release=canary
Annotations:  <none>
Status:       Running
IP:           10.244.1.33
IPs:
  IP:           10.244.1.33
Controlled By:  ReplicaSet/myapp-deploy-d74b56f6c
Containers:
  myapp:
    Container ID:   docker://f046b263f0e88c0adb02b7692fab975cf55d79ad60d94c9580a1663e1073827c
    Image:          ikubernetes/myapp:v2 

[root@k8s-master deployment]# kubectl rollout pause deploy myapp-deploy  pause这表示暂时更新。resume:表示继续运行。


进行版本回滚。

[root@k8s-master deployment]# kubectl rollout history deployment myapp-deploy  查看控制器有几个版本。
deployment.apps/myapp-deploy 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
[root@k8s-master deployment]# kubectl rollout undo deployment myapp-deploy --to-revision=1  --to-revision:指定回滚的版本。  undo命令指定版本回滚
deployment.apps/myapp-deploy rolled back
[root@k8s-master deployment]# kubectl get pods -o wide
NAME                               READY   STATUS              RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES

myapp-deploy-756d67b77d-hlpnp      1/1     Running             0          3s      10.244.2.42   k8s-node2   <none>           <none>
myapp-deploy-756d67b77d-hqsvx      1/1     Running             0          7s      10.244.2.41   k8s-node2   <none>           <none>
myapp-deploy-756d67b77d-hvvhs      0/1     ContainerCreating   0          0s      <none>        k8s-node1   <none>           <none>
myapp-deploy-756d67b77d-qp4dd      1/1     Running             0          1s      10.244.2.43   k8s-node2   <none>           <none>
myapp-deploy-756d67b77d-wfr5x      1/1     Running             0          6s      10.244.1.35   k8s-node1   <none>           <none>
myapp-deploy-d74b56f6c-26x6x       1/1     Terminating         0          24m     10.244.1.33   k8s-node1   <none>           <none>
myapp-deploy-d74b56f6c-2bbxd       1/1     Running             0          24m     10.244.1.32   k8s-node1   <none>           <none>
myapp-deploy-d74b56f6c-svrq2       0/1     Terminating         0          24m     10.244.2.39   k8s-node2   <none>           <none>
nginx-deployment-bd4bfb877-xs62q   1/1     Running             1          16d     10.244.1.18   k8s-node1   <none>           <none>

[root@k8s-master deployment]# kubectl describe pods myapp-deploy-756d67b77d-hlpnp  进行版本回退后镜像版本为v1了
Name:         myapp-deploy-756d67b77d-hlpnp
Namespace:    default
Priority:     0
Node:         k8s-node2/10.5.100.106
Start Time:   Fri, 25 Dec 2020 19:15:16 +0800
Labels:       app=myapp
              pod-template-hash=756d67b77d
              release=canary
Annotations:  <none>
Status:       Running
IP:           10.244.2.42
IPs:
  IP:           10.244.2.42
Controlled By:  ReplicaSet/myapp-deploy-756d67b77d
Containers:
  myapp:
    Container ID:   docker://fe83f1cde893e610294df85cb7d86de38a04a47b13d5302dc45055ff6116f15e
    Image:          ikubernetes/myapp:v1

#通过打补丁方式进行版本更新
[root@k8s-master deployment]#  kubectl patch  deployment myapp-deploy -p '{"spec":{"template":{"spec":{"containers":[{"name":"myapp-containers","image":"ikubernetes/myapp:v2"}]}}}}'
[root@k8s-master deployment]# kubectl rollout history deployment myapp-deploy
deployment.apps/myapp-deploy 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>



版权声明:本文为yurun_house原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。