Kubernetes – Service object – simplifying the concepts

Hi All,
Greetings for the day!!!
We are continuing Kubernetes series. In this article we will discuss Kubernetes Service object in depth.
Take away from this article
- What is Kubernetes Service object
- Naming convention for Service object
- YAML specification of Service object
- kubectl CMLDLET for service object
What is Kubernetes Service Object
- Service object act as internal load balancer in Kubernetes cluster
- Service object forward the request to appropriate POD based on the labels and labels selector
- A Service object allow network access to set of PODs
- When Service object receives network request it selects all the PODs in cluster matching the service selector. Choose one of the POD and forwards the network requests
- Default protocol for service is TCP.
- We can use any supported protocols – UDP, SCTP, HTTP, PROXY protocol
Name of Service object
- The name of a Service object must be a valid RFC 1035 label name.
- RFC 1035 Label Names – Some resource types require their names to follow the DNS label standard as defined in RFC 1035. This means the name must:
- contain at most 63 characters
- contain only lowercase alphanumeric characters or ‘-‘
- start with an alphabetic character
- end with an alphanumeric character
YAML specification for Kubernetes Service object
kind: Service
apiVersion: v1
metadata:
name: <Service - name>
spec:
# Expose the service on a static port on each node
# so that we can access the service from outside the cluster
type: NodePort
# When the node receives a request on the static port (30163)
# "select pods with the label 'app' set to 'echo-hostname'"
# and forward the request to one of them
selector:
app: <pod app name>
ports:
# Three types of ports for a service
# nodePort - a static port assigned on each the node
# port - port exposed internally in the cluster
# targetPort - the container port to send requests to
- nodePort: 30163
protocol: TCP
port: 8080
targetPort: 80
Here,
- type property :
- There are 4 types of Kubernetes services
- Tells us how the Service is exposed to Network
- Possible values are
- NodePort
- Using NodePort we make accessible Service on a static port on each node in cluster
- Means Service is able to handle request from outside cluster
- We could connect to NodePort service from outside the cluster by <NodeIP> : <Node Port>
- NodePort number must be range from 30000-32767
- If we didnt specify NodePort, kubernetes will automatically assign
- If we specify NodePort explicitly we need to make sure respective NodePort is assigned to another Service
- Usages
- When we want to connect our application outside Kubernetes cluster
- ClusterIP
- This is default value
- In this type we can not make request to PODs from outside the cluster
- The service is accessible only within the Kubernetes cluster
- This type can be used while inter service communication within the cluster like communication between front-end and back-end of our application
- LoadBalancer
- The Service is accessible through externally using cloud providers load balancer functionality
- Cloud providers like Azure, AWS, GCP, OpenStack – provides this kind of functionality
- ExternalName
- This service type is used when our POD needs to access an application outside the Kubernetes cluster like external database
- NodePort
Example – Creating service specification for – private registry in Service
apiVersion: v1
kind: Service
metadata:
name: registry-service-knoweldgejunction
namespace: sharepoint-knowledgejunction
spec:
selector:
app: registry
ports:
- port: 8800
Once we have specification ready we can use kubectl CMDLET to create Service object
kubectl create -f <service object YAML specification path>
Example :
kubectl create -f service.yaml

What happened when Service object created
- Kubernetes assigns IP address to Service
- Controller for Service selector continuously scans for PODs that match its selector
Multi-Port services
- For Service object Kubernetes support multiple port definitions
- For allowing multiple ports, we specify port name to avoid unambiguous
apiVersion: v1
kind: Service
metadata:
name: registry-service-knoweldgejunction
spec:
selector:
app: registry
ports:
- name: http
protocol: TCP
port: 80
targetPort: 9376
- name: https
protocol: TCP
port: 443
targetPort: 9377
Thanks for reading !!! Please feel free to discuss / suggestions / share thoughts !!!
HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂
You must log in to post a comment.