Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minikube - how to access pod via pod ip using curl

I use minikube to create local kubernetes cluster.

I create ReplicationController via webapp-rc.yaml file.

apiVersion: v1
kind: ReplicationController
metadata:
  name: webapp
spec:
  replicas: 2
  template: 
    metadata:
      name: webapp
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: tomcat
        ports:
        - containerPort: 8080

and, I print the pods' ip to stdout:

kubectl get pods -l app=webapp -o yaml | grep podIP

podIP: 172.17.0.18
podIP: 172.17.0.1

and, I want to access pod using curl

curl 172.17.0.18:8080

But, the stdout give me: curl: (52) Empty reply from server

I know I can access my application in docker container in pod via service.

I find this code in a book. But the book does not give the context for executing this code.

Using minikube, how to access pod via pod ip using curl in host machine?

update 1

I find a way using kubectl proxy:

➜  ~ kubectl proxy
Starting to serve on 127.0.0.1:8001

and then I can access pod via curl like this:

curl http://localhost:8001/api/v1/namespaces/default/pods/webapp-jkdwz/proxy/

webapp-jkdwz can be found by command kubectl get pods -l app=webapp

update 2

  1. minikube ssh - log into minikube VM

  2. and then, I can use curl <podIP>:<podPort>, for my case is curl 172.17.0.18:8080

like image 516
slideshowp2 Avatar asked May 28 '18 10:05

slideshowp2


2 Answers

First of all, tomcat image expose port 8080 not 80, so the correct YAML would be:

apiVersion: v1
kind: ReplicationController
metadata:
  name: webapp
spec:
  replicas: 2
  template: 
    metadata:
      name: webapp
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: tomcat
        ports:
        - containerPort: 8080

minikube is executed inside a virtual machine, so the curl 172.17.0.18:8080 would only work from inside that virtual machine.

You can always create a service to expose your apps:

kubectl expose rc webapp --type=NodePort

And use the following command to get the URL:

minikube service webapp --url

If you need to query a specific pod, use port forwarding:

kubectl port-forward <POD NAME> 8080

Or just ssh into minikube's virtual machine and query from there.

like image 120
Ignacio Millán Avatar answered Oct 06 '22 11:10

Ignacio Millán


That command is correct, but it only works from a machine that has access to the overlay network. (In case of minikube the host machine does not have that by default).

You can set up a proxy to your pod with:

kubectl port-forward [name of your pod] [pod port]

Thereafter you can (from another shell):

curl 127.0.0.1:port/path

See also: https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/#forward-a-local-port-to-a-port-on-the-pod

like image 9
Janos Lenart Avatar answered Oct 06 '22 11:10

Janos Lenart