简体   繁体   English

如何从 Kubernetes pod 中运行 curl 命令

[英]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:我使用以下命令登录到 Kubernetes pod:

     ./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. “ip addr show”命令显示其分配的 pod 的 ip。 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. 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:-从 Kubernetes 节点,我执行sudo docker ps然后执行以下操作:-

     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.我想使用 curl 从 pod 中访问我创建的 nginx 服务。 How can I install curl within this pod or container to access the service from inside.如何在此 pod 或容器中安装 curl 以从内部访问服务。 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.以下是在 kubernetes 网络中获取 curl 命令行以测试和探索内部 REST 端点的方法。

To get a prompt of a busybox running inside the network, execute the following command.要获得网络内部正在运行的 busybox 的提示,请执行以下命令。 (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.您可以省略 --rm 并保持实例运行以备后用。 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.使用kubectl get pods命令,您可以看到所有正在运行的 POD。 The is something similar to curl-yourname-944940652-fvj28.类似于 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这是一个示例,请确保放入您的区域、集群和项目: 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 . Kubernetes的想法是将pod分配到主机上,但没有什么是确定的或永久的,因此您不应该尝试从容器中查找容器或 pod 的 IP,而应使用 Kubernetes 所谓的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. Kubernetes服务是一个路径,它具有一组定义的选择器,通过kube-proxy将请求负载平衡到所有具有给定选择器的 Pod。

In short:简而言之:

create a Pod with a label called 'name' for example.例如,创建一个带有名为“name”的标签Pod 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.假设name=mypod创建一个具有选择器name=mypod服务,例如您调用myService ,例如,您为其分配端口9000

then you can curl from a pod to the pods served by this Service using curl http://myService:9000然后您可以使用curl http://myService:9000从 pod 卷曲到此服务提供的 pod

This is assuming you have the DNS pod running of course.这当然是假设您运行了 DNS pod。 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.如果您在创建时请求LoadBalancer类型的服务,并在 AWS 或 GKE 上运行,则该服务也可从您的集群外部使用。 For internal only service, just set the flag clusterIP: None and it will not be load balanced on the outside.对于仅限内部的服务,只需设置标志clusterIP: None并且不会在外部进行负载平衡。

see reference here:请参阅此处的参考:

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

  1. Kubernetes uses the IP-per-pod model. Kubernetes 使用IP-per-pod模型。 All containers in the same pod share the same IP address as if they are running on the same host.同一个 Pod 中的所有容器共享相同的 IP 地址,就好像它们在同一主机上运行一样。

  2. The command should follow docker exec [OPTIONS] CONTAINER COMMAND [ARG...] .该命令应遵循docker exec [OPTIONS] CONTAINER COMMAND [ARG...] In your case, sudo docker exec -it 71721cb14283 '/bin/bash' should work.在您的情况下, sudo docker exec -it 71721cb14283 '/bin/bash'应该可以工作。 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对于 nginx,尝试apt-get update && apt-get install curl

There's an official curl team image these days:这些天有一个官方的 curl 团队形象:

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

Run it with:运行它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Docker yml 文件中运行 curl 命令? - How do I run curl command within Docker yml file? 无法在 kubernetes pod 中安装 curl 命令 - Unable to install curl command in kubernetes pod 如何登录运行在特定Kubernetes容器内的Docker容器并运行test.sh文件? - How do I login into a Docker container running inside the specific Kubernetes pod and run an test.sh file? 如何在具有运行中的spring-boot应用程序的kubernetes容器/容器中运行cron? - How do I run a cron inside a kubernetes pod/container which has a running spring-boot application? 如何从 Kubernetes pod 到本地运行的 Wireshark 的 pipe 实时 pcap 记录? - How do I pipe live pcap records from a Kubernetes pod to Wireshark running locally? 如何使用 nsenter 实用程序从 POD 连接和管理 Kubernetes 集群 - How do I connect and manage Kubernetes cluster from POD using nsenter utility 如何基于每个pod从Kubernetes将日志发送到GELF UDP端点 - How do I send logs to GELF UDP endpoint from Kubernetes on a per-pod basis 如何在此Kubernetes示例中选择主Redis窗格? - How do I select the master Redis pod in this Kubernetes example? 如何修改我的 DOCKERFILE 以将 wget 安装到 kubernetes pod 中? - How do I modify my DOCKERFILE to install wget into kubernetes pod? 如何将Kubernetes配置图复制到Pod的可写区域? - How do I copy a Kubernetes configmap to a write enabled area of a pod?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM