一、Deployment

Deployment的主要功能就是自动部署一个容器应用的多份副本,以及持续监控副本的数量,在集群内始终维持用户指定的副本数量

1.配置参数
K8S实践Ⅲ(Pod控制器)

Selector(选择器):
  .spec.selector是可选字段,用来指定 label selector ,圈定Deployment管理的pod范围。如果被指定, .spec.selector 必须匹配 .spec.template.metadata.labels,否则它将被API拒绝。如果 .spec.selector 没有被指定, .spec.selector.matchLabels 默认是.spec.template.metadata.labels。

2.Deployment配置实例

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
        app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
# kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           58s
# kubectl get pods
NAME                               READY   STATUS             RESTARTS   AGE
liveness-http                      0/1     CrashLoopBackOff   473        28h
nginx-deployment-6dd86d77d-6hk7w   1/1     Running            0          71s
nginx-deployment-6dd86d77d-mxr8l   1/1     Running            0          71s
nginx-deployment-6dd86d77d-xxhpd   1/1     Running            0          71s
# kubectl get pods --show-labels
NAME                               READY   STATUS    RESTARTS   AGE    LABELS
nginx-deployment-6dd86d77d-6hk7w   1/1     Running   0          2m5s   app=nginx,pod-template-hash=6dd86d77d
nginx-deployment-6dd86d77d-mxr8l   1/1     Running   0          2m5s   app=nginx,pod-template-hash=6dd86d77d
nginx-deployment-6dd86d77d-xxhpd   1/1     Running   0          2m5s   app=nginx,pod-template-hash=6dd86d77d

当 Deployment 创建或者接管 ReplicaSet 时,Deployment controller 会自动为 Pod 添加 pod-template-hash label。这样做的目的是防止 Deployment 的子ReplicaSet 的 pod 名字重复

3.Deployment更新升级

现将nginx镜像更新为1.9.1版本,可以通过kubectl set image命令为Deployment设置新的镜像版本

# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment.extensions/nginx-deployment image updated
# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-6dd86d77d    0         0         0       70m
nginx-deployment-784b7cc96d   3         3         3       21m

# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-784b7cc96d-4575d   1/1     Running   0          2m13s
nginx-deployment-784b7cc96d-c98s8   1/1     Running   0          91s
nginx-deployment-784b7cc96d-r6sm9   1/1     Running   0          2m51s
#此处名称已经更新

查看Deployment详细信息

# kubectl describe deployment/nginx-deployment
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  17m   deployment-controller  Scaled up replica set nginx-deployment-784b7cc96d to 1
  Normal  ScalingReplicaSet  17m   deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 2
  Normal  ScalingReplicaSet  17m   deployment-controller  Scaled up replica set nginx-deployment-784b7cc96d to 2
  Normal  ScalingReplicaSet  16m   deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 1
  Normal  ScalingReplicaSet  16m   deployment-controller  Scaled up replica set nginx-deployment-784b7cc96d to 3
  Normal  ScalingReplicaSet  16m   deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 0

另一种更新方法是使用kubectl edit命令修改 Deployment配置:
#kubectl edit deployment/nginx-deployment

4.Deployment版本回滚
查看deployment升级记录

# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
  • 默认情况下,kubernetes 会在系统中保存前两次 Deployment 的 rollout 历史记录,可以修改revision history limit来更改保存的revision数。
  • 这里在创建Deployment时没有增加--record参数,所以并不能看到revision的变化。在创建 Deployment 的时候加上--record参数就可以看到每个版本使用的命令。
  • Deployment的更新操作是在进行rollout时触发的,这意味着当且仅当 Deployment 的 Pod template(如.spec.template)被更改时才会创建新的revision,其他的更新操作(增加副本数)将不会触发Deployment的更新操作,这意味着当回滚到之前的版本时,只有 Deployment 中的 Pod template 部分才会回退。

查看特点版本的详细信息

# kubectl rollout history deployment/nginx-deployment --revision=2

回滚至上一个版本

# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back

也可以使用--to-revision参数指定某个历史版本

# kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment.extensions/nginx-deployment rolled back

5.Deployment暂停和恢复

有时需要多次对deployment配置进行修改,为了避免频繁触发其更新操作,可以先暂停deployment的更新操作,然后进行配置修改。
需要注意的是,在暂停过程中无法回滚该deployment

暂停deployment的更新操作

# kubectl rollout pause deployment/nginx-deployment
deployment.extensions/nginx-deployment paused

恢复更新

# kubectl rollout resume deploy nginx-deployment
deployment.extensions/nginx-deployment resumed

6.Deployment扩容

# kubectl scale deploy nginx-deployment --replicas=5
deployment.extensions/nginx-deployment scaled
# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-784b7cc96d-9z8df   1/1     Running   0          52m
nginx-deployment-784b7cc96d-dqfv4   1/1     Running   0          8s
nginx-deployment-784b7cc96d-q4jcw   1/1     Running   0          52m
nginx-deployment-784b7cc96d-rs8fn   1/1     Running   0          34m
nginx-deployment-784b7cc96d-v5drb   1/1     Running   0          8s

二、DaemonSet

DaemonSet用于管理在集群中每个Node上仅运行一份Pod的副本实例。
适用场景:

  • 在每个Node上都运行一个 GlusterFS存储或者Ceph存储的进程
  • 在每个Node上都运行一个日志采集程序,例如:Fluentd、Logstash
  • 在每个Node上都运行一个性能监控daemon,例如:Prometheus Node Exporter、collectd

1.使用daemonset创建redis-filebeat示例

# cat redis-filebeat-ds.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
      - name: redis
        image: redis:4.0-alpine
        ports:
        - name: redis
          containerPort: 6379

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  namespace: default
spec:
  selector:
    matchLabels:
      app: filebeat
      release: stable
  template:
    metadata:
      labels:
        app: filebeat
        release: stable
    spec:
      containers:
      - name: filebeat
        image: ikubernetes/filebeat:5.6.5-alpine
        env:
        - name: REDIS_HOST
          value: redis.default.svc.cluster.local
        - name: REDIS_LOG_LEVEL
          value: info
# kubectl expose deployment redis --port=6379
# kubectl get ds
NAME          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
filebeat-ds   2         2         2       2            2           <none>          41s

每个node上都创建了一个pod

# kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP          NODE    NOMINATED NODE   READINESS GATES
filebeat-ds-ss4jx                   1/1     Running   0          70s   10.44.0.5   k8s-2   <none>           <none>
filebeat-ds-z5bdx                   1/1     Running   0          70s   10.36.0.4   k8s-3   <none>           <none>
redis-58b9f5776-sd2tc               1/1     Running   0          70s   10.36.0.3   k8s-3   <none>           <none>

2.DaemonSet的更新

  • 用修改配置文件更新,使用kubectl apply
  • 或者使用kubectl edit
# kubectl edit ds/filebeat-ds

对镜像版本进行更新

# kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine
daemonset.extensions/filebeat-ds image updated

查看更新状态

# kubectl get pods -w
NAME                                READY   STATUS              RESTARTS   AGE
filebeat-ds-9l84r                   0/1     ContainerCreating   0          2s
filebeat-ds-z5bdx                   1/1     Running             0          17m
redis-58b9f5776-sd2tc               1/1     Running             0          17m
filebeat-ds-9l84r                   1/1     Running             0          20s
filebeat-ds-z5bdx                   1/1     Terminating         0          18m
filebeat-ds-z5bdx                   0/1     Terminating         0          18m
filebeat-ds-z5bdx                   0/1     Terminating         0          18m
filebeat-ds-z5bdx                   0/1     Terminating         0          18m
filebeat-ds-psdgb                   0/1     Pending             0          0s
filebeat-ds-psdgb                   0/1     Pending             0          0s
filebeat-ds-psdgb                   0/1     ContainerCreating   0          0s
filebeat-ds-psdgb                   1/1     Running             0          19s

三、Pod的扩缩容

1.手动扩缩容
使用kubectl scale设置副本数

# kubectl scale deployment nginx-deployment --replicas=5

2.自动扩缩容 HPA

四、StatefulSet