简体   繁体   English

有没有办法从我使用 Kubernetes Python 客户端创建的服务中获取 cluster_ip?

[英]Is there a way to get the cluster_ip from a service i created using Kubernetes Python Client?

After creating a deployment with Kubernetes Python Client , and exposing the service with type ClusterIP, how can i get the Cluster-IP using the python client instead of using Kubectl command ?使用 Kubernetes Python 客户端创建部署并公开类型为 ClusterIP 的服务后,如何使用 python 客户端而不是使用 Kubectl 命令获取集群 IP?

Service was created based on the example code服务是基于示例代码创建的

def create_service():
core_v1_api = client.CoreV1Api()
body = client.V1Service(
    api_version="v1",
    kind="Service",
    metadata=client.V1ObjectMeta(
        name="service-example"
    ),
    spec=client.V1ServiceSpec(
        selector={"app": "deployment"},
        ports=[client.V1ServicePort(
            port=5678,
            target_port=5678
        )]
    )
)
# Creation of the Deployment in specified namespace
# (Can replace "default" with a namespace you may have created)
core_v1_api.create_namespaced_service(namespace="default", body=body) 

There is a command to list pod ips on the documentation有一个命令可以在文档中列出 pod ips

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))

im wondering if there's a way to do something similar with Cluster-IP from services.我想知道是否有办法对来自服务的 Cluster-IP 做类似的事情。

Simply get the service and read its spec.cluster_ip property:只需获取服务并读取其spec.cluster_ip属性:

from kubernetes import client, config
config.load_kube_config()
api = client.CoreV1Api()
service = api.read_namespaced_service(name="kubernetes", namespace="default")
print(service.spec.cluster_ip)
# 10.100.0.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要使用 kubernetes python 客户端在 Kubernetes 集群中获取 Pod 的数量 - I need to get number of Pods in a Kubernetes Cluster with kubernetes python client 使用 Kubernetes Python 客户端获取 Ingress 网关 IP 地址 - Get Ingress gateway IP address using Kubernetes Python client 我需要使用 kubernetes python 客户端在 Kubernetes 集群中获取 Pod 的资源使用情况 - I need to get resource usage of Pods in a Kubernetes Cluster with kubernetes python client 如何从 python 客户端扩展 Kubernetes 集群中的部署副本? - How can I scale deployment replicas in Kubernetes cluster from python client? Kubernetes 集群中使用 Python 套接字的服务器-客户端连接 - Server-client connection in Kubernetes cluster using Python socket 通过 Kubernetes Python 客户端访问 Kubernetes 中的服务 - Accessing a service in Kubernetes via the Kubernetes Python client Python 客户端,用于访问 GKE 上的 kubernetes 集群 - Python client for accessing kubernetes cluster on GKE 在部署在 Kubernetes 集群上的容器化 Python 应用程序中,我应该指向哪个 IP 地址? - What IP Address should I point to in my containerized Python application that is deployed on a Kubernetes cluster? "如何使用 python 获取 ECS 集群内实例的 IP 地址?" - How can I get the ip addresses of my instances inside the ECS Cluster using python? 如何减少 kubernetes-client-python 中 kubernetes 集群的重试次数 - How to reduce the retry count for kubernetes cluster in kubernetes-client-python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM