Day 34 : Services in Kubernetes

Day 34 : Services in Kubernetes

Today I have covered,

- What are Services in Kubernetes and how to create them?

- What are the types of Kubernetes Services?

What is Service in Kubernetes?

  • A Kubernetes Service is an abstraction which defines a logical set of Pods running somewhere in your cluster, that all provide the same functionality. When created, each Service is assigned a unique IP address (also called clusterIP).

  • This address is tied to the lifespan of the Service, and will not change while the Service is alive. Pods can be configured to talk to the Service and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.


Types of Services in Kubernetes:

6403ec82472efb1d6ff79f4e_d

  • Visual Representation of Services in Kubernetes:

1_tnK94zrEwyNe1hL-PhJXOA


So before starting the task let's do necessary installation and setup.

  • Before Doing the task let's set up the git repository for the same and push the code to the repository.

Steps to follow: Here we will use AWS for Deployment and Git for version control.

Step-01: Open your terminal or any other cloud service.

Step-02: Update and install Docker on your machine.

sudo apt update
sudo apt install docker.io

Screenshot from 2023-04-17 22-08-47

Screenshot from 2023-04-17 22-15-44

Step-03: Now give Docker permission for super user of your local machine.

sudo usermod -aG docker $USER && newgrp docker

Screenshot from 2023-04-17 22-24-25

Step-04: Now let's install Minikube.

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Screenshot from 2023-04-17 22-19-41

Screenshot from 2023-04-17 22-20-07

Screenshot from 2023-04-17 22-20-36

Step-05: Now let's start Minikube.

minikube start
  • let's connect with docker.
minikube start  driver=docker

Screenshot from 2023-04-17 22-22-18

Screenshot from 2023-04-17 23-06-54

Step-06: Now let's check the status of Minikube.

minikube status

Step-07: Now we will install the Command line instruction for Minikube which is Kubectl.

  • But first, install snap on your machine.
sudo apt install snap

Screenshot from 2023-04-17 23-10-55

  • CLI for Minikube is Kubectl.
sudo snap install kubectl --classic

Screenshot from 2023-04-17 23-12-11

Step-08: Now let's check the version of Kubectl.

kubectl version

Step-09: Now Clone the repository from the github. Github Repo link : https://github.com/LondheShubham153/django-todo-cicd.git

git clone https://github.com/LondheShubham153/django-todo-cicd.git

Step-10: Now let's check the files in the repository.

ls
cd django-todo-cicd

Screenshot from 2023-05-05 16-03-41


Task-1:

  • Create a Service for the deployment.

Step-01: Create a file service.yaml and write the code for the service.

apiVersion: v1
kind: Service
metadata:
  name: django-todo-services
  namespace: python-django-app
spec:
  type: NodePort
  selector:
    app: django-app
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 30007

Screenshot from 2023-06-02 23-35-16

  • Now let's create a namespace first for the deployment.
kubectl create namespace python-django-app

Screenshot from 2023-06-02 23-35-52

Screenshot from 2023-06-02 23-36-14

  • As we have created services and namespace now lets create a deployment.

Syntax: kubectl create -f <filename> -n <namespace>

kubectl create -f service.yaml -n python-django-app

Screenshot from 2023-06-02 23-36-24

Screenshot from 2023-06-02 23-36-59

  • Now verify the deployment and service.
kubectl get all -n python-django-app

Task-2:

  • Create a ClusterIP service for the deployment.

Step-01: Create a file clusterip-service.yaml and write the code for the service.

apiVersion: v1
kind: Service
metadata:
  name: django-todo-services
  namespace: python-django-app
spec:
  type: NodePort
  selector:
    app: django-app
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 8000
  type: ClusterIP

Screenshot from 2023-06-02 23-53-54

  • Now let's create a deployment.

Syntax: kubectl create -f <filename> -n <namespace>

kubectl create -f clusterip-service.yaml -n python-django-app

Screenshot from 2023-06-02 23-54-36

  • Check of the Service is created or not.
kubectl get svc -n python-django-app

Screenshot from 2023-06-02 23-55-46

  • Now verify the deployment and service.
kubectl get all -n python-django-app

Task-3:

  • Create a LoadBalancer service for the deployment.

step-1: Create a file loadbalancer-service.yaml and write the code for the service.

apiVersion: v1
kind: Service
metadata:
  name: django-todo-lb-services
  namespace: python-django-app
spec:
  type: NodePort
  selector:
    app: django-app
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 8000
  type: LoadBalancer

Screenshot from 2023-06-03 00-13-53

  • Now let's create a deployment. syntax: kubectl create -f <filename> -n <namespace>
kubectl create -f loadbalancer-service.yaml -n python-django-app

Screenshot from 2023-06-03 00-15-07

  • Check of the Service is created or not.
kubectl get svc -n python-django-app
minikube service list

Screenshot from 2023-06-03 00-16-02

  • Now verify the deployment and service.
kubectl get all -n python-django-app

Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!