如何将负载均衡器添加到 digitalocean 上的集群

How to add a loadbalancer to a cluster on digitalocean

我使用 Kubeadm 和 3 个 droplet 在 digitalocean 上创建了一个集群。由于这不是来自 Digital ocean 的托管 Kubernetes 集群,我该如何手动设置 LoadBalancer?

我尝试通过将以下行添加到部署配置文件来添加外部负载平衡器

...
replicaCount: 1

image:
  repository: turfff/node-replicas
  tag: latest
  pullPolicy: IfNotPresent
...
service:
  type: LoadBalancer
  port: 80
  targetPort: 8080
...

然而,当我 运行 配置并检查创建的 svc

kubectl get svc

NAME                              TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes                        ClusterIP      10.96.0.1       <none>        443/TCP        13d
mongo-mongodb-replicaset          ClusterIP      None            <none>        27017/TCP      3h15m
mongo-mongodb-replicaset-client   ClusterIP      None            <none>        27017/TCP      3h15m
nodejs-nodeapp                    LoadBalancer   10.109.213.98   <pending>     80:31769/TCP   61m

kubectl describe svc nodejs-nodeapp

Name:                     nodejs-nodeapp
Namespace:                default
Labels:                   app.kubernetes.io/instance=nodejs
                          app.kubernetes.io/managed-by=Tiller
                          app.kubernetes.io/name=nodeapp
                          app.kubernetes.io/version=1.0
                          helm.sh/chart=nodeapp-0.1.0
Annotations:              <none>
Selector:                 app.kubernetes.io/instance=nodejs,app.kubernetes.io/name=nodeapp
Type:                     LoadBalancer
IP:                       10.109.213.98
Port:                     http  80/TCP
TargetPort:               http/TCP
NodePort:                 http  31769/TCP
Endpoints:                10.244.2.19:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

kubectl get pods

NAME                              READY   STATUS    RESTARTS   AGE
mongo-mongodb-replicaset-0        1/1     Running   0          3h18m
mongo-mongodb-replicaset-1        1/1     Running   0          3h17m
mongo-mongodb-replicaset-2        1/1     Running   0          3h16m
nodejs-nodeapp-7b89db8888-sjcbq   1/1     Running   0          65m

kubectl describe pod nodejs-nodeapp

Name:               nodejs-nodeapp-7b89db8888-sjcbq
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               worker-02/206.81.3.65
Start Time:         Sun, 14 Jun 2020 11:21:07 +0100
Labels:             app.kubernetes.io/instance=nodejs
                    app.kubernetes.io/name=nodeapp
                    pod-template-hash=7b89db8888
Annotations:        <none>
Status:             Running
IP:                 10.244.2.19
Controlled By:      ReplicaSet/nodejs-nodeapp-7b89db8888
Containers:
  nodeapp:
    Container ID:   docker://f0d4d01f....
    Image:          turfff/node-replicas:latest
    Image ID:       docker-pullable://turfff/node-replicas@sha256:34d...
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sun, 14 Jun 2020 11:21:08 +0100
    Ready:          True
    Restart Count:  0
    Liveness:       http-get http://:http/sharks delay=0s timeout=1s period=10s #success=1 #failure=3
    Readiness:      http-get http://:http/sharks delay=0s timeout=1s period=10s #success=1 #failure=3
    Environment:
      MONGO_USERNAME:    <set to the key 'MONGO_USERNAME' in secret 'nodejs-auth'>          Optional: false
      MONGO_PASSWORD:    <set to the key 'MONGO_PASSWORD' in secret 'nodejs-auth'>          Optional: false
      MONGO_HOSTNAME:    <set to the key 'MONGO_HOSTNAME' of config map 'nodejs-config'>    Optional: false
      MONGO_PORT:        <set to the key 'MONGO_PORT' of config map 'nodejs-config'>        Optional: false
      MONGO_DB:          <set to the key 'MONGO_DB' of config map 'nodejs-config'>          Optional: false
      MONGO_REPLICASET:  <set to the key 'MONGO_REPLICASET' of config map 'nodejs-config'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from nodejs-nodeapp-token-4wxvd (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  nodejs-nodeapp-token-4wxvd:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  nodejs-nodeapp-token-4wxvd
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

创建负载均衡器失败。如何手动设置 LoadBalancer?

我不建议手动配置负载均衡器。如果安装 digital ocean cloud controller manager which is the Kubernetes cloud controller manager implementation for DigitalOcean. Read more about cloud controller managers here.

,则可以自动执行此操作

DigitalOcean 云控制器管理器运行服务控制器,它负责监视类型 LoadBalancer 的服务并创建 DO 负载均衡器以满足其要求。 Here 是如何使用它的示例。

Here 是一个 yaml 文件,您可以使用它在 Kubernetes 集群上部署它。这需要将数字海洋 api 令牌放置在清单的 access-token: 部分。