Configure Access to Multiple Clusters [page]deterministic
This page shows how to configure access to multiple clusters by using configuration files. After your clusters, users, and contexts are defined in one or more configuration files, you can quickly switch between clusters by using the `kubectl config use-context` command.
> Note: A file that is used to configure access to a cluster is sometimes called a *kubeconfig file*. This is a generic way of referring to configuration files. It does not mean that there is a file named `kubeconfig`.
Only use kubeconfig files from trusted sources. Using a specially-crafted kubeconfig file could result in malicious code execution or file exposure. If you must use an untrusted kubeconfig file, inspect it carefully first, much as you would a shell script.
##
To check that [kubectl](#gloss:kubectl) is installed, run `kubectl version --client`. The kubectl version should be [within one minor version](/releases/version-skew-policy/#kubectl) of your cluster's API server.
## Define clusters, users, and contexts
Suppose you have two clusters, one for development work and one for test work. In the `development` cluster, your frontend developers work in a namespace called `frontend`, and your storage developers work in a namespace called `storage`. In your `test` cluster, developers work in the default namespace, or they create auxiliary namespaces as they see fit. Access to the development cluster requires authentication by certificate. Access to the test cluster requires authentication by username and password.
Create a directory named `config-exercise`. In your `config-exercise` directory, create a file named `config-demo` with this content:
```yaml apiVersion: v1 kind: Config preferences: {}
clusters: - cluster: name: development - cluster: name: test
users: - name: developer - name: experimenter
contexts: - context: name: dev-frontend - context: name: dev-storage - context: name: exp-test ```
A configuration file describes clusters, users, and contexts. Your `config-demo` file has the framework to describe two clusters, two users, and three contexts.
Go to your `config-exercise` directory. Enter these commands to add cluster details to your configuration file:
```shell kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 --certificate-authority=fake-ca-file kubectl config --kubeconfig=config-demo set-cluster test --server=https://5.6.7.8 --insecure-skip-tls-verify ```
Add user details to your configuration file:
> Caution: Storing passwords in Kubernetes client config is risky. A better alternative would be to use a credential plugin and store them separately. See: [client-go credential plugins](/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins)
```shell kubectl config --kubeconfig=config-demo set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile kubectl config --kubeconfig=config-demo set-credentials experimenter --username=exp --password=some-password ```
> Note: - To delete a user you can run `kubectl --kubeconfig=config-demo config unset users.<name>` - To remove a cluster, you can run `kubectl --kubeconfig=config-demo config unset clusters.<name>` - To remove a context, you can run `kubectl --kubeconfig=config-demo config unset contexts.<name>`
Add context details to your configuration file:
```shell kubectl config --kubeconfig=config-demo set-context dev-frontend --cluster=development --namespace=frontend --user=developer kubectl config --kubeconfig=config-demo set-context dev-storage --cluster=development --namespace=storage --user=developer kubectl config --kubeconfig=config-demo set-context exp-test --cluster=test --namespace=default --user=experimenter ```
Open your `config-demo` file to see the added details. As an alternative to opening the `config-demo` file, you can use the `config view` command.
```shell kubectl config --kubeconfig=config-demo view ```
The output shows t …(trimmed)