使用资源定义方式创建haproxy的pod进行负载均衡
准备工作
先创建好两个httpd镜像,设置他们的 index.html文件不一样
[root@master ~]
FROM busybox
RUN mkdir /data && echo 'hello amu,this is v1 > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master ~]# docker build -t gaofan1225/httpd:v1 /opt
Successfully built 3ae78ac950
Successfully tagged gaofan1225/httpd:v1
#apache2
[root@master ~]# vim /opt/Dockerfile
FROM busybox
RUN mkdir /data && echo 'hello amu,this is v2 > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master ~]
Successfully built 31dcde996d51
Successfully tagged gaofan1225/httpd:v2
用上面两个httpd镜像创建rs1、rs2容器
// 创建目录
[root@master ~]
[root@master opt]
root@master opt]
[root@master manifest]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: amu1
template:
metadata:
labels:
app: amu1
spec:
containers:
- image: gaofan1225/httpd:v1
imagePullPolicy: IfNotPresent
name: rs1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rs2
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: amu2
template:
metadata:
labels:
app: amu2
spec:
containers:
- image: bgaofan1225/httpd:v2
imagePullPolicy: IfNotPresent
name: rs2
---
apiVersion: v1
kind: Service
metadata:
name: rs1
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: amu1
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: rs2
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: amu2
type: NodePort
[root@master manifest]
deployment.apps/rs1 created
deployment.apps/rs2 created
service/rs1 created
service/rs2 created
查看创建好的 rs1、rs2容器的 pod、svc类型
[root@master manifest]
NAME READY STATUS RESTARTS AGE
pod/rs1-6f8b6577bd-f8t6g 1/1 Running 0 48s
pod/rs2-5cff4c7479-kz5wb 1/1 Running 0 1m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4m
service/rs1 NodePort 10.105.78.131 <none> 80:31286/TCP 1m
service/rs2 NodePort 10.102.240.88 <none> 80:31286/TCP 1m
写资源清单 定义haproxy
[root@master manifest]
---
apiVersion: apps/v1
kind: Pod
metadata:
name: haproxy
namespace: default
labels:
app: test
spec:
restartPolicy: OnFailure // 健康检查出问题就重启容器
initContainers:
- name: cfgfile
volumeMounts:
- name: haproxyconfigfile
mountPath: /tmp
containers:
- image: gaofan1225/haproxy:latest
imagePullPolicy: IfNotPresent
name: haproxy
env:
- name: RSIP
value: "rs1 rs2"
livenessProbe: // 检查80端口是否存在
tcpSocket:
port: 80
initialDelaySeconds: 60
periodSeconds: 20
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 60
periodSeconds: 20
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: test
type: NodePort
[root@master manifest]
deployment.apps/haproxy created
service/haproxy created
[root@master manifest]
NAME READY STATUS RESTARTS AGE
pod/haproxy-56948856b7-hhcbp 1/1 Running 0 15m
pod/rs1-6f8b6577bd-f8t6g 1/1 Running 0 5m
pod/rs2-5cff4c7479-kz5wb 1/1 Running 0 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.96.97.77 <none> 80:336/TCP 15s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m
service/rs1 NodePort 10.105.78.131 <none> 80:31286/TCP 7m
service/rs2 NodePort 10.102.240.88 <none> 80:31286/TCP 7m
访问测试
// 单独访问
[root@master manifest]
hello amu,this is v1
[root@master manifest]
hello amu,this is v2
// 通过负载均衡访问
[root@master manifest]
hello amu,this is v1
[root@master manifest]
hello amu,this is v2