Skip to main content

Setting the Kubeconfig File as an Environment Variable

To configure Kubernetes access on different operating systems, you can set the KUBECONFIG environment variable to point to your specific Kubernetes configuration file (kubeconfig.yaml). This allows Kubernetes command-line tools like kubectl to automatically use the configuration without needing the --kubeconfig flag every time.

For Linux

  1. Download the Kubeconfig File:

    • Download the kubeconfig file from your account dashboard, typically named kubeconfig.yaml or something specific to your environment. Download File From Myaccount Dashboard
  2. Set the Kubeconfig Path:

    • Open your terminal.
    • Edit the .bashrc file in your home directory with a text editor:
      vim ~/.bashrc
  3. Add the Environment Variable:

    • Add the following line at the end of the .bashrc file, where /path/to/your/kube-config-file is the path to your downloaded kubeconfig.yaml:
      export KUBECONFIG="/path/to/your/kube-config-file"
  4. Apply the Changes:

    • To apply the changes made in .bashrc, run the following command:
      source ~/.bashrc
  5. Verify the Configuration:

    • After applying the changes, you should be able to use kubectl without the --kubeconfig flag:
      kubectl get nodes

For Windows

  1. Download the Kubeconfig File:

    • Download the kubeconfig file from your account dashboard, typically named kubeconfig.yaml.
  2. Set the Environment Variable for a Specific User:

    • Open PowerShell with Administrator privileges:

      • Right-click the PowerShell icon and select Run as Administrator.

      Run powershell as a administrator

    • Set the KUBECONFIG environment variable for a specific user:

      [System.Environment]::SetEnvironmentVariable("KUBECONFIG", "C:\Users\UserName\Downloads\kubeconfig.yaml", [System.EnvironmentVariableTarget]::User)
  3. For All Users (Global System-Wide):

    • To set the environment variable for all users, use this command:
      [System.Environment]::SetEnvironmentVariable("KUBECONFIG", "C:\Users\Administrator\Downloads\kubeconfig.yaml", [System.EnvironmentVariableTarget]::Machine)
  4. Verify the Configuration:

    • After executing the command, you should be able to use kubectl without the --kubeconfig flag:
      kubectl get nodes

For macOS

  1. Download the Kubeconfig File:

    • Download the kubeconfig file from your account dashboard, typically named kubeconfig.yaml.
  2. Set the Kubeconfig Path:

    • Open the terminal.
    • Edit the .zshrc file in your home directory with a text editor:
      vim ~/.zshrc
  3. Add the Environment Variable:

    • Add the following line at the end of the .zshrc file, where /path/to/your/kube-config-file is the path to your downloaded kubeconfig.yaml:
      export KUBECONFIG="/path/to/your/kube-config-file"
  4. Apply the Changes:

    • To apply the changes made in .zshrc, run the following command:
      source ~/.zshrc
  5. Verify the Configuration:

    • After applying the changes, you should be able to use kubectl without the --kubeconfig flag:
      kubectl get nodes

Key Points

  • KUBECONFIG Variable: Setting this environment variable tells Kubernetes tools like kubectl where to find your kubeconfig file.
  • Path Customization: Replace /path/to/your/kube-config-file with the actual path to the kubeconfig file on your system.
  • Verification: After setting the KUBECONFIG environment variable and applying the changes, use kubectl get nodes to confirm that you can access your Kubernetes cluster.