「MetalLB」- 安装 | 升级

该笔记将记录:在 Kuberntes Cluster 中,我们部署 MetalLB 组件的过程,以及常见问题的解决办法;

参考 MetalLB, bare metal load-balancer for Kubernetes 文档,以获取官方详细说明;

该部分的笔记仅是我们阅读官方文档而产生的阅读摘录(MetalLB v0.12.1),并记录下安装过程需要完成的工作;

MetalLB 的 Speaker 使用 Host Network,所以 BGP Peer 使用 Worker 的网络地址(单物理网卡)来建立 BGP 连接;

准备工作

环境要求

A Kubernetes cluster, running Kubernetes 1.13.0 or later, that does not already have network load-balancing functionality.

A cluster network configuration(与各网络插件的兼容性)that can coexist with MetalLB.(我们使用 Calico/Cilium 插件,采用默认部署,所以问题不大)
https://metallb.universe.tf/installation/network-addons/
https://metallb.universe.tf/installation/clouds/

Some IPv4 addresses for MetalLB to hand out.(我们是内网开发环境使用,地址有很多,但是我们使用额外地址空间)

When using the BGP operating mode, you will need one or more routers capable of speaking BGP.
鉴于我们的环境需要使用额外地址空间,所以我们选择 BGP 模式,但需要配置 BGP Peer 设备,通常是路由器,某些交换机也支持 BGP 路由协议;

When using the L2 operating mode, traffic on port 7946 (TCP & UDP, other port can be configured) must be allowed between nodes, as required by memberlist.

云商兼容

多数是不兼容的,但是,目前 05/02/2025 我们暂时不在云环境中部署该组件,所以暂时并不需要关注这部分内容;

网络插件的兼容性

我们目前使用 Cilium 网络插件,MetalLB 对其支持较好,所以我们暂时不需要进行特殊配置;

针对 kube-proxy IPVS 配置

如果使用 kube-proxy IPVS 模式,需要开启 strict ARP 选项:

# kubectl edit configmap -n kube-system kube-proxy
...
    ipvs:
...
      strictARP: true
...

服务部署

MetalLB/Installation

通过 Helm Chart 安装

helm repo add metallb https://metallb.github.io/metallb
helm repo update metallb

helm pull metallb/metallb
helm show values ./metallb-0.14.9.tgz > metallb-0.14.9.tgz.helm-values.yaml
... nameOverride: "metallb"
... fullnameOverride: "metallb"
... controller.image:
... speaker.image:
... speaker.ignoreExcludeLB: true

helm upgrade --install --namespace metallb-system --create-namespace           \
     metallb ./metallb-0.14.9.tgz -f metallb-0.14.9.tgz.helm-values.yaml

针对 speaker.ignoreExcludeLB: true 参数:

配置使用

MetalLB, bare metal load-balancer for Kubernetes
MetalLB/Configuration

#1 定义地址池

cat > pool-lan.yaml <<EOF
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: pool-lan
  namespace: metallb-system
spec:
  autoAssign: true
  addresses:
  - 192.168.28.200-192.168.28.230
EOF

kubectl apply -f pool-lan.yaml

通过 BGP 通告

配置 MetalLB 资源,通过 BGP 通告:

cat > bgp-peer.yaml <<EOF
apiVersion: metallb.io/v1beta2
kind: BGPPeer
metadata:
  name: router-2001
  namespace: metallb-system
spec:
  myASN: 65007
  peerASN: 65000
  peerAddress: 192.168.100.1
EOF

kubectl apply -f bgp-peer.yaml

cat > bgp-advertisement.yaml <<EOF
apiVersion: metallb.io/v1beta1
kind: BGPAdvertisement
metadata:
  name: bgp-advertisement
  namespace: metallb-system
EOF

kubectl apply -f bgp-advertisement.yaml

配置路由器(我们使用华为交换机,具体配置取决于网络环境,如下仅供参考):

bgp 65000

group develop-130-worker external
peer develop-130-worker as-number 65130
peer develop-130-worker connect-interface Vlanif10

peer 192.168.10.134 group develop-130-worker
...
peer 192.168.10.139 group develop-130-worker

[bgp] display bgp group develop-130-worker
...
 Peer Members:
  Peer            V          AS  MsgRcvd  MsgSent  OutQ  Up/Down       State PrefRcv

  192.168.10.134  4       65130       13       20     0 00:05:32 Established       0
  192.168.10.135  4       65130       16       19     0 00:07:30 Established       0
  192.168.10.136  4       65130       16       19     0 00:07:05 Established       0
  192.168.10.137  4       65130       13       20     0 00:05:34 Established       0
  192.168.10.138  4       65130       13       20     0 00:05:32 Established       0
  192.168.10.139  4       65130       13       20     0 00:05:34 Established       0

#2 地址通告:或,通过 ARP 实现

通过 ARP 通告:

cat > arp-advertisement.yaml <<EOF
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: pool-lan
  namespace: metallb-system
spec:
  ipAddressPools:
  - pool-lan
EOF

kubectl apply -f arp-advertisement.yaml

第四步、服务测试

部署 LoadBalancer 类型的 Service 资源进行验证:

cat > metallb-testing.yaml <<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metallb-testing
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: metallb-testing
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 80
  selector:
    app: nginx
EOF

kubectl apply -f metallb-testing.yaml

补充说明:

  • 如果 Service 无正确的后端服务,则地址不会被通告;

版本升级

Helm | Helm Upgrade

旧版本的 MetalLB 使用使用 ConfigMap 来存储资源,从 v0.13.0 起,可以全部使用 CR 来定义;

官方提供了迁移工具,quay.io/metallb/configmaptocrs,来完成配置的转化;