美文网首页
Kubectx and Kubens

Kubectx and Kubens

作者: 海胆阶段 | 来源:发表于2019-02-18 14:35 被阅读12次

    I want to introduce you two useful tools for kubernetes development, github link with detail.

    Note: this is not an official Google product.

    kubectx

    kubectx helps you switch between clusters back and forth

    install

    since kubectx/kubens are written in Bash, you should be able to install them to any POSIX environment that has Bash installed.

    git clone https://github.com/ahmetb/kubectx /opt/kubectx
    

    make sure kubectx script is executable:

    [root@myk8s1 ~] ls /opt/kubectx/ -ltr
    total 40
    -rw-r--r-- 1 root root 11357 Feb 17 21:25 LICENSE
    -rw-r--r-- 1 root root   968 Feb 17 21:25 CONTRIBUTING.md
    -rw-r--r-- 1 root root  7784 Feb 17 21:25 README.md
    drwxr-xr-x 2 root root   121 Feb 17 21:25 completion
    drwxr-xr-x 2 root root    84 Feb 17 21:25 img
    drwxr-xr-x 3 root root   100 Feb 17 21:25 test
    -rwxr-xr-x 1 root root  5273 Feb 17 21:25 kubens
    -rwxr-xr-x 1 root root  5218 Feb 17 21:25 kubectx
    

    create symlinks to kubectx/kubens from somewhere in your PATH, like /usr/local/bin

    ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
    

    usage

    you should first understand how to switch among different clusters by using configuration files. please investigate this link, actually kubectx is built on top of it.

    for example, I have one cluster on AWS and one cluster on Fyre, in each cluster there is a ~/.kube/config file, rename it as config.aws and config.fyre and put them to another client machine ~/.kube/ folder with kubectl installed.

    [root@centctl1 .kube]# ls -ltr
    total 16
    drwxr-xr-x 3 root root   23 Nov 26 16:38 cache
    -rw-r--r-- 1 root root 2214 Dec  6 10:16 config.aws
    drwxr-xr-x 2 root root   73 Dec  6 10:16 kubens
    drwxr-xr-x 3 root root 4096 Feb 17 22:05 http-cache
    -rw------- 1 root root 5474 Feb 17 22:22 config.fyre
    

    append config files to environment variable KUBECONFIG

    export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config.aws:$HOME/.kube/config.fyre
    

    now if you run kubectx you will see there are 2 contexts

    [root@centctl1 .kube] kubectx
    arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster
    kubernetes-admin@kubernetes
    

    switch to kubernetes-admin@kubernetes

    [root@centctl1 .kube] kubectx kubernetes-admin@kubernetes
    Switched to context "kubernetes-admin@kubernetes".
    

    switch back

    [root@centctl1 .kube] kubectx -
    Switched to context "arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster".
    

    actually the effect is the same as

    kubectl config view
    kubectl config --kubeconfig=config.fyre use-context kubernetes-admin@kubernetes
    

    kubens

    kubens helps you switch between Kubernetes namespaces smoothly, so you don't need to add -n <namespace> in every command.

    install

    download the same Github repository as kubectx, add symlinks:

    ln -s /opt/kubectx/kubens /usr/local/bin/kubens
    

    usage

    [root@myk8s1 ~] kubens
    default
    kube-public
    kube-system
    [root@myk8s1 ~] kubens kube-system
    Context "kubernetes-admin@kubernetes" modified.
    Active namespace is "kube-system".
    [root@myk8s1 ~] kubens -
    Context "kubernetes-admin@kubernetes" modified.
    Active namespace is "default".
    
    

    相关文章

      网友评论

          本文标题:Kubectx and Kubens

          本文链接:https://www.haomeiwen.com/subject/jaqaeqtx.html