在 GKE 上使用静态 IP 时服务不会启动

Service won't come up when using static IP on GKE

我已经在我的 GCP 帐户上分配了一个静态 IP。然后,我更新了应用程序的服务定义以在负载均衡器中使用它,如下所示:

kind: Service
apiVersion: v1
metadata:
  # Unique key of the Service instance
  name: my-app-service
spec:
  ports:
    # Accept traffic sent to port 80
    - name: http
      port: 80
      targetPort: 5000
  selector:
    # Loadbalance traffic across Pods matching
    # this label selector
    app: web
  # Create an HA proxy in the cloud provider
  # with an External IP address - *Only supported
  # by some cloud providers*
  type: LoadBalancer
  # Use the static IP allocated
  loadBalancerIP: 35.186.xxx.xxx

如果我注释掉最后一行并让 GKE 分配一个临时 public IP,服务就会正常运行。知道我做错了什么吗?

根据回答中的建议,我创建了一个 Ingress 如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapp
  annotations:
    kubernetes.io/ingress.global-static-ip-name: "myapp"
spec:
  backend:
    serviceName: my-app-service
    servicePort: 80

现在,我看到 Ingress 被分配了正确的静态 IP。但是,我的 Service 也被分配了一个(不同的)public IP。 IngressService 没有连接。如果我注释掉 type: LoadBalancer 行,Service 未分配 public IP,但 Ingress 仍然无法连接。我在访问静态 IP 时收到 default backend - 404 响应。我试过以不同的顺序创建服务和入口,但也没有帮助。

如果我将其保留足够长的时间,静态 IP 会将流量路由到我的服务,但服务本身会停留在外部 IP 分配中:

$ kubectl get service my-app-service
NAME                   CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
my-app-service          10.x.x.x        <pending>     80:31432/TCP   12m

您需要创建一个 Ingress 并在 Ingress for Kubernetes 上使用 kubernetes.io/ingress.global-static-ip-name: "name-of-your-ip" 注释才能找到它。

您可以在这里找到教程:https://github.com/kelseyhightower/ingress-with-static-ip#tutorial