apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx2
name: nginx2
spec:
containers:
- image: centos:7
name: nginx2
command: ["/bin/bash","-c","sleep 3600"]
readinessProbe:
exec:
command: ["/usr/bin/test","-e","/etc/passwd"]
initialDelaySeconds: 4
periodSeconds: 2
因为-c是查看字符串,但/usr/bin/test和/etc/passwd是文件,所以readinessProbe探针探测不到
改成-e即可
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx1
name: nginx1
spec:
containers:
- image: tomcat:8.0.52
name: nginx1
readinessProbe:
httpGet:
scheme: HTTP
port: 8080
path: /index.jsp
initialDelaySeconds: 4
periodSeconds: 2
readinessProbe
就绪探针,pod的状态是running ,ready是notready
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx1
name: nginx1
spec:
containers:
- image: tomcat:8.0.52
name: nginx1
startupProbe:
tcpSocket:
port: 8081
initialDelaySeconds: 4
periodSeconds: 2
端口不正确,所以启动探针探测出错误
改成8080
启动探针没有成功之前,后续的探针都不会执行
启动探针成功之后,在pod的生命周期内不会再检测启动探针
postpre:容器启动钩子。容器启动之后触发的条件
prestop:容器退出的钩子,容器推出之后触发的条件。
volumeMounts:
- name: test1
mountPath: /opt
readOnly: false
声明容器内部的挂载目录
要给这个挂载卷取名字,不同的挂载卷的名字不能重复
readOnly: false: 可读写
volumes:
- name: test1
hostPath:
path: /opt/test
type: DirectoryOrCreate
声明的是node节点上和容器内的/opt的挂载目录
挂载卷的名称和要挂载的容器内挂载卷名称要一一对应
hostPath:指定和容器的挂载目录
type: DirectoryOrCreate:如果节点上的目录不存在,自动创建该目录。
pod会经常被重启,销毁,一旦容器和node节点做了挂载卷,数据不会丢失
启动和退出的作用
1.启动可以自动逸配置容器内的环境变量
2.通知机制,告诉用户容器启动完毕
3.退出时,可以执行自定义命令,删除或者生成一些必要的程序,
apiVersion: v1
kind: Pod
metadata:
name: nginx3
spec:
containers:
- name: nginx3
image: centos:7
command: ["/bin/bash", "-c", "sleep 3600"]
volumeMounts:
- name: test1
mountPath: /opt
readOnly: false
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","echo hello from start >> /opt/123.txt ; sleep 10"]
preStop:
exec:
command: ["/bin/bash","-c","echo hello from stop >> /opt/123.txt"]
volumes:
- name: test1
hostPath:
path: /opt/test
type: DirectoryOrCreate
apiVersion: v1
kind: Pod
metadata:
name: nginx3
spec:
containers:
- name: nginx3
image: tomcat:8.0.52
volumeMounts:
- name: test1
mountPath: /opt
readOnly: false
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","echo hello from start >> /opt/123.txt ; sleep 10"]
preStop:
exec:
command: ["/bin/bash","-c","echo hello from stop >> /opt/123.txt"]
startupProbe:
exec:
command: ["/usr/bin/test","-e","/etc/passwd"]
initialDelaySeconds: 4
periodSeconds: 2
livenessProbe:
httpGet:
scheme: HTTP
port: 8080
path: /index.jsp
initialDelaySeconds: 4
periodSeconds: 2
readinessProbe:
httpGet:
scheme: HTTP
port: 8080
path: /index.jsp
initialDelaySeconds: 4
periodSeconds: 2
volumes:
- name: test1
hostPath:
path: /opt/test
type: DirectoryOrCreate
pod的重启策略:
在k8s当中都是重启pod
Always:默认策略--》当pod内的容器退出,不论是一个还是两个容器退出,整个pod都会重启
never:当pod内的容器退出时,退出一个还是退出N个,pod都不重启。
Onfailure:当pod内的容器退出时,状态码0,整个pod都不会重启,只有一个或者N个容器非正常退出,状态码非0,整个pod才会重启。
k8s就是集群化管理容器