Multi tier application in kubernetes

A multi-tier application is an application distributed among more than one layer. It logically separates the operational layers. The number of layers varies with business and application requirements

Deploy a Multi-Tier Application
Create a new file named "test-db-deployment.yaml" with the following content. This will define a deployment of MongoDB which will act as a database, a backend layer
# cat test-db-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-db
spec:
  selector:
    matchLabels:
      appdb: testdb
  replicas: 1
  template:
    metadata:
      labels:
        appdb: testdb
    spec:
      containers:
      - name: test-db
        image: mongo:3.3
        env:
        - name: MONGODB_DATABASE
          value: testdata
        ports:
        - containerPort: 27017
Now, create a service that will serve requests to MongoDB from our frontend application. This service will listen on Port 27017 and forward requests to MongoDB on the same port.
# cat test-db-service.yml
apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: testdb
spec:
  ports:
  - port: 27017
    protocol: TCP
  selector:
    appdb: testdb
The next step is to define a frontend application. Create a new file containing the following deployment definition. This will listen to Port 5000. Requests to MongoDB will be forwarded to MONGODB_HOST i.e. mongodb service.
# cat test-web-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  selector:
    matchLabels:
      app: test
  replicas: 1
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test-app
        image: teamcloudyuga/rsvpapp
        env:
        - name: MONGODB_HOST
          value: mongodb
        ports:
        - containerPort: 5000
          name: web-port
To access the frontend, we will create a service of type NodePort listening on Port 31081. This means the Python Flask Webserver can be accessed on IP=IP-Of-Any-Node and Port=31081. Create a new with the following service definition.
# cat test-web-service.yml
apiVersion: v1
kind: Service
metadata:
  name: test
  labels:
    apps: test
spec:
  type: NodePort
  ports:
  - name: tcp-31081-5000
    nodePort: 31081
    port: 5000
    protocol: TCP
  selector:
    app: test
Now, we are all set to create a 2 tier sample application. Before we proceed, let's check if the cluster has any objects
Execute the following commands to check pods
#kubectl get pods
#kubectl get deployments
#kubectl get service
Now, execute the following command one by one 
# kubectl create -f test-db-deployment.yml
deployment.apps/test-db created
# kubectl create -f test-db-service.yml
service/mongodb created
# kubectl create -f test-web-deployment.yml
deployment.apps/test created
# kubectl create -f test-web-service.yml
service/test created

To verify the same, execute the following commands
# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-7cdc9bf95d-mfhzk   1/1     Running   3          81m
test-8577b85d5d-nwxn2                     1/1     Running   0          92s
test-db-544bd6b575-52wfv                  1/1     Running   0          2m39s

# kubectl get deployments
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
nfs-client-provisioner   1/1     1             1                 82m
test                          1/1       1             1                2m17s
test-db                  1/1       1                 1               3m24s

# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP          169m
mongodb      ClusterIP   10.108.225.243           27017/TCP        6m1s
test         NodePort    10.101.97.123            5000:31081/TCP   5m6s
we can see that 2 pods and 2 services have been created. The pods which been created are controlled by the deployment we created.

Now, we are all set to access the application on IP-of-any-Node: NodePort i.e. IP-of-any-Node:31081, Once we hit the URL in the browser, we will see the application

Now, when we no longer need these applications, we can delete all the objects by executing the following command.
# kubectl delete -f
To check if all the objects we created have been deleted successfully, execute the following command.
# kubectl get all
we created a deployment of the MongoDB backend database and service to access the database and for the frontend, we created Python Flask-based application deployment and service to access this frontend. This is a sample 2 tier application.






Recent Comments

No comments

Leave a Comment