资源定义haproxy
重启策略:
Always:当容器终止退出后,总是重启容器,默认策略。
OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。
Never:当容器终止退出,从不重启容器。
健康检查类型:
livenessProbe (存活检查)∶如果检查失败,将杀死容器,根据Pod的restartPolicy来操作。
readinessProbe (就绪检查)︰如果检查失败,Kubernetes会把Podservice endpoints中剔除。
#端口探测
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:顾名思义,用于初始化工作,执行完就结束,可以理解为一次性任务
- 支持大部分应用容器配置,但不支持健康检查
- 优先应用容器执行
应用场景:
环境检查:例如确保应用容器依赖的服务启动后再启动应用容器。初始化配置:例如给应用容器准备配置文件
haproxy
[root@master haproxy]# cat haproxy.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
initContainers:
- name: cp
volumeMounts:
- name: haproxy-cfg
mountPath: /tmp/
restartPolicy: Always #健康检查出问题就重启容器
containers:
- image: best2001/haproxy:v3.0
imagePullPolicy: Always
env:
- name: RSs
value: "10.97.0.10 10.97.0.50"
name: haproxy
ports:
- containerPort: 80
hostPort: 80
livenessProbe: #检查80端口是否存在
tcpSocket:
port: 80
volumes:
- name: haproxy-cfg
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: haproxy
type: NodePort
nginx
[root@master haproxy]# cat nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
labels:
app: nginx1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
initContainers:
- name: in
command:
- "wget"
- "-O"
- "/usr/local/nginx/html"
- "http://www.baidu.com"
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: "/usr/local/nginx/html"
name: document-root
containers:
- image: best2001/nginx:v0.3
imagePullPolicy: Always
name: nginx1
---
apiVersion: v1
kind: Service
metadata:
name: nginx1
labels:
app: nginx1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx1
clusterIP: 10.97.0.50
apache
[root@master haproxy]# cat apache1.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd1
labels:
app: httpd1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: httpd1
template:
metadata:
labels:
app: httpd1
spec:
containers:
- image: best2001/httpd
imagePullPolicy: Always
name: httpd1
---
apiVersion: v1
kind: Service
metadata:
name: httpd1
labels:
app: httpd1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd1
clusterIP: 10.97.0.10