简体   繁体   中英

How do I run curl command from within a Kubernetes pod

I have the following questions:

  1. I am logged into a Kubernetes pod using the following command:

     ./cluster/kubectl.sh exec my-nginx-0onux -c my-nginx -it bash

    The 'ip addr show' command shows its assigned the ip of the pod. Since pod is a logical concept, I am assuming I am logged into a docker container and not a pod, In which case, the pod IP is same as docker container IP. Is that understanding correct?

  2. from a Kubernetes node, I do sudo docker ps and then do the following:-

     sudo docker exec 71721cb14283 -it '/bin/bash'

    This doesn't work. Does someone know what I am doing wrong?

  3. I want to access the nginx service I created, from within the pod using curl. How can I install curl within this pod or container to access the service from inside. I want to do this to understand the network connectivity.

Here is how you get a curl command line within a kubernetes network to test and explore your internal REST endpoints.

To get a prompt of a busybox running inside the network, execute the following command. (A tip is to use one unique container per developer.)

kubectl run curl-<YOUR NAME> --image=radial/busyboxplus:curl -i --tty --rm

You may omit the --rm and keep the instance running for later re-usage. To reuse it later, type:

kubectl attach <POD ID> -c curl-<YOUR NAME> -i -t

Using the command kubectl get pods you can see all running POD's. The is something similar to curl-yourname-944940652-fvj28.

EDIT: Note that you need to login to google cloud from your terminal (once) before you can do this! Here is an example, make sure to put in your zone, cluster and project: gcloud container clusters get-credentials example-cluster --zone europe-west1-c --project example-148812

The idea of Kubernetes is that pods are assigned on a host but there is nothing sure or permanent, so you should NOT try to look up the IP of a container or pod from your container, but rather use what Kubernetes calls a Service .

A Kubernetes Service is a path to a pod with a defined set of selectors, through the kube-proxy , which will load balance the request to all pods with the given selectors.

In short:

create a Pod with a label called 'name' for example. let's say name=mypod create a Service with the selector name=mypod that you call myService for example, to which you assign the port 9000 for example.

then you can curl from a pod to the pods served by this Service using curl http://myService:9000

This is assuming you have the DNS pod running of course. If you ask for a LoadBalancer type of Service when creating it, and run on AWS or GKE, this service will also be available from outside your cluster. For internal only service, just set the flag clusterIP: None and it will not be load balanced on the outside.

see reference here:

https://kubernetes.io/docs/concepts/services-networking/service/ https://kubernetes.io/docs/tutorials/services/

  1. Kubernetes uses the IP-per-pod model. All containers in the same pod share the same IP address as if they are running on the same host.

  2. The command should follow docker exec [OPTIONS] CONTAINER COMMAND [ARG...] . In your case, sudo docker exec -it 71721cb14283 '/bin/bash' should work. If not, you should provide the output of your command.

  3. It depends on what image you use. There is nothing special about installing a software in a container. For nginx, try apt-get update && apt-get install curl

There's an official curl team image these days:

https://hub.docker.com/r/curlimages/curl

Run it with:

kubectl run -it --rm --image=curlimages/curl curly -- sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM