赞
踩
使用yaml创建Deployment,k8s deployment资源创建流程具体如下:
deployment总共包含5个属性
完整样例报文
kind: Deployment # 指定创建资源的角色/类型 apiVersion: apps/v1 # 指定api版本,此值必须在kubectl api-versions中 metadata: # 资源的元数据/属性 annotations: # 自定义注释列表 deployment.kubernetes.io/revision: '5' resourceVersion: '222060129' name: alert-webui # 资源的名字,在同一个namespace中必须唯一 uid: 7132d0b2-7519-4c3c-8ad6-7b4d06b758af creationTimestamp: '2022-10-21T08:23:10Z' generation: 7 managedFields: - manager: kubectl-create operation: Update apiVersion: apps/v1 time: '2022-10-21T08:23:10Z' fieldsType: FieldsV1 fieldsV1: 'f:metadata': 'f:labels': .: {} 'f:app': {} 'f:application': {} 'f:createdBy': {} 'f:spec': 'f:progressDeadlineSeconds': {} 'f:revisionHistoryLimit': {} 'f:selector': {} 'f:strategy': 'f:rollingUpdate': .: {} 'f:maxSurge': {} 'f:maxUnavailable': {} 'f:type': {} 'f:template': 'f:metadata': 'f:labels': .: {} 'f:app': {} 'f:spec': 'f:volumes': .: {} 'k:{"name":"volume-gzsy1"}': .: {} 'f:configMap': .: {} 'f:defaultMode': {} 'f:items': {} 'f:name': {} 'f:name': {} 'f:containers': 'k:{"name":"alert-webui"}': .: {} 'f:imagePullPolicy': {} 'f:name': {} 'f:resources': .: {} 'f:limits': .: {} 'f:cpu': {} 'f:memory': {} 'f:requests': .: {} 'f:cpu': {} 'f:memory': {} 'f:securityContext': .: {} 'f:privileged': {} 'f:terminationMessagePath': {} 'f:terminationMessagePolicy': {} 'f:volumeMounts': .: {} 'k:{"mountPath":"/etc/nginx/conf.d/"}': .: {} 'f:mountPath': {} 'f:name': {} 'f:dnsPolicy': {} 'f:serviceAccount': {} 'f:restartPolicy': {} 'f:schedulerName': {} 'f:terminationGracePeriodSeconds': {} 'f:imagePullSecrets': .: {} 'k:{"name":"amcrobot"}': {} 'f:serviceAccountName': {} 'f:securityContext': {} - manager: Mozilla operation: Update apiVersion: apps/v1 time: '2023-01-07T01:57:54Z' fieldsType: FieldsV1 fieldsV1: 'f:spec': 'f:template': 'f:spec': 'f:containers': 'k:{"name":"alert-webui"}': 'f:image': {} - manager: kube-controller-manager operation: Update apiVersion: apps/v1 time: '2023-04-12T08:01:00Z' fieldsType: FieldsV1 fieldsV1: 'f:metadata': 'f:annotations': .: {} 'f:deployment.kubernetes.io/revision': {} 'f:status': 'f:availableReplicas': {} 'f:conditions': .: {} 'k:{"type":"Available"}': .: {} 'f:lastTransitionTime': {} 'f:lastUpdateTime': {} 'f:message': {} 'f:reason': {} 'f:status': {} 'f:type': {} 'k:{"type":"Progressing"}': .: {} 'f:lastTransitionTime': {} 'f:lastUpdateTime': {} 'f:message': {} 'f:reason': {} 'f:status': {} 'f:type': {} 'f:observedGeneration': {} 'f:readyReplicas': {} 'f:replicas': {} 'f:updatedReplicas': {} subresource: status namespace: amc # 部署在哪个namespace中 labels: # 设定资源的标签,用于标识该资源 app: new-amc application: alert-webui createdBy: xxx spec: # 资源的规格说明和预期状态 replicas: 1 # 声明副本数目,指定期望的pod数量,默认是1 selector: # 选择器 matchLabels: # 匹配标签 app: alert-webui template: # 必填字段,设置deployment控制的pod的样式 metadata: creationTimestamp: null labels: app: alert-webui spec: restartPolicy: Always # 容器重启策略,Always: 不管pod以何种方式终止运行都会将其重启;Never: 不管pod以何种方式终止运行都不会将其重启 serviceAccountName: privilege-user readinessProbe: # 健康检测,具体参考2.6.3 httpGet: httpHeaders: - name: Authorization value: Bearer xxxxxxx # token path: /health # 请求路径 port: 8888 # 请求端口 scheme: HTTP # 请求协议 initialDelaySeconds: 30 # 容器启动完成后多长时间进行首次健康检测,单位为秒 periodSeconds: 30 # 健康监测时间周期,单位为秒,默认10秒一次 successThreshold: 1 # 从检测错误到成功需要几次才认为健康检测成功,默认为1次 failureThreshold: 2 # 检测失败几次后就认为健康检测失败,默认为3次 timeoutSeconds: 3 # 健康检测响应超时时间,单位为秒,默认为1秒 imagePullSecrets: - name: amcrobot schedulerName: default-scheduler terminationGracePeriodSeconds: 30 securityContext: {} containers: - name: alert-webui image: 'xxx' resources: limits: cpu: '2' memory: 512Mi requests: cpu: 100m memory: 100Mi volumeMounts: - name: volume-gzsy1 mountPath: /etc/nginx/conf.d/ terminationMessagePath: /dev/termination-log terminationMessagePolicy: File imagePullPolicy: Always securityContext: privileged: true serviceAccount: privilege-user volumes: - name: volume-gzsy1 configMap: name: alert-webui items: - key: default.conf path: default.conf defaultMode: 420 dnsPolicy: ClusterFirst strategy: # 用来指定新的pod替换旧的pod的策略,包括RollingUpdate和Recreate两种 type: RollingUpdate # RollingUpdate: 使用滚动的方式更新pod;Recreate: 在创建出新的pod之前会先杀掉所有已存在的pod rollingUpdate: maxUnavailable: 25% maxSurge: 25% revisionHistoryLimit: 10 # 保留历史版本 progressDeadlineSeconds: 600 status: # 资源的实际状态 observedGeneration: 7 replicas: 1 updatedReplicas: 1 readyReplicas: 1 availableReplicas: 1 conditions: - type: Available status: 'True' lastUpdateTime: '2023-03-15T16:11:41Z' lastTransitionTime: '2023-03-15T16:11:41Z' reason: MinimumReplicasAvailable message: Deployment has minimum availability. - type: Progressing status: 'True' lastUpdateTime: '2023-04-12T08:01:00Z' lastTransitionTime: '2022-10-21T08:23:10Z' reason: NewReplicaSetAvailable message: ReplicaSet "alert-webui-644c99fd98" has successfully progressed.
metadata样例如下:
metadata: # 资源的元数据/属性 annotations: # 自定义注释列表 deployment.kubernetes.io/revision: '5' resourceVersion: '222060129' name: alert-webui # 资源的名字,在同一个namespace中必须唯一 uid: 7132d0b2-7519-4c3c-8ad6-7b4d06b758af creationTimestamp: '2022-10-21T08:23:10Z' generation: 7 managedFields: - manager: kubectl-create operation: Update apiVersion: apps/v1 time: '2022-10-21T08:23:10Z' fieldsType: FieldsV1 fieldsV1: 'f:metadata': 'f:labels': .: {} 'f:app': {} 'f:application': {} 'f:createdBy': {} 'f:spec': 'f:progressDeadlineSeconds': {} 'f:revisionHistoryLimit': {} 'f:selector': {} 'f:strategy': 'f:rollingUpdate': .: {} 'f:maxSurge': {} 'f:maxUnavailable': {} 'f:type': {} 'f:template': 'f:metadata': 'f:labels': .: {} 'f:app': {} 'f:spec': 'f:volumes': .: {} 'k:{"name":"volume-gzsy1"}': .: {} 'f:configMap': .: {} 'f:defaultMode': {} 'f:items': {} 'f:name': {} 'f:name': {} 'f:containers': 'k:{"name":"alert-webui"}': .: {} 'f:imagePullPolicy': {} 'f:name': {} 'f:resources': .: {} 'f:limits': .: {} 'f:cpu': {} 'f:memory': {} 'f:requests': .: {} 'f:cpu': {} 'f:memory': {} 'f:securityContext': .: {} 'f:privileged': {} 'f:terminationMessagePath': {} 'f:terminationMessagePolicy': {} 'f:volumeMounts': .: {} 'k:{"mountPath":"/etc/nginx/conf.d/"}': .: {} 'f:mountPath': {} 'f:name': {} 'f:dnsPolicy': {} 'f:serviceAccount': {} 'f:restartPolicy': {} 'f:schedulerName': {} 'f:terminationGracePeriodSeconds': {} 'f:imagePullSecrets': .: {} 'k:{"name":"amcrobot"}': {} 'f:serviceAccountName': {} 'f:securityContext': {} - manager: Mozilla operation: Update apiVersion: apps/v1 time: '2023-01-07T01:57:54Z' fieldsType: FieldsV1 fieldsV1: 'f:spec': 'f:template': 'f:spec': 'f:containers': 'k:{"name":"alert-webui"}': 'f:image': {} - manager: kube-controller-manager operation: Update apiVersion: apps/v1 time: '2023-04-12T08:01:00Z' fieldsType: FieldsV1 fieldsV1: 'f:metadata': 'f:annotations': .: {} 'f:deployment.kubernetes.io/revision': {} 'f:status': 'f:availableReplicas': {} 'f:conditions': .: {} 'k:{"type":"Available"}': .: {} 'f:lastTransitionTime': {} 'f:lastUpdateTime': {} 'f:message': {} 'f:reason': {} 'f:status': {} 'f:type': {} 'k:{"type":"Progressing"}': .: {} 'f:lastTransitionTime': {} 'f:lastUpdateTime': {} 'f:message': {} 'f:reason': {} 'f:status': {} 'f:type': {} 'f:observedGeneration': {} 'f:readyReplicas': {} 'f:replicas': {} 'f:updatedReplicas': {} subresource: status namespace: amc # 部署在哪个namespace中 labels: # 设定资源的标签,用于标识该资源 app: new-amc application: alert-webui createdBy: xxx
在线服务场景中,会同时存在多个版本的模型服务,每个版本的模型服务都会对应一个deployment
,所有版本的模型服务共用一个service
。此时,通过labels:app:
将deployment
与service
进行关联,又通过app+version
对同一service
下不同的deployment
进行区分。
spec样例如下:
spec: # 资源的规格说明和预期状态 replicas: 1 # 声明副本数目,指定期望的pod数量,默认是1 selector: # 选择器 matchLabels: # 匹配标签 app: alert-webui template: # 必填字段,设置deployment控制的pod的样式 metadata: creationTimestamp: null labels: app: alert-webui spec: restartPolicy: Always # 容器重启策略,Always: 不管pod以何种方式终止运行都会将其重启;Never: 不管pod以何种方式终止运行都不会将其重启 nodeSelector: # Pod调度策略,详见2.6.5 node: worker # pod会调度到有worker标签的node上 serviceAccountName: privilege-user imagePullSecrets: - name: amcrobot schedulerName: default-scheduler terminationGracePeriodSeconds: 30 # 容器删除策略,具体请参考2.6.7 securityContext: {} containers: - name: alert-webui image: 'xxx' resources: limits: cpu: '2' memory: 512Mi requests: cpu: 100m memory: 100Mi volumeMounts: - name: volume-gzsy1 mountPath: /etc/nginx/conf.d/ terminationMessagePath: /dev/termination-log terminationMessagePolicy: File imagePullPolicy: Always securityContext: privileged: true serviceAccount: privilege-user volumes: - name: volume-gzsy1 configMap: name: alert-webui items: - key: default.conf path: default.conf defaultMode: 420 dnsPolicy: ClusterFirst strategy: # 用来指定新的pod替换旧的pod的策略,包括RollingUpdate和Recreate两种 type: RollingUpdate # RollingUpdate: 使用滚动的方式更新pod,具体参考2.5 rollingUpdate: maxUnavailable: 25% # 具体参考2.5 maxSurge: 25% # 具体参考2.5 revisionHistoryLimit: 10 # 保留历史版本,具体查看2.3 progressDeadlineSeconds: 600 # 查看2.1
可选字段,表示deployment controller
等待多少秒才能确定(通过deployment status)deployment
进程卡住了,单位:秒
可选字段,指定期望的pod数量,默认是1
可选字段,用来指定可以保留的旧的ReplicaSet数量,余下的将在后台被当作垃圾收集,用于历史版本回滚
可选字段,用来指定deployment
管理的pod
的范围
用来指定新的pod
替换旧的pod
的策略,包括RollingUpdate
和Recreate
两种:
maxUnavailable
来指定在升级过程中不可用pod的最大数量;该值既可以是绝对值,也可以是百分比;通过百分比计算的绝对值向下取整maxSurge
来指定可以超过期望的pod数量的最大个数;该值既可以是绝对值,也可以是百分比;通过百分比计算的绝对值向上取整;在创建出新的pod之前会先杀掉所有已存在的pod
必填字段,设置deployment控制的pod的样式,它跟 pod有一模一样的schema,是嵌套的类型,并且不需要apiVersion和 kind字段。
template: # 必填字段,设置deployment控制的pod的样式 metadata: creationTimestamp: null labels: app: alert-webui spec: restartPolicy: Always # 容器重启策略,具体参考2.6.6章节 nodeSelector: # Pod调度策略,详见2.6.5 node: worker # pod会调度到有worker标签的node上 serviceAccountName: privilege-user readinessProbe: # 健康检测,具体参考2.6.3 httpGet: httpHeaders: - name: Authorization value: Bearer xxxxxxx # token path: /health # 请求路径 port: 8888 # 请求端口 scheme: HTTP # 请求协议 initialDelaySeconds: 30 # 容器启动完成后多长时间进行首次健康检测,单位为秒 periodSeconds: 30 # 健康监测时间周期,单位为秒,默认10秒一次 successThreshold: 1 # 从检测错误到成功需要几次才认为健康检测成功,默认为1次 failureThreshold: 2 # 检测失败几次后就认为健康检测失败,默认为3次 timeoutSeconds: 3 # 健康检测响应超时时间,单位为秒,默认为1秒 imagePullSecrets: - name: amcrobot schedulerName: default-scheduler terminationGracePeriodSeconds: 30 # 容器删除策略,具体请参考2.6.7 securityContext: {} containers: - name: alert-webui image: 'xxx' resources: limits: # 设置资源上限值 cpu: '2' # cpu,单位为core memory: 512Mi # 内存,单位为Mib/Gib,若不添加单位,则默认为byte requests: # 设置资源必需值 cpu: 100m # cpu,若不足一个,则需要添加m memory: 100Mi # 内存 volumeMounts: - name: volume-gzsy1 mountPath: /etc/nginx/conf.d/ terminationMessagePath: /dev/termination-log terminationMessagePolicy: File imagePullPolicy: Always securityContext: privileged: true serviceAccount: privilege-user volumes: - name: volume-gzsy1 configMap: # name: alert-webui items: - key: default.conf path: default.conf defaultMode: 420 dnsPolicy: ClusterFirst
spec.containers.env: - name: VECLIB_MAXIMUM_THREADS value: "1" - name: MKL_NUM_THREADS value: "1" - name: NUMEXPR_NUM_THREADS value: "1" - name: OPENBLAS_NUM_THREADS value: "1" - name: OMP_NUM_THREADS value: "1" - name: NVIDIA_VISIBLE_DEVICES value: none - name: ConCurrencyFlag value: "false" - name: SERVER_PROCESS_NUM value: "1"
readinessProbe: # 健康检测
httpGet:
httpHeaders:
- name: Authorization
value: Bearer xxxxxxx # token
path: /health # 请求路径
port: 8888 # 请求端口
scheme: HTTP # 请求协议
initialDelaySeconds: 30 # 容器启动完成后多长时间进行首次健康检测,单位为秒
periodSeconds: 30 # 健康监测时间周期,单位为秒,默认10秒一次
successThreshold: 1 # 从检测错误到成功需要几次才认为健康检测成功,默认为1次
failureThreshold: 2 # 检测失败几次后就认为健康检测失败,默认为3次
timeoutSeconds: 3 # 健康检测响应超时时间,单位为秒,默认为1秒
resources:
limits: # 设置资源上限值
cpu: '2' # cpu,单位为core
memory: 512Mi # 内存,单位为Mib/Gib,若不添加单位,则默认为byte
requests: # 设置资源必需值
cpu: 100m # cpu,若不足一个,则需要添加m
memory: 100Mi # 内存
spec.nodeSelector:
node: worker # pod会调度到有worker标签的node上
terminationGracePeriodSeconds: 30
pod的升级(删除)过程:
status样例如下:
status: # 资源的实际状态 observedGeneration: 7 # 观察到的实例 replicas: 1 # 实例总数 updatedReplicas: 1 # 已更新的实例 readyReplicas: 1 # 准备好的实例 availableReplicas: 1 # 表示至少在一段时间内准备好多少个pod,这可以防止状态波动 conditions: - type: Available status: 'True' lastUpdateTime: '2023-03-15T16:11:41Z' lastTransitionTime: '2023-03-15T16:11:41Z' reason: MinimumReplicasAvailable message: Deployment has minimum availability. - type: Progressing status: 'True' lastUpdateTime: '2023-04-12T08:01:00Z' lastTransitionTime: '2022-10-21T08:23:10Z' reason: NewReplicaSetAvailable message: ReplicaSet "alert-webui-644c99fd98" has successfully progressed.
status:表示K8S对象在当前集群中实际的状态,往往通过资源的Controller控制
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。