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 ?
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
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.
CodePudding user response:
Simply get the service and read its spec.cluster_ip
property:
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