通过资源定义方式创建一个haproxy的pod进行负载均衡
yaml文件
[root@k8s-master manifes]# cat haproxy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- image: xxkk/nginx:v5.0
imagePullPolicy: IfNotPresent
name: rs1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs2
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx2
template:
metadata:
labels:
app: nginx2
spec:
containers:
- image: xxkk/nginx:v6.0
imagePullPolicy: IfNotPresent
name: rs2
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
namespace: default
spec:
restartPolicy: OnFailure
initContainers:
- name: cfgfile
volumeMounts:
- name: haproxyconfigfile
mountPath: /tmp
containers:
- image: xxkk/haproxy:v1-alpine
imagePullPolicy: IfNotPresent
name: haproxy
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 10
env:
- name: RSIP
value: "rs1 rs2"
---
apiVersion: v1
kind: Service
metadata:
name: rs1
namespace: default
spec:
ports:
- port: 8888
protocol: TCP
targetPort: 80
selector:
app: nginx1
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: rs2
namespace: default
spec:
ports:
- port: 8889
protocol: TCP
targetPort: 80
selector:
app: nginx2
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: haproxy
type: NodePort
[root@k8s-master manifes]# kubectl apply -f haproxy.yaml
deployment.apps/rs1 unchanged
deployment.apps/rs2 unchanged
deployment.apps/haproxy created
service/rs1 unchanged
service/rs2 unchanged
service/haproxy unchanged
[root@k8s-master manifes]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-694cfb4785-tdpd9 0/1 Running 2 22s
pod/rs1-5bb7b4bdc-w4p9z 1/1 Running 0 84m
pod/rs2-545946b5b8-mjv2n 1/1 Running 0 74m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.107.151.102 <none> 80:31478/TCP 74m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d7h
service/rs1 NodePort 10.102.21.157 <none> 8888:30200/TCP 74m
service/rs2 NodePort 10.104.7.193 <none> 8889:31889/TCP 74m
测试