Lin Du
Lin Du

Reputation: 102497

How to do authentication when create new context for the cluster of standalone k8s server which docker desktop includes

I am using the standalone Kubernetes server and client that docker desktop includes.

I created two namespaces for k8s named: development and production.

☁  kubernetes-labs [master] ⚡  k get namespace
NAME              STATUS   AGE
default           Active   3d22h
development       Active   2d23h
kube-node-lease   Active   3d23h
kube-public       Active   3d23h
kube-system       Active   3d23h
production        Active   5m1s

Then, set a new cluster named kubernetes-labs:

☁  kubernetes-labs [master] ⚡  k config set-cluster kubernetes-labs --server=https://kubernetes.docker.internal:6443
Cluster "kubernetes-labs" set.

As you can see, the new cluster's server point to https://kubernetes.docker.internal:6443 which is used by the standalone Kubernetes server.

Next, created two contexts:

☁  kubernetes-labs [master] ⚡  kubectl config set-context kubernetes-labs-dev --cluster=kubernetes-labs --namespace=development --user=dev
Context "kubernetes-labs-dev" modified.
☁  kubernetes-labs [master] ⚡  kubectl config set-context kubernetes-labs-prod --cluster=kubernetes-labs --namespace=production --user=prod
Context "kubernetes-labs-prod" created.

Switch to kubernetes-labs-dev context:

☁  kubernetes-labs [master] ⚡  k config use-context kubernetes-labs-dev
Switched to context "kubernetes-labs-dev".

Now, when I try to get pods from the current namespace:

☁  kubernetes-labs [master] ⚡  k get pods                                                                           
Please enter Username: dev
Please enter Password: 

Need an authentication, I don't know what username and password should be entered.

Besides, when I try to view the config used by the current context, got an error.

☁  kubernetes-labs [master] ⚡  k config view --minify=true    
error: cannot locate user dev

Upvotes: 1

Views: 219

Answers (1)

Wytrzymały Wiktor
Wytrzymały Wiktor

Reputation: 13888

In order to make it work you need to Configure Access to Multiple Clusters:

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.

You need to make sure that your configuration file is correct. A configuration file describes clusters, users, and contexts. Than, you can add users details to your configuration file, for example:

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

The same can be done with contexts, for example:

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-scratch --cluster=scratch --namespace=default --user=experimenter

and clusters, for example:

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 scratch --server=https://5.6.7.8 --insecure-skip-tls-verify

Bear in mind that you need to set the proper pathnames of the certificate files in your environment for your configuration file to work properly.

Also, remember that:

Each context is a triple (cluster, user, namespace). For example, the dev-frontend context says, "Use the credentials of the developer user to access the frontend namespace of the development cluster".

You can find more details and examples in the linked documentation. The step by step guide will make it easier for you to setup properly.

Upvotes: 1

Related Questions