如何使用 shell 计算 kubeconfig 文件中 k8s 中的 diff 集群数?
how to count num of diff clusters in k8s in kubeconfig file using shell?
文件是这样的:
- name: development
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
- name: kubernetes-on-aws
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
- name: test-cluster-1
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
contexts:
- name: test-user@development
context:
cluster: development
user: test-user
- name: aws-user@kubernetes-on-aws
context:
cluster: kubernetes-on-aws
user: aws-user
- name: test-user@production
context:
cluster: production
user: test-user
- name: research
context:
cluster: test-cluster-1
user: dev-user
users:
- name: test-user
user:
client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
client-key: /etc/kubernetes/pki/users/test-user/test-user.key
- name: dev-user
user:
client-certificate: /etc/kubernetes/pki/users/dev-user/developer-user.crt
client-key: /etc/kubernetes/pki/users/dev-user/dev-user.key
- name: aws-user
user:
client-certificate: /etc/kubernetes/pki/users/aws-user/aws-user.crt
client-key: /etc/kubernetes/pki/users/aws-user/aws-user.key
current-context: test-user@development
preferences: {}
我不想数有多少差异集群。如何用 shell cmd 来计算它?
cat config.txt > grep 'cluster' wc -l
不工作
您可以在 -o jsonpath
的帮助下使用 kubectl config view
命令列出集群名称:
$ kubectl config view -o jsonpath='{.clusters[*].name}'
cluster-1 cluster-2 cluster-3
数一数:
$ kubectl config view -o jsonpath='{.clusters[*].name}' | wc -w
3
您还可以使用 tr " " "\n"
命令美化您的输出,该命令将用换行符替换白色 space。
$ kubectl config view -o jsonpath='{.clusters[*].name}' | tr " " "\n"
cluster-1
cluster-2
cluster-3
这个很简单
kubectl config get-contexts | grep -v NAME | wc -l
文件是这样的:
- name: development
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
- name: kubernetes-on-aws
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
- name: test-cluster-1
cluster:
certificate-authority: /etc/kubernetes/pki/ca.crt
server: https://controlplane:6443
contexts:
- name: test-user@development
context:
cluster: development
user: test-user
- name: aws-user@kubernetes-on-aws
context:
cluster: kubernetes-on-aws
user: aws-user
- name: test-user@production
context:
cluster: production
user: test-user
- name: research
context:
cluster: test-cluster-1
user: dev-user
users:
- name: test-user
user:
client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
client-key: /etc/kubernetes/pki/users/test-user/test-user.key
- name: dev-user
user:
client-certificate: /etc/kubernetes/pki/users/dev-user/developer-user.crt
client-key: /etc/kubernetes/pki/users/dev-user/dev-user.key
- name: aws-user
user:
client-certificate: /etc/kubernetes/pki/users/aws-user/aws-user.crt
client-key: /etc/kubernetes/pki/users/aws-user/aws-user.key
current-context: test-user@development
preferences: {}
我不想数有多少差异集群。如何用 shell cmd 来计算它?
cat config.txt > grep 'cluster' wc -l
不工作
您可以在 -o jsonpath
的帮助下使用 kubectl config view
命令列出集群名称:
$ kubectl config view -o jsonpath='{.clusters[*].name}'
cluster-1 cluster-2 cluster-3
数一数:
$ kubectl config view -o jsonpath='{.clusters[*].name}' | wc -w
3
您还可以使用 tr " " "\n"
命令美化您的输出,该命令将用换行符替换白色 space。
$ kubectl config view -o jsonpath='{.clusters[*].name}' | tr " " "\n"
cluster-1
cluster-2
cluster-3
这个很简单
kubectl config get-contexts | grep -v NAME | wc -l