kubenetes-控制器deployment

作用

deployment 是用来管理无状态应用的,面向的集群的管理,而不是面向的是一个不可变的个体

Deployment 为Pod 和 ReplicaSet 之上,提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。

deloyment从1.16版本以后主要通过标签控制pod.

创建

[root@bjcy-200 deployment]# kubectl create deployment nginx-dm --image=harbor.tcc.com/public/nginx --dry-run=client -o yaml > nginx-dm.yaml

[root@bjcy-200 deployment]# cat nginx-dm.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:                       # 标签
    app: nginx-dm
  name: nginx-dm        # dm名字
spec:
  replicas: 1                  # 副本数
  selector:                     # 主要通过 selector 定义控制的pod 标签
    matchLabels:
      app: nginx-dm
  strategy: {}
  template:                  # 定义 pod
    metadata:
      creationTimestamp: null
      labels:                   #pod 标签
        app: nginx-dm
    spec:
      containers:
      - image: harbor.tcc.com/public/nginx
        name: nginx
        resources: {}
status: {}
    

deployment常规操作

副本数扩容

通过命令行方式

[root@bjcy-200 deployment]# kubectl get deployments.apps 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dm   1/1     1            1           35m

[root@bjcy-200 deployment]# kubectl scale deployment nginx-dm --replicas=5
deployment.apps/nginx-dm scaled


[root@bjcy-200 deployment]# kubectl get deployments.apps 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dm   5/5     5            5           37m

在线修改

 kubectl edit deployments.apps nginx-dm 
修改值: replicas 

修改yaml文件

略......

滚动升级

升级

[root@bjcy-200 deployment]# kubectl get deployments.apps -o wide 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                        SELECTOR
nginx-dm   5/5     5            5           70m   nginx        harbor.tcc.com/public/nginx   app=nginx-dm

[root@bjcy-200 deployment]# kubectl set image deployment nginx-dm nginx=harbor.tcc.com/public/nginx:v1.7.9 --record 
deployment.apps/nginx-dm image updated

[root@bjcy-200 deployment]# kubectl get deployments.apps -o wide 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                               SELECTOR
nginx-dm   5/5     5            5           71m   nginx        harbor.tcc.com/public/nginx:v1.7.9   app=nginx-dm

回滚

[root@bjcy-200 deployment]# kubectl rollout history  deployment nginx-dm 
deployment.apps/nginx-dm 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment nginx-dm nginx=harbor.tcc.com/public/nginx:v1.7.9 --record=true

[root@bjcy-200 deployment]# kubectl rollout undo deployment nginx-dm 
deployment.apps/nginx-dm rolled back
[root@bjcy-200 deployment]# kubectl get deployments.apps -o wide 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                        SELECTOR
nginx-dm   4/5     5            4           73m   nginx        harbor.tcc.com/public/nginx   app=nginx-dm

[root@bjcy-200 deployment]# kubectl rollout undo  deployment nginx-dm  --to-revision 2
deployment.apps/nginx-dm rolled back
[root@bjcy-200 deployment]# kubectl get deployments.apps -o wide 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                               SELECTOR
nginx-dm   5/5     5            5           75m   nginx        harbor.tcc.com/public/nginx:v1.7.9   app=nginx-dm

 滚动升级的相关参数

    maxSurge 值越大,初始创建的新副本数量就越多;maxUnavailable 值越大,初始销毁的旧副本数量就越多。

  strategy:
    rollingUpdate:
      maxSurge: 25%              # 升级过程中一次升级几个
      maxUnavailable: 25%      # 升级过程中,有几个不可用,一次性删除几个pod

HPA

    HPA(Horizontal Pod Autoscaler)是kubernetes(以下简称k8s)的一种资源对象,能够根据某些指标对在statefulSet、replicaController、replicaSet等集合中的pod数量进行动态伸缩,使运行在上面的服务对指标的变化有一定的自适应能力。

    HAP由一个controller控制,controller会间隔循环HPA,检查每个HPA中监控的指标是否触发伸缩条件,默认的间隔时间为15s。一旦触发伸缩条件,controller会向k8s发送请求,修改伸缩对象(statefulSet、replicaController、replicaSet)子对象scale中控制pod数量的字段。k8s响应请求,修改scale结构体,然后会刷新一次伸缩对象的pod数量。伸缩对象被修改后,自然会通过list/watch机制增加或减少pod数量,达到动态伸缩的目的。

配置前

]# kubectl edit deployments.apps nginx-dm
    spec:
      containers:
      - image: harbor.tcc.com/public/nginx:v1.7.9
        imagePullPolicy: Always
        name: nginx
        resources:
          requests:
             cpu: 400m

启用HPA监控功能

~]# vim /etc/kubernetes/manifests/kube-controller-manager.yaml 
spec:
  containers:
  - command:
    - kube-controller-manager
    - --horizontal-pod-autoscaler-use-rest-clients=true
    - --horizontal-pod-autoscaler-downscale-delay=5m0s                                                                                                                                                                                      
    - --horizontal-pod-autoscaler-upscale-delay=1m0s
    - --horizontal-pod-autoscaler-sync-period=20s    

配置项说明:

  • horizontal-pod-autoscaler-use-rest-clients: 开启基于rest-clients的自动伸缩
  • horizontal-pod-autoscaler-sync-period:自动伸缩的检测周期为20s,默认为30s
  • horizontal-pod-autoscaler-upscale-delay:当检测到满足扩容条件时,延迟多久开始缩容,即该满足的条件持续多久开始扩容,默认为3分钟
  • horizontal-pod-autoscaler-downscale-delay:当检测到满足缩容条件时,延迟多久开始缩容,即该满足条件持续多久开始缩容,默认为5分钟

设置autoscale

]# kubectl autoscale deployment nginx-dm --min=2 --max=10

设置阈值

[root@bjcy-200 deployment]# kubectl delete  hpa nginx-dm
horizontalpodautoscaler.autoscaling "nginx-dm" deleted
[root@bjcy-200 deployment]# kubectl autoscale deployment nginx-dm   --min=2 --max=10 --cpu-percent=60
horizontalpodautoscaler.autoscaling/nginx-dm autoscaled
[root@bjcy-200 deployment]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-dm Deployment/nginx-dm 0%/60% 2 10 5 8m55s

  

  

 

上一篇:快速了解Kubernetes


下一篇:k8s_deployment 以及灰度发布、滚动发布和蓝绿发布