在本指南中尝试了解 HAProxy 与 Kubernetes 的用途

Trying to understand the purpose of HAProxy with Kubernetes in this guide

有人可以浏览本指南并告诉我本指南中 HAProxy 的用例吗?

Install and configure a multi-master Kubernetes cluster with kubeadm

我已经阅读了指南并进行了设置。据我所知,我的 Kubernetes 集群和 HAProxy 之间一切正常。

HAProxy 已设置在与我的 Kubernetes 集群分开的虚拟机上。 HAProxy IP 是 10.1.160.170.

我希望访问我的 HAProxy IP 并被重定向到我正在负载平衡的 Kuberenetes 节点之一。事实并非如此。

我可以设置 Nginx 部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

然后创建服务:

kubectl expose deployment my-nginx --port=80 --type=NodePort

user@KUBENODE01:~$ kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        93d
my-nginx     NodePort    10.108.33.134   <none>        80:30438/TCP   46s

如果我现在尝试访问我的 HAProxy IP 10.1.160.170,我不会被重定向到端口 30438 上的我的 Kubernetes 节点。

user@computer:~/nginx_testing$ curl http://10.1.160.170
curl: (7) Failed to connect to 10.1.160.170 port 80: Connection refused

user@computer:~/nginx_testing$ curl https://10.1.160.170
curl: (7) Failed to connect to 10.1.160.170 port 443: Connection refused

user@computer:~/nginx_testing$ curl 10.1.160.170:30438
curl: (7) Failed to connect to 10.1.160.170 port 30438: Connection refused

本文中的HAProxy不是要将连接请求转发到实际的Kubernetes节点吗?

我也尝试过使用服务类型 LoadBalancer。

这是我的 haproxy.cfg:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:EC>
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend kubernetes
# bind 10.1.160.170:80
bind 10.1.160.170:6443
# http-request redirect scheme https unless { ssl_fc }
option tcplog
mode tcp
default_backend kubernetes-master-nodes


backend kubernetes-master-nodes
mode tcp
balance roundrobin
option tcp-check
server kubenode01 10.1.160.79:6443 check fall 3 rise 2
server kubenode02 10.1.160.80:6443 check fall 3 rise 2
server kubenode03 10.1.160.81:6443 check fall 3 rise 2

端口 6443 用于 k8s API 服务器。 kubectl 访问这个 API 服务器来完成它的工作。

在k8s场景下只有一个master,可以用那个master节点IP访问k8sAPI

但是在 k8s 场景中有 3 个 master 被认为是 HA 设置你应该使用负载平衡即使你仍然可以直接访问任何 master 因为这就是重点。

例如,在 HA 设置中,您应该在 kubeconfig 文件中将您的服务器地址设置为 HAProxy IP,这样您的 kubectl 命令将被 HAProxy 重定向到健康的主机之一