Microk8s: config set-context is not working

Created on 4 Feb 2019  路  9Comments  路  Source: ubuntu/microk8s

inspection-report-20190204_171917.tar.gz
Please run microk8s.inspect and attach the generated tarball to this issue.

When I run following command

$ microk8s.kubectl config set-context qqq-staging --kubeconfig="/home/www/admin.conf"

Context "qqq-staging" modified.

and running following does not list created context:

$ microk8s.kubectl config get-contexts
CURRENT   NAME       CLUSTER            AUTHINFO   NAMESPACE
*         microk8s   microk8s-cluster   admin

Am I missing something here, or is it a known issue?

Most helpful comment

Hi @reyou,

Here is what I tried. First I moved the kubeconfig file that you need to access MicroK8s in the default location that is also read/write:

microk8s.kubectl config view > ~/.kube/config

Then I made sure I have a kubectl (other than the kubectl you get with microk8s):

sudo snap install kubectl --classic

At this point you should be able to access the MicroK8s cluster with kubectl. For example:

> kubectl get no
NAME               STATUS   ROLES    AGE    VERSION
jackal-vgn-fz11m   Ready    <none>   8m2s   v1.13.2

Adding the context with kubectl now works:

> kubectl config set-context qqq-staging 
Context "qqq-staging" created.

> kubectl config get-contexts
CURRENT   NAME          CLUSTER            AUTHINFO   NAMESPACE
*         microk8s      microk8s-cluster   admin      
          qqq-staging                             

microk8s.kubectl uses always the kubeconfig of microk8s stored on a read only location this is why you need a non-microk8s kubectl.

All 9 comments

You trying to add a context on a file (/home/www/admin.conf) then ask context on default location (well the microk8s default location which is a read only file /snap/microk8s/current/client.config )
So, no mystery here: it will never return your context.

If you want to manage multiple contexts, you should do the opposite: extract the context of microk8s, add it to your main context file (~/.kube/config ) and use a standard kubectl

@GregoireW thanks for the insights, let me try this one.

@GregoireW I tried steps you mentioned, and this is what I have;

I am thinking following kubectl installed by conjure-up/microk8s wizard:

$ which kubectl
/snap/bin/kubectl
$ cat ~/.kube/config 

{myAdminConfigContentHere}



md5-09c4bc80f868c2958c9fc47ad0700a35



$ kubectl config view

apiVersion: v1
clusters:
- cluster:
    server: http://127.0.0.1:8080
  name: microk8s-cluster
contexts:
- context:
    cluster: microk8s-cluster
    user: admin
  name: microk8s
current-context: microk8s
kind: Config
preferences: {}
users:
- name: admin
  user:
    username: admin

Do you have an alias on kubectl ? alias kubectl ?
Do you have a KUBECONFIG environment variable ? echo $KUBECONFIG

Else... hum... I've no clue.

@GregoireW let me try those as well, thanks for the walkthrough :)
If those not work as well, I will try to install minukube instead of microk8s, maybe it will help.

Hi @reyou,

Here is what I tried. First I moved the kubeconfig file that you need to access MicroK8s in the default location that is also read/write:

microk8s.kubectl config view > ~/.kube/config

Then I made sure I have a kubectl (other than the kubectl you get with microk8s):

sudo snap install kubectl --classic

At this point you should be able to access the MicroK8s cluster with kubectl. For example:

> kubectl get no
NAME               STATUS   ROLES    AGE    VERSION
jackal-vgn-fz11m   Ready    <none>   8m2s   v1.13.2

Adding the context with kubectl now works:

> kubectl config set-context qqq-staging 
Context "qqq-staging" created.

> kubectl config get-contexts
CURRENT   NAME          CLUSTER            AUTHINFO   NAMESPACE
*         microk8s      microk8s-cluster   admin      
          qqq-staging                             

microk8s.kubectl uses always the kubeconfig of microk8s stored on a read only location this is why you need a non-microk8s kubectl.

If you always specify --kubeconfig=${KUBECONFIG} you will get consistent results. If that var is set microk8s.kubectl should always use it, but as per #259 this is not happening.

You could try use an alias, shell script or shell function to replace microk8s.kubectl with microk8s.kubectl --kubeconfig=${KUBECONFIG} "$@" and it should circumvent the issue as long as $KUBECONFIG is set to a regular file in a writable location.

Hello @ktsakalozos,

Thanks for your help.
Once I try to run following command:

sudo snap install kubectl --classic

I am getting following error as mentioned here https://github.com/ubuntu/microk8s/issues/20#issuecomment-390999351

sudo snap install kubectl --classic
error: cannot install "kubectl": snap "kubectl" command namespace conflicts with alias "kubectl"

Is there any easy way to fix this?

OK, I had to follow below;

https://blog.ubuntu.com/2017/01/28/ubuntu-core-how-to-enable-aliases-for-your-snaps-commands

snap aliases

snap unalias kubectl
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xrpn picture xrpn  路  4Comments

jareks picture jareks  路  3Comments

singram picture singram  路  4Comments

linxuyalun picture linxuyalun  路  3Comments

khteh picture khteh  路  5Comments