Kubernetes Pod的应用

基础的Pod

1
2
3
4
5
6
7
8
9
10
11
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent #镜像拉取策略:只有当镜像在本地不存在时才会拉取

具体镜像概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/containers/images/

多个容器组成Pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Pod
metadata:
name: nginx-tomcat
labels:
app: nginx
spec:
containers: # 在同一个containers内写多个容器
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
- name: tomcat
image: tomcat:latest
imagePullPolicy: IfNotPresent

Pod内的容器共享存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: v1
kind: Pod
metadata:
name: nginx-tomcat
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
volumeMounts: # nginx容器挂载卷
- name: data # 引用卷名为data
mountPath: /code/ # 挂载到Pod的/code目录

- name: tomcat
image: tomcat
imagePullPolicy: IfNotPresent
volumeMounts: #tomcat容器挂载卷
- name: data #引用卷名为data
mountPath: /code/ #挂载到Pod的/code目录
volumes: #定义存储卷
- name: data #卷名为data
emptyDir: {} #pod的空目录

存储相关概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/storage/storage-classes/

Pod跟宿主机共享目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: v1
kind: Pod
metadata:
name: nginx-tomcat
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: data
mountPath: /code/

- name: tomcat
image: tomcat
imagePullPolicy: IfNotPresent
volumeMounts:
- name: data
mountPath: /code/

volumes: #定义存储卷
- name: data #卷名
hostPath:
path: /code/ #宿主机共享的路径

Pod的初始化容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
initContainers: #在容器前整一个initContainers就是初始化容器
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
args: [/bin/sh, -c, 'echo k8s >> /usr/share/nginx/html/index.html']
# 执行命令,和command一样,但同时存在时,args作为变量存在
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

volumes:
- name: data
emptyDir: {}

init容器具体概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/init-containers/

配置 Pod 初始化 https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-pod-initialization/

Pod的资源限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Pod
metadata:
name: resource-demo
spec:
containers:
- name: resource-demo
images: nginx
ports:
- containerPort: 80
resources:
requests:
memory: 50Mi
cpu: 1500m
limits:
memory: 100Mi
cpu: 200m
1
2
3
4
5
# 参数解释:
requests :节点所需的最⼩计算资源量,k8s调度的时候的依据值
limits :限制允许的最⼤计算资源量,真正的资源限制参数
# 数值的转换:
1 CPU = 1000m,0.5 CPU = 500m,1 Mib = 1024 Kib,1 MB = 1000 KB

Pod配置存活、就绪和启动探针

具体概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

POD启动和停止钩子

PostStart(启动钩子):在容器启动创建后立刻执行,但是时间不能太长,否则容器将不会是running状态
PreStop(停止钩子):在容器停止被删除前执行,主要用于优雅的关闭应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
lifecycle: #生命周期
postStart: #启动钩子
exec: #在容器运行的意思
command: [/bin/sh, -c, 'echo k8s > /usr/share/nginx/html/index.html']
preStop: #停止钩子
exec:
command: [/bin/sh, -c, 'echo beybey > /code/stop.log']
volumeMounts:
- name: data
mountPath: /code/

volumes:
- name: data
hostPath:
path: /code/

Pod的存活性探针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html']
preStop:
exec:
command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log']
livenessProbe: # 存活性探针
exec: # 在容器里运行的意思
command: # 执行的命令(本质是echo $? 结果非0,默认3次判断为不存活,自动重启pod)
- /bin/bash
- -c
- cat /usr/share/nginx/html/index.html
initialDelaySeconds: 3 # 初始化延迟3秒(因为有些服务启动慢,适当提高时间)
periodSeconds: 3 # 每3次测试一次(默认允许失败3次,3次后重启pod)

volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

volumes:
- name: data
hostPath:
path: /code/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html']
preStop:
exec:
command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log']
livenessProbe: # 存活性探针
httpGet: # http探针
host: www.opsape.com # 多个域名选择指定域名检测
path: /index.html # 检测这个url,默认3次访问不成功就判断为不存活,自动重启pod
port: 80 # 端口为80
initialDelaySeconds: 3 # 初始化延迟3秒(因为有些服务启动慢,适当提高时间)
periodSeconds: 3 # 每3次测试一次(默认允许失败3次,3次后重启pod)

volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

volumes:
- name: data
hostPath:
path: /code/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html']
preStop:
exec:
command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log']
livenessProbe: # 存活性探针
tcpSocket: # 端口探针
port: 80 # 监听端口为80,默认3次不存在就判断为不存活,自动重启pod
initialDelaySeconds: 3 # 初始化延迟3秒(因为有些服务启动慢,适当提高时间)
periodSeconds: 3 # 每3次测试一次(默认允许失败3次,3次后重启pod)

volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

volumes:
- name: data
hostPath:
path: /code/

Pod的就绪性探针

就绪性探针和存活性探针一样有命令、http、端口三种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html']
preStop:
exec:
command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log']
livenessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe: # 就绪性探针
httpGet: # http探针
path: /index.html # 这个url访问成功,就说明准备就绪,k8s就会接入流量,提供给外界访问
port: 80
initialDelaySeconds: 30 # 初始化延迟30秒(因为有些服务启动慢,适当提高时间)
periodSeconds: 3 # 每3次测试一次(默认允许失败3次,3次后重启pod)

volumeMounts:
- name: data
mountPath: /usr/share/nginx/html/

volumes:
- name: data
hostPath:
path: /code/