Skip to main content
Skip table of contents

Provide Context for Commands with a kubeconfig File

This page contains some basic recommendations regarding kubeconfig as it relates to target clusters and the --kubeconfig=<CLUSTER_NAME>.conf flag. Refer to the Kubernetes documentation for more information.

For kubectl and dkp commands to run, it is often necessary to specify the environment or cluster in which you want to run them. This specially applies to commands that create, delete, or update a cluster’s resources.

To specify the context, there are two options:

Export an environment variable

Specify the target cluster in the command

Export an environment variable from a cluster’s kubeconfig file, which sets the environment for the commands you run after exporting it.

Specify an environment variable for one command at the time by running it with the --kubeconfig=<CLUSTER_NAME>.conf flag.

  • Better suited for single-cluster environments

  • Better suited for multi-cluster environments

Single-cluster Environment

In a single-cluster environment, you don’t need to switch between clusters to execute commands and perform operations. However, it is still necessary to specify an environment for each terminal session, so the DKP CLI runs the operations on the DKP cluster, and does not accidentally run operations on, for example, the bootstrap cluster.

Use a kubeconfig file to set the environment variable for all your operations

Set the environment variable for all your operations

  1. When you create a cluster, a kubeconfig file is generated automatically. Get this kubeconfig file and write it to the ${CLUSTER_NAME}.conf variable:

    CODE
    dkp get kubeconfig -c ${CLUSTER_NAME} > ${CLUSTER_NAME}.conf

    After setting this configuration, whenever you use run a command with the variable ${CLUSTER_NAME}.conf, it will automatically run on your cluster.

  2. Set the context by exporting your kubeconfig from the source file:
    :note: Execute this command for each terminal session.

    CODE
    export KUBECONFIG=${CLUSTER_NAME}.conf

    For the current terminal session, you don’t need to provide the --kubeconfig flag.

Multi-cluster Environment

Since having multiple clusters means switching between them to run operations, D2iQ recommends two approaches.

A. You can start several terminal sessions, one per cluster, and set the environment variable as shown in the single-cluster environment example above, one time per cluster.

B. You can use a single terminal session, and run the commands with a flag every time. The flag specifies the target cluster for the operation every time, so you can run the same command several times but with a different flag. Expand the following content for an explanation of option B.

B. Use a flag to reference the target cluster

Use a flag to reference the target cluster

The --kubeconfig=<CLUSTER_NAME>.conf flag defines the configuration file for the cluster that you are configuring and accessing.

When operating and using multiple clusters, this is the easiest way to ensure that you are working on the correct cluster. If you create additional clusters and do not store the name as an environment variable, you can type out the name of the cluster followed by .conf to access your cluster. 

Ensure you run dkp get kubeconfig for each cluster you create to generate a kubeconfig file.

Example flag:

CODE
--kubeconfig=azurecluster1.conf

Example command with flag:

CODE
dkp install kommander --kubeconfig=azurecluster1.conf
Advanced: Use a kubeconfig file to configure access to multiple clusters using contexts

For advanced users only.

It is possible to set up a kubeconfig file to manage access to several clusters. For more information, refer to https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/.

You can also set your environment variable to multiple kubeconfig files by merging them. For more information, refer to https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/.

Next Topic:

Storage

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.