How to switch namespace in kubernetes

前端 未结 7 1406
半阙折子戏
半阙折子戏 2021-01-30 02:42

Say, I have two namespaces k8s-app1 and k8s-app2

I can list all pods from specific namespace using the below command

kubectl get pods -n 

        
相关标签:
7条回答
  • 2021-01-30 03:17

    You could use the following package called kubectx which make it very easy to switch between clusters using kubectx

    and switching between namespaces using kubens

    0 讨论(0)
  • 2021-01-30 03:19

    I like my answers short, to the point and with references to official documentation:

    Answer:

    kubectl config set-context --current --namespace=my-namespace
    

    From:

    https://kubernetes.io/docs/reference/kubectl/cheatsheet/

    # permanently save the namespace for all subsequent kubectl commands in that context.
    kubectl config set-context --current --namespace=ggckad-s2
    
    0 讨论(0)
  • 2021-01-30 03:19

    I didn't like kubectx and kubens because they are adding one more letter for bash-complection to kubectl command.

    So I just wrote tiny kubectl-use plugin:

    # kubectl use prod
    Switched to context "prod".
    
    # kubectl use default
    Switched to namespace "default".
    
    # kubectl use stage kube-system
    Switched to context "stage".
    Switched to namespace "kube-system".
    

    If you interesting to it, check https://github.com/kvaps/kubectl-use

    0 讨论(0)
  • 2021-01-30 03:20

    I was able to switch namespace using the below steps

    kubectl config set-context $(kubectl config current-context) --namespace=<namespace>
    kubectl config view | grep namespace
    kubectl get pods
    

    This is how i have tested

    # Create namespaces k8s-app1, k8s-app2 and k8s-app3
    master $ kubectl create ns k8s-app1
    namespace/k8s-app1 created
    master $ kubectl create ns k8s-app2
    namespace/k8s-app2 created
    master $ kubectl create ns k8s-app3
    namespace/k8s-app3 created
    
    # Create Service Account app1-sa in k8s-app1
    # Service Account app2-sa in k8s-app2
    # Service Account app3-sa in k8s-app3
    master $ kubectl create sa app1-sa -n k8s-app1
    serviceaccount/app1-sa created
    master $ kubectl create sa app2-sa -n k8s-app2
    serviceaccount/app2-sa created
    master $ kubectl create sa app3-sa -n k8s-app3
    serviceaccount/app3-sa created
    
    # Switch namespace
    master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app1
    Context "kubernetes-admin@kubernetes" modified.
    master $ kubectl config view | grep namespace
        namespace: k8s-app1
    master $ kubectl get sa
    NAME      SECRETS   AGE
    app1-sa   1         1m
    default   1         6m
    master $
    master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app2
    Context "kubernetes-admin@kubernetes" modified.
    master $ kubectl get sa
    NAME      SECRETS   AGE
    app2-sa   1         2m
    default   1         7m
    master $
    master $ kubectl config set-context $(kubectl config current-context) --namespace=k8s-app3
    Context "kubernetes-admin@kubernetes" modified.
    master $ kubectl get sa
    NAME      SECRETS   AGE
    app3-sa   1         2m
    default   1         7m
    
    0 讨论(0)
  • 2021-01-30 03:25

    A Solution

    npm install -g k8ss
    
    k8ss switch --namespace=your_namespace
    kubectl get pods
    

    TLDR; Explanation as requested

    There is a npm package called k8ss which stands for K8S Switching between clusters and namespaces.

    The full usage is

    k8ss switch --cluster=your_new_cluster --namespace=your_new_namespace
    

    As in your case, you only need to switch namespace, so you can use the command without any configuration (as you already put a config file in the ~/.kube/config).

    Advanced Usage

    If you need to switch between different clusters then you need to put multiple config files in your home directory. In this case you can go to the package README to learn more.

    0 讨论(0)
  • 2021-01-30 03:28

    There are a few options:

    • Switch namespace only using the kubectl commands::
    kubectl config set-context --current --namespace=<namespace>
    
    • Or, Create a new context with namespace defined:
    kubectl config set-context gce-dev --user=cluster-admin --namespace=dev
    kubectl config use-context gce-dev
    
    • Or, Use addons, like kubectx & kubens, the below command will switch the context to kube-system:
    $ kubens kube-system 
    
    • Or, Another easy alternative that I like without installing third party tools, is using bash alias(linux).
    $ alias kubens='kubectl config set-context --current --namespace '
    $ alias kubectx='kubectl config use-context '
    
    // Usage
    $ kubens kube-system    //Switch to a different namespace
    $ kubectx docker        //Switch to separate context
    
    0 讨论(0)
提交回复
热议问题