【问题标题】:Trying to understand the purpose of HAProxy with Kubernetes in this guide在本指南中尝试了解 HAProxy 与 Kubernetes 的用途
【发布时间】:2021-07-04 05:43:41
【问题描述】:

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

Install and configure a multi-master Kubernetes cluster with kubeadm

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

HAProxy 已设置在与我的 Kubernetes 集群分开的 VM 上。 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

【问题讨论】:

    标签: kubernetes haproxy


    【解决方案1】:

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

    在只有一个master的k8s场景下,你可以通过那个master节点IP访问k8s API。

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

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

    【讨论】:

    • 所以我确实将我的 kubeconfig 的 CertSAN 设置为我的 HAProxy IP。我还设置了 controlPlaneEndpoint: "10.1.160.170:6443" 这是我的 HAProxy IP。那么 HAProxy 是用于 Kubectl 的负载均衡器吗?我想我可能会很好。
    • 是的,但不仅适用于 kubectl,任何使用 k8s API 服务器的东西都应该通过 HAProxy(负载均衡器)访问它
    • 这就解释了。我可以通过LoadBalancerIP:6443 访问我的节点。谢谢你的解释。
    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2016-05-05
    相关资源
    最近更新 更多