Question

how to use doctl and kubectl to use contexts

Hello, on my first attempt used the default context with doct auth init. Now I am using a different context. If I give:

 doctl auth init --context 3di
Using token for context 3di

Validating token... ✔

but then when I do kubectl get pods I get all sort of errors.

kubectl get pods
Error: Unable to initialize DigitalOcean API client: access token is required. (hint: run 'doctl auth init')
 13819 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: Get : getting credentials: exec: executable doctl failed with exit code 1"

So how do I tell kubectl to use the doctl initialized context?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 7, 2025
Accepted Answer

Hey Hoygen! 👋

You’re absolutely correct! To make kubectl work with the desired context, you need to configure the kubeconfig to point to the correct Kubernetes cluster managed by DigitalOcean. Here’s a step-by-step summary:

  1. Run this command to get the list of clusters and their IDs:

    doctl kubernetes cluster list
    
  2. Use this command to save the kubeconfig for your cluster and set the current context:

    doctl kubernetes cluster kubeconfig save --set-current-context CLUSTER_ID
    

    This ensures kubectl uses the correct credentials and context when interacting with your cluster.

  3. To double-check the context is set correctly, run:

    kubectl config current-context
    

    You should see the context name associated with your cluster.

  4. Finally, test your connection with:

    kubectl get pods
    

Let me know if you run into any other issues!

- Bobby

ok I think I figured out, but eventually I’ll gladly accept more answers. at first I had to find the desired cluster id:

doctl kubernetes cluster list

and then I had to set the kubectl current context to that cluster ID

doctl kubernetes cluster kubeconfig save --set-current-context CLUSTER_ID

please correct me if I am wrong.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.