kubernetes 合并kubeconfig JSON和YAML

zpgglvta  于 2023-02-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(185)

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

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
5m1hhzi4

5m1hhzi41#

您可以将KUBECONFIG环境变量设置为多个由:分隔的配置文件,kubectl将在后台自动合并它们。
例如:

export KUBECONFIG=config:my-config.json

在上面的导出中,config是包含在~/.kube中的默认配置文件,my-config.json是第二个配置文件,您说它是JSON格式的。
您可以使用以下命令查看合并后的配置,它显示了kubectl当前使用的配置的统一视图:

kubectl config view

因为kubectl会自动合并多个配置,所以你不需要把合并后的配置保存到一个文件中,但是如果你真的想这么做,你可以重定向输出,如下所示:

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

查看Mastering the KUBECONFIG fileOrganizing Cluster Access Using kubeconfig Files以获得更多解释并查看其他一些示例。

相关问题