简体   繁体   English

如何判断我的容器是否在 Kube.netes 集群中运行?

[英]How do I tell if my container is running inside a Kubernetes cluster?

How can I tell whether or not I am running inside a kube.netes cluster?如何判断我是否在 kube.netes 集群中运行? With docker I can check if /.dockerinit exist.使用 docker 我可以检查/.dockerinit是否存在。 Is there an equivalent?有等价物吗?

You can check for KUBERNETES_SERVICE_HOST environment variable. 您可以检查KUBERNETES_SERVICE_HOST环境变量。

This variable is always exported in an environment where the container is executed. 始终在执行容器的环境中导出此变量。

Refer to https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#environment-variables 请参阅https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#environment-variables

You can pass environment variables to your containers in the pod spec. 您可以在pod规范中将环境变量传递给容器。 You can even expose some pod information to the containers via environment variables using the downward API . 您甚至可以使用向下API通过环境变量向容器公开一些pod信息。

With the default configuration, Kubernetes will mount the serviceaccount secrets into pods. 使用默认配置,Kubernetes会将serviceaccount机密挂载到pod中。 Simply check for the existence of this folder: /var/run/secrets/kubernetes.io . 只需检查此文件夹是否存在: /var/run/secrets/kubernetes.io No need to set environment variables. 无需设置环境变量。 In ruby I would do the following: 在ruby中我会做以下事情:

if File.exists?('/.dockerenv')
  puts "I'm running in a docker container"
end

if File.exists?('/var/run/secrets/kubernetes.io')
  puts "I'm also running in a Kubernetes pod"
end

One option is to check the /etc/hosts file - there is by default the comment that the file is maintained by K8s.一种选择是检查/etc/hosts文件——默认情况下,该文件由 K8s 维护。

Anyway the best way is to def your own env variable in deployment, so use some template tools like helm to gen deployment and define some general template.无论如何,最好的方法是在部署中定义自己的 env 变量,因此使用一些模板工具,如 helm 来生成部署并定义一些通用模板。

暂无
暂无

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

相关问题 我如何才能将其放入OpenShift / Kubernetes集群中运行的容器中? - How can I ssh into a container running inside an OpenShift/Kubernetes cluster? 如何使用 kubernetes go-client 从集群内运行的容器创建命名空间 - How do i create a namespace using kubernetes go-client from running container inside a cluster 如何从运行在同一 Kube.netes 集群中的容器连接到 Docker,它在 Kube.netes 集群中的主机虚拟机上运行? - How do I connect to Docker, running on host VMs in a Kubernetes cluster from a container running in the same Kubernetes cluster? 运行启用了自动缩放功能的Google Container / Kubernetes Engine群集时,如何避免不可调度的pod? - How do I avoid unschedulable pods when running an autoscale-enabled Google Container/Kubernetes Engine cluster? 如何在Google Container Engine / Kubernetes上扩展我的群集? - How do I scale up my cluster on Google Container Engine / Kubernetes? 如何登录运行在特定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:在现有集群中启动一个容器,用于运行 cron 作业 - Kubernetes : Launch a single container inside existing cluster, for running cron jobs 我如何从集群上的容器内部在kubernetes集群上运行ksonnet命令 - How can i run ksonnet command on kubernetes cluster from inside a container on the cluster 如何在我的 jelastic kubernetes 集群上安装letsencrypt? - How do I install letsencrypt on my jelastic kubernetes cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM