使用 MetalLB 公开 Kubernetes 集群

Exposing Kubernetes cluster with MetalLB

我有一个由主节点和 2 个工作节点组成的裸机集群。主节点有 2 个网络接口。其中一个网络接口分配了 PUBLIC_IP。

使用主节点中的第二个网络接口连接节点。如下:

kubeadm init --apiserver-advertise-address INTERNAL_MASTER_IP

然后,工人加入如下:

kubeadm join INTERNAL_MASTER_IP:6443 --token TOKEN --discovery-token-ca-cert-hash sha256:CERT

我已经安装了Weave,如下:

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

到目前为止一切正常,状态如下:

kubectl get nodes -o wide
NAME    STATUS   ROLES    AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
node0   Ready    master   3h44m   v1.17.0   PUBLIC_IP       <none>        Ubuntu 18.04.3 LTS   4.15.0-72-generic   docker://18.6.2
node1   Ready    <none>   3h40m   v1.17.0   10.6.129.228    <none>        Ubuntu 18.04.3 LTS   4.15.0-72-generic   docker://18.6.2
node2   Ready    <none>   3h40m   v1.17.0   10.6.129.47     <none>        Ubuntu 18.04.3 LTS   4.15.0-72-generic   docker://18.6.2

我正在尝试通过 PUBLIC_IP 公开服务。为此,我按如下方式安装了 MetalLB:

kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.8.3/manifests/metallb.yaml

ConfigMap如下:

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - PUBLIC_IP

NGINX部署如下:

kubectl run nginx --image=nginx --port=80
kubectl expose deployment nginx --type=LoadBalancer --name=nginx-service

然而,结果如下:

kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           31m


kubectl get services
NAME            TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP      10.96.0.1      <none>        443/TCP        3h50m
nginx-service   LoadBalancer   10.96.114.88   <pending>     80:31246/TCP   31m


kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5578584966-vkgvv   1/1     Running   0          32m`enter code here`


kubectl describe service nginx-service
Name:                     nginx-service
Namespace:                default
Labels:                   run=nginx
Annotations:              <none>
Selector:                 run=nginx
Type:                     LoadBalancer
IP:                       10.96.114.88
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31246/TCP
Endpoints:                10.36.0.0:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

可以看出,外网IP还是pending。你有什么想法吗?提前谢谢你:)

您需要在 metallb 配置中提供 IP 地址 范围。您提供给 metallb 的 IP 也应该是虚拟 IP。不应该有使用 IP 的端点。

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - PUBLIC_IP-PUBLIC_IP      ## This line