curl不通 k8s,我如何从Kubernetes容器中运行curl命令

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.

解决方案

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- --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 -c curl- -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

在Java,要通过Kubernetes (K8s) API 进入Pod容器执行命令,通常需要使用客户端库如kube-client或者kubectl本身提供的API。以下是一个简单的步骤概述: 1. **设置环境**:首先确保你安装了Java、kubernetes客户端库(例如`kubernetes-client`)以及一个支持HTTP请求的工具(如curl或者`http4k`等)。 2. **认证**:你需要获取到访问K8s集群所需的凭据,通常是通过服务账户密钥对(service account key pair),或者配置好`kubectl` 的kubeconfig文件。 3. **创建Kubernetes API客户端**:使用`kubernetes-client` 库创建一个`CoreV1Api` 或者 `BatchV1Api` 的实例,取决于你是想运行一次性任务还是长期运行的任务。 ```java import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.openapi.apis.CoreV1Api; ``` 4. **创建Pod和Container的引用**:指定你要操作的Pod的名称和容器名。 5. **调用API执行命令**:对于`CoreV1Api`,你可以使用`exec`方法来执行命令。例如,执行`ls`命令: ```java String command = "ls"; ApiClient client = Configuration.defaultClient(); CoreV1Api coreV1Api = new CoreV1Api(client); try { V1ExecActionOptions options = new V1ExecActionOptions() .command(Arrays.asList(command)) .containerName("your-container-name") .namespace("your-namespace"); V1ContainerExecResult result = coreV1Api.execNamespacedPodCmd(YOUR_POD_NAME, namespace, null, null, options, null); // process the result, usually a stream of output } catch (Exception e) { e.printStackTrace(); } ``` 6. **处理响应**:根据API返回的结果(可能是标准输出流)来处理命令执行结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值