Kubernetes Services | Complete Guide (original) (raw)

Last Updated : 28 Apr, 2026

A Kubernetes Service is a stable networking abstraction that exposes a group of Pods as a single network endpoint. Pods in Kubernetes are ephemeral ; they can restart, crash, or move across nodes. Pod IP addresses are not permanent, which makes direct communication unreliable, applications in a cluster need a stable way to communicate with each other. Kubernetes Services solve this problem by providing:

virtual_network

Virtual Network

Types of Services

There are four major types of services which are having their advantages in their perspective as explained following.

1. ClusterIP

Example:

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP
selector:
app: nginx
ports:
- port: 80
targetPort: 80

Explanation:

Now we can create this service by applying the following command:

$ kubectl apply -f [file-name]

kubectl apply

2. NodePort Service (External Service)

Example:

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30000

Explanation:

Now we can create this service by applying the following command

$ kubectl apply -f [file-name]

3. LoadBalancer

Example:

apiVersion: v1
kind: Service
metadata:
name: javawebappsvc
spec:
type: LoadBalancer
selector:
app: javawebapp
ports:
- port: 80
targetPort: 8080

Explanation:

4. ExternalName

An ExternalName is a type of service in kubernetes that maps the service to a DNS name outside the cluster. It redirects the requests to the specified DNS name.

Example:

apiVersion: v1
kind: Service
metadata:
name: my-external-service
spec:
type: ExternalName
externalName: example.com

Explanation:

5. Headless Service

Example:

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

Explanation:

Kubernetes Service Working

Defining a Kubernetes Service

In the kubernetes cluster, there are no of objects which are having there own use cases and individual advantages. Same way service is part of objects which are present in kubernetes. To create an object or operate an object you can use the kubectl tool.

Example

apiVersion: v1
kind: Service
metadata:
name: deployment-service
spec:
selector:
Tomcat: deploymentapp
ports:
- protocol: TCP
port: 80
targetPort: 8080

In the above we are exposing the Tomcat application deployed in the kubernetes cluster with labels of Tomcat: deployment app port: 80 is the port exposed internally in the cluster and port 8080 is the port of the Tomcat application.

Port Definitions

Kubernetes Services define three important port fields:

Using Named Ports

Instead of using numeric targetPort, you can use a named container port.

Example:

kind: Pod
metadata:
name: tomcat
labels:
deployemnt: deployemnet-java
spec:
containers:


apiVersion: v1
kind: Service
metadata:
name:tomcat-service
spec:
selector:
deployemnt: deployemnet-java
ports:

For example, we can see that instead of using the "targetPortnumber" you can use the name of the container as mentioned below. The target port is the port number that the service or pod is forwarding requests to.

Services Without Selectors

This type of service will be exposed to all the pods in the kubernetes cluster. If the service is having selectors then that service will be exposed to only certain pods in the kubernetes cluster. We can use this special case of pods for different situations as the following:

Example

apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 8080

You can map the service to the required object by using the network address and port with the help of the Endpoint slice object manually.

EndpointSlices

Custom EndpointSlices

The custom name can be given to endpoint slices when you create a new endpoint slice. By using the following EndpointSlices you can create custom Endpoint Slices.

Example

apiVersion: discovery.k8s.io/v1beta1
kind: EndpointSlice
metadata:
name: my-custom-endpointslice
spec:
endpoints:

This manually connects a Service to an external IP.

Application Protocol

The application protocol is the field where you can mention the type of protocol that a specific service is listening to. The protocol can be anything that depends upon our requirements here are the some of application protocols.

  1. TCP
  2. UDP
  3. HTTP
  4. HTTPS

Important clarification:

Multi-Port Services

Multi-port Service allows you to expose different protocols to the same service or different applications on the same service. You can configure multiple ports using the object in kubernetes called to service.

Example

apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
ports:

By using the yaml which is mentioned above you can expose multiple ports but the IP address and DNS name will be the only one with that you can use various services.

Choosing Your Own IP Address

Example

apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
clusterIP: 10.10.10.10
ports:

Once you applied the above specification after that you can access the Tomcat application from the internet by using the IP address you mentioned in the yaml file our case it will be 10.10.10.10. You can also try the above yaml file to create your own customized IP address.

Choosing Your Own Port

Example

apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
type: NodePort
selector:
app: Webapp
ports:
- port: 80
targetPort: 8080

Control Plane in kubernetes will allocates an default

IPaddress which is in the range of various between 30000-32767.
nodePort: 300023
Service type

Access A Kubernetes Service: A Step-By-Step Guide

Create A Pod:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:

kubectl apply -f my-pod.yaml

Accessing Service Using NodePort

Step 1: Create A Service With NodePort

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

kubectl apply -f my-service.yaml

Step 2: Access The Service

kubectl get svc my-service

curl http://:

Accessing Application Using Service Type ClusterIP

Step 1: Create A Service Using ClusterIP

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

kubectl apply -f my-service.yaml

Step 2: Access the Service Using ClusterIP

curl http://my-service:80

Accessing Service Using Load Balancer

Step 1: Create A Service With Load Balancer

apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer

kubectl apply -f my-loadbalancer-service.yaml

Step 2: Access The Load Balancer

kubectl get svc my-loadbalancer-service

curl http://:80

Accessing Service Using External IP

Step 1: Create A Service External IP

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

kubectl apply -f external-service.yaml

Step 2: Access The Service using External IP

curl http://:80

Accessing Service using Headless Service

Step 1: Create A Service of Headless Type

apiVersion: v1
kind: Service
metadata:
name: my-headless-service
spec:
selector:
app: my-app
clusterIP: None
ports:
- protocol: TCP
port: 80
targetPort: 80

kubectl apply -f my-headless-service.yaml

Step 2: Access Pods Directly via Headless Service

kubectl get pods -o wide

curl http://:80

Create and Use A Kubernetes Service to Expose Your Application: A Step-By-Step Guide

The following are the steps that guides on how to use a service to expose your application:

Step 1: Deploy your Application

Deploy your application using a Deployment or Pod yaml file. Here we are taking deployment yaml configure file that is provided as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: nginx
ports:
- containerPort: 80

kubectl apply -f deployment.yaml

Step 2: Create A Kubernetes Service

Create a yaml file for defining the service to the created deployment and making the application be exposed. The service configured yaml file is given as follows:

apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:

kubectl apply -f service.yaml

Step 3: Access Your Application

kubectl get svc my-app-service

curl http://:

Step 4: Optional: Use Ingress

Kubernetes Service Vs pod

The following are the differences between Kubernetes Service and Kubernetes Pod:

Kubernetes Service Kubernetes Pod
It abstracts and exposes a set of pods as a network service It acts as smallest deployable unit with consisting of one or more containers.
It is persistent and provides the stable endpoints It is ephemeral, the containers can be restarted individually.
It provides a single stable IP address In each pod kubernetes provides it own IP address.
It facilitates with laod balancing across the multiple pods It facilitates with scaling the pods at container level with a pod.
It enables service discovery using DNS These are directly discoverable, it needs a service for exposure.

Discovering services

The kubernetes service can be discovered by using two methods as follows.

  1. DNS discovery
  2. Environmental Variables

DNS discovery

When you first create a service kubernetes will automatically create DNS records for the service. The application finds the service in kubernetes with the help of DNS discovery which will point to the IP address of the service.

All the cluster services will be stored in the cluster-wide domain which is svc. cluster. local. The format of the DNS record is as follows.

..svc.cluster.local

DNS in Kubernetes assigns the domain and sub-domain name to the pods and for service. By which other objects or resources in the Kubernetes cluster can discover services by the domain names.

Environmental Variables

Environmental variables will be helpful for the DNS discovery to discover the service in the Kubernetes cluster. You need to mention the environmental variables in the service yaml file as follows then only DNS discovery will discover the service which is available in the Kubernetes cluster;

apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
selector:
app: my-app
ports: