0

我想通过client-go自定义一个负载均衡器控制器,现在我只是尝试为服务设置一个地址,但它不起作用。这是我的代码:</p>

func (c *Controller) syncToStdout(key string) error {
    obj, exists, err := c.indexer.GetByKey(key)
    if err != nil {
        klog.Errorf("Fetching object with key %s from store failed with %v", key, err)
        return err
    }

    if !exists {
        fmt.Printf("Pod %s does not exist anymore%s\n", key)
    } else {
        service := obj.(*v1.Service)
        if service.ObjectMeta.Annotations["service.beta.kubernetes.io/netease-cloud"] == "nlb" {
            klog.Info("service belong to NLB, name = ", service.GetName())
            // [0].IP = "10.123.234.213";
            service.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{IP: "10.123.234.213"}}
            _, _ = clientset.CoreV1().Services(v1.NamespaceDefault).Update(service)
            klog.Info("service belong to NLB, service = ", service)
        } else {
            klog.Info("service not belong to NLB, name = ", service.GetName())
        }
    }
    return nil
}

然后我得到服务配置,没有设置ip。

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/netease-cloud: nlb
  creationTimestamp: "2019-11-29T07:39:41Z"
  name: httpd-service
  namespace: default
  resourceVersion: "480971"
  selfLink: /api/v1/namespaces/default/services/httpd-service
  uid: 3000dddd-3d56-4989-af41-6d6a9bab7565
spec:
  clusterIP: 10.108.100.92
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30000
    port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: httpd
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer: {}

如何为 LoadBalancer 服务设置 ip?

4

1 回答 1

1

使用clientset.CoreV1().Services(v1.NamespaceDefault).UpdateStatus(service)可以解决。

于 2019-12-03T09:15:17.473 回答