Skip to main content

Command Palette

Search for a command to run...

Navigating the DevOps Seas: A Journey from Docker to Kubernetes

Updated
3 min read
Navigating the DevOps Seas: A Journey from Docker to Kubernetes
A

Associate DevOps Engineer Kubernetes | Docker | GCP/ AWS / Azure | Linux Web Development (HTML5, CSS3, Bootstrap, JS) Video Editing (DaVinci Resolve) Currently working on Terraform

Introduction:

Embarking on a DevOps journey often feels like setting sail into uncharted waters. In our recent project, we set out to deploy a dynamic ReactJS frontend and a robust Node.js backend using Docker and Kubernetes. Join us as we unravel the tale of crafting Dockerfiles to steering through Kubernetes Deployments, Services, and Ingress resources.

Setting the Stage: Writing Multi-stage Dockerfiles

Our journey began with the cornerstone of containerization — Docker. We adopted the best practice of multi-stage builds to optimize image sizes and enhance security. By separating the build and runtime environments, our Dockerfiles became sleek and efficient.

Dockerfile for ReactJS frontend :

# Example of a Multi-stage Dockerfile for Frontend
# Stage - 1
FROM node as build  # An alias is created named "build"
WORKDIR /app

COPY package.json package-lock.json /app/
RUN npm install

COPY . /app
RUN npm run build

# Stage - 2
FROM nginx:alpine

COPY --from=build /app/build /usr/share/nginx/html

EXPOSE 80
#API Call env var
ENV backendURL="http://backendconnect.com:2000"

#serving the site using Nginx
CMD ["nginx", "-g", "daemon off;"]

Writing the Dockerfile for Backend:

# Example of a Multi-stage Dockerfile for Backend
# Stage - 1
FROM node:12 AS builder
WORKDIR /app
COPY package.json package-lock.json /app/
RUN npm install
COPY . /app
RUN npm install -g nodemon

# Stage - 2
FROM node:12-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
EXPOSE 2000
ENV backendURL="http://backendconnect.com:2000"
CMD ["nodemon", "./index.js"]

Sailing the Kubernetes Seas: Deployments & Services

Having containerized our applications, we set our course for Kubernetes. Deployments became our guiding stars. Configuring them allowed us to declare the desired state of our applications, ensuring scalability and availability.

# Example of a Kubernetes Deployment for Node.js
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
        - name: backend
          image: your-registry/backend:latest
          ports:
            - containerPort: 2000

Simultaneously, Kubernetes Services acted as the lighthouses, guiding traffic to our deployed applications.

# Example of a Kubernetes Service
apiVersion: v1
kind: Service
metadata:
  name: backend-service
spec:
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 2000
      targetPort: 2000

Ingress: Charting External Territories with Nginx

With our applications deployed and services exposed, we needed a way to navigate external territories. Enter Ingress, our compass in the Kubernetes realm. Using Nginx as our Ingress controller, we defined rules for external access.

# Example of a Kubernetes Ingress with Nginx
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
    - host: your-domain.com
      http:
        paths:
          - path: /backend
            pathType: Prefix
            backend:
              service:
                name: backend-service
                port:
                  number: 2000

Minikube and Kubectl - Your Command-Line Companions

Embark on local Kubernetes exploration with Minikube. Learn how to set up a local cluster for testing and development. Discover the power of kubectl commands to interact with your Kubernetes cluster, from creating deployments to checking pod logs.

# Minikube commands
minikube start --driver=vmware

# kubectl commands
kubectl apply -f frontendDeploy.yaml
kubectl apply -f backendDeploy.yaml
kubectl apply -f frontendSvc.yaml
kubectl apply -f backendSvc.yaml

kubectl get pods -o wide 
kubectl logs my-pod

Conclusion: Unveiling the Horizon

Our deployment journey from crafting Dockerfiles to steering Kubernetes resources has been an exhilarating expedition. With Docker as our sturdy ship and Kubernetes as our guiding star, we've navigated the complexities of modern DevOps. The horizon is vast, and the journey continues. Stay tuned for more insights, tutorials, and the upcoming blog site unveiling the full saga of our DevOps adventure.

Master Kubernetes

Part 2 of 3

Learn Kubernetes from the basics with this comprehensive blog series. Start with the fundamentals of what Kubernetes is, how it works and its main concepts.

Up next

Unlocking the Power of Nginx Ingress Controller: A Step-by-Step Guide

Streamlining External Access in Kubernetes: Nginx Ingress Controller Unleashed

More from this blog

D

DevToDevops

8 posts

At DevToDevOps, we are on a mission to empower individuals and teams in the ever-evolving landscape of DevOps. Our goal is to provide a platform where enthusiasts, beginners, and seasoned professional