1 云服务器 使用k8s,可以选择云厂商的负载均衡产品.

2 本地安装部署的k8s集群, 默认不支持LoadBlancer的, 需要安装组件来支持.

推荐Metallb:GitHub - metallb/metallb: A network load-balancer implementation for Kubernetes using standard routing protocols

#创建的LoadBlancer service 一直处于pending
[root@master11 soft]# kubectl get node,po,svc -A -owide|grep discuz
default                pod/discuz-859bc9f964-d79mj                                 1/1     Running   0             4h32m   10.244.126.205   slave13    <none>           <none>
default                pod/discuz-859bc9f964-dgxfw                                 1/1     Running   0             4h32m   10.244.126.206   slave13    <none>           <none>
default                pod/discuz-859bc9f964-v8ff9                                 1/1     Running   0             4h32m   10.244.199.16    slave12    <none>           <none>
default                service/discuz-service                         LoadBalancer   10.108.23.34     <pending>     88:30036/TCP                    105s   app=discuz

3  如果在 IPVS 模式下使用 kube-proxy,从 Kubernetes v1.14.2 开始,必须启用 ARP 模式.

kubectl edit configmap -n kube-system kube-proxy
strictARP: false-->strictARP: true

4  安装 MetalLB

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.5/config/manifests/metallb-native.yaml
#查看服务
[root@master11 soft]# kubectl get pod   -n metallb-system -A -owide| grep  metallb
metallb-system         controller-86f5578878-g7r6t                             1/1     Running   0             117m    10.244.199.17    slave12    <none>           <none>
metallb-system         speaker-85x75                                           1/1     Running   0             117m    192.168.50.13    slave13    <none>           <none>
metallb-system         speaker-d7sxh                                           1/1     Running   0             117m    192.168.50.12    slave12    <none>           <none>
metallb-system         speaker-zt6kl                                           1/1     Running   0             117m    192.168.50.11    master11   <none>           <none>

5 Layer 2 配置

#配置IPAddressPool
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: tonyhi6-pool
  namespace: metallb-system
spec:
  addresses:
  - 192.168.10.0/24
  - 192.168.9.1-192.168.9.5
  - fc00:f853:0ccd:e799::/124
#本例如下
[root@master11 soft]# cat  IPAddressPool.yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: tonyhi6-pool
  namespace: metallb-system
spec:
  addresses:
  - 192.168.50.180-192.168.1.200
#L2Advertisement关联IPAddressPool
[root@master11 soft]# cat L2Advertisement.yaml 
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: tonyhi6
  namespace: metallb-system
spec:
  ipAddressPools:
  - first-pool
#创建报错
[root@master11 soft]# kubectl delete -f IPAddressPool.yaml 
Error from server (NotFound): error when deleting "IPAddressPool.yaml": ipaddresspools.metallb.io "first-pool" not found
#解决,重新启动集群所有的服务
[root@master11 ~]# systemctl restart docker
[root@master11 ~]# systemctl restart cri-docker
#
[root@master11 soft]# kubectl apply -f IPAddressPool.yaml 
ipaddresspool.metallb.io/tonyhi6-pool created
[root@master11 soft]# kubectl apply -f L2Advertisement.yaml 
l2advertisement.metallb.io/test created

 6 通过之前部署的Discuz 来测试LoadBalancer

[root@master11 soft]# kubectl apply -f discuz-srv.yaml 
service/discuz-service created
#查看
kubectl get node,po,svc -A -owide|grep discuz
default                pod/discuz-859bc9f964-d79mj                                 1/1     Running   1 (8m26s ago)   5h56m   10.244.126.210   slave13    <none>           <none>
default                pod/discuz-859bc9f964-dgxfw                                 1/1     Running   1 (8m26s ago)   5h56m   10.244.126.207   slave13    <none>           <none>
default                pod/discuz-859bc9f964-v8ff9                                 1/1     Running   1 (8m29s ago)   5h56m   10.244.199.19    slave12    <none>           <none>
default                service/discuz-service                         LoadBalancer   10.107.79.137    192.168.50.180   88:30036/TCP                    30s     app=discuz

#可以看到discuz-service已经获取到LoadBalancer负载均衡地址池的ip,metallb为该服务分配一个外部 IP 地址,在 IPAddressPool 中定义的地址池.

7 访问192.168.50.180

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐