K8s资源定义haproxy模板
[root@master httpd1]# vim Dockerfile
FROM busybox
RUN mkdir /data && echo '1' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master httpd1]# docker build -t yanghaixx/httpd1:latest .
Successfully built 5d0da3dc9764
Successfully tagged yanghaixx/httpd1:latest
[root@master httpd2]# vim Dockerfile
FROM busybox
RUN mkdir /data && echo '2' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master httpd2]# docker build -t yanghaixx/httpd2:latest .
Successfully built c059bfaa849c
Successfully tagged yanghaixx/httpd2:latest
用这两个镜像做出两个pod并做出两个service
httpd1
[root@master mainfest]# vim httpd1.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: httpd1
name: httpd1
spec:
replicas: 1
selector:
matchLabels:
app: httpd1
template:
metadata:
labels:
app: httpd1
spec:
containers:
- image: yanghaixx/httpd1:latest
imagePullPolicy: IfNotPresent
name: httpd1
---
apiVersion: v1
kind: Service
metadata:
name: httpd1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd1
[root@master mainfest]# kubectl create -f httpd1.yaml
httpd2
[root@master mainfest]# vim httpd2.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: httpd2
name: httpd2
spec:
replicas: 1
selector:
matchLabels:
app: httpd2
template:
metadata:
labels:
app: httpd2
spec:
containers:
- image: yanghaixx/httpd2:latest
imagePullPolicy: IfNotPresent
name: httpd2
---
apiVersion: v1
kind: Service
metadata:
name: httpd2
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd2
[root@master mainfest]# kubectl create -f httpd2.yaml
编写写资源清单定义haproxy
[root@master mainfest]# vi haproxy.yaml
---
apiVersion: v1
kind: Pod
metadata:
labels:
app: haproxy
name: haproxy
spec:
restartPolicy: OnFailure
initContainers:
- name: cfgfile
volumeMounts:
- name: haproxyconfigfile
mountPath: /tmp
containers:
- image: 93quan/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: "httpd1 httpd2"
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
spec:
ports:
- port: 80
targetPort: 80
selector:
app: haproxy
type: NodePort
测试
[root@master mainfest]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.97.133.221 <none> 80:31421/TCP 6m32s
service/httpd1 ClusterIP 10.107.234.122 <none> 80/TCP 14m
service/httpd2 ClusterIP 10.108.239.127 <none> 80/TCP 54m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d6h
[root@master mainfest]# curl 192.168.197.131:31421
1
[root@master mainfest]# curl 192.168.197.131:31421
2