kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.12/config/manifests/metallb-native.yaml
其他参考:MetalLB, bare metal load-balancer for Kubernetes
layer2模式配置:
cat <<EOF > IPAddressPool.yamlapiVersion: metallb.io/v1beta1kind: IPAddressPoolmetadata: name: pool40-50 namespace: metallb-systemspec: addresses: # 可分配的 IP 地址,可以指定多个,包括 ipv4、ipv6 - 172.19.82.40-172.19.82.50EOF kubectl apply -f IPAddressPool.yaml cat <<EOF > L2Advertisement.yamlapiVersion: metallb.io/v1beta1kind: L2Advertisementmetadata: name: example namespace: metallb-systemspec: ipAddressPools: - pool40-50 #上一步创建的 ip 地址池,通过名字进行关联EOF kubectl apply -f L2Advertisement.yaml
BGP模式配置:
cat <<EOF > BGPPeer.yamlapiVersion: metallb.io/v1beta2kind: BGPPeermetadata: name: sample namespace: metallb-systemspec: myASN: 64500 # MetalLB 使用的 AS 号 peerASN: 64501 # 路由器的 AS 号 peerAddress: 10.0.0.1 # 路由器地址EOF kubectl apply -f BGPPeer.yaml cat <<EOF > IPAddressPool.yamlapiVersion: metallb.io/v1beta1kind: IPAddressPoolmetadata: name: first-pool namespace: metallb-systemspec: addresses: - 192.168.1.240-192.168.1.250 # 可分配的 IP 地址EOF kubectl apply -f IPAddressPool.yaml cat <<EOF > L2Advertisement.yamlapiVersion: metallb.io/v1beta1kind: L2Advertisementmetadata: name: example namespace: metallb-systemspec: ipAddressPools: - first-poolEOF kubectl apply -f L2Advertisement.yaml
cat <<EOF > nginx-dp.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deployment labels: app: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: docker.io/nginx:latest ports: - containerPort: 80EOF kubectl apply -f nginx-dp.yaml cat <<EOF > nginx-svc.yamlapiVersion: v1kind: Servicemetadata: name: nginx2 labels: app: nginxspec: selector: app: nginx ports: - name: nginx-port protocol: TCP port: 80 targetPort: 80 type: LoadBalancerEOF kubectl apply -f nginx-svc.yaml
结果与检查:
|16:18:51|root@Debain12-template:[metalb]> kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 112dnginx2 LoadBalancer 10.107.216.124 172.19.82.240 80:38417/TCP 2m24szookeeper NodePort 10.104.239.48 <none> 2181:32970/TCP,2888:31332/TCP,3888:30597/TCP 101dzookeeper-headless ClusterIP None <none> 2181/TCP,2888/TCP,3888/TCP 101d |16:19:02|root@Debain12-template:[metalb]> curl 172.19.82.240<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p> <p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p></body></html>