Kubernetes:如何从kubectl配置中删除集群和上下文?

xuo3flqw  于 2022-12-11  发布在  Kubernetes
关注(0)|答案(4)|浏览(270)

kubectl config view shows contexts and clusters corresponding to clusters that I have deleted.
How can I remove those entries?
The command

  1. kubectl config unset clusters

appears to delete all clusters. Is there a way to selectively delete cluster entries? What about contexts?

uajslkp6

uajslkp61#

kubectl config unset takes a dot-delimited path. You can delete cluster/context/user entries by name. E.g.

  1. kubectl config unset users.gke_project_zone_name
  2. kubectl config unset contexts.aws_cluster1-kubernetes
  3. kubectl config unset clusters.foobar-baz

Side note, if you teardown your cluster using cluster/kube-down.sh (or gcloud if you use Container Engine), it will delete the associated kubeconfig entries. There is also a planned kubectl configrework for a future release to make the commands more intuitive/usable/consistent.

cotxawn7

cotxawn72#

For clusters and contexts you can also do

  1. kubectl config delete-cluster my-cluster
  2. kubectl config delete-context my-cluster-context

There's nothing specific for users though, so you still have to do

  1. kubectl config unset users.my-cluster-admin
kmb7vmvb

kmb7vmvb3#

Run command below to get all contexts you have:

  1. $ kubectl config get-contexts
  2. CURRENT NAME CLUSTER AUTHINFO NAMESPACE
  3. * Cluster_Name_1 Cluster_1 clusterUser_resource-group_Cluster_1

Delete context:

  1. $ kubectl config delete-context Cluster_Name_1
dfddblmv

dfddblmv4#

与问题无关,但可能是一个有用的资源。
看一看kubectx + kubens: Power tools for kubectl
它们使切换上下文和命名空间变得很容易+可以选择删除

更改上下文:

  1. kubectx dev-cluster-01

更改名称空间:

  1. kubens dev-ns-01

删除上下文:

  1. kubectx -d my-context

相关问题