# Kubernetes cluster access

### Access kubernetes cluster from the server (ssh connection)

Kubernetes configuration file is stored on server at users home directory `~/.kube/config`

All user accounts added to inventory files (infrastructure/server-setup/inventory) already have kubernetes access.

Once you logged in on the server use `kubectl` utility to access cluster.

### Configure kubectl locally to work with remote cluster

{% hint style="info" %}
Kubernetes gives you an option to interact with cluster remotely using kubectl utility \[[1](https://kubernetes.io/docs/reference/kubectl/quick-reference/)]. You will need to have a Kubernetes cluster account to work remotely and configure kubectl to work with your remote cluster.
{% endhint %}

Transfer kubectl configuration file (`~/.kube/config` same as `/home/<username>/.kube/config`) from server to your laptop (or PC).

Example `scp` command: Command will transfer file from remote server 10.1.1.21 users bob home directory (`/home/bob/.kube/config`) into temporal directory (`/tmp/kubeconfig`). If ssh is running on non-default port, you may change it by add `-P <port number>`. Private key is defined as `-i ~/.ssh/id_rsa`

```
scp -P 22 -i ~/.ssh/id_rsa bob@10.1.1.21:~/.kube/config /tmp/kubeconfig
```

#### Option 1: Use the /tmp/kubeconfig temporarily

1. Open terminal window
2. Set environment variable:

   ```
   export KUBECONFIG=/tmp/kubeconfig
   ```
3. Run any command, e/g:

   ```
   kubectl get namespaces
   ```

#### Option 2: Make cluster connection your default kubeconfig

1. Check if you already have any kubeconfig on your laptop:

   ```
   ls -l ~/.kube/config
   ```
2. If you don't have any other clusters, you can run:

   ```
   mv /tmp/kubeconfig ~/.kube/config
   ```

{% hint style="info" %}
This will overwrite your existing default configuration. If you want to keep multiple cluster connections in the same file, you may need to add configuration manually, please search in google for more details.
{% endhint %}

#### Option 3: Add new cluster to your default kubeconfig

If you already have `~/.kube/config` on your laptop run following command to append new cluster to your existing configuration:

```
KUBECONFIG=~/.kube/config:/tmp/kubeconfig kubectl config view --merge --flatten > /tmp/merged.kubeconfig
cp ~/.kube/config ~/.kube/config.$(date +%F)
mv /tmp/merged.kubeconfig ~/.kube/config
```

{% hint style="warning" %} Sometime cluster with the same context may already exist in your local configuration file, you may get error:

```
error: You must be logged in to the server (Unauthorized)
```

In that case backup kubernetes config file located on your laptop at `~/.kube/config` and remove it. Run `mv /tmp/kubeconfig ~/.kube/config` after. {% endhint %}### Connecting to cluster remotely

#### Verify cluster connection

1. Check existing clusters:

   ```
   kubectl config get-contexts
   ```

   Example output

   ```
   $ kubectl config get-contexts
   CURRENT   NAME                                     CLUSTER                                  AUTHINFO                                 NAMESPACE
             docker-desktop                           docker-desktop                           docker-desktop                           
             minikube                                 minikube                                 minikube                                 default
   *         bob@public-k8s-prod-k8s-master-01        public-k8s-prod-k8s-master-01            bob                                      opencrvs-prod
             bob@public-k8s-qa-k8s-server             public-k8s-qa-k8s-server                 bob                        
   ```
2. Change cluster context:

   ```
   kubectl config use-context bob@public-k8s-qa-k8s-server
   ```

   Example output:

   ```
   Switched to context "bob@public-k8s-qa-k8s-server".
   ```

## Links

\[1] <https://kubernetes.io/docs/reference/kubectl/quick-reference/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.opencrvs.org/v2.0/technical/guides/installation/advanced-topics/kubernetes-cluster-access.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
