top | item 23881485

Kubectl – Configuration Guide

72 points| lukasbar | 5 years ago |knowledgepill.it | reply

27 comments

order
[+] kissgyorgy|5 years ago|reply
I find the kubectx and kubens tools invaluable for switching contexts and namespaces quickly: https://github.com/ahmetb/kubectx
[+] remram|5 years ago|reply
I've just been using this function in my bashrc:

    kubea(){
        if [ "$#" = 1 ]; then
            KUBECONFIG=~/.kube/configs/"$1"
        else
            ls -1 ~/.kube/configs/
        fi
    }
That way I can "activate" a specific config the same way I activate my Python virtualenv, for a specific terminal. I also add the basename of the config to my prompt.

    kubea myproject-PROD
[+] neximo64|5 years ago|reply
I've always been confused with kubectx. How do I actually add a seperate yaml file (say the one I download with my browser from DigitalOcean) into the context?
[+] echelon|5 years ago|reply
These are amazing! Thanks for sharing.
[+] mlthoughts2018|5 years ago|reply
My strong advice is do not ever rely on use-context or any similar abilities to change implicit state.

You should absolutely always be typing out huge, verbose commands like kubectl —context my-context -n my-namespace <some commands> ...

The opportunity for tragic error when you have implicit context or namespace settings applied is just too great.

[+] longcommonname|5 years ago|reply
Or you can signify the context using shell prompt and use kube rbac to give read only permissions instead of letting people mess with your prod cluster.
[+] rtempaccount1|5 years ago|reply
kube-ps1, kubectx and kubens can help quite a bit here. kube-ps1 to give you a visual indication of current context and the others to make switching easier.

You can also explicitly add namespaces to manifests, which help avoid applying to the wrong namespace.

[+] joeskyyy|5 years ago|reply
Agreed. With autocomplete I find myself being able to make those long commands painless while making sure I'm being explicit. Only took me one time running the wrong command against the wrong cluster to drill that one into my brain forever haha
[+] jaimehrubiks|5 years ago|reply
I like to have one config file per cluster and point kubeconfig to all these files. Then I use a bash function to select the cluster by name. My zsh shell previews the cluster name on the right when I start typing k, kubectl, helm and similar... So it is harder for me to make a mistake
[+] mastegizmo|5 years ago|reply
I think this is the Best method - it is quite safe, I’m doing the same
[+] jml78|5 years ago|reply
starship.rs will show your current context all the time. Personally I have that and the git plugins configured
[+] nunez|5 years ago|reply
Does anyone else hate that we have to deal with Kubeconfigs at all?
[+] freedomben|5 years ago|reply
It's a mixed bag. On one hand it's a pain when you have to switch back and forth. On the other hand it's awesome that all the state is in one, human-readable file and can easily be duplicated or pointed somewhere else, etc.

I primarily use OpenShift which fixes part of it, and with a few bash aliases the rest are pretty painless as well.

[+] pgwhalen|5 years ago|reply
I’m a little confused, what would the alternative be?
[+] ex3ndr|5 years ago|reply
Super annoying why ever have very specific way to merge multiple contexts. Google utils writes to this file one way, other tools another way. So weird and so not-transparent.