Overview
TheService class represents a Kubernetes Service - a named abstraction of software service consisting of a local port that the proxy listens on and a selector that determines which pods answer requests sent through the proxy.
Class Definition
v1
Constructor
Service accepts all parameters from NamespacedResource, plus:bool | None
Defines if NodePorts will be automatically allocated for services with type LoadBalancer. Default is true.
str | None
IP address of the service. Usually assigned randomly. Setting to “None” creates a headless service.
list[Any] | None
List of IP addresses assigned to this service (for dual-stack support)
list[Any] | None
List of IP addresses for which nodes in the cluster will accept traffic
str | None
External reference for ExternalName type services (e.g., a DNS CNAME record)
str | None
How nodes distribute service traffic. Values:
Cluster, Localint | None
Health check nodePort for LoadBalancer services with Local externalTrafficPolicy
str | None
How nodes distribute service traffic on ClusterIP. Values:
Cluster, Locallist[Any] | None
List of IP families (e.g., IPv4, IPv6) assigned to this service
str | None
Dual-stack policy. Values:
SingleStack, PreferDualStack, RequireDualStackstr | None
Class of the load balancer implementation this Service belongs to
str | None
LoadBalancer IP when type is LoadBalancer (deprecated)
list[Any] | None
Restrict traffic through cloud-provider load-balancer to specified client IPs
list[Any] | None
List of ports exposed by this service. Each port should be a dict with
port, targetPort, protocol, etc.bool | None
Indicates that endpoints should include not-ready addresses. Used for StatefulSet headless services.
dict[str, Any] | None
Route service traffic to pods with matching label keys and values
str | None
Session affinity. Values:
ClientIP, None. Defaults to None.dict[str, Any] | None
Configuration for session affinity
str | None
Express preferences for how traffic is distributed to Service endpoints
str | None
Service type. Values:
ClusterIP, NodePort, LoadBalancer, ExternalName. Defaults to ClusterIP.Inherited Methods
Service inherits all methods from NamespacedResource and Resource.Examples
Creating a ClusterIP Service
Creating a NodePort Service
Creating a LoadBalancer Service
Creating a Headless Service
Creating an ExternalName Service
Service with Multiple Ports
Service with Session Affinity
Service with External IPs
Listing Services
Using Context Manager
Creating from YAML
Service Types
ClusterIP (Default)
Exposes the service on a cluster-internal IP. The service is only reachable from within the cluster.NodePort
Exposes the service on each Node’s IP at a static port. A ClusterIP service is automatically created.LoadBalancer
Exposes the service externally using a cloud provider’s load balancer. NodePort and ClusterIP services are automatically created.ExternalName
Maps the service to an external DNS name. No proxying is set up.See Also
- NamespacedResource - Base class
- Pod - Container workloads
- Deployment - For managing pod replicas
- Ingress - For HTTP/HTTPS routing