Day 34 Task: Working with Services in Kubernetes

Day 34 Task: Working with Services in Kubernetes

Navigating Kubernetes Services: Building Efficient Networking Solutions

ยท

3 min read

๐Ÿ”ถ What are Services in K8s

In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.

๐Ÿ”ถ Task-1: Create a Service for your todo-app Deployment

  • Create a Service for your todo-app Deployment from Day 32

  • installation for minikube refer this link

          # First all update system and install Docker 
          sudo apt-get update
          sudo apt-get install docker.io -y
          sudo usermod -aG docker $USER && newgrp docker
    
          # To install Minikube & Kubectl
          curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
          sudo install minikube-linux-amd64 /usr/local/bin/minikube 
    
          sudo snap install kubectl --classic
          minikube start --driver=docker
    
  • Clone this repository to your local machine:

        git clone https://github.com/niluflip/django-todo-cicd.git
    
       sudo docker build . -t nileshsahare/django-todo:latest
    
  • Create a Service definition for your todo-app Deployment in a YAML file.

  •       apiVersion: v1
          kind: Service
          metadata:
            name: django-todo-services
            namespace: django-app
          spec:
            type: NodePort
            selector:
              app: django-app
            ports:
            - protocol: TCP
              port: 8000
              targetPort: 30007
    
  • Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.

        kubectl create namespace django-app
    
        kubectl create -f service.yml -n django-app
    

  • Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.

๐Ÿ”ถ Task-2: Create a ClusterIP Service for accessing the todo-app from within the cluster

  • Create a ClusterIP Service for accessing the todo-app from within the cluster

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

  • Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.

  • Apply the ClusterIP Service definition to your K8s (minikube) cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name> command.

         kubectl create -f cluster-service.yml -n django-app
    
  • Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

kubectl get all -n django-app

๐Ÿ”ถ Task-3: Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  • Create a LoadBalancer Service for accessing the todo-app from outside the cluster

        #LoadBalncer
        apiVersion: v1
        kind: Service
        metadata:
          name: django-todo-lb-services
          namespace: django-app
        spec:
          type: NodePort
          selector:
            app: django-app
          ports:
          - protocol: TCP
            port: 8000
            targetport: 8000
          type: LoadBalancer
    
  • Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

  • Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the kubectl apply -f load-balancer-service.yml -n <namespace-name> command.

        kubectl apply -f loadbalancer.yml -n django-app
    
  • Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.

        kubectl get svc -n django-app
        minikube service list
    

        kubectl get all -n django-app
    

Struggling with Services? Take a look at this video for a step-by-step guide.

Need help with Services in Kubernetes? Check out the Kubernetes documentation for assistance.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #jenkins #k8s

ย