doctl Verified current stable Not installed? Kubernetes

Doctl / Fetch And Save Kubeconfig

Fetch And Save Kubeconfig

Fetches and saves the kubeconfig for a specified Kubernetes cluster.

$
Terminal
doctl k c cfg s <cluster_name>

When To Use

Before deploying applications directly to a Kubernetes cluster or integrating with CI/CD pipelines.

Pro Tip

Use the `--overwrite` flag to ensure existing kubeconfig entries are replaced without raising errors.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
doctl k c cfg s <cluster_name>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Fetching kubeconfig for cluster 'my-cluster'...

Kubeconfig saved to: /home/user/.kube/config

Cluster Name: my-cluster
Context: my-cluster-context

$ kubectl config get-contexts

CURRENT   NAME                   CLUSTER              AUTHINFO              NAMESPACE
*         my-cluster-context    my-cluster          my-cluster-admin     default

Anatomy of Output

Understanding the result

apiVersion: v1 Api Version

Indicates the version of the Kubernetes API.

clusters: Clusters

Lists the Kubernetes clusters available in the kubeconfig.

- cluster: Cluster Entry

Details of the specified cluster.

server: https://<cluster_endpoint> Cluster Endpoint

The API server endpoint for the cluster.

Troubleshooting

Common pitfalls

Error: You do not have access to the requested cluster.

Solution: Ensure you have the correct permissions set on the DigitalOcean dashboard.

Error: Failed to retrieve kubeconfig for cluster: Not Found.

Solution: Validate the cluster name; it must match an existing cluster.

Error: kubeconfig already exists and overwriting is disabled.

Solution: Use the --overwrite flag to enforce overwriting.

Command Breakdown

What each part is doing

doctl
Base Command
The executable that performs this operation. Here it runs Doctl before the shell applies any redirect operators.
k
k|kubernetes
The value supplied for k|kubernetes.
c
c|cluster
The value supplied for c|cluster.
cfg
cfg|kubeconfig
The value supplied for cfg|kubeconfig.
s
s|save
The value supplied for s|save.
<cluster_name>
cluster name
The value supplied for cluster name.

How To Run

Execution path

  1. Step 1

    Run `doctl kubernetes cluster cfg kubeconfig save my-cluster` to fetch the config.

  2. Step 2

    Verify the saved kubeconfig with `kubectl config get-contexts`.

Alternative Approaches

Comparable commands in other tools

Alternative kubernetes tools for the same job.