[root@master ~]# cat Dockerfile
FROM busybox
RUN mkdir /data && echo '1' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
制作1的镜像
[root@master ~]# docker build -t 1163582076/http:v0.1 .
Sending build context to Docker daemon 3.518MB
Step 1/3 : FROM busybox
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo '1' > /data/index.html
---> Running in f09723701162
Removing intermediate container f09723701162
---> 967111db275a
Step 3/3 : CMD ["/bin/httpd","-f","-h","/data"]
---> Running in feb60691a9ff
Removing intermediate container feb60691a9ff
---> 5e20562f260e
Successfully built 5e20562f260e
Successfully tagged 1163582076/http:v0.1
[root@master ~]# cat Dockerfile
FROM busybox
RUN mkdir /data && echo '2' > /data/index.html
CMD ["/bin/httpd","-f","-h","/data"]
制作2的镜像
[root@master ~]# docker build -t 1163582076/http:v0.2 .
Sending build context to Docker daemon 3.518MB
Step 1/3 : FROM busybox
---> ffe9d497c324
Step 2/3 : RUN mkdir /data && echo '2' > /data/index.html
---> Running in 7088ca8c2449
Removing intermediate container 7088ca8c2449
---> 2216b0611a91
Step 3/3 : CMD ["/bin/httpd","-f","-h","/data"]
---> Running in f00a12d73d85
Removing intermediate container f00a12d73d85
---> b3d3d9fc016f
Successfully built b3d3d9fc016f
Successfully tagged 1163582076/http:v0.2
用这两个镜像做出两个pod并做出两个service
[root@master ~]# cat 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: 1163582076/http:v0.1
imagePullPolicy: IfNotPresent
name: httpd1
---
apiVersion: v1
kind: Service
metadata:
name: httpd1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd1
[root@master ~]# kubectl create -f httpd1.yaml
deployment.apps/httpd1 created
service/httpd1 created
[root@master ~]# cat 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: 1163582076/http:v0.2
imagePullPolicy: IfNotPresent
name: httpd2
---
apiVersion: v1
kind: Service
metadata:
name: httpd2
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd2
[root@master ~]# kubectl create -f httpd2.yaml
deployment.apps/httpd2 created
service/httpd2 created
查看状态
[root@master mainfest]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/httpd1-67bd8d7975-csxm9 1/1 Running 0 4m27s
pod/httpd2-6799fc88d8-g29sq 1/1 Running 0 2m30s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpd1 ClusterIP 10.103.109.188 <none> 80/TCP 2m31s
service/httpd2 ClusterIP 10.101.12.169 <none> 80/TCP 11s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d10h
编写写资源清单定义haproxy
[root@master ~]# 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 pod,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-5bfbbd69cc-sdcj7 0/1 Terminating 0 7m48s
pod/httpd1-67bd8d7975-csxm9 1/1 Running 0 4m27s
pod/httpd2-6799fc88d8-g29sq 1/1 Running 0 2m30s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.97.133.221 <none> 80:31778/TCP 7m48s
service/httpd1 ClusterIP 10.103.109.188 <none> 80/TCP 2m31s
service/httpd2 ClusterIP 10.101.12.169 <none> 80/TCP 11s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d10h
K8S—通过资源定义方式创建一个haproxy的pod进行负载均衡
最新推荐文章于 2024-07-13 17:35:55 发布