How to create a Service in Kubernetes

0
177

A service is an abstraction layer over Pods. It defines a logical set of Pods. It provides a single IP address and DNS name by which pods can be accessed. It is used to expose the pods.

There are 3 different types of Services in Kubernetes:

  1. ClusterIP:
    It exposes the service within the Kubernetes cluster. This Service is only reachable from within the cluster. It can not be accessed from outside the cluster.
  2. NodePort:
    It will expose the service on a static port on the deployed node. This service can be accessed from outside the cluster using the NodeIP:Nodeport.
  3. Load Balancer:
     Exposes the Service externally using a cloud provider’s load balancer, this creates a Public IP on the Cloud provider
  4. ExternalName:
    It  maps the Service to the contents of the externalName field by returning a CNAME record


Click here to know more about Kubernetes Service.

In this article, we will see the steps to create a Serice of the type NodePort.  

Pre-requisites

  1. Kubernetes Cluster with at least 1 worker node.
    If you want to learn to create a Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu EC2 Instances. 

What we will do

  1. Create a Service

Create a Service

First, we will create a deployment using the following definition to which the service will redirect all requests coming to it.

Create a new file and add the following content to it, this will create a deployment for Nginx.

vim my-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nginx
  replicas: 3 
  template: 
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

To create a deployment, execute the following command.

kubectl create -f my-deployment.yml

This will create a deployment for Nginx with 3 replicas.

You can get the details of deployment, replicaset and pod using the following commands.

kubectl get deployment | grep nginx
kubectl get replicaset | grep nginx
kubectl get pod | grep nginx

In the above screenshot, you can see 3 replicas of the Nginx have been created.

Now, create a Service definition using the following content.

vim my-service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  externalTrafficPolicy: Local
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:    
app: nginx type: NodePort

The above service definition will create a Service of type NodePort using namespace default and redirect requests to Pod matching label nginx, i.e. pods we created using the previous deployment creation step.

Execute the following command to create a Service.

kubectl create -f my-service.yml

Get the details of the service and check for the NodePort on which the service will be available.

kubectl get service | grep nginx
kubectl describe service nginx

In the above screenshot, it can be seen that the Service is available on Port 30747. This can be different for you as the port is randomly assigned from the available range.

Now, the nginx application can be accessed through this service on NodeIp:NodePort

Here, it is: 106.210.138.189:30747

Frequently Asked Questions on Creating a Service in Kubernetes

What is a Kubernetes Service and why is it important?

A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy to access them. Services enable communication between different components within and outside of your Kubernetes cluster, providing a stable interface to Pods that may change over time.

How do I create a Service in Kubernetes?

To create a Service, you can use a YAML file to define the Service configuration. Once defined, apply it with kubectl apply -f [filename.yaml]. This command creates a Service based on the specifications in your YAML file.

What are the different types of Services in Kubernetes?

The primary types are ClusterIP (default, accessible within the cluster), NodePort (exposes the Service on each Node’s IP at a static port), LoadBalancer (integrates with cloud-based load balancers), and ExternalName (maps a Service to a DNS name).

How do I expose my Service outside of the Kubernetes cluster?

Use a NodePort or LoadBalancer type Service. NodePort exposes the Service on a static port on the Node’s IP. LoadBalancer uses the cloud provider’s load balancer to expose the Service.

Can I create a Service without a YAML file?

Yes, you can use kubectl expose to create a Service from a command line. For example, kubectl expose deployment [deployment-name] --type=LoadBalancer --name=[service-name].

How do I determine the IP address and port of my Kubernetes Service?

Use kubectl get services to list all Services. This will show you the internal IP address and port assigned to each Service.

How does a Service discover Pods in Kubernetes?

Services use labels to select Pods. When you define a Service, you specify a selector that matches the labels of the Pods you want it to target.

Can a Kubernetes Service load balance traffic to Pods?

Yes, Services automatically load balance traffic to all Pods that match the Service’s selector.

How do I update a Service in Kubernetes?

Update the Service’s YAML configuration file and then apply the changes using kubectl apply -f [filename.yaml]. Kubernetes will update the Service according to the modified specifications.

What happens to the Service if the Pods it targets are deleted?

The Service continues to exist but won’t forward traffic until new Pods matching its selector are created.

How can I delete a Service in Kubernetes?

Use kubectl delete service [service-name] to delete a Service. This will not affect the Pods, but they will no longer be accessible through that Service.

Can a Service in Kubernetes span multiple namespaces?

No, Services are namespace-specific. To communicate across namespaces, you need to use the namespace as part of the Service DNS name.

Conclusion

In this article, we created a deployment for Nginx with 3 replicas and created a Service of type NodePort. We saw how the Nginx application created using the deployment can be accessed on NodeIP:Port.


LEAVE A REPLY

Please enter your comment!
Please enter your name here