【问题标题】:Is "current-context" a mandatory key in a kubeconfig file?“current-context”是 kubeconfig 文件中的强制键吗?
【发布时间】:2021-10-23 13:24:22
【问题描述】:

剧情:

我正在开发一个我们有 PROD 和 ITG 设置的 kubernetes 环境。 ITG 设置具有多集群环境,而 PROD 设置是单集群环境。 我正在尝试使用 Python 自动化一些过程,我必须处理 kubeconfig 文件,并且我正在使用 kubernetes 库。

问题:

PROD 的 kubeconfig 文件具有可用的“current-context”键,但在 ITG 的 kubeconfig 文件中缺少

prdconfig

apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster3.url.com:3600
  name: cluster-ABC
contexts:
- context:
    cluster: cluster-LMN
    user: cluster-user
  name: cluster-LMN-context
current-context: cluster-LMN-context
kind: Config
preferences: {}
users:
- name: cluster-user
  user:
    exec:
      command: kubectl
      apiVersion: <clientauth/version>
      args:
      - kubectl-custom-plugin
      - authenticate
      - https://cluster.url.com:8080
      - --hpecp-user=user
      - --hpecp-token=/api/v2/session/xxxx
      - --hpecp-token-expiry=1000000000
      - --force-reauth=false
      - --insecure-skip-tls-verify=true

itgconfig

apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster1.url.com:3600
  name: cluster-ABC
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster2.url.com:3601
  name: cluster-XYZ
contexts:
- context:
    cluster: cluster-ABC
    user: cluster-user
  name: cluster-ABC-context
- context:
    cluster: cluster-XYZ
    user: cluster-user
  name: cluster-XYZ-context
kind: Config
preferences: {}
users:
- name: cluster-user
  user:
    exec:
      command: kubectl
      apiVersion: <clientauth/version>
      args:
      - kubectl-custom-plugin
      - authenticate
      - https://cluster.url.com:8080
      - --hpecp-user=user
      - --hpecp-token=/api/v2/session/xxxx
      - --hpecp-token-expiry=1000000000
      - --force-reauth=false
      - --insecure-skip-tls-verify=true

当我尝试使用 config.load_kube_config(os.path.expanduser('~/.kube/prdconfig')) 为 PROD 加载 kubeconfig 文件时,它可以工作。

当我尝试使用 config.load_kube_config(os.path.expanduser('~/.kube/itgconfig')) 为 ITG 加载 kubeconfig 文件时,我收到以下错误:

ConfigException:无效的 kube-config 文件。预期键 C:\Users\chowdhsi/.kube/itgconfig 中的当前上下文

虽然从错误消息中可以清楚地看出它正在将 kubeconfig 文件视为 invalid,因为它没有“current-context”键。

子图:

使用 kubectl 时,缺少的“当前上下文”没有任何区别,因为我们始终可以在命令中指定上下文。但是 'load_kube_config()' 函数强制必须有“current-context”可用。

问题:

那么,“current-context”是 kubeconfig 文件中的强制键吗?

免责声明:

我对 Kubernetes 很陌生,使用它的经验很少。

【问题讨论】:

  • 您好,"当使用 kubectl 时,缺少的 "current-context" 没有任何区别,因为我们总是可以在命令中指定上下文",另外请提供您的itgconfig kubeconfig file的编辑输出
  • By, "在使用 kubectl 时,缺少的 "current-context" 没有任何区别,因为我们总是可以在命令中指定上下文",我的意思是我们总是可以像这样使用 kubectl 指定上下文:kubectl config --kubeconfig=config-demo use-context dev-frontend
  • 所以看起来有区别 "current-context:" 将默认应用为必填字段,并且要将 kubeconfig 与多个上下文一起使用,您必须指定哪个您将使用的上下文 - 是的,您可以使用此命令在上下文之间切换,但使用的上下文包含重要信息,如何连接到 api 服务器、应使用哪些凭据,因此有必要将 kubectl 指向正确的上下文。

标签: kubernetes kubernetes-cluster kubeconfig


【解决方案1】:

如 cmets 中所述: 如果我们想在默认情况下使用kubeconfig 文件开箱即用,对于使用kubectl 或python 脚本的特定集群,我们可以通过指定current-contextkubeconfig 文件中的一个上下文标记为默认值。

注意Context:

kubeconfig 文件 is used to group access parameters 中的一个 context 元素,使用方便的名称。每个上下文都有三个参数:集群、命名空间和用户。 By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster.

为了将我们的 kubeconfig 文件中的一个上下文(例如 dev-fronted)标记为默认上下文,请运行:

kubectl config use-context dev-fronted

现在,无论何时运行 kubectl 命令,该操作都会应用于集群和 dev-frontend 上下文中列出的命名空间。该命令将使用 dev-frontend 上下文中列出的用户凭据

请看:

- Mering kubeconfig files:

  1. 根据该链中的第一个命中确定要使用的上下文:

    使用 --context 命令行标志(如果存在)。 使用合并的 kubeconfig 文件中的当前上下文。

此时允许空上下文

  1. 确定集群和用户。在这一点上,可能有也可能没有上下文。根据该链中的第一个命中确定集群和用户,该链运行两次:一次用于用户,一次用于集群:

    使用命令行标志(如果存在):--user 或--cluster。 如果上下文非空,则从上下文中获取用户或集群。

此时用户和集群可以为空。

每当我们在没有指定 current-context 的情况下运行 kubectl 命令时,我们应该提供额外的配置参数来告诉 kubectl 使用哪个配置,在您的示例中可能是 f.e.:

kubectl --kubeconfig=/your_directory/itgconfig get pods --context cluster-ABC-context

如前所述 - 为了简化此任务,我们可以在 kubeconfig 文件配置中使用配置 current-context

kubectl config --kubeconfig=c/your_directory/itgconfig use-context cluster-ABC-context

进一步研究您的脚本生成的错误,我们应该注意到来自config/kube_config.py 的错误:

config/kube_config.py", line 257, in set_active_context context_name = self._config['current-context']

kubernetes.config.config_exception.ConfigException:: Invalid kube-config file. Expected key current-context in ...

这是一个带有附加 context="cluster-ABC-context" 参数的示例:

from kubernetes import client, config

config.load_kube_config(config_file='/example/data/merged/itgconfig', context="cluster-ABC-context")

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

...
Listing pods with their IPs:
10.200.xxx.xxx  kube-system coredns-558bd4d5db-qpzb8
192.168.xxx.xxx kube-system etcd-debian-test
...

附加信息

【讨论】:

  • 谢谢@Mark。很好解释。由于您的详细回答,我的大部分疑问都得到了解决。
猜你喜欢
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
相关资源
最近更新 更多