Search code examples
kuberneteskubectlkubeconfig

How can I use an alternative kubeconfig file?


By default, kubectl configuration is located at ~/.kube/config. Is it possible to specify the kubeconfig file explicitly when using the kubectl command?


Solution

  • kubectl can use the KUBECONFIG in three different ways:

    1. Using the KUBECONFIG present in ~/.kube/config if nothing is specified in kubectl command.

    2. Using the KUBECONFIG defined by using the KUBECONFIG environment variable if nothing is specified in kubectl command.

    3. Passing the KUBECONFIG as part of the kubectl command using the --kubeconfig flag as:

      kubectl --kubeconfig=<KUBECONFIG_PATH>
      

    Further, the order of precedence is as follows:

    1. Use the KUBECONFIG if explicitly passed in the kubectl command
    2. Use the KUBECONFIG from the environment variable if --kubeconfig flag is not specified but the environment variable is set.
    3. Use the KUBECONFIG from ~/.kube/config if both --kubeconfig and KUBECONFIG are not specified.