合并kubeconfig JSON和YAML

时间:2020-08-05 14:16:16

标签: kubernetes kubectl kubeconfig

我有两个kubeconfigs文件,第一个是我用来与集群通信的文件,第二个是用于JSON格式的Aquasec的文件。如何合并这两个?

apiVersion: v1
    clusters:
    - cluster:
        certificate-authority-data: DATA+OMITTED
        server: https://656835E69F31E2933asdAFAKE3F5904sadFDDC112dsasa7.yld432.eu-west-2.eks.amazonaws.com
      name: arn:aws:eks:eu-west-2:test651666:cluster/Magento
    - cluster:
        certificate-authority-data: DATA+OMITTED
        server: https://kubernetes.docker.internal:6443
      name: docker-desktop
    - cluster:
        certificate-authority-data: DATA+OMITTED
        server: https://192.142.242.111:6443
      name: kubernetes
    contexts:
    - context:
        cluster: arn:aws:eks:eu-west-2:test651666:cluster/testing
        user: arn:aws:eks:eu-west-2:test651666:cluster/testing
      name: arn:aws:eks:eu-west-2:test651666:cluster/testing
    - context:
        cluster: docker-desktop
        user: docker-desktop
      name: docker-desktop
    - context:
        cluster: docker-desktop
        user: docker-desktop
      name: docker-for-desktop
    - context:
        cluster: kubernetes
        user: kubernetes-admin
      name: kubernetes-admin@kubernetes
    current-context: arn:aws:eks:eu-west-2:test651666:cluster/testing
    kind: Config
    preferences: {}
    users:
    - name: arn:aws:eks:eu-west-2:test651666:cluster/testing

1 个答案:

答案 0 :(得分:2)

您可以将KUBECONFIG环境变量设置为以:分隔的多个配置文件,而kubectl会自动将它们合并到幕后。

例如:

export KUBECONFIG=config:my-config.json

在上面的导出中,config~/.kube中包含的默认配置文件,而my-config.json是您的第二个配置文件,您说的是JSON格式。

您可以使用此命令查看合并的配置,该命令显示了kubectl当前正在使用的配置的统一视图:

kubectl config view

由于kubectl自动合并多个配置,因此您无需将合并的配置保存到文件中。但是,如果您确实想这样做,则可以重定向输出,如下所示:

kubectl config view > merged-config.yaml

编辑:显然,如果要将合并的输出保存到其自己的文件中,则需要使用--flatten,因此将是这样的:

kubectl config view --flatten > merged-config.yaml

请查看Mastering the KUBECONFIG fileOrganizing Cluster Access Using kubeconfig Files,以获得更多说明并查看其他示例。