Kubernetes 中端点的自定义列

问题描述 投票:0回答:2

我尝试从端点提取自定义列值以获取名称和 IP 地址,如下所示:

kubectl get endpoints -o custom-columns=NAME:.metadata.name,ENDPOINTS:subsets[0].addresses[0][0].ip

但是我没有得到任何结果,尝试了这个命令的不同组合。

端点格式:

Name: "mysvc",
 Subsets: [
   {
     Addresses: [{"ip": "10.10.1.1"}, {"ip": "10.10.2.2"}],
     Ports: [{"name": "a", "port": 8675}, {"name": "b", "port": 309}]
   },
   {
     Addresses: [{"ip": "10.10.3.3"}],
     Ports: [{"name": "a", "port": 93}, {"name": "b", "port": 76}]
   },
]
kubectl endpoint customcolumn
2个回答
0
投票

IIUC,你想要

metadata.name
和所有 (!?)
ip
的吗?

subset
包含一个数组
addresses
所以:

你不能

addresses[0][0]

但是你可以

addresses[0].ip

或者,如果您想要所有 IP,您可以

addresses[*].ip

kubectl get endpoints \
--output=custom-columns=NAME:.metadata.name,ENDPOINTS:subsets[0].addresses[].ip \
--all-namespaces

或者:

kubectl get endpoints \
--output=custom-columns=NAME:.metadata.name,ENDPOINTS:subsets[0].addresses[*].ip \
--all-namespaces

0
投票

如果有人正在搜索哪个入口属于哪个 Pod 的结果,则有代码:

import subprocess
import json
import pandas as pd


def get_kubectl_data(command):
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True)
    if result.returncode != 0:
        print(f"Error executing '{command}': {result.stderr}")
        return None
    try:
        return json.loads(result.stdout)
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON from '{command}': {e}")
        return None


def main():
    # Fetching Ingresses
    ingresses = get_kubectl_data("kubectl get ingress --all-namespaces -o json")

    # Parsing data
    data = []
    for ingress in ingresses['items']:
        ingress_name = ingress['metadata']['name']
        namespace = ingress['metadata']['namespace']
        ingress_address = ', '.join([addr.get('ip', '') or addr.get('hostname', '') for addr in
                                     ingress.get('status', {}).get('loadBalancer', {}).get('ingress', [])])

        for rule in ingress.get('spec', {}).get('rules', []):
            hostname = rule.get('host', 'N/A')  # Extract hostname
            for path in rule.get('http', {}).get('paths', []):
                service_name = path.get('backend', {}).get('service', {}).get('name')
                if service_name:
                    # Fetching pods for each service
                    selector_data = get_kubectl_data(f"kubectl get svc {service_name} -n {namespace} -o json")
                    if selector_data:
                        selector = selector_data.get('spec', {}).get('selector', {})
                        selector_str = ",".join(f"{k}={v}" for k, v in selector.items())
                        pods = get_kubectl_data(f"kubectl get pods -n {namespace} -l {selector_str} -o json")
                        if pods:
                            for pod in pods.get('items', []):
                                pod_name = pod['metadata']['name']
                                data.append([pod_name, service_name, ingress_name, ingress_address, hostname])
                        else:
                            print(f"Skipping pods for service {service_name} as they could not be fetched.")
                    else:
                        print(f"Skipping service {service_name} as it could not be fetched.")

    # Creating DataFrame
    df = pd.DataFrame(data, columns=['Pod', 'Service', 'Ingress', 'Ingress Address', 'Hostname'])

    # Writing to Excel
    df.to_excel('k8s_data_prod.xlsx', index=False)


if __name__ == "__main__":
    main()
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.