简体   繁体   English

使用 pod 名称执行 kubernetes pod

[英]Executing a kubernetes pod with the use of pod name

I am writing a shell script for executing a pod for which the syntax is:我正在编写一个 shell 脚本来执行一个 pod,其语法为:

winpty  kubectl --kubeconfig="C:\kubeconfig" -n namespace exec -it podname bash

This works fine but since podname is not stable and changes for every deployment so is there any alternative for this?这工作正常,但由于 podname 不稳定并且每次部署都会发生变化,所以有什么替代方案吗?

Thanks.谢谢。

You can use the following command:您可以使用以下命令:

kubectl -n <namespace> exec -it deploy/<deployment-name> -- bash

Add a service to your application:向您的应用程序添加服务

As you know, pods are ephemeral;如您所知,豆荚是短暂的。 They come in and out of existence dynamically to ensure your application stays in compliance with your configuration.它们动态地进出,以确保您的应用程序与您的配置保持一致。 This behavior implements the scaling and self-healing aspects of kubernetes.此行为实现了 kubernetes 的缩放和自我修复方面。

You application will consist of one or more pods that are accessible through a service , The application's service name and address does not change and so acts as the stable interface to reach your application.您的应用程序将由一个或多个可通过服务访问的 pod 组成,应用程序的服务名称和地址不会更改,因此充当访问您的应用程序的稳定接口。

This method works both if your application has one pod or many pods.如果您的应用程序有一个 pod 或多个 pod,此方法都适用。

Does that help?这有帮助吗?

You can use normally $ kubectl exec command but define value for changing pod name.您可以正常使用$ kubectl exec命令,但定义更改 pod 名称的值。

Assuming that you have deployment and labeled pods: app=example, simply execute:假设您有部署和标记的 pod:app=example,只需执行:

$ kubectl exec -it $(kubectl get pods -l app=example -o custom-columns=:metadata.name) -- bash

EDIT:编辑:

You can also execute:您还可以执行:

POD_NAME = $(kubectl get pods -l app=example -o custom-columns=":metadata.name")

or或者

POD_NAME = $(kubectl get pods -l app=example -o jsonpath = "{. Items [0] .metadata.name}")

finally最后

$ winpty kubectl exec -ti $POD_NAME --bash

Make sure that you execute command in proper namespace - you can also add -n flag and define it.确保您在正确的命名空间中执行命令 - 您还可以添加-n标志并定义它。

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

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