Skip to main content

Creating and Managing Services

1. Creating ClusterIP Service

Field Description Example Value
apiVersion API version to use for the service v1
kind Kind of the Kubernetes object Service
metadata.name Name of the service my-clusterip-service
spec.selector Label selector to identify pods {"app": "my-app"}
spec.ports.port Port to expose on the service 80
spec.ports.targetPort Port to forward traffic to on the pod 8080
spec.ports.protocol Protocol to use (TCP or UDP) TCP

Example: Creating a ClusterIP Service

apiVersion: v1
kind: Service
metadata:
  name: my-clusterip-service
spec:
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
Note: ClusterIP is the default service type in Kubernetes. It exposes the service on an internal IP within the cluster.

2. Creating NodePort Service

Field Description Example Value
apiVersion API version to use v1
kind Resource type Service
metadata.name Name of the service my-nodeport-service
spec.type Service type - must be NodePort NodePort
spec.ports.port Port exposed by the service 80
spec.ports.nodePort Optional: Port on the node to expose (range 30000-32767) 30080
spec.ports.targetPort Target port in the pods 8080
spec.selector Label selector to identify pods {"app": "my-app"}

Example: NodePort Service YAML

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30080
      targetPort: 8080
  selector:
    app: my-app
Note: The nodePort is optional; if omitted, Kubernetes assigns one from the valid range (30000-32767).

3. Creating LoadBalancer Service

Field Description Example
apiVersion API version of the Service resource v1
kind Kind of the Kubernetes resource Service
metadata.name Name of the Service my-loadbalancer-service
spec.type Type of Service to create. Must be LoadBalancer LoadBalancer
spec.selector Label selector to identify pods to route traffic to
{
  "app": "my-app"
}
spec.ports.port Port to expose externally 80
spec.ports.targetPort Internal port of the pod to route traffic to 8080
spec.ports.protocol Protocol to use (TCP or UDP) TCP

Example: Creating a LoadBalancer Service

apiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
Note: The external IP address will be assigned by the cloud provider or the Ingress controller once the service is created.

4. Creating Headless Service

Field Description Value
apiVersion API version v1
kind Resource type Service
metadata.name Service name my-headless-service
spec.clusterIP Cluster IP setting None
spec.selector Label selector for pods {app: web}
spec.ports.port Port to expose 80
spec.ports.protocol Protocol to use TCP

Example: Headless Service Definition

apiVersion: v1
kind: Service
metadata:
  name: my-headless-service
spec:
  clusterIP: None
  selector:
    app: web
  ports:
    - port: 80
      protocol: TCP
Note: Setting clusterIP: None makes the service headless, meaning it does not assign a cluster IP and instead returns the IPs of the pods directly.

5. Listing Services

Command Description Flags
kubectl get services List all services in the current namespace -o wide to show more details
kubectl get svc Short alias for get services --namespace <name> to target a specific namespace
kubectl get services -o yaml Get services in YAML format for detailed inspection --export to export without cluster-specific info
kubectl get services -o json Get services in JSON format for programmatic access --output json or -o json

Example: List all services with extended output

kubectl get services -o wide

Example: List services in a specific namespace

kubectl get svc --namespace production
Note: By default, kubectl get services lists only basic information like service name, type, cluster IP, external IP (if any), and age. Use -o wide or -o yaml for more details.

6. Describing Service Details

Property Description Example
metadata.name Name of the Service my-service
metadata.namespace Namespace where the Service is defined default
spec.selector Label selector to identify pods
{
  "app": "my-app"
}
spec.ports Port configuration for the Service
[
  {
    "port": 80,
    "targetPort": 8080
  }
]
spec.type Type of Service (ClusterIP, NodePort, LoadBalancer) ClusterIP
spec.clusterIP Cluster IP address assigned to the Service 10.96.123.45

Example: Describe Service Details

apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: default
spec:
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 8080
  type: ClusterIP
Note: The clusterIP is automatically assigned by the Kubernetes control plane when using ClusterIP type.

Service Types Overview

  • ClusterIP: Exposes the Service on an internal IP within the cluster.
  • NodePort: Exposes the Service on each Node's IP at a static port.
  • LoadBalancer: Exposes the Service externally using a cloud provider's load balancer.

7. Exposing Deployments

Service Type Description Use Case External Access
ClusterIP Default service type; exposes service internally within the cluster. Internal communication between pods and services. No
NodePort Exposes service on each Node's IP at a static port. Testing or development environments; simple external access. Yes
LoadBalancer Exposes service externally via cloud provider's load balancer. Production environments with external traffic. Yes
ExternalName Maps service to an external DNS name. Integrating with external services without direct access. Yes

Example: Exposing a Deployment with NodePort

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30080

Example: Exposing a Deployment with LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: my-app-service-lb
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
Note: For LoadBalancer, the cloud provider must support external load balancers (e.g., AWS, GCP).

8. Configuring Service Selectors

Field Description Example
selector Label selector used to identify pods to be included in the service "app": "frontend"
matchLabels Map of key-value pairs to match pod labels "environment": "production"
matchExpressions List of expressions to match pod labels with operators {"key": "version", "operator": "In", "values": ["v1", "v2"]}

Example: Basic Service with Label Selector

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Example: Service with MatchExpressions

apiVersion: v1
kind: Service
metadata:
  name: advanced-service
spec:
  selector:
    matchExpressions:
      - key: app
        operator: In
        values: [frontend, backend]
      - key: version
        operator: NotIn
        values: [v1]
  ports:
    - protocol: TCP
      port: 80
Note: The selector must match labels on at least one pod for the service to route traffic.

9. Configuring Session Affinity

Property Value Description
sessionAffinity None | ClientIP Controls whether the service should use session affinity. None means no session affinity, ClientIP uses client IP for affinity.
sessionAffinityConfig.clientIP.timeoutSeconds 10800 Timeout for client IP session affinity in seconds. Default is 10800 (3 hours).

Example: Service with ClientIP Session Affinity

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800

10. Setting External IPs

Field Description Example
externalIPs List of IP addresses for the service to advertise externally ["192.0.2.1", "203.0.113.5"]

Example: Service with External IPs

apiVersion: v1
kind: Service
metadata:
  name: external-ip-service
spec:
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  externalIPs:
    - 192.0.2.1
    - 203.0.113.5
Note: External IPs must be routable from the cluster network. Ensure they are accessible and properly configured in your network infrastructure.

11. Using ExternalName Services

Field Description Example
kind Must be Service Service
apiVersion Must be v1 v1
metadata.name Name of the service external-db
spec.type Must be ExternalName ExternalName
spec.externalName FQDN of the external service mydb.example.com

Example: Define an ExternalName Service

apiVersion: v1
kind: Service
metadata:
  name: external-db
spec:
  type: ExternalName
  externalName: mydb.example.com
Note: ExternalName services do not have selectors or ports. They are used to map a service name to an external DNS name.

12. Viewing Service Endpoints

Command Description Example
kubectl get endpoints Lists all endpoints in the cluster kubectl get endpoints
kubectl get endpoints <service-name> Displays endpoints for a specific service kubectl get endpoints my-service
kubectl describe endpoints <endpoint-name> Provides detailed information about a specific endpoint kubectl describe endpoints my-endpoint

Example: Get Endpoints for a Service

kubectl get endpoints my-app-service
NAME             ENDPOINTS                       AGE
my-app-service   10.244.0.6:8080,10.244.0.7:8080   5m

Was this article helpful?