kubectl config set-context
# kubectl config set-context
设置集群中的某个上下文。
# 该命令可用于切换命名空间
参数选项:
--current
:在当前集群内进行设置。--namespace
:指定命名空间。
kubectl config set-context --current --namespace <目标命名空间>
# 示例
kubectl config set-context --current --namespace ns1
1
2
3
4
2
3
4
# 小技巧
你可以在~/.bashrc
文件中设置短命令:
# ~/.bashrc
alias kn="kubectl config set-context --current --namespace "
1
2
2
然后更新配置文件:
source ~/.bashrc
1
在这之后,你可以快速更换命名空间:
kn <目标命名空间>
# 示例
kn ns1
1
2
3
4
2
3
4
编辑 (opens new window)