【问题标题】:Why can't I curl endpoint on GCP?为什么我不能在 GCP 上卷曲端点?
【发布时间】:2021-10-20 17:07:41
【问题描述】:

我正在使用 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 或授权/需要服务帐户有关吗?我该怎么做才能使 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

【问题讨论】:

  • 您在哪里运行curl 命令?集群 IP 将仅在集群本身内可用。你可以发布你的整个服务 yaml 吗?你在用type: NodePort吗?
  • 连接到集群后,我正在 gcloud shell 中运行 curl 命令。所有其他 kubectl 命令都有效吗?
  • 为了能够通过 Cloud Shell 进行连接,您需要使用负载均衡器服务或 NodePort 服务,以使端点可以在集群本身之外访问。您也可以执行到容器中,或者部署一个睡眠容器并执行到其中。 kubectl 在连接到可路由的 API 端点时工作。我在移动设备上回答,但是一旦回到我的电脑前,我会在答案中发布更多细节。并感谢 YAML。这有帮助。
  • 这里是这个复数图的另一个例子:kubectl create deployment hello-world-nodeport --image=gcr.io/google-samples/hello-app:1.0 kubectl expose deployment hello-world-nodeport - -port=80 --target-port=8080 --type NodePort export NODEPORT=$(kubectl get service hello-world-nodeport -o jsonpath='{ .spec.ports[].nodePort }') 然后我得到我的 pod命名并运行 curl http://$PODNAME:$NODEPORT 会超时吗?
  • 您可以查看此document,了解如何在 GKE 中部署和公开服务的一个很好的示例,同时强调 @GariSingh 指出的内容,Service Networking 上有一个很棒的文档,您可以在其中看看暴露服务的理论。

标签: kubernetes google-kubernetes-engine


【解决方案1】:

对 Gari cmets 有更深入的了解,当在集群外部公开服务时,必须将此服务配置为 NodePortLoadBalancer,因为 ClusterIP 仅在集群内部 IP 上公开服务,使服务只能从集群内部访问,并且由于 Cloud Shell 是一个 shell 环境,用于管理托管在 Google Cloud 上的资源,而不是集群的一部分,这就是为什么你'没有得到任何回应。要更改此设置,您可以使用以下内容更改 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多