Goglides Dev 🌱

Jeewan Gautam
Jeewan Gautam

Posted on

Process to expose application using the Kubernetes ClusterIP type service object

What is kubernetes Service object?

A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them. Services enable a loose coupling between dependent Pods. A Service is defined using YAML like all Kubernetes objects. The set of Pods targeted by a Service is usually determined by a LabelSelector. (We will see example in below section).we can expose Services in different ways by specifying a type in the Service Specification section.Let's discuss about them in short.

ClusterIP -ClusterIP is used to Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.Dependent applications can interact with other applications internally using the ClusterIP service.

How to create ClusterIP?
ClusterIP can be created using by specifying a type in specs while creating Service object.

Using yaml definition file Example
Step-1 Create deploy-service.yaml using vim or any IDE and provide below definition.

Command to create deploy-service yaml file using vim

vim deploy-service.yaml
Enter fullscreen mode Exit fullscreen mode
apiVersion: v1
kind: Service
metadata:
  name: spring-service
spec:
  type: ClusterIP 
  selector:
    app: spring-app
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
Enter fullscreen mode Exit fullscreen mode

Step-2 User kubectl apply command to create service.

Example

kubeclt apply -f deploy-service.yaml
Enter fullscreen mode Exit fullscreen mode

Service also can be created using kubectl create command. let's see how we can create.

Using kubectl create command

kubectl create service clusterip spring-service --tcp=8081:8081
Enter fullscreen mode Exit fullscreen mode

when you run this command it will create spring-service named service type spring-service which will have port: 8081 protocol: TCP targetPort: 8081.

As of now, we have discussed how to create ClusterIP type service. Now, we will discuss how to expose our application which is deployed using deployment in the last blog.

Here I, will use deploy-service.yaml file to create definition for Deployment and Service object. --- in yaml is used to separate the documents.

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: spring-service
  name: spring-service
spec:
  ports:
  - name: spring-service-port
    port: 8081
    protocol: TCP
    targetPort: 8081
  selector:
    app: spring-service
  type: ClusterIP

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-deployment
spec:
  replicas: 3 
  selector:
    matchLabels:
      app: spring-service
  template:
    metadata:
      labels:
        app: spring-service
    spec:
      containers:
      - name: demo-springapp
        image: jeewangautam02261995/springbootapp:v2
Enter fullscreen mode Exit fullscreen mode

Let's apply this file using kubectl apply command.

kubectl apply -f deploy-service.yaml
Enter fullscreen mode Exit fullscreen mode

Output
Image description

We can see, deployment is created and exposed with service object type ClusterIP.

You can see the deployment and service using below command.

kubectl get deployment -o wide #to view the deployment
kubectl get svc -o wide # to view the service
Enter fullscreen mode Exit fullscreen mode

Result:
Image description

Let's access our application from one the pods using ClusterIP 10.96.89.35.To do so, we need to navigate to terminal of container inside the pod using below command first. First, let get pods.

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

Result:

Image description

Let, navigate to container's terminal of spring-deployment-57f5dbb74d-t82v2

kubectl exec -it spring-deployment-57f5dbb74d-t82v2 -- bash
Enter fullscreen mode Exit fullscreen mode

Result

Image description
Now, we are in terminal, run below curl request to access the application using ClusterIP and port.

curl http://10.96.89.35:8081
Enter fullscreen mode Exit fullscreen mode

Result:

Image description
you can see, we successfully access our application using ClusterIP and port. There are various ways to access our application. let's see each of them shortly.

Using service name and port

curl spring-service:8081
Enter fullscreen mode Exit fullscreen mode

Result:

Image description

Here, we Access our application using service name and port number associated with it only because our application is deploy in default name space. let's see how to access application if this is deployed in different namespace in the same cluster. To do so we need to use DNS query.

Syntax
curl <service name>.<namespace>:<portno>

curl spring-service.default:8081 #application is deployed in default namespace.
Enter fullscreen mode Exit fullscreen mode

Result
Image description

or

curl spring-service.default.svc.cluster.local:8081
Enter fullscreen mode Exit fullscreen mode

As of now, we deployed application using deployment and exposed to ClusterIP type service object and accessed from inside the cluster. But this application is not accessible from outside of the cluster. How we can make application accessible to external user? we will discuss this approach in next blog.

Top comments (0)