k8s部署metallb实现service的LoadBalancer模式

开启ipvs并开启严格ARP模式

参考https://metallb.io/installation/

kubectl edit configmap -n kube-system kube-proxy

mode: ""
ipvs:
  strictARP: false

改成

    mode: "ipvs"
    ipvs:
      strictARP: true

k8s原生部署metallb

下载

wget https://raw.githubusercontent.com/metallb/metallb/v0.14.8/config/manifests/metallb-native.yaml

部署

kubectl apply -f metallb-native.yaml 

helm方式部署metallb

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

helm search repo metallb/metallb -l

kubectl create ns metallb-ns
helm install metallb metallb/metallb --namespace=metallb-ns --version 0.14.8

这个命令helm install metallb metallb/metallb --namespace=metallb-ns --version 0.14.8可能下载metallb失败;可以通过其他方式下载后再安装

wget https://github.com/metallb/metallb/releases/download/metallb-chart-0.14.8/metallb-0.14.8.tgz
tar -zxvf metallb-0.14.8.tgz
cd metallb/
kubectl create ns metallb-ns
helm install metallb . --namespace=metallb-ns
kubectl get all -n metallb-ns 

使用metallb

创建一个IPAddressPool地址池,用来指定用于分配的IP池;再创建了一个二层通告,去关联这个地址池将其中的IP地址们通告出去

ip-pool.yaml

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: production-public-ips
  namespace: metallb-ns
spec:
  addresses:
  - 192.168.10.0/24

---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: production-adver
  namespace: metallb-ns
spec:
  ipAddressPools:
  - production-public-ips
kubectl apply -f ip-pool.yaml

部署一个nginx

nginx-deployment.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.27.1
          ports:
            - name: web
              containerPort: 80
kubectl apply -f nginx-deployment.yaml

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
    metallb.universe.tf/address-pool: production-public-ips #添加注解,指定地址池
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
  type: LoadBalancer  #需要指定为LoadBalancer类型
kubectl apply -f nginx-service.yaml

查看分配的ip

kubectl get svc

结果

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>         443/TCP        76d
nginx        LoadBalancer   10.102.47.232   192.168.10.0   80:32195/TCP   41s

向nginx容器里的静态目录写入内容

kubectl exec -it nginx-7ccbfc6ff8-x9dx6 -- /bin/sh -c "echo i like metallb > /usr/share/nginx/html/metallb.html"

访问http://192.168.10.0/metallb.html

卸载metallb

原生方式

kubectl delete -f metallb-native.yaml 

helm方式

helm uninstall metallb --namespace=metallb-ns

参考

https://metallb.io/installation/
https://blog.****.net/weixin_64334766/article/details/134818813
https://www.cnblogs.com/bmwhero/p/17950012
上一篇:深度优先搜索(DFS)——算法详解与Java实例


下一篇:《ToDesk云电脑vs青椒云性能测试,谁更能实现游戏*?》-本文小结