svg
Post Image
By Daniel Tanque6 de Novembro, 2023In Sem categoria

Kubernetes Services

“Kubernetes Service” or simply “Service” in the context of Kubernetes, is a resource that provides network access to a set of pods running in a Kubernetes cluster. It is a fundamental building block for creating reliable and scalable applications in Kubernetes.

The primary purpose of a Kubernetes Service is to abstract and expose a group of pods as a single network endpoint, making it easy for other applications or services to interact with them.

In this article you will learn more about kubernetes service and how to set up.

Networking

In Kubernetes you have pods, and those are inside nodes, a pod has it’s own IP, and the node also has it’s own IP. It’s possible to have pods and nodes communicating with each other. But still you will need a way to communicate with the exterior world. For that you have services that can operate on multiple ways, but most importantly make you node/pod/container accessible.

Services

A service enables communication and connects applications. A service is just like a replicaset or deployment in YAML structure.

There are 3 types of service: NodePort, ClusterIP and LoadBalancer.

  1. ClusterIP: This is the default service type. It exposes the service only within the cluster, making it accessible by other pods in the same Kubernetes cluster. It’s not externally accessible.
  2. NodePort: This type exposes the service on a port on each node in the cluster. It makes the service accessible from outside the cluster, often through the node’s IP address and the NodePort. It’s commonly used for exposing services to external clients.
  3. LoadBalancer: This type is typically used in cloud environments and automatically provisions a cloud provider’s load balancer to distribute traffic to the service. It’s ideal for applications that require external accessibility and load balancing.

The structure follows as such:

apiVersion: v1
kind: Service
metadata:
name: myapp-service

spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
app: myapp
type: front-end
  1. TargetPort: The targetPort is the port on the pods that the Service forwards traffic to. In other words, it’s the port where your application inside the pods is listening for incoming connections. The targetPort is a required field in the Service definition.For example, if your application running in the pods listens on port 8080, you would set the targetPort to 8080 in the Service definition.
  2. Port: The port is the port number at which the Service itself is exposed within the cluster. It’s the port you use to access the Service from other pods or applications inside the Kubernetes cluster. The port is also a required field in the Service definition.For example, if you define a Service with port: 80, other pods within the cluster can access the Service at port 80.
  3. NodePort: The nodePort is an optional field used only when the Service type is set to NodePort. It specifies a port number that is exposed on every node in the cluster. This allows external traffic to reach the Service by connecting to any node’s IP address on the specified nodePort.For example, if you set nodePort: 30000, the Service will be accessible from outside the cluster on port 30000 of each node. So, if you have three nodes, the Service will be accessible on each of these nodes at NodeIP:30000. The nodePort should be in the range 30000-32767.

If the pods are in different nodes the service extends its reach to all pods in the cluster.

Commands

To create a service after you’ve defined the YAML file, you run:

kubectl create -f <yaml file>

To see all existing services you run:

kubectl get svc
kubectl get services

To get info from a service you run:

kubectl describe service <name of the service>

Notes

The ClusterIP enables scaling easily the app.

Load Balancer on works with supported cloud platforms such as GCP, Azure or AWS.

svgKubernetes: Pods, Replicas and Deployments
svg
svgMicroservices

Leave a reply