[英]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.