Configuring kubectl for Minikube Access
Tags: kubectl, kubernetes, minikube, webdev
Published: 2024-04-12 09:41:47

I attempted to connect to Minikube running on an Ubuntu Desktop from my Mac. Here is how I did it.

When you're working with Kubernetes via Minikube, properly configuring kubectl—the Kubernetes command-line client—is crucial for managing your clusters. This guide will walk you through setting up kubectl to communicate with Minikube, focusing on specifying the server address, setting up authentication, and defining the active context.

## Step 1: Set the Cluster
To set the cluster details in kubectl, you need to specify the server's URL and provide the certificate authority (CA) data to ensure secure communication. This can be done using the following command:

```
kubectl config set-cluster minikube --server=https://192.168.49.2:8443 --certificate-authority=ca.crt --embed-certs=true
```

- `--server`: This is the URL to your Minikube Kubernetes API server.
- `--certificate-authority`: This is the path to the CA certificate, which kubectl uses to verify the server's certificate.
- `--embed-certs=true`: This option tells kubectl to embed the certificate data directly into the kubeconfig file, making the configuration portable.

**Obtaining the Certificate Authority File**
The `ca.crt` file is typically created when Minikube sets up its Kubernetes environment. You can find this file within the .minikube directory located in your home directory (e.g., `~/.minikube/ca.crt`).

## Step 2: Set the Credentials
Setting credentials involves specifying the client certificate and key files used to authenticate with Kubernetes:

```
kubectl config set-credentials minikube --client-certificate=client.crt --client-key=client.key --embed-certs=true
```

- `--client-certificate` and `--client-key`: These are paths to your client certificate and key files, respectively.
- `--embed-certs=true`: Similar to the cluster setup, this embeds the certificates in the kubeconfig file

**Locating Your Client Certificates**
The client.crt and client.key files are also typically located in the .minikube directory. You should check this directory for files named similarly, possibly under a certs subdirectory (e.g., `~/.minikube/profiles/minikube/client.crt`).

## Step 3: Set the Context
A context in kubectl ties together a cluster and its credentials. Set the context for Minikube using:

```
kubectl config set-context minikube --cluster=minikube --user=minikube
```

This command configures kubectl to use the specified cluster and user (credentials) for the 'minikube' context. You can switch to this context using:

```
kubectl config use-context minikube
```

This command activates the 'minikube' context, and all subsequent kubectl commands will interact with your Minikube cluster.

If you encounter issues connecting to Minikube on a different computer within the same network, refer to this previous article for solutions.

[How to Troubleshoot Connectivity Issues with Minikube on Docker](https://dev.to/venom90/how-to-troubleshoot-connectivity-issues-with-minikube-on-docker-16m5)

## Conclusion
Configuring kubectl with the correct cluster settings, credentials, and context is essential for managing your Kubernetes clusters efficiently. By setting these parameters carefully, you ensure that your interactions with Kubernetes via kubectl are secure and correctly targeted at your Minikube instance.

Once these settings are in place, you're ready to manage your Kubernetes cluster, deploy applications, and monitor their performance directly from your command line.