k8s-部署haproxy负载均衡

nginx
通过资源定义文件的方式创建nginx

[root@master haproxy]# vim nginx.yml 
---
apiVersion: apps/v1
kind: Deployment      //无状态类型(不在本机保存数据,使用挂载或者共享存储方式保存数据)
metadata:
  name: nginx
  labels:
    app: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: wjm/nginx:v0.1 
        imagePullPolicy: Always
        name: nginx

---
apiVersion: v1
kind: Service     //创建一个service
metadata:
  name: nginx
  labels: 
    app: nginx
spec:
  ports:   //暴露端口
  - port: 80     //主机80端口
    targetPort: 80   //目标控制器的端口
  selector:
    app: nginx1
  clusterIP: 10.22.0.90



//创建nginx容器
[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.24.0.1    <none>        443/TCP   100m
service/nginx1       ClusterIP   10.22.0.90   <none>        80/TCP    20s

httpd

[root@master haproxy]# vim 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: wjm/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.22.0.30
  
//创建
[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.22.0.30   <none>        80/TCP    11s
service/kubernetes   ClusterIP   10.24.0.1    <none>        443/TCP   104m
service/nginx1       ClusterIP   10.22.0.90   <none>        80/TCP    60s

haproxy

[root@master haproxy]# vim 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: 93quan/haproxy:v1-alpine
        imagePullPolicy: Always
        env: 
        - name: RSIP
          value: "10.22.0.90 10.22.0.30"
        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          15s
pod/httpd1-57c7b6f7cb-sk86h    1/1     Running   0          31s
pod/nginx1-7cf8bc594f-t5btg    1/1     Running   0          45s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/haproxy      NodePort    10.24.45.68   <none>        80:31884/TCP   11s
service/httpd        ClusterIP   10.22.0.30     <none>        80/TCP         47s
service/kubernetes   ClusterIP   10.22.0.1      <none>        443/TCP        50m
service/nginx        ClusterIP   10.22.0.90     <none>        80/TCP         152s

访问测试

[root@master haproxy]# curl 192.168.220.9:31884
<html><body><h1>It works!</h1></body></html>

[root@master haproxy]# curl 192.168.220.9:31884
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
......
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐