What is a Service in Kubernetes?
For Kubernetes, a service is an abstraction that represents a set of logical pods where an application or component is running, as well as embedding an access policy to those pods. Actual pods are ephemeral, Kubernetes guarantees the availability of pods and replicas specified but not the liveliness of each individual pod. This means that other pods that need to communicate with this application or component cannot rely on the underlying individual pod’s IP addresses.
A service gets allocated a virtual IP address as well and lives until explicitly destroyed. Requests to the service get redirected to the appropriate pods, thus the service serves as a stable endpoint used for inter-component or application communication. For Kubernetes-native applications, requests can also be made via an API in Kubernetes’ apiserver which automatically exposes and maintains the actual pods endpoints at any moment.
What are the Components of a Kubernetes Services?
A Kubernetes service associates a set of pods with an abstract service name and persistent IP address. This enables pods to discover each other and route requests to each other. A service uses labels and selectors to match pods with other applications. For example, a service might connect the front end of an application to a back end, each running in a separate Deployment within the cluster.
The basic components of a Kubernetes service are a label selector that identifies pods to route traffic to, a clusterIP and port number, port definitions, and optional mapping of incoming ports to a targetPort.
Types of Services present in Kubernetes:
1.ClusterIP
A ClusterIP service is the default type of service in Kubernetes. It creates a service inside the Kubernetes cluster, which can be accessed by other applications in the cluster, without allowing external access.
apiVersion: v1
kind: Service
metadata:
name: my-clusterip-service
spec:
type: ClusterIP
clusterIP: 10.10.5.10
ports:
—name: http
protocol: TCP
port: 80
targetPort: 8080
2.NodePort
A NodePort service opens a specific port on all the Nodes in the cluster, and any traffic sent to that port is forwarded to the service. The service cannot be accessed from the cluster IP.
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
selector:
app: nginx
ports:
—name: http
protocol: TCP
port: 80
targetPort: 8080
nodePort: 30000
3.LoadBalancer
A Load Balancer is a standard way to expose a Kubernetes service externally so it can be accessed over the internet. If you are using Google Kubernetes Engine (GKE) this creates a Network Load Balancer with one IP address, which external users can access and are then forwarded to the relevant node in your Kubernetes cluster. A Load Balancer can also be accessed in the same way as a ClusterIP or NodePort.
apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer-service
spec:
type: LoadBalancer
clusterIP: 10.0.160.135
loadBalancerIP: 168.196.90.10
selector:
app: nginx
ports:
—name: http
protocol: TCP
port: 80
targetPort: 8080
4.ExternalName
An ExternalName service maps the service to a DNS name instead of a selector. You define the name using the spec:externalName parameter. It returns a CNAME record matching the contents of the externalName field (for example, my.service.domain.com), without using a proxy.
This type of service can be used to create services in Kubernetes that represent external components such as databases running outside of Kubernetes.
apiVersion: v1
kind: Service
metadata:
name: my-externalname-service
spec:
type: ExternalName
externalName: my.database.domain.com
How to Create a Kubernetes Service
Create a Kubernetes Deployment or Pod: Before creating a service, you need to have a Deployment or Pod running in your cluster. A Deployment manages a set of replica Pods.
Write a Service Definition YAML File: You need to create a YAML file that describes your service. This file specifies details such as the service name, type, and the selector that determines which Pods the service should route traffic to.
Apply the Service Definition: Use the
kubectl apply
command to apply the service definition YAML file to your Kubernetes cluster.
Here's an example of a simple YAML file for creating a Kubernetes service:
yamlCopy codeapiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
apiVersion
: Specifies the API version of the resource.kind
: Specifies the type of resource, in this case, a Service.metadata.name
: The name of the service.spec.selector
: Specifies the Pods that the service should target. In this example, it selects Pods with the labelapp: my-app
.spec.ports
: Defines the ports that the service exposes. It specifies the protocol, port number, and the target port on the Pods.spec.type
: Specifies the type of service. Options include ClusterIP, NodePort, LoadBalancer, and ExternalName.
To apply this YAML file, save it to a file and then run:
kubectl apply -f my-service.yaml
After applying the YAML file, Kubernetes will create the service according to the specifications provided.