k8s-负载均衡
nginx #yml文件 [root@master haproxy]# cat nginx.yml apiVersion: apps/v1 kind: Deployment metadata: name: nginx1 labels: app: nginx1 namespace: default spec: replicas: 1 selector: matchLabels: app: nginx
·
nginx
#yml文件
[root@master haproxy]# cat nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
labels:
app: nginx1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- image: best2001/nginx:v0.3
imagePullPolicy: Always
name: nginx1
---
apiVersion: v1
kind: Service
metadata:
name: nginx1
labels:
app: nginx1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx1
clusterIP: 10.97.0.50
#创建
[root@master haproxy]# kubectl create -f nginx.yml
deployment.apps/nginx1 created
service/nginx1 created
#查看
[root@master haproxy]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx1-7cf8bc594f-t5btg 1/1 Running 0 26s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74m
service/nginx1 ClusterIP 10.97.0.50 <none> 80/TCP 26s
apache
#yml文件
[root@master haproxy]# cat apache1.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd1
labels:
app: httpd1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: httpd1
template:
metadata:
labels:
app: httpd1
spec:
containers:
- image: best2001/httpd
imagePullPolicy: Always
name: httpd1
---
apiVersion: v1
kind: Service
metadata:
name: httpd1
labels:
app: httpd1
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpd1
clusterIP: 10.97.0.10
#创建
[root@master haproxy]# kubectl create -f apache1.yml
deployment.apps/httpd1 created
service/httpd1 created
#查看
[root@master haproxy]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/httpd1-57c7b6f7cb-sk86h 1/1 Running 0 28s
pod/nginx1-7cf8bc594f-t5btg 1/1 Running 0 112s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpd1 ClusterIP 10.97.0.10 <none> 80/TCP 28s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 75m
service/nginx1 ClusterIP 10.97.0.50 <none> 80/TCP 112s
haproxy
#yml文件
[root@master haproxy]# cat haproxy.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
containers:
- image: best2001/haproxy:v3.0
imagePullPolicy: Always
env:
- name: RSs
value: "10.97.0.10 10.97.0.50"
name: haproxy
ports:
- containerPort: 80
hostPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: haproxy
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: haproxy
type: NodePort
#创建
[root@master haproxy]# kubectl create -f haproxy.yml
deployment.apps/haproxy created
service/haproxy created
#查看是否创建成功
[root@master haproxy]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/haproxy-7565dc6587-h8sdg 1/1 Running 0 18s
pod/httpd1-57c7b6f7cb-sk86h 1/1 Running 0 81s
pod/nginx1-7cf8bc594f-t5btg 1/1 Running 0 2m45s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/haproxy NodePort 10.99.52.161 <none> 80:31884/TCP 18s
service/httpd1 ClusterIP 10.97.0.10 <none> 80/TCP 81s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 76m
service/nginx1 ClusterIP 10.97.0.50 <none> 80/TCP 2m45s
访问
[root@master haproxy]# curl 192.168.240.30:31884
<html><body><h1>It works!</h1></body></html>
[root@master haproxy]# curl 192.168.240.30:31884
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
更多推荐
已为社区贡献5条内容
所有评论(0)