K8s资源定义haproxy
先提前做出两个运行httpd程序的pod,其中默认的index.html文件不一样
httpd1
[root@master opt]# vim httpd1/Dockerfile
FROM busybox
RUN mkdir /data && echo 'nihao' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master opt]# docker build -t yanghaixx/httpd1:latest httpd1
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
3cb635b06aa2: Pull complete
Digest: sha256:b5cfd4befc119a590ca1a81d6bb0fa1fb19f1fbebd0397f25fae164abe1e8a6a
Status: Downloaded newer image for busybox:latest
httpd2
[root@master opt]# vim httpd2/Dockerfile
FROM busybox
RUN mkdir /data && echo 'wuhu' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
[root@master opt]# docker build -t yanghaixx/httpd2:latest httpd2
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM busybox
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo 'wuhu' > /data/index.html
---> Running in aa279810c655
用这两个镜像做出两个pod并做出两个service
[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
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
deployment.apps/httpd1 created
service/httpd1 created
httpd2
[root@master mainfest]# vi 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
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
deployment.apps/httpd2 created
service/httpd2 created
查看pod和service
[root@master mainfest]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/httpd1-79cd6b6b9b-cdvcf 1/1 Running 0 7m
pod/httpd2-657ddc4ddc-vjdmf 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpd1 ClusterIP 10.107.234.122 <none> 80/TCP 9m24s
service/httpd2 ClusterIP 10.108.239.127 <none> 80/TCP 64m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d4h
编写写资源清单定义haproxy
[root@master mainfest]# vi haproxy.yaml
---
apiVersion: apps/v1
kind: deployment
metadata:
labels:
app: haproxy
name: haproxy
spec:
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadate:
labels:
app: haproxy
spec:
containers:
- image: yanghaixx/haproxy:v1-alpine
name: haproxy
env:
- name: RSIP
value: "10.107.234.122 10.108.239.127"
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
spec:
ports:
- port: 80
target: 80
selector:
app: haproxy
type: NodePort
测试
[root@master mainfest]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-7b5bdbcb89-efvfd 1/1 Running 0 8m31ss
pod/httpd1-79cd6b6b9b-cdvcf 1/1 Running 0 14m
pod/httpd2-657ddc4ddc-vjdmf 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.97.133.221 <none> 80:31540/TCP 7m48s
service/httpd1 ClusterIP 10.107.234.122 <none> 80/TCP 10m
service/httpd2 ClusterIP 10.108.239.127 <none> 80/TCP 65m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d4h
[root@master mainfest]# curl 192.168.197.131:31540
wuhu
[root@master mainfest]# curl 192.168.197.131:31540
nihao