Davtho1983
Davtho1983

Reputation: 3954

Why can't I curl endpoint on GCP?

I am working my way through a kubernetes tutorial using GKE, but it was written with Azure in mind - tho it has been working ok so far.

The first part where it has not worked has been with exercises regarding coreDNS - which I understand does not exist on GKE - it's kubedns only?

Is this why I can't get a pod endpoint with:

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

and then curl:

curl http://$PODIP:8080

My deployment is definitely on the right port:

ports:
        - containerPort: 8080

And, in fact, the deployment for the tut is from a google sample.

Is this to do with coreDNS or authorisation/needing a service account? What can I do to make the curl request work?

Deployment yaml is:

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

Upvotes: 0

Views: 1956

Answers (1)

Cesar
Cesar

Reputation: 75

Having a deeper insight on what Gari comments, when exposing a service outside your cluster, this services must be configured as NodePort or LoadBalancer, since ClusterIP only exposes the Service on a cluster-internal IP making the service only reachable from within the cluster, and since Cloud Shell is a a shell environment for managing resources hosted on Google Cloud, and not part of the cluster, that's why you're not getting any response. To change this, you can change your yaml file with the following:

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

After redeploying your service, you can run command kubectl get all -o wide on cloud shell to validate that NodePort type service has been created with a node and target port.

To test your deployment just throw a CURL test to he external IP from one of your nodes incluiding the node port that was assigned, the command should look like something like:

curl <node_IP_address>:<Node_port>

Upvotes: 1

Related Questions