资源定义haproxy
部署两个网站pod
//nginx
[root@master ~]# cat nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
//httpd
[root@master ~]# cat httpd.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: httpd
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: httpd
type: NodePort
//创建
[root@master ~]# kubectl apply -f nginx.yaml
deployment.apps/nginx created
service/nginx created
[root@master ~]# kubectl apply -f httpd.yml
deployment.apps/httpd created
service/httpd created
//查看
[root@master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/httpd-66dd47b7cd-9m6mb 0/1 ContainerCreating 0 10s
pod/nginx-7cf7d6dbc8-nfq6l 1/1 Running 0 65s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpd NodePort 10.99.197.72 <none> 80:31467/TCP 10s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 64m
service/nginx NodePort 10.96.217.207 <none> 80:32306/TCP 65s
haproxy
[root@master ~]# cat haproxy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
namespace: default
spec:
restartPolicy: OnFailure
containers:
- image: 1968046xux/haproxy:v1
imagePullPolicy: IfNotPresent
name: haproxy
env:
- name: RSs
value: "nginx httpd"
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 20
periodSeconds: 10
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 20
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: haproxy
type: NodePort
//创建
[root@master ~]# kubectl create -f haproxy.yaml
deployment.apps/haproxy created
service/haproxy created
//查看
[root@master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-594d954b8c-cq4v4 0/1 ContainerCreating 0 8s
pod/httpd-66dd47b7cd-9m6mb 1/1 Running 0 5m25s
pod/nginx-7cf7d6dbc8-nfq6l 1/1 Running 0 6m20s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.104.137.77 <none> 80:31685/TCP 7s
service/httpd NodePort 10.99.197.72 <none> 80:31467/TCP 5m25s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 69m
service/nginx NodePort 10.96.217.207 <none> 80:32306/TCP 6m20s
测试
[root@master ~]# curl 10.104.137.77
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]# curl 10.104.137.77
<html><body><h1>It works!</h1></body></html>