<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Suman Gaire</title>
    <description>The latest articles on DEV Community by Suman Gaire (@sumangaire52).</description>
    <link>https://dev.to/sumangaire52</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1357217%2F67ddb8de-b9a3-4997-b264-c843b079eba9.jpeg</url>
      <title>DEV Community: Suman Gaire</title>
      <link>https://dev.to/sumangaire52</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sumangaire52"/>
    <language>en</language>
    <item>
      <title>FastAPI Microservices Deployment Using Kubernetes</title>
      <dc:creator>Suman Gaire</dc:creator>
      <pubDate>Sun, 02 Jun 2024 09:05:14 +0000</pubDate>
      <link>https://dev.to/sumangaire52/fastapi-microservices-deployment-using-kubernetes-4n4j</link>
      <guid>https://dev.to/sumangaire52/fastapi-microservices-deployment-using-kubernetes-4n4j</guid>
      <description>&lt;p&gt;In this article we are going to create 2 microservices in FastAPI and deploy them using kubernetes on minkube. We are going to use kubernetes ingress to route requests to respective microservices.&lt;/p&gt;

&lt;p&gt;Fist let’s go through kubernetes features we are going to use in this article.&lt;/p&gt;

&lt;p&gt;Kubernetes Deployment: A Kubernetes Deployment automates the management of application updates and scaling. It defines the desired state for an application, such as the number of replicas, the container image to use, and update strategies. The Deployment controller ensures that the actual state of the application matches the desired state by creating and updating pods as needed. Deployments support rolling updates, rollback capabilities, and self-healing, making it easier to maintain application availability and reliability during changes. This abstraction simplifies deploying, scaling, and managing stateless applications in a Kubernetes cluster. &lt;a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/"&gt;https://kubernetes.io/docs/concepts/workloads/controllers/deployment/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kuberenetes Service: A Kubernetes Service is an abstraction that defines a logical set of pods and a policy for accessing them, usually via a stable IP address and DNS name. Services enable communication between different parts of an application without needing to know the pod’s IP addresses, which can change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/"&gt;https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kubernetes Ingress: Kubernetes Ingress manages external access to services within a Kubernetes cluster. In our application we are going to use it to route the traffics to respective services (i.e microservice 1 or microservice 2). It defines rules to route traffic based on hostnames and paths to specific services. An Ingress controller, such as NGINX or Traefik, implements these rules, providing centralized management, load balancing, SSL termination, and path-based routing. Deploying an Ingress controller is required to use Ingress resources effectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#what-is-ingress"&gt;https://kubernetes.io/docs/concepts/services-networking/ingress/#what-is-ingress&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Microservice 1&lt;/strong&gt;:&lt;br&gt;
Microservice 1 is going to be pretty straightforward. It is going to have only one endpoint which is going to return “You requested microservice 1”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a folder named microservice_1 and add a file named main.py.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# main.py (microservice 1)

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "You requested microservice 1"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Add requirements.txt file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;fastapi==0.111.0&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Dockerfile
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Build the docker image and push it to dockerhub.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to tag the image in format username/image_name:version format. Otherwise kubernetes won’t be able to recognize the image. Run this command from the directory where Dockerfile for microservice 1 is located.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker build . -t sumangaire96/microservice1:v1
$ docker push sumangaire96/microservice1:v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Microservice 2&lt;/strong&gt;:&lt;br&gt;
Microservice 2 is also going to have only one endpoint which is going to return “You requested microservice 2”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a folder named microservice_1 and add a file named main.py.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`# main.py (microservice 2)
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "You requested microservice 2"}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add requirements.txt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;fastapi==0.111.0&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Dockerfile
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Build the docker image and push it to dockerhub.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ docker build . -t sumangaire96/microservice2:v1&lt;br&gt;
$ docker push sumangaire96/microservice2:v1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Minikube&lt;/strong&gt;:&lt;br&gt;
Make sure you have minikube installed. If it is not installed follow this link &lt;a href="https://minikube.sigs.k8s.io/docs/start/"&gt;https://minikube.sigs.k8s.io/docs/start/&lt;/a&gt;. Make sure to have kubectl installed to interact with minikube cluster from the terminal. If it is not installed follow this link &lt;a href="https://kubernetes.io/docs/tasks/tools/"&gt;https://kubernetes.io/docs/tasks/tools/&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a kubernetes cluster.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ minikube start&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enable ingress in minikube.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ minikube addons enable ingress&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create kubernetes manifest file for microservice 1.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# kubernetes/microservice_1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: microservice1-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: microservice1
  template:
    metadata:
      labels:
        app: microservice1
    spec:
      containers:
      - name: microservice1
        image: sumangaire96/microservice1:v1
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: microservice1-service
spec:
  selector:
    app: microservice1
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create kubernetes manifest file for microservice 2.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# kubernetes/microservice_2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: microservice2-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: microservice2
  template:
    metadata:
      labels:
        app: microservice2
    spec:
      containers:
      - name: microservice2
        image: sumangaire96/microservice2:v1
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: microservice2-service
spec:
  selector:
    app: microservice2
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create kubernetes ingress manifest file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# kubernetes/ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: microservice1.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: microservice1-service
            port:
              number: 80
  - host: microservice2.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: microservice2-service
            port:
              number: 80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Apply manifests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ kubectl apply -f microservice_1.yaml&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ kubectl apply -f microservice_2.yaml&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ kubectl apply -f ingress.yaml&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update hosts file to map the hostnames to the Minikube IP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ echo "$(minikube ip) microservice1.local" | sudo tee -a /etc/hosts&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$ echo "$(minikube ip) microservice2.local" | sudo tee -a /etc/hosts&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now go to url microservice1.local and microservice2.local. You should see output like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnhk5f2k8eid3l1vfipih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnhk5f2k8eid3l1vfipih.png" alt="Image description" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9942uf8ytb2u1wmdewq0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9942uf8ytb2u1wmdewq0.png" alt="Image description" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! We have created 2 microservices in FastAPI and successfully deployed them using kubernetes. Make sure to follow me for upcoming articles.&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>docker</category>
      <category>kubernetes</category>
      <category>minikube</category>
    </item>
  </channel>
</rss>
