Run Kubectl Commands Locally
Last updated
Last updated
If you wish to run kubectl commands from your local system, you need to have access to your cluster. Traditionally, the kubeconfig file (./kube/config
) helps you connect with the cluster from your local system.
Kubeconfig becomes painstakingly difficult to maintain especially when it comes to:
Granting or revoking access to the cluster for multiple people
Changing the permissions and subsequently the access token
Adding/Updating/Deleting the entries of cluster URLs and tokens
Keeping a record of multiple kubeconfig files
Devtron helps in reducing the challenges and simplifying the maintenance of kubeconfig file through:
Devtron's Proxy URL for Cluster - A standardized URL that you can use in place of your Kubernetes cluster URL.
Devtron's Access Token - A kubectl-compatible token which can be generated and centrally maintained from Global Configurations → Authorization → API tokens.
Prerequisite: An API token with necessary permissions for the user(s) to access the cluster.
If you are not a super-admin and can't generate a token yourself, you can find the session token (argocd.token) using the Developer Tools available in your web browser as shown below.
There are 2 methods of getting kubeconfig in your system:
In Resource Browser, hover on the cluster name and click the Get kubeconfig
icon.
Copy the commands and run them on your terminal.
Go to ~/.kube
folder on your local machine and open the config
file. Or you may create one with the following content:
Edit the following placeholders in the server
field and the token
field:
<devtron_host_name>
Hostname of the Devtron server
demo.devtron.ai
<cluster_name>
Name of the cluster (or cluster ID)
devtron-cluster
<devtron_token>
API token or session token
-
Test the connection to the cluster by running any kubectl command, e.g., kubectl get ns
or kubectl get po -A
Once the connection is successful, you may run any kubectl operations from your system.
Assume your applications are running in a Kubernetes cluster on cloud. Now, if you wish to test or debug them on your local machine, you can perform port forwarding. It creates a tunnel between a port on your machine and a port on a resource within your cluster. Therefore, you can access applications running inside the cluster as though they are running locally on your machine.
Once you have successfully connected to the cluster, you may run the port-forward command. Refer kubectl port-forward to see a few examples.