How do i run curl command from within a Kubernetes

2020-02-25 04:59发布

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 doesnt 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.

3条回答
霸刀☆藐视天下
2楼-- · 2020-02-25 05:28
  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

查看更多
男人必须洒脱
3楼-- · 2020-02-25 05:33

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/

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-25 05:39

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

查看更多
登录 后发表回答