为什么我不能在 GCP 上卷曲端点?

Why can't I curl endpoint on GCP?

我正在使用 GKE 完成 kubernetes 教程,但它是在考虑 Azure 的情况下编写的 - 虽然到目前为止它一直运行良好。

它没有起作用的第一部分是关于 coreDNS 的练习——我知道它在 GKE 上不存在——它只是 kubedns?

这就是我无法通过以下方式获得 pod 端点的原因:

export PODIP=$(kubectl get endpoints hello-world-clusterip -o jsonpath='{ .subsets[].addresses[].ip}')

然后卷曲:

curl http://$PODIP:8080

我的部署肯定在正确的端口上:

ports:
        - containerPort: 8080

事实上,tut 的部署来自 google 示例。

这与 coreDNS 或 authorisation/needing 服务帐户有关吗?我该怎么做才能使 curl 请求正常工作?

部署 yaml 是:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-customdns
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world-customdns
  template:
    metadata:
      labels:
        app: hello-world-customdns
    spec:
      containers:
      - name: hello-world
        image: gcr.io/google-samples/hello-app:1.0
        ports:
        - containerPort: 8080
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 9.9.9.9
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world-customdns
spec:
  selector:
    app: hello-world-customdns
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080

深入了解 Gari 的评论,当在集群外公开服务时,必须将此服务配置为 NodePortLoadBalancer,因为 ClusterIP 仅在集群内部 IP 上公开服务,使得服务只能从集群内部访问,并且由于 Cloud Shell 是一个 shell 环境用于管理 Google 云上托管的资源,而不是集群的一部分,这就是您没有收到任何响应的原因。要更改此设置,您可以使用以下内容更改您的 yaml 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-customdns
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world-customdns
  template:
    metadata:
      labels:
        app: hello-world-customdns
    spec:
      containers:
      - name: hello-world
        image: gcr.io/google-samples/hello-app:1.0
        ports:
        - containerPort: 8080
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 9.9.9.9
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world-customdns
spec:
  selector:
    app: hello-world-customdns
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080

重新部署服务后,您可以在 cloud shell 上 运行 命令 kubectl get all -o wide 来验证是否已创建 NodePort 类型服务节点和目标端口。

要测试您的部署,只需从您的一个节点(包括分配的节点端口)对外部 IP 进行 CURL 测试,该命令应如下所示:

curl <node_IP_address>:<Node_port>