K8S Context和Namespace管理工具kubectx/kubens
一、命名空间介绍
Kubernetes 支持多个虚拟集群,它们底层依赖于同一个物理集群。这些虚拟集群被称为命名空间。
Kubernetes三个初始名称空间:
(1)default 没有其他名称空间的对象的默认名称空间。
(2)kube-system Kubernetes系统创建的对象的名称空间。
(3)kube-public该名称空间是自动创建的,并且对所有用户(包括未经身份验证的用户)可读。此名称空间主要保留给集群使用,以防某些资源在整个集群中公开可见。此名称空间的公共方面仅是约定,不是要求。
二、Kubectx/kubens工具介绍
工具地址和介绍:https://github.com/ahmetb/kubectx
2.1 context
官方文档解析:https://kubernetes.io/zh/docs/concepts/configuration/organize-cluster-access-kubeconfig/
通过 kubeconfig 文件中的 context 元素,使用简便的名称来对访问参数进行分组。每个上下文都有三个参数:cluster、namespace 和 user。默认情况下,kubectl 命令行工具使用 当前上下文 中的参数与集群进行通信。kubectl子命令config的三元组:集群(set-cluster)、用户(set-credentials)和配置上下文(set-context)实现切换。
# 集群初始配置 [root@10-25-38-209 ~]# kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://192.168.0.153:6443 name: kubernetes contexts: - context: cluster: kubernetes namespace: default user: user-admin name: kubernetes current-context: kubernetes kind: Config preferences: {} users: - name: user-admin user: token: XXXXXX
(1)创建cluster
[root@10-25-38-209 ~]# kubectl config set-cluster starcto-cluster --server=https://192.168.0.153:6443 --insecure-skip-tls-verify=true 集群名称
(2)创建user
[root@10-25-38-209 ~]# kubectl config set-credentials starcto-user --username=stargao --password=password 用户 用户名 密码
( 3)创建context
[root@10-25-38-209 ~]# kubectl config set-context starcto-context --cluster=starcto-cluster --namespace=default --user=starcto-user context上下文名称 集群名称 命名空间 用户
(4)指定当前使用的context
[root@10-25-38-209 ~]# kubectl config use-context starcto-context context上下文名称
(5)查看所有context
[root@10-25-38-209 ~]# kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE kubernetes kubernetes user-admin default * starcto-context starcto-cluster starcto-user default
(6)查看修改后的配置
[root@10-25-38-209 ~]# kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://192.168.0.153:6443 name: kubernetes - cluster: # 新增1 insecure-skip-tls-verify: true server: https://192.168.0.153:6443 name: starcto-cluster contexts: - context: cluster: kubernetes namespace: default user: user-admin name: kubernetes - context: # 新增2 cluster: starcto-cluster namespace: default user: starcto-user name: starcto-context current-context: starcto-context kind: Config preferences: {} users: - name: starcto-user # 新增3 user: password: password username: stargao - name: user-admin user: token: XXXXX
(7)reference
https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
【注】完成上述配置才能通过kubens正常切换namespace,否则会报错如下:
(8)其它用法
[root@10-25-38-209 ~]# kubectl config --help Modify kubeconfig files using subcommands like "kubectl config set current-context my-context" The loading order follows these rules: 1. If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place. 2. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimitting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list. 3. Otherwise, ${HOME}/.kube/config is used and no merging takes place. Available Commands: current-context Displays the current-context delete-cluster Delete the specified cluster from the kubeconfig delete-context Delete the specified context from the kubeconfig get-clusters Display clusters defined in the kubeconfig get-contexts Describe one or many contexts rename-context Renames a context from the kubeconfig file. set Sets an individual value in a kubeconfig file set-cluster Sets a cluster entry in kubeconfig set-context Sets a context entry in kubeconfig set-credentials Sets a user entry in kubeconfig unset Unsets an individual value in a kubeconfig file use-context Sets the current-context in a kubeconfig file view Display merged kubeconfig settings or a specified kubeconfig file Usage: kubectl config SUBCOMMAND [options] Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
(9)删除操作
# 删除集群 [root@10-25-38-209 ~]# kubectl config delete-cluster starcto-cluster deleted cluster starcto-cluster from /root/.kube/config # 删除上下文 [root@10-25-38-209 ~]# kubectl config delete-context starcto-context deleted context starcto-context from /root/.kube/config # 删除用户 [root@10-25-38-209 ~]# kubectl config unset users.starcto Property "users.starcto" unset.
2.2 NameSpace与context介绍
K8S集群可以通过namespace和context的设置来对不同的工作组进行区分,使它们既可以共享同一个Kubernetes集群的服务,也能够互不干扰。即,当kubernetes集群中存在多租户的情况下,就需要有一种机制实现每个租户的资源隔离。而namespace的目的就是为了实现资源隔离。
(1)创建命名空间
kubectl create namespace stargao
(2)查看命令空间
kubectl get namespace
(3)删除命名空间
kubectl delete namespaces stargao
三、Kubectx/kubens工具安装与使用
3.1 工具安装
yum install git git clone https://github.com/ahmetb/kubectx cp kubectx/kube* /usr/local/bin/
3.2 使用kubectx快速切换
(1)列出全部context
kubectx
(2)切换到指定context
kubectx context_name
(3)可以过以下命令在最近使用过的2个context快速切换
kubectx -
3.3 使用kubens快速切换namespace
(1)列出全部namespace
kubens
(2)切换到指定namespace
kubens namespace
(3)可以过以下命令在最近使用过的2个namespace快速切换
kubens -
作者:UStarGao
链接:https://www.starcto.com/k8s/63.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2023-08-18Linux 数据盘盘符变化导致启动异常
- 2021-02-17搭建图形化容器镜像仓库-harbor
- 2021-08-25Docker镜像逆向工程-镜像分析
- 2021-09-20MySQL半同步复制与刷盘策略
- 2021-08-21开源IP地址管理系统phpipam部署