#端口探测
apiVersion: v1
kind: Pod
metadata:
name: probe-demo
namespace: demo
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 8o
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30#启动容器后多少秒健康检查
periodSeconds: 10#以后间隔多少秒检查一次
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 10
示例:执行Shell命令
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
示例:HTTP请求
livenessProbe:httpGet:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: Custom-Header
value: Awesome
支持的检查方法:
httpGet:发送HTTP请求,返回200-400范围状态码为成功。
exec:执行Shell命令返回状态码是0为成功。
tcpSocket:发起TCP Socket建立成功。
初始化容器
InitContainer:顾名思义,用于初始化工作,执行完就结束,可以理解为一次性任务
- 支持大部分应用容器配置,但不支持健康检查
- 优先应用容器执行
应用场景:
环境检查:例如确保应用容器依赖的服务启动后再启动应用容器。初始化配置:例如给应用容器准备配置文件
//创建两个pod
[root@master ~]# vim Dockerfile
FROM busybox
RUN mkdir /data && echo '2 > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master ~]# docker build -t wssswsss/apache:v1.0
Successfully built 2d8ab5bd79f9
Successfully tagged wssswsss/apache:v1.0
[root@master ~]# vim Dockerfile
FROM busybox
RUN mkdir /data && echo 'hello,this is a 1' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master ~]# docker build -t wssswsss/apache:v2.0 .
Successfully built 7aeb28a25ca2
Successfully tagged wssswsss/apache:v2.0
//创建两个service
[root@master ~]# vim web.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: rs1
template:
metadata:
labels:
app: rs1
spec:
containers:
- image: wssswsss/apache:v1.0
imagePullPolicy: IfNotPresent
name: rs1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs2
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: rs2
template:
metadata:
labels:
app: rs2
spec:
containers:
- image: wssswsss/apache:v2.0
imagePullPolicy: IfNotPresent
name: rs2
---
apiVersion: v1
kind: Service
metadata:
name: rs1
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: rs1
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: rs2
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: rs2
type: NodePort
[root@master ~]# kubectl apply -f web.yaml
deployment.apps/rs1 created
deployment.apps/rs2 created
service/rs1 created
service/rs2 created
//编写资源清单定义haproxy'
[root@master ~]# vim haproxy.yaml
---
apiVersion: apps/v1
kind: Pod
metadata:
name: haproxy
namespace: default
labels:
app: haproxy
spec:
restartPolicy: OnFailure
initContainers:
- name: cfgfile
volumeMounts:
- name: haproxyconfigfile
mountPath: /tmp
containers:
- image: wssswsss/haproxy:latest
imagePullPolicy: IfNotPresent
name: haproxy
env:
- name: RSIP
value: "rs1 rs2 "
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: haproxy
type: NodePort
[root@master ~]# kubectl apply -f haproxy.yml
deployment.apps/haproxy created
service/haproxy created
//查看
[root@master haproxy]# kubectl get pod,svc
[root@master haproxy]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-7f745cff47-77kxh 1/1 Running 0 94s
pod/rs1-6cd64b79f6-fffxv 1/1 Running 0 4m5s
pod/rs02-55f6787ccf-swwks 1/1 Running 0 3m35s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
haproxy NodePort 10.103.16.159 <none> 80:30227/TCP 6s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d6h
nginx ClusterIP 10.105.50.111 <none> 8080/TCP 2d2h
rs1 ClusterIP 10.108.27.105 <none> 80/TCP 149m
rs2 ClusterIP 10.109.190.25 <none> 80/TCP 149m
[root@master ~]# curl 10.108.27.105
2
[root@master ~]# curl 10.109.190.25
1
[root@master ~]# curl 192.168.8.120:30277
2
[root@master ~]# curl 192.168.8.120:30277
1