Pod和Container的资源请求和限制
• spec.containers[].resources.limits.cpu #运行条件
• spec.containers[].resources.limits.memory
• spec.containers[].resources.requests.cpu #运行最大限制
• spec.containers[].resources.requests.memory
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: db
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Kubernetes Pod 调度约束
可以将pod调度到指定的节点Node内
默认:根据节点资源利用率等分配Node节点。
nodeName用于将Pod调度到指定的Node名称上
nodeSelector用于将Pod调度到匹配Label的Node上
spec:
nodeName: 192.168.31.65
containers:
- name: nginx
image: nginx:1.15
首先给node设置标签
kubectl label nodes 192.168.122.135 node=a
spec:
nodeSelector:
node: a
containers:
- name: nginx
image: nginx:1.15
Pod的重启策略包括
Always、OnFailure和Never,默认值为Always。
Always:当容器失效时,由kubelet自动重启该容器。
OnFailure:当容器终止运行且退出码不为0时,由kubelet自动重启该容器。
Never:不论容器运行状态如何,kubelet都不会重启该容器。
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:1.12
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubernetes之pod健康检查
pod通过两类探针来检查容器的健康状态。分别是LivenessProbe(存活性探测)和ReadinessProbe(就绪型探测)
三种检查方式:exec,httpGet、tcpSocket
1、exec
spec:
containers:
- name: liveness-demo
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- test
- -e
- /tmp/healthy
initialDelaySeconds: 15
timeoutSeconds: 1
2、httpGet
通过容器的ip地址,端口号及路径调用HTTPGet方法,如果响应的状态码大于等于200且小于400,则认为容器健康
host :请求的主机地址,默认为Pod IP;也可以在httpHeaders中使用Host:来定义
port :请求的端口,必选字段
httpHeader <[]Object>:自定义的请求报文首部
path :请求的HTTP资源路径,即URL path
scheme:建立连接使用的协议,仅可为HTTP或HTTPS,默认为HTTP
livenessProbe:
httpGet:
path: /healthz
port: http
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 1
3、TcpSocket
过容器的IP地址和端口号进行TCP检查,如果能够建立TCP连接,则表明容器健康。
spec:
containers:
- name: liveness-tcp-demo
image: nginx:1.12-alpine
ports:
- name: http
containerPort: 80
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
timeoutSeconds: 1