key:
child-key1: value1
child-key2: value2
- value1
- value2
- value2
deployment
总共包含5个属性
apiVersion
:资源的版本号kind
:资源的类型metadata
:资源的元数据信息spec
:资源的规格说明和预期状态status
:资源的实际状态完整样例报文
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-11-04T09:00:37Z"
generation: 1
labels:
app: gbavqfbyfltzqfuu-serving
version: v1
name: gbavqfbyfltzqfuu-serving-v1
namespace: aipaas-modelserving
resourceVersion: "121103285"
selfLink: /apis/extensions/v1beta1/namespaces/aipaas-modelserving/deployments/gbavqfbyfltzqfuu-serving-v1
uid: 34db5072-1e7c-11eb-b71b-fa163efea19e
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: gbavqfbyfltzqfuu-serving
version: v1
strategy:
rollingUpdate:
maxSurge: 100%
maxUnavailable: 100%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: gbavqfbyfltzqfuu-serving
version: v1
spec:
containers:
- env:
- name: VECLIB_MAXIMUM_THREADS
value: "1"
- name: MKL_NUM_THREADS
value: "1"
- name: NUMEXPR_NUM_THREADS
value: "1"
- name: NVIDIA_VISIBLE_DEVICES
value: none
- name: OPENBLAS_NUM_THREADS
value: "1"
- name: OMP_NUM_THREADS
value: "1"
image: 172.16.1.222:10004/library/pmml-serving:v1.2
imagePullPolicy: IfNotPresent
name: gbavqfbyfltzqfuu-serving-v1
ports:
- containerPort: 8888
name: aipaas
protocol: TCP
readinessProbe:
failureThreshold: 2
httpGet:
httpHeaders:
- name: Authorization
value: Bearer eyJhbG......
path: /openscoring/model/serving
port: 8888
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 3
resources:
limits:
cpu: "1"
memory: "2147483648"
requests:
cpu: 250m
memory: "536870912"
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
nodeSelector:
node: worker
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: "2020-11-04T09:00:37Z"
lastUpdateTime: "2020-11-04T09:00:37Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2020-11-04T09:00:37Z"
lastUpdateTime: "2020-11-04T09:01:36Z"
message: ReplicaSet "gbavqfbyfltzqfuu-serving-v1-7bd89cd5c9" has successfully
progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 1
readyReplicas: 1
replicas: 1
updatedReplicas: 1
metadata:
annotations: # 自定义注释列表
generation:
labels: # 标签,用于标识该资源
app:
version:
name: # deployment名称,同一个命名空间下该名称不可重复
namespace: # deploymetn所属命名空间
resourceVersion:
selfLink:
uid:
在线服务场景中,会同时存在多个版本的模型服务,每个版本的模型服务都会对应一个deployment
,所有版本的模型服务共用一个service
。此时,通过labels:app:
将deployment
与service
进行关联,又通过app+version
对同一service
下不同的deployment
进行区分。
spec.progressDeadlineSeconds
可选字段,表示deployment controller
等待多少秒才能确定(通过deployment status)deployment
进程卡住了,单位:秒
spec.replicas
可选字段,指定期望的pod数量,默认是1
spec.revisionHistoryLimit
可选字段,用来指定可以保留的旧的ReplicaSet数量,余下的将在后台被当作垃圾收集,用于历史版本回滚
spec.selector
可选字段,用来指定deployment
管理的pod
的范围
spec.strategy
用来指定新的pod
替换旧的pod
的策略,包括RollingUpdate
和Recreate
两种:
RollingUpdate
maxUnavailable
来指定在升级过程中不可用pod的最大数量;该值既可以是绝对值,也可以是百分比;通过百分比计算的绝对值向下取整maxSurge
来指定可以超过期望的pod数量的最大个数;该值既可以是绝对值,也可以是百分比;通过百分比计算的绝对值向上取整;spec.template:
metadata:
creationTimestamp: null
labels:
app:
version:
spec:
containers:
- env: # 容器内的环境变量列表,详情见6.1
image: # 容器所对应镜像的名称
imagePullPolicy: # 容器拉取镜像的策略,详情见6.2
name: # 容器的名称
ports: # 容器内部需要暴露的端口号列表
- containerPort: 8888 # 端口号
name: aipaas # 端口号名称
protocol: TCP # 端口协议,支持TCP和UDP,默认TCP
readinessProbe: # 健康检测,详见6.3
resources: # 资源配置,详见6.4
terminationMessagePath: /dev/termination-log #日志保存路径
terminationMessagePolicy: File
dnsPolicy: ClusterFirst # DNS策略
nodeSelector: # Pod调度策略,详见6.5
restartPolicy: # 容器重启策略,详见6.6
schedulerName: # 调度器,详见6.7
terminationGracePeriodSeconds: # 容器删除策略,详见6.8
securityContext: {}
6.1 环境变量
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"
VECLIB_MAXIMUM_THREADS、MKL_NUM_THREADS、NUMEXPR_NUM_THREADS、OPENBLAS_NUM_THREADS、OMP_NUM_THREADS 这五个环境变量是用于控制多线程的线程个数的,其值等于pod的cpu个数。
NVIDIA_VISIBLE_DEVICES为gpu配置,当pod中不含有gpu时,添加该环境变量并将其设置为none。
ConCurrencyFlag和SERVER_PROCESS_NUM为MPS相关的环境变量,详情见6.9
6.2 容器拉取镜像的策略
Always:每次都会从镜像仓库拉取镜像
Never:仅使用本地镜像
IfNotPresent:优先使用本地镜像,若本地镜像不存在,则会拉取仓库镜像
6.3 健康检测
livenessProbe:当健康检测不通过时会直接重启容器
readinessProbe:当健康检测不通过时会停止向容器发送流量
spec.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秒
6.4 资源信息
spec.resources:
limits: # 设置资源上限值
cpu: "1" # cpu,单位为core
memory: "2147483648" # 内存,单位为Mib/Gib,若不添加单位,则默认为byte
nvidia.com/gpu: "1" # gpu
requests: # 设置资源必需值
cpu: 250m # cpu,若不足一个,则需要添加m
memory: "536870912" # 内存
nvidia.com/gpu: "1" # gpu
6.5 pod调度策略
spec.nodeSelector:
node: worker # pod会调度到有worker标签的node上
6.6 重启策略
Always :不管pod以何种方式终止运行都会将其重启
Never:不管pod以何种方式终止运行都不会将其重启
OnFailure:只有pod以非0退出码退出才会重启
pec.restartPolicy: Always
6.7 调度器
经过预选筛选和优选打分之后,K8S会选择分数最高的node来运行pod,如果最终有多个 node 的分数最高,那么 Scheduler 将从当中随机选择一个 node 来运行pod
spec.schedulerName: default-scheduler
6.8 优雅删除
spec.terminationGracePeriodSeconds: 30
pod的升级(删除)过程:
表示K8S对象在当前集群中实际的状态,往往通过资源的Controller控制
status:
conditions:
- lastTransitionTime: "2020-10-27T01:06:52Z"
lastUpdateTime: "2020-10-27T01:06:52Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2020-10-27T01:06:52Z"
lastUpdateTime: "2020-10-27T01:07:52Z"
message: ReplicaSet "uvzobilkwkmsfqca-serving-v1-d9c5f7bdf" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
availableReplicas: 1
observedGeneration: 1 # 观察到的实例
readyReplicas: 1 # 准备好的实例
replicas: 1 # 实例总数
updatedReplicas: 1 # 已更新的实例
使用gpu的在线服务可以通过开启MPS来提升gpu的使用率
# spec.containers.env
- name: ConCurrencyFlag
value: "True"
- name: SERVER_PROCESS_NUM
value: "1"
lifecycle:
preStop:
exec:
command:
- /bin/bash
- /model_serving/model_server/stop_mps.sh
securityContext:
capabilities:
add:
- SYS_ADMIN
procMount: Default