K8 Microservices App with Kind
For this project let us consider a simple flask application which get’s user ip and provides The Users Country and City name
Client
Here the client.process_get(url) will take in a url and performs requests.get(url) and returns response.
Server
Now that the app is running let’s try to convert the following app into Micro services
Server Docker File
FROM python:3.9.5-slim-busterADD . ./appWORKDIR /appRUN pip3 install -r requirements.txtEXPOSE 5003CMD [ "python","app.py" ]
Client Docker File
FROM python:3.9.5-slim-busterADD . ./appWORKDIR /appRUN pip3 install -r requirements.txtEXPOSE 5004CMD [ "python","app.py" ]
Now that we have the Docker Container ready let’s write services and deployment for kubernetes.
The folder structure for the above project
be-depl.yaml
Here the resources are used to specify the configuration of pods.
fe-depl.yaml
Now let’s configure the services.
fe-svc.yaml
be-svc.yaml
Here the ports specify the ports to be exposed.
target port is the port of dockerized application while port specifies the port to be exposed to the environment.
Once all the above steps are finished let’s create a cluster and deploy the application
Since we are using kind we have to first create cluster using kind create command
- Create a Cluster
kind create cluster --name ip-k8s
You can name your cluster however you want i named it ip-k8s
2. Let’s build the docker container
docker build -t be backend/docker build -t fe frontend/
You have to be in root directory of project while running the above commands.
3. Let’s load our docker images into kind cluster
kind load docker-image fe --name ip-k8skind load docker-image be --name ip-k8s
4. Let’s apply the services and deployment to kubernetes
kubectl apply -f k8s
The above command will apply the services & deployments to kuberentes
Once after you applied all your changes you can simply run your service using the following command
kubectl port-forward svc/fe 9000:80
here svc denotes service and since we need to port forward frontend service we are targetting svc/fe and 9000 is the port to be exposed and 80 is the port of service.
Once you do that you can simply visit localhost:9000 to visit your page.
name: bespec:selector:matchLabels:app: betemplate:metadata:labels:app: bespec:containers:- name: beimage: beresources:limits:memory: "128Mi"cpu: "500m"ports:- containerPort: 5003imagePullPolicy: IfNotPresent