Docker desktop (Mac) - How to Access a pod via pod ip using curl

4/15/2020

I'm using docker desktop on Mac in order to learn Kubernetes. I'm creating a deployment like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-k8s-hands-on
  annotations:
    kubernetes.io/change-cause: "Changing image"
  labels:
    app: backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: nginx
        image: k8s-hands-on
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: backend-k8s-hands-on
  labels:
    app: backend 
spec:
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9090

Then I create another pod (a temporary one) which will work as a frontend, in order to access the service I've just created with this command:

kubectl run --rm -ti --generator=run-pod/v1 podtest3 --image=nginx:alpine -- sh

So I run nginx inside the pod. Then I check the IP of the pod with kubectl get pods -o wide

NAME                                    READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
backend-k8s-hands-on-6489c76ff4-4q9th   1/1     Running   0          60m   10.1.0.153   docker-desktop   <none>           <none>
backend-k8s-hands-on-6489c76ff4-6tvns   1/1     Running   0          60m   10.1.0.151   docker-desktop   <none>           <none>
backend-k8s-hands-on-6489c76ff4-jrc7k   1/1     Running   0          60m   10.1.0.152   docker-desktop   <none>           <none>
podtest3                                1/1     Running   0          39m   10.1.0.155   docker-desktop   <none>           <none>

And finally when I type into my browser the IP 10.1.0.155 it tells me that safari can't find the page.

-- sergiomahi
kubernetes
macos

1 Answer

4/15/2020

You can create a Service first and port forward to that service:

kubectl -n <namespace> port-forward svc/<svc-name> <localhost-port>:<pod-port> 

The other choice is directly port forward to your pod:

kubectl -n <namespace> port-forward pod/<podname> <localhost-port>:<pod-port> 

And then:

curl localhost:<localhost-port>

Here's more direct example:

✗ kubectl run nginx-example --image=nginx --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-example created
✗ kubectl port-forward pod/nginx-example 8080:80
Error from server (NotFound): pods "nginx-example" not found
✗ kubectl get po
NAME                                  READY   STATUS    RESTARTS   AGE
nginx-example-6cdd6bf4c5-km8mh        1/1     Running   0          55s
✗ kubectl port-forward pod/nginx-example-6cdd6bf4c5-km8mh  8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
✗ curl localhost:8080
Handling connection for 8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
-- irvifa
Source: StackOverflow