Accessing a k8s Cluster#
After obtaining a kubeconfig for a cluster it’s time to get setup and access that cluster. This is done mostly via the command line using the kubectl
commands.
Specify Kubeconfig#
There’s a few different ways that a kubeconfig can be specified. The k8s documentation here goes in to more detail on those methods.
Note
Access to k8s resources is also controlled via kubeconfigs. In the examples below you may not have the applicable permissions to view the resources specified. If you get a response that contains Error from server (Forbidden)
you are communicating with the cluster, but you do not have permission to view the requested resources.
Default kubeconfig#
By default kubectl
commands look to the ~/.kube/config.yml
file. If you do not specify a kubeconfig to use explicitly kubectl
by is configured to use this file by default. You can use any kubeconfig files to overwrite this file and then you don’t have to specify what file to use, this is not recommended. Instead you should use one of the methods below
Specify inline#
When you run any kubectl
commands you can specify the kubeconfig to use inline by adding the --kubeconfig
flag like in the following example:
kubectl get nodes --kubeconfig ~/Downloads/siodine.yml
Export to KUBECONFIG
variable#
Another method to specify the kubeconfig is to specify the file location in an environment variable, specifically KUBECONFIG
. The following example shows how to export the variable (Unix) from the command line:
export KUBECONFIG=/home/user/Downloads/siodine.yml
kubectl get nodes
Note
The export command does not persist after a reboot. If you want the export to persist it should be added to the ~/.bashrc file or the equivalent for your environment. Remember to also run source ~/.bashrc
, or your environment equivalent, after to apply any changes. If you are unsure how to create persistent variables for your operating system please search the web on how to accomplish this.