在 Google 云中使用带有 Kubernetes 服务的外部 IP 将其公开到互联网

Use External IP in Google cloud with Kubernetes service to expose it to the internet

我在 kubernetes 集群上有一个 phpmyadmin 服务 运行。我想在 google 云上保留一个外部 IP(静态)以用于此服务,以便可以从 Internet 访问它。 我尝试在 GCP 上保留一个 IP 地址并在 kubernetes 服务文件中使用它,如下所示:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: phpmyadmin
  name: phpmyadmin
spec:
  externalIPs: [xx.xxx.xxx.xxx]  #the external IP from Google cloud
  ports:
  - name: "8080"
    port: 8080
    targetPort: 80
  selector:
    io.kompose.service: phpmyadmin
status:
  loadBalancer: {}

当我指定 spec.type: LoadBalancer 时,可以使用 type: LoadBalancer .

生成的默认 IP 地址从 Internet 访问该服务

我试图通过允许端口 8080 上的入口来更改外部 IP 地址的防火墙规则,但这没有用。

防火墙规则应用于实例级别。他们无法阻止流量到达负载均衡器本身。

参考:https://cloud.google.com/load-balancing/docs/https/#firewall_rules

您的 GKE LB 服务可能默认使用 HTTP 负载均衡器,也许您可​​以查看 NLB 负载均衡器https://cloud.google.com/load-balancing/docs/choosing-load-balancer#summary-of-google-cloud-load-balancers

所有端口:https://cloud.google.com/kubernetes-engine/docs/how-to/service-parameters#all_ports

apiVersion: v1
kind: Service
metadata:
  name: helloworld
  labels:
    app: helloworld
  annotations:
    cloud.google.com/neg: '{"exposed_ports": {"8080":{}}}'
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: helloworld
  # Use LoadBalancer type instead of ClusterIP
  type: LoadBalancer

示例:https://spring-gcp.saturnism.me/deployment/kubernetes/load-balancing/external-load-balancing

而不是设置 exteranlIPs,您应该设置 spec.loadBalancerIPspec.typeLoadBalancer 值:

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: /snap/kompose/19/kompose-linux-amd64 convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: phpmyadmin
  name: phpmyadmin
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 80
  selector:
    io.kompose.service: phpmyadmin
  type: LoadBalancer
  loadBalancerIP: "YOUR_IP_ADDRESS"
status:
  loadBalancer: {}

Note that exposing your Pods through an external static IP only supports regional load balanced traffic hence your reserved static IP address needs to be regional.

对于全球 IP 地址,您需要通过 Ingress 对象公开 HTTP(s) 负载平衡器