使用kubectl通过终端连接到k8s集群之后。可以设置要在那个命名空间下进行操作。

查看当前的工作上下文

kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://130.211.122.180
  name: lithe-cocoa-92103_kubernetes
contexts:
- context:
    cluster: lithe-cocoa-92103_kubernetes
    user: lithe-cocoa-92103_kubernetes
  name: lithe-cocoa-92103_kubernetes
current-context: lithe-cocoa-92103_kubernetes
kind: Config
preferences: {}
users:
- name: lithe-cocoa-92103_kubernetes
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: 65rZW78y8HbwXXtSXuUw9DbP4FLjHi4b
- name: lithe-cocoa-92103_kubernetes-basic-auth
  user:
    password: h5M0FtUUIflBSdI7
    username: admin

设置工作上下文,前提是该命名空间已经存在,如何创建命名空间可以参考

https://mp.csdn.net/postedit/83752311

 

kubectl config set-context prod --namespace=production --cluster=lithe-cocoa-92103_kubernetes --user=lithe-cocoa-92103_kubernetes

切换

kubectl config use-context pro

验证是否已经切换到了新的上下文

kubectl config current-context

 

 

参考官方文档:

https://k8smeetup.github.io/docs/tasks/administer-cluster/namespaces-walkthrough/

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐