Java教程

通过资源方式创建一个haproxy的pos进行负载均衡

本文主要是介绍通过资源方式创建一个haproxy的pos进行负载均衡,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通过资源方式创建一个haproxy的pos进行负载均衡

编写yaml文件

nginx

[root@master ~]# mkdir haproxy
[root@master ~]# cd haproxy/
[root@master haproxy]# 
[root@master haproxy]# cat nginx.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx1
  name: nginx1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx1
  template:
    metadata:
      labels:
        app: nginx1
    spec:
      containers:
      - image: 3199560936/nginx:v0.1
        name: nginx1        
---
apiVersion: v1
kind: Service
metadata:
  name: nginx1
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx1
[root@master haproxy]# 
创建成功
[root@master haproxy]# kubectl create -f nginx.yml 
deployment.apps/nginx1 created
service/nginx1 created
[root@master haproxy]# 
查看是否创建成功
[root@master haproxy]# kubectl get deploy,pod,svc 
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/httpd2   1/1     1            1           9m14s
deployment.apps/nginx1   1/1     1            1           79s

NAME                          READY   STATUS    RESTARTS   AGE
pod/httpd2-fd86fb676-mtcch    1/1     Running   0          9m14s
pod/nginx1-7dc8479b8f-vqlxz   1/1     Running   0          79s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/httpd2       ClusterIP   10.106.56.19    <none>        80/TCP    9m14s
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   2d12h
service/nginx1       ClusterIP   10.96.149.182   <none>        80/TCP    79s
[root@master haproxy]# 

httpd

编写yml文件

[root@master haproxy]# cat httpd.yml 
---
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: 3199560936/httpd:v0.4
        name: httpd2
---
apiVersion: v1
kind: Service
metadata:
  name: httpd2
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: httpd2
[root@master haproxy]# 

创建

[root@master haproxy]# kubectl create -f httpd.yml 
deployment.apps/httpd1 created
service/httpd1 created
[root@master haproxy]# 

查看

[root@master haproxy]# kubectl get deploy,pod,svc 
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/httpd2   1/1     1            1           9m14s
deployment.apps/nginx1   1/1     1            1           79s

NAME                          READY   STATUS    RESTARTS   AGE
pod/httpd2-fd86fb676-mtcch    1/1     Running   0          9m14s
pod/nginx1-7dc8479b8f-vqlxz   1/1     Running   0          79s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/httpd2       ClusterIP   10.106.56.19    <none>        80/TCP    9m14s
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   2d12h
service/nginx1       ClusterIP   10.96.149.182   <none>        80/TCP    79s
[root@master haproxy]# 

haproxy

编写yml文件

service/haproxy created
[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: 3199560936/haproxy:v0.2
        imagePullPolicy: Always
        env: 
        - name: RS
          value: "10.106.56.19 10.96.149.182"
        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]# 

创建

[root@master haproxy]# kubectl create -f haproxy.yml 
deployment.apps/haproxy created
service/haproxy created

查看

[root@master haproxy]# kubectl get deploy,pod,svc
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/haproxy   1/1     1            1           117s
deployment.apps/httpd2    1/1     1            1           44m
deployment.apps/nginx1    1/1     1            1           36m

NAME                           READY   STATUS    RESTARTS   AGE
pod/haproxy-74f8f5c6cf-cmvqz   1/1     Running   0          117s
pod/httpd2-fd86fb676-mtcch     1/1     Running   0          44m
pod/nginx1-7dc8479b8f-vqlxz    1/1     Running   0          36m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/haproxy      NodePort    10.101.9.205    <none>        80:30982/TCP   117s
service/httpd2       ClusterIP   10.106.56.19    <none>        80/TCP         44m
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        2d12h
service/nginx1       ClusterIP   10.96.149.182   <none>        80/TCP         36m
[root@master haproxy]# 

测试

[root@master haproxy]# curl 192.168.100.169:30982
<html><body><h1>It works!</h1></body></html>
[root@master haproxy]# curl 192.168.100.169:30982
<!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>

在这里插入图片描述
在这里插入图片描述

这篇关于通过资源方式创建一个haproxy的pos进行负载均衡的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!