Documentation on my success to deploy web application to a featured Kubernetes environment.

learningwithmokhutso-dev

Mokhutso Manaswe

Posted on May 21, 2023

Documentation on my success to deploy web application to a featured Kubernetes environment.

Prerequisites

  • Download and Install Docker
  • Download and install Docker desktop
  • IDE (Visual Studio Code)
  • Sign-up to Docker Hub
  • Install compose plugin

Get app

  • Cloned node.js todo wep app github

Build the app’s container image

  • In the app directory created a Dockerfile
  • Added the folowing content to the Dockerfile [image]
  • Build the container image docker build -t sca-project
  • Started the app container docker run -dp 3000:3000 sa-project
  • Opened web browser to localhost to see the app

localhost3000

Share application

  • Created docker repository [image] on Docker Hub docker-repo
  • Docker login -u my_username
  • docker tag sca-project my_username/sca-project
  • docker push my_username/sca-project

Persist the database

  • docker volume create todo-db
  • docker run -dp 3000:3000 –mount type-volume, src=todo-db,target=/etc/todos sa-project
  • Got to see my list being stored
  • docker volume inspect todo-db

Run app in a development container with a bind mount

docker run -dp 3000:3000 \
    -w /app --mount type=bind,src="$(pwd)",target=/app \
    node:18-alpine \
    sh -c "yarn install && yarn run dev"
Enter fullscreen mode Exit fullscreen mode
docker run -dp 3000:3000 `
    -w /app --mount "type=bind,src=$pwd,target=/app" `
    node:18-alpine `
    sh -c "yarn install && yarn run dev"
Enter fullscreen mode Exit fullscreen mode
  • docker logs -f <container-id>
  • Make changes to app.js , save file (restarts the app inside the container automatically)
  • Refresh the pages and saw my changes being reflected almost immediately

Multi container apps

- docker network create todo-app

docker run -d \
     --network todo-app --network-alias mysql \
     -v todo-mysql-data:/var/lib/mysql \
     -e MYSQL_ROOT_PASSWORD=secret \
     -e MYSQL_DATABASE=todos \
     mysql:8.0
Enter fullscreen mode Exit fullscreen mode
  • docker exec -it <my-sql-container-id> myqsl -u root -p
  • Into mysql shell -> show databases; (don’t forget the semi-colons or else nothing will be displayed)
  • Exit the mysql shell

Connect mysql

- docker network create todo-app

docker run -d `
     --network todo-app --network-alias mysql `
     -v todo-mysql-data:/var/lib/mysql `
     -e MYSQL_ROOT_PASSWORD=secret `
     -e MYSQL_DATABASE=todos `
     mysql:8.0
Enter fullscreen mode Exit fullscreen mode
  • Docker logs -f <container-id>
  • docker exec -it <my-sql-container-id> myqsl -p todos

mysql

  • Into mysql shell -> select * from todos_items
  • Exit the mysql shell

    Docker compose

  • docker compose version

  • In the root folder created docker-compose.yml

  • Insert into docker-compose.yml file

services:
Enter fullscreen mode Exit fullscreen mode

Define the app service

  • docker-compose.yml file should look like:
services:
  app:
    image: node:18-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:8.0
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:
Enter fullscreen mode Exit fullscreen mode

Run the application stack

  • docker compose up -d
  • See the app stack in docker desktop dashboard

    Describe apps using kubernetes yaml

  • Create deployment.yaml in the app root folder

  • Fill deploment.yaml with this code

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bb-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      bb: web
  template:
    metadata:
      labels:
        bb: web
    spec:
      containers:
      - name: bb-site
        image: getting-started
        imagePullPolicy: Never
---
apiVersion: v1
kind: Service
metadata:
  name: bb-entrypoint
  namespace: default
spec:
  type: NodePort
  selector:
    bb: web
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 30002
Enter fullscreen mode Exit fullscreen mode

Create and monitor deployment

  • Enabled Kubernetes on Docker Desktop setting
  • Kubectl apply -f deplyment.yaml
  • Kubectl rollout status deployment/app-deployment

deploy

  • Now can access the app with the yaml configuration nodePort :30002

30002

  • Kubectl get deployments -> view pods configured by yaml file]
  • Kubectl get pods
  • Kubectl get services
  • Kubectl describe deploments

monitor

Scale deployment

  • Kubectl scale deployment/app-deployment –replicas=3
  • Horizontal pod autoscaling
  • Kubectl autoscale deployment/app-deployment –min=3 –max–10 –cpu-percentage=80

scaling

💖 💪 🙅 🚩
learningwithmokhutso-dev
Mokhutso Manaswe

Posted on May 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related