0% found this document useful (0 votes)
7 views5 pages

5. Docker Orchestration tools- Kubernetes, Docker Swarm

The document provides a comprehensive guide on containerization using Docker, including installation, creating a sample Node.js application, building a Docker image, and running a container. It also covers orchestration tools like Docker Swarm and Kubernetes, highlighting their features, setup processes, and use cases. A comparison between Docker Swarm and Kubernetes is included, emphasizing their complexity, scalability, and community support.

Uploaded by

satish.pise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

5. Docker Orchestration tools- Kubernetes, Docker Swarm

The document provides a comprehensive guide on containerization using Docker, including installation, creating a sample Node.js application, building a Docker image, and running a container. It also covers orchestration tools like Docker Swarm and Kubernetes, highlighting their features, setup processes, and use cases. A comparison between Docker Swarm and Kubernetes is included, emphasizing their complexity, scalability, and community support.

Uploaded by

satish.pise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Let's explore containerization with Docker and delve into orchestration tools like Kubernetes and

Docker Swarm.

Part 1: Containerization with Docker

1. Install Docker:
o Docker Desktop (Windows/macOS): The easiest way to get started. Download
and install from the official Docker website. This includes Docker Engine,
Docker CLI, Docker Compose, and optionally Kubernetes.
o Docker Engine (Linux): Follow the distribution-specific instructions on the
Docker website.
2. Create a Sample Application (Node.js Example):

Create the following files in a new directory:

o app.js:

JavaScript

const express = require('express');


const app = express();
const port = 3000;

app.get('/', (req, res) => {


res.send('Hello from Docker!');
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
});

o package.json:

JSON

{
"name": "docker-node-app",
"version": "1.0.0",
"description": "A simple Node.js app in Docker",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.18.2"
}
}

3. Create a Dockerfile:
Create a file named Dockerfile (no extension) in the same directory:

Dockerfile

FROM node:16-alpine # Use a lightweight Node.js base image

WORKDIR /app # Set the working directory inside the container

COPY package*.json ./ # Copy package files


RUN npm install # Install dependencies

COPY . . # Copy the application code

EXPOSE 3000 # Expose port 3000

CMD [ "npm", "start" ] # Command to run the application

4. Build the Docker Image:

Open a terminal in the directory containing the Dockerfile and run:

Bash

docker build -t my-node-app .

o -t my-node-app: Tags the image with the name my-node-app.


o .: Specifies the build context (the current directory).
5. Run the Docker Container:

Bash

docker run -p 3000:3000 my-node-app

o -p 3000:3000: Maps port 3000 on the host to port 3000 inside the container.

Open your web browser and go to http://localhost:3000. You should see "Hello
from Docker!".

6. Stop and Remove Containers/Images:

Bash

docker ps # List running containers


docker stop <container_id> # Stop a container
docker rm <container_id> # Remove a stopped container
docker images # List Docker images
docker rmi <image_id> or <image_name> # Remove an image

Part 2: Docker Orchestration


Orchestration tools manage containers at scale, handling tasks like deployment, scaling,
networking, and load balancing.

1. Docker Swarm:

 Integrated Orchestration: Built into Docker Engine.


 Simple Setup: Easier to configure than Kubernetes.
 Suitable for Smaller Deployments: Good for applications that don't require the full
power of Kubernetes.

Simplified Swarm Example:

1. Initialize a Swarm (Manager Node):

Bash

docker swarm init

2. Get the Join Token (Worker Nodes):

Bash

docker swarm join-token worker

3. Join Worker Nodes:

On each worker machine, run the command obtained in the previous step.

4. Deploy a Service:

Bash

docker service create --name my-web --replicas 3 -p 3000:3000 my-node-


app

o --name my-web: Name of the service.


o --replicas 3: Number of container instances (replicas).
5. Scale a Service:

Bash

docker service scale my-web=5

6. Remove a Service:

Bash
docker service rm my-web

2. Kubernetes:

 Industry Standard: The most widely used container orchestration platform.


 Powerful and Complex: Offers advanced features like auto-scaling, self-healing, rolling
updates, and complex deployments.
 Suitable for Complex, Large-Scale Deployments: Ideal for microservices architectures.

Key Kubernetes Concepts:

 Pods: The smallest deployable unit; can contain one or more containers.
 Deployments: Manage the desired state of your application (number of replicas,
updates).
 Services: Expose applications to internal or external traffic.
 Nodes: Worker machines where pods run.
 Control Plane (Master): Manages the cluster.

Simplified Kubernetes Example (using Minikube for local development):

1. Install Minikube: Follow the instructions on the Minikube website.


2. Start Minikube:

Bash

minikube start

3. Create a Deployment (deployment.yaml):

YAML

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-container
image: my-node-app
ports:
- containerPort: 3000
4. Create a Service (service.yaml):

YAML

apiVersion: v1
kind: Service
metadata:
name: my-node-service
spec:
selector:
app: my-node-app
ports:
- protocol: TCP
port: 3000 # External port
targetPort: 3000 # Container port
type: LoadBalancer # Expose externally (Minikube uses a tunnel)

5. Apply the YAML files:

Bash

kubectl apply -f deployment.yaml


kubectl apply -f service.yaml

6. Access the Application (Minikube):

Bash

minikube service my-node-service

This will open your browser to the service URL.

Comparison of Swarm and Kubernetes:

Feature Docker Swarm Kubernetes


Complexity Simpler More complex
Setup Easy More involved
Features Basic orchestration Advanced orchestration features
Scalability Moderate High
Community Smaller Very large and active
Use Cases Simple apps, smaller clusters Complex microservices, large clusters

You might also like