试图在本指南中理解使用Kubernetes的HAProxy的目的



有人能浏览一下本指南并告诉我本指南中HAProxy的用例吗?

使用kubeadm 安装和配置多主机Kubernetes集群

我已经浏览了指南并设置了这个。据我所知,我的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中尝试过这种方法。

这是我的haprox.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服务器来完成它的工作。

在具有一个主节点的k8场景中,您可以使用该主节点IP访问k8 API。

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

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

最新更新