Deployment

  • We can easily rollout new updates to our application using deployments.
  • Deployments will perform update in rollout manner to ensure that your app is not down.

image-20200119165134971

实践

接上节的replicaset定义:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  # modify replicas according to your case
  replicas: 5
  selector:
    matchLabels:
      tier: frontend-end
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: nginx

如果想部署deployment,只需要将kind: ReplicaSet改下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  # modify replicas according to your case
  replicas: 5
  selector:
    matchLabels:
      tier: frontend-end
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: nginx

kubectl apply下,成功部署:

image-20200119162932886

如果此时要更新版本,将nginx升级成1.17.7,就要重新编写yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  # modify replicas according to your case
  replicas: 5
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: nginx:1.17.7

再次kubectl apply:

此时会创建一个新的rs,替换掉原来的rs

image-20200119164850947

[@BDSZYF000132741:pod]$ kubectl describe deploy frontend
Name: frontend
Namespace: default
CreationTimestamp: Sun, 19 Jan 2020 16:26:59 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 3
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"frontend","namespace":"default"},"spec":{"replicas":5,"se...
Selector: tier=frontend
Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
  Labels: tier=frontend
  Containers:
   php-redis:
    Image: nginx:1.17.7
    Port: <none>
    Host Port: <none>
    Environment: <none>
    Mounts: <none>
  Volumes: <none>
Conditions:
  Type Status Reason
  ---- ------ ------
  Available True MinimumReplicasAvailable
  Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: frontend-6b48d58678 (5/5 replicas created)
Events:
  Type Reason Age From Message
  ---- ------ ---- ---- -------
  Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set frontend-5778f7c84d to 5
  Normal ScalingReplicaSet 4m34s deployment-controller Scaled up replica set frontend-5b7b9bf75b to 2
  Normal ScalingReplicaSet 4m34s deployment-controller Scaled down replica set frontend-5778f7c84d to 4
  Normal ScalingReplicaSet 4m34s deployment-controller Scaled up replica set frontend-5b7b9bf75b to 3
  Normal ScalingReplicaSet 55s deployment-controller Scaled down replica set frontend-5b7b9bf75b to 0
  Normal ScalingReplicaSet 55s deployment-controller Scaled up replica set frontend-6b48d58678 to 3
  Normal ScalingReplicaSet 53s deployment-controller Scaled down replica set frontend-5778f7c84d to 3
  Normal ScalingReplicaSet 53s deployment-controller Scaled up replica set frontend-6b48d58678 to 4
  Normal ScalingReplicaSet 53s deployment-controller Scaled down replica set frontend-5778f7c84d to 2
  Normal ScalingReplicaSet 23s (x3 over 53s) deployment-controller (combined from similar events): Scaled down replica set frontend-5778f7c84d to 0

RollingUpdateStrategy: 25% max unavailable, 25% max surge,每次下掉25%的pod,然后在新的rs上创建25%的pod。

events中可以发现,每次下掉一个pod,再创建一个新的pod。

可以使用rollout查看部署的历史:

image-20200119165538149

image-20200119165604012

rollout

假如部署了新版本的应用,发现新版本有问题要回滚,可以使用rollout命令回滚。回滚到第一个版本:

kubectl rollout undo deployment.v1.apps/kplabs-deployment --to-revision 1

此时第一个revision被删除:

[@BDSZYF000132741:pod]$ kubectl rollout history deployment.v1.apps/frontend
deployment.apps/frontend 
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>

kubectl describe deployment frontend:

image-20200119170704361