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

前端 未结 3 1340
慢半拍i
慢半拍i 2021-01-17 09:33

How can I tell whether or not I am running inside a kubernetes cluster? With docker I can check if /.dockerinit exist. Is there an equivalent?

3条回答
  •  清酒与你
    2021-01-17 10:05

    With the default configuration, Kubernetes will mount the serviceaccount secrets into pods. Simply check for the existence of this folder: /var/run/secrets/kubernetes.io. No need to set environment variables. In ruby I would do the following:

    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
    

提交回复
热议问题