sha-since1999 / k8s-project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

docker image creation(for app) and kubernetes Deployment

app - developing with Docker

This app shows a simple user profile app set up using

  • index.html with pure js and css styles
  • nodejs backend with express module
  • mongodb for data storage

All components are docker-based

With Docker

To start the application

run : cd app/ Step 1: Create docker network

docker network create mongo-network 

Step 2: start mongodb

docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb --net mongo-network mongo    

Step 3: start mongo-express

docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express   

NOTE: creating docker-network in optional. You can start both containers in a default network. In this case, just emit --net flag in docker run command

Step 4: open mongo-express from browser

http://localhost:8081

Step 5: create user-account db and users collection in mongo-express

Step 6: Start your nodejs application locally - go to app directory of project

cd app
npm install 
node server.js

Step 7: Access you nodejs application UI from browser

http://localhost:3000

With Docker Compose

To start the application

Step 1: start mongodb and mongo-express

docker-compose -f docker-compose.yaml up

You can access the mongo-express under localhost:8080 from your browser

Step 2: in mongo-express UI - create a new database "my-db"

Step 3: in mongo-express UI - create a new collection "users" in the database "my-db"

Step 4: start node server

cd app
npm install
node server.js

Step 5: access the nodejs application from browser

http://localhost:3000

To build a docker image from the application

docker build -t my-app:1.0 .       

The dot "." at the end of the command denotes location of the Dockerfile.

kubernetes deployments

K8s manifest files

  • mongo-config.yaml
  • mongo-secret.yaml
  • mongo.yaml
  • web-app.yaml
  • web-app-service.yaml

K8s commands

cd .. kubectl apply -f ./mongo-config.yaml kubectl apply -f ./mongo-secret.yaml kubectl apply -f ./mongo.yaml kubectl apply -f ./web-app.yaml kubectl apply -f ./web-app-service.yaml

start Minikube and check status
minikube start --vm-driver=docker 
minikube status
get minikube node's ip address
minikube ip
get basic info about k8s components
kubectl get node
kubectl get pod
kubectl get svc
kubectl get all
get extended info about components
kubectl get pod -o wide
kubectl get node -o wide
get detailed info about a specific component
kubectl describe svc {svc-name}
kubectl describe pod {pod-name}
get application logs
kubectl logs {pod-name}
stop your Minikube cluster
minikube stop

⚠️ Known issue - Minikube IP not accessible

If you can't access the NodePort service webapp with MinikubeIP:NodePort, execute the following command:

minikube service webapp-service

Links

About