Port Definitions

In the kubernetes service, there is one section called targetPort in the nodePort service instead of using the port number you can directly use the name of the pods.

Example

kind: Pod
metadata:
name: tomcat
labels:
deployemnt: deployemnet-java
spec:
containers:
- name: tomcat
image: tomact:latest
ports:
- containerPort: 8080
name: http-deplyment-port

---
apiVersion: v1
kind: Service
metadata:
name:tomcat-service
spec:
selector:
deployemnt: deployemnet-java
ports:
- name: name-of-service-port
protocol: TCP
port: 80
targetPort:http-deplyment-port

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.

What are Kubernetes Services? | Complete Guide

In Kubernetes, each pod is assigned its IP address but the pods are ephemeral that is they can be destroyed easily and when a new pod is created in place of them a new IP address is assigned to them. Here the role of services comes into the picture. A service is like a permanent IP address assigned to a pod. A service IP address is stable. So instead of sending a request to a pod, the client requests a service, and the service forwards that request to the desired pod. Services also help in load-balancing.

Table of Content

  • What Are Services in Kubernetes?
  • How do Kubernetes Service Work?
  • How do You Define A Kubernetes Service?
  • Port Definitions
  • Services Without Selectors
  • EndpointSlices
  • Application Protocol
  • Multi-Port Services
  • Load Balancers With Mixed Protocol Types
  • Choosing Your Own IP Address
  • Choosing Your Own Port
  • Types of Services
  • How do You Access A Kubernetes Service? A Step-By-Step Guide
  • Headless Services
  • How to Create and Use A Kubernetes Service to Expose Your Application?: A Step-By-Step Guide
  • What are the Differences between Kubernetes Service vs pod?
  • What are the Differences between a Service and a Deployment?
  • What are the Differences of Docker Service and Kubernetes Service?
  • Discovering services
  • Kubernetes Service – FAQs

Similar Reads

What Are Services in Kubernetes?

Service in Kubernetes will expose the application which is running in the pods to the internet. Each service will have its own set of endpoints from where they can be accessed from the internet....

How do Kubernetes Service Work?

Kubernetes services act as an abstraction layer that exposes the applications in the running pods to other services or external clients. They provide a stable endpoint and load balancing features for accessing pods within a kubernetes cluster. It provides a seamless communication between different parts of an application. Kubernetes services use labels and selectors for dynamically routing the traffic to the appropriate pods. It ensure high availability and reliability to the application....

How do You Define 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....

Port Definitions

In the kubernetes service, there is one section called targetPort in the nodePort service instead of using the port number you can directly use the name of the pods....

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:...

EndpointSlices

EndpontSlices are the object available in the kubernetes cluster from Kubernetes v1.21 [stable]. Endpontslice main function is to break endpoints into smaller manageable segments. The limit of pods that can find in each endpoint slice was 100 pods. The new endpoint slice will be created only when the Endpointslice is filled up with 100 endpoints....

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....

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....

Load Balancers With Mixed Protocol Types

The service type LoadBalancer can have multiple ports but it should have only one protocol and which is supported by the cloud provider. You can have a kubernetes service that exposes protocols like TCP and UDP ports, which is called a mixed protocol service....

Choosing Your Own IP Address

Kubernetes will assign the IP address from within the service-cluster-ip-range CIDR range or you can choose your own customized IP address. You can reuse the DNS or IP address already in the kubernetes cluster. For reusing, you need to request while creating the service itself....

Choosing Your Own Port

While exposing the pod to the internet we will use service objects in the Kubernetes cluster. We will use mainly the nodePort service in Kubernetes while exposing the pod in Kubernetes. You can mention your own port in the yaml in the section which is called nodePort: . On this port, the users can access the service from the internet and the service will listen on the port which you have mentioned....

Types of Services

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

How do You Access A Kubernetes Service? A Step-By-Step Guide

We can access the kubernetes service using the services such as ClusterIP, NodePort ot LoadBalancer IP along with defining the port number. Clients within the cluster uses the service name and port. And the clients who are outside the cluster uses the cluster’s external IP or DNS name. The following are the step by step guide for accessing the kubernetes service....

Headless Services

Headless Services in Kubernetes comes with specifying the setting of `.spec.clusterIP` to None and doesn’t provide the load balancing features with single IP allocation. Instead it facilitates to have a direct connections to the pods individually. It facilitates with interfacing the external service discovery mechanisms. DNS records, DNS services that are managed by Kubernetes provides endpoint IP’s for these Headless Services....

How to 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:...

What are the Differences between Kubernetes Service vs pod?

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

What are the Differences between a Service and a Deployment?

The following are the differences between a Service and A Deployment:...

What are the Differences of Docker Service and Kubernetes Service?

The following are the differences of Docker Serive and Kubernetes Service:...

Discovering services

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

Kubernetes Service – FAQs

What Is Kubernetes Service discovery spring boot?...