MetalLB 官网
官方安装文档

为什么要使用 MetalLB

  为什么Kubernetes不提供网络负载均衡器的实现(LoadBalancer类型的服务)用于裸机集群。
  因为Kubernetes附带的Network LB的实现都是调用各种IaaS平台(GCP,AWS,Azure等)的粘合代码。如果您不在支持的IaaS平台(GCP,AWS,Azure等)上运行,则LoadBalancers在创建时将无限期保持“待处理”状态。

  裸机集群运营商只剩下两个较小的工具,即“ NodePort”和“ externalIP”服务,可将用户流量引入其集群。这两个选项在生产用途上都有很大的不利之处,这使裸金属集群成为Kubernetes生态系统中的第二等公民。

  MetalLB旨在通过提供与标准网络设备集成的Network LB实现来纠正这种不平衡,从而使裸机群集上的外部服务也尽可能“正常运行”。


图片来自 kuboard
使用MetalLB来解决K8S service LoadBalancer问题插图




使用 Helm 安装

安装 Helm
添加Chart源
helm repo add metallb https://metallb.github.io/metallb
helm repo update

## 查看
[root@k8s-master ~]# helm search repo metallb -l
NAME            CHART VERSION   APP VERSION     DESCRIPTION
metallb/metallb 0.13.9          v0.13.9         A network load-balancer implementation for Kube...
metallb/metallb 0.13.7          v0.13.7         A network load-balancer implementation for Kube...
metallb/metallb 0.13.6          v0.13.6         A network load-balancer implementation for Kube...
metallb/metallb 0.13.5          v0.13.5         A network load-balancer implementation for Kube...
metallb/metallb 0.13.4          v0.13.4         A network load-balancer implementation for Kube...
metallb/metallb 0.13.3          v0.13.3         A network load-balancer implementation for Kube...
metallb/metallb 0.13.2          v0.13.2         A network load-balancer implementation for Kube...
metallb/metallb 0.12.1          v0.12.1         A network load-balancer implementation for Kube...
metallb/metallb 0.12.0          v0.12.0         A network load-balancer implementation for Kube...
metallb/metallb 0.11.0          v0.11.0         A network load-balancer implementation for Kube...
metallb/metallb 0.10.3          v0.10.3         A network load-balancer implementation for Kube...
metallb/metallb 0.10.2          v0.10.2         A network load-balancer implementation for Kube...
metallb/metallb 0.10.1          v0.10.1         A network load-balancer implementation for Kube...
metallb/metallb 0.10.0          v0.10.0         A network load-balancer implementation for Kube...


使用 Helm 3 安装 MetalLB
export METALLB_CHART_VERSION=0.13.6
## 将Chart包下载到本地
helm pull metallb/metallb --version $METALLB_CHART_VERSION

## 为 Kong MetalLB 创建命名空间
export METALLB_NAMESPACE=metallb-ns

values.yaml
# controller.
controller:
  enabled: true
  # -- Controller log level. Must be one of: `all`, `debug`, `info`, `warn`, `error` or `none`
  logLevel: info
  # command: /controller
  # webhookMode: enabled
  image:
    repository: quay.io/metallb/controller


# speaker contains configuration specific to the MetalLB speaker
# daemonset.
speaker:
  enabled: true
  # -- Speaker log level. Must be one of: `all`, `debug`, `info`, `warn`, `error` or `none`
  logLevel: info
  image:
    repository: quay.io/metallb/speaker

安装部署
## 安装部署
helm install metallb ./metallb-$METALLB_CHART_VERSION.tgz \
  -n $METALLB_NAMESPACE \
  --create-namespace \
  -f values.yaml


## 卸载
helm uninstall metallb -n $METALLB_NAMESPACE


查看 运行情况
[root@k8s-master ~]# kubectl -n metallb-ns get all
NAME                                      READY   STATUS    RESTARTS   AGE
pod/metallb-controller-78d64b88b5-btjqv   1/1     Running   0          3m31s
pod/metallb-speaker-c2mcj                 1/1     Running   0          3m31s
pod/metallb-speaker-d6zhl                 1/1     Running   0          3m31s
pod/metallb-speaker-qkncb                 1/1     Running   0          3m31s
pod/metallb-speaker-xn445                 1/1     Running   0          3m31s

NAME                              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/metallb-webhook-service   ClusterIP   10.96.190.24   <none>        443/TCP   3m31s

NAME                             DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
daemonset.apps/metallb-speaker   4         4         4       4            4           kubernetes.io/os=linux   3m31s

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/metallb-controller   1/1     1            1           3m31s

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/metallb-controller-78d64b88b5   1         1         1       3m31s


添加IP地址池

这里在新版本中做了修改,与旧版中的 ConfigMap不同
参考官网地址

[root@k8s-master ~]# cat > ip-address-pool.yaml << ERIC
---
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: metallb-pool
  namespace: metallb-ns

## 示例
#spec:
#  addresses:
#  - 192.168.10.0/24
#  - 192.168.9.1-192.168.9.5
#  - fc00:f853:0ccd:e799::/124

spec:
  addresses:
  - 192.168.0.50-192.168.0.50

---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: layer2
  namespace: metallb-ns

ERIC


kubectl apply -f ip-address-pool.yaml

查看结果
[root@dk8s-master meatllb]# kubectl get gatewayclass,gateway -A
NAME                                             CONTROLLER                             ACCEPTED   AGE
gatewayclass.gateway.networking.k8s.io/contour   projectcontour.io/gateway-controller   True       13m

NAMESPACE        NAME                                           CLASS       ADDRESS        PROGRAMMED   AGE
projectcontour   gateway.gateway.networking.k8s.io/contour      contour     192.168.0.50   True         13m




关注后解锁

分类: Kubernetes

毛巳煜

高级软件开发全栈架构师

工信部备案号:辽ICP备17016257号-2