Join us
Hello everyone. We are going to start a Kubernetes series. In this series, we will learn the basics of Kubernetes and try to deploy our sample microservice code with Kubernetes.
Disclaimer :) If you don’t have prior knowledge of Docker, please refer to this blog. It will help you to understand Kubernetes more precisely.
What we are going to achieve in this article
Step 1: What is Kubernetes? Why and when should we use it?
I assume that if you came here, you probably have some idea about Kubernetes. But even if you don’t know about Kubernetes, please follow the resources below, which will give you a clear idea about Kubernetes.
Step 2: Entire Flow.
Let's understand this flow. So, we can make our Kubernetes manifest file accordingly.
Step 3: Create a Basic microservice
Step 4: Add Kubernetes necessary file and explain about these files
deployment.yaml
apiVersion: apps/v1 # Take version of deployement
kind: Deployment # Kind of Kubernetes file
# Define metadata of deployment file
metadata:
name: mongodb-deployment
labels:
app: mongodb
# Specifications of deployment file
spec:
replicas: 1 # How many replicas are we trying to create.
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
# Specification of image. Docker image information
spec:
containers:
- name: mongo
image: mongodb #Image name which is taking from docker hub
service.yaml
1. In this file we need to look at ClusterIP
2. ClusterIP is used to expose any pod inside the same cluster.
3. We used here type as ClusterIP because MongoDB service no need to expose outside the Cluster.
apiVersion: v1
# Kind of Kubernetes file
kind: Service
# Metadata of service file
metadata:
name: mongodb-service
spec:
type: ClusterIP
ports:
- protocol:
port: 27017
targetPort: 27017
selector:
app: mongodb
4. Now let's run the MongoDB service by running the following command
cd mongodb
kubectl apply -f service.yaml -f deployment.yaml
5. Now our container is created and the service also.
Explanation of User folder
deployment.yaml
apiVersion: apps/v1
kind: Deployment # Kind of Kubernetes file
# Define metadata of deployment file
metadata:
name: user-deployment
labels:
app: user
# Specifications of deployment file
spec:
replicas: 1 # How many replicas are we trying to create.
selector:
matchLabels:
app: user
template:
metadata:
labels:
app: user
# Specification of image. Docker image information
spec:
containers:
- name: user
image: aiirakesh/user:latest
ports:
- containerPort: 8000
env:
- name: MONGODB_HOST
value: "mongodb-service.default"
service.yaml
apiVersion: v1
# Kind of Kubernetes file
kind: Service
# Metadata of service file
metadata:
name: user-service
spec:
type: ClusterIP
ports:
- protocol:
port: 8000
targetPort: 8000
selector:
app: user
Let's run user service
cd user
kubectl apply -f deployment.yaml -f service.yaml
Now if we see we have 2 services and deployments are running
Explanation of Gateway folder
deployment.yaml
apiVersion: apps/v1
kind: Deployment # Kind of Kubernetes file
# Define metadata of deployment file
metadata:
name: gateway-deployment
labels:
app: gateway
# Specifications of deployment file
spec:
replicas: 1 # How many replicas are we trying to create.
selector:
matchLabels:
app: gateway
template:
metadata:
labels:
app: gateway
# Specification of image. Docker image information
spec:
containers:
- name: gateway
image: aiirakesh/gateway:latest
ports:
- containerPort: 8000
env:
- name: MONGODB_HOST
value: "gateway-service.default:8001" # This is provided like this formate <nameOfService>.<namespace>:<port>
service.yaml
apiVersion: v1
# Kind of Kubernetes file
kind: Service
# Metadata of service file
metadata:
name: gateway-service
spec:
type: LoadBalancer
ports:
- protocol:
port: 8001
targetPort: 3000
selector:
app: gateway
Let’s run user service
cd gateway/
kubectl apply -f service.yaml -f deployment.yaml
Now if we see we have 3services and 3 deployments are running
NOTE:
In the above picture, if we see in services part. Gateway service can be accessed from localhost:8001.
Next, article we will deploy the same application on AWS EKS. Which is managed service by AWS
Feedback: Thank you for reading this article. I hope you understood the basics of deploying your application from scratch on Kubernetes. Please feel free to ask questions or give suggestions to improve the content quality.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.