MetalLB — Kubernetes LoadBalancer服务

网友投稿 980 2022-10-25

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

MetalLB — Kubernetes LoadBalancer服务

Kubernetes对外提供了三种访问方式,即NodePort、LoadBalancer和Ingress。其中LoadBalancer类型是提供给受支持的IaaS平台使用的(如GCP,AWS等公有云),在支持的平台中创建Service指定LoadBalancer类型,就可以外部访问,如不支持,EXTERNAL-IP会一直在pending的状态。

关于MetalLB

在裸机集群中的LoadBalancer,MetalLB可以为其分配EXTERNAL-IP. MetalLB旨在通过提供与标准网络设备集成的网络LB实现来纠正这种不平衡,以便裸机集群上的外部服务也“尽可能”地工作。

MetalLB有两种模式,layer 2和BGP。layer 2模式与Keepalived有很多相似之处,单节点瓶颈和潜在的故障转移速度很慢,而且并不是真正意义上的负载均衡,实际上通过一个节点去访问。BGP模式中群集中的每个节点都与网络路由器建立BGP对等会话,但是与CNI插件集成时有兼容性问题,且kube-proxy版本需要1.13以上。如果kube-proxy使用ipvs,则需要1.14.2版本以上。

安装

设置kube-proxy参数

mode: "ipvs"ipvs:  strictARP: true

使用 yaml 部署:

kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/namespace.yamlkubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml

使用 helm 部署:

helm install --name metallb stable/metallb

查看MetalLB状态

[root@kube-master01 ~]# kubectl get node -o wideNAME            STATUS   ROLES    AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION   CONTAINER-RUNTIMEkube-master01   Ready    master   22h   v1.18.0   172.31.209.239           CentOS Linux 8 (Core)   5.4.28           cri-o://1.18.0-devkube-worker01   Ready    node     22h   v1.18.0   172.31.209.240           CentOS Linux 8 (Core)   5.4.28           cri-o://1.18.0-devkube-worker02   Ready    node     22h   v1.18.0   172.31.209.241           CentOS Linux 8 (Core)   5.4.28           cri-o://1.18.0-dev[root@kube-master01 ~]# kubectl get pod -n metallb-system NAME                          READY   STATUS    RESTARTS   AGEcontroller-57f648cb96-bs2xg   1/1     Running   0          6h12mspeaker-2cs2r                 1/1     Running   0          6h12mspeaker-8jh4f                 1/1     Running   0          6h12mspeaker-wlvkr                 1/1     Running   0          6h12m[root@kube-master01 ~]#

MetalLB配置

layer 2模式

[root@kube-master01 metallb]# cat metallb-config-layer2.yamlapiVersion: v1kind: ConfigMapmetadata:  namespace: metallb-system  name: configdata:  config: |    address-pools:    - name: default      protocol: layer2      addresses:      - 10.233.9.100-10.233.9.250[root@kube-master01 metallb]#

BGP模式

BGP模式需要4条信息

MetalLB应连接的路由器IP地址;

路由器的AS号;

MetalLB应该使用的AS编号;

IP地址范围,表示为CIDR前缀;

例如为MetalLB提供范围10.233.9.0/24和AS号64500,并将其连接到10.0.0.1的AS号为64501的路由器

[root@kube-master01 metallb]# cat metallb-config-bgp.yamlapiVersion: v1kind: ConfigMapmetadata:  namespace: metallb-system  name: configdata:  config: |    peers:    - peer-address: 10.0.0.1      peer-asn: 64501      my-asn: 64500    address-pools:    - name: default      protocol: bgp      addresses:      - 10.233.9.0/24      bgp-advertisements:      - aggregation-length: 32        localpref: 100        communities:        - no-advertise      - aggregation-length: 24    bgp-communities:      no-advertise: 65535:65282[root@kube-master01 metallb]#

测试

为NGINX创建LoadBalancer类型的Service

[root@kube-master01 metallb]# kubectl get pod,svc    NAME                         READY   STATUS    RESTARTS   AGEpod/busybox                  1/1     Running   0          17hpod/nginx-6df6d6b46f-tcxjg   1/1     Running   0          17hpod/nginx-6df6d6b46f-zzhlk   1/1     Running   0          17hNAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGEservice/kubernetes   ClusterIP   10.254.0.1              443/TCP   23hservice/nginx        ClusterIP   10.254.231.58           80/TCP    17h[root@kube-master01 metallb]# [root@kube-master01 metallb]# [root@kube-master01 metallb]# kubectl create -f test.yaml service/nginx-lb created[root@kube-master01 metallb]# cat test.yaml apiVersion: v1kind: Servicemetadata:  name: nginx-lbspec:  ports:  - name: http    port: 80    protocol: TCP    targetPort: 80  selector:    test-app: nginx  type: LoadBalancer[root@kube-master01 metallb]#

查看MetalLB分配的EXTERNAL-IP

[root@kube-master01 ~]# kubectl get svcNAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGEkubernetes   ClusterIP      10.254.0.1              443/TCP        23hnginx        ClusterIP      10.254.231.58           80/TCP         17hnginx-lb     LoadBalancer   10.254.88.168   10.233.9.0    80:47133/TCP   3m25s[root@kube-master01 ~]# curl 10.233.9.0Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

[root@kube-master01 ~]#

上一篇:攻守兼备 实力有型 妮维雅男士水活保湿系列整装待发,三步进阶实力抗燥
下一篇:国维集团史维学赋能城市建设,助力高质量经济发展
相关文章

 发表评论

暂时没有评论,来抢沙发吧~