1.kubeconfig文件

使用 kubeconfig 文件来组织有关集群、用户、命名空间和身份认证机制的信息。kubectl 命令行工具使用 kubeconfig 文件来查找选择集群所需的信息,并与集群的 API 服务器进行通信。

注:用于配置集群访问的文件称为 kubeconfig 文件。这是引用配置文件的通用方法。这并不意味着有一个名为 kubeconfig 的文件。

默认情况下,kubectl 在 $HOME/.kube 目录下查找名为 config 的文件。可以通过设置 KUBECONFIG 环境变量或者设置 --kubeconfig 参数来指定其他 kubeconfig 文件。

k8s集群可以通过namespace和context的设置来对不同的工作组进行区分,使它们既可以共享同一个Kubernetes集群的服务,也能够互不干扰。

 

2. 上下文(Context)

通过 kubeconfig 文件中的 context 元素,使用简便的名称来对访问参数进行分组。每个上下文都有三个参数:cluster、namespace 和 user。默认情况下,kubectl 命令行工具使用 当前上下文 中的参数与集群进行通信。

kubectl config 命令可以设置和使用context

$ kubectl config --help

查看kubectl config命令的使用说明

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 delimiting 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).


从以上我们看到kubectl config 命令可以设置、删除集群,设置、使用、删除上下文等操作。kubectl config view 用于显示合并的kubeconfig设置或指定的kubeconfig文件。

 

namespace和context示例:

创建development(开发)和production(生产)两个namespace和context

1)创建命名空间

namespace-development.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: development

namespace-production.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: production

$ kubectl create -f namespace-development.yaml

$ kubectl create -f namespace-production.yaml

查看集群中的命名空间

$ kubectl get namespaces

2)创建上下文

创建ctx-dev上下文,指定其命名空间为development

$ kubectl config set-context ctx-dev --namespace=development --cluster=kubernetes --user=kubernetes-admin

创建ctx-prod上下文,指定其命名空间为production

$ kubectl config set-context ctx-prod --namespace=production --cluster=kubernetes --user=kubernetes-admin

注:--cluster=kubernetes --user=kubernetes-admin,cluster和user这里使用k8s集群本身的cluster和user,这样直接使用集群的密钥进行认证

查看kubeconfig内容

$ kubectl config view

可以从显示的信息中看到目前集群中有3个上下文,ctx-dev、ctx-prod和kubernetes-admin@kubernetes (k8s集群默认的上下文)。

从current-context看到当前的上下文为:kubernetes-admin@kubernetes

上下文切换

$ kubectl config use-context ctx-dev

在当前上下文创建一个RC

redis-slave-controller.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      labels:
        name: redis-slave
    spec:
      containers:
      - name: slave
        image: kubeguide/guestbook-redis-slave
        ports:
        - containerPort: 6379

在当前上下文环境创建RC

$ kubectl create -f redis-slave-controller.yaml

查看对应的rc资源信息

$ kubectl get rc

切换到ctx-prod上下文

$ kubectl config use-context ctx-prod

查看当前上下文的资源信息

$ kubectl get rc

我们发现ctx-prod上下文环境中没有rc资源信息,所以在不同的上下文中,资源是相互隔离的,互不干扰。

 

context和kubeconfig的详细信息可参考官网:

https://kubernetes.io/zh/docs/concepts/configuration/organize-cluster-access-kubeconfig/#%E4%B8%8A%E4%B8%8B%E6%96%87-context

https://kubernetes.io/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable
————————————————
版权声明:本文为CSDN博主「思维的深度」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/skh2015java/article/details/108409458

posted on 2021-08-05 18:01  51core  阅读(445)  评论(0)    收藏  举报