How to switch namespace in kubernetes

前端 未结 7 1410
半阙折子戏
半阙折子戏 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:28

    There are a few options:

    • Switch namespace only using the kubectl commands::
    kubectl config set-context --current --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
    

提交回复
热议问题