5. Docker Orchestration tools- Kubernetes, Docker Swarm
5. Docker Orchestration tools- Kubernetes, Docker Swarm
Docker Swarm.
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):
o app.js:
JavaScript
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
Bash
Bash
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!".
Bash
1. Docker Swarm:
Bash
Bash
On each worker machine, run the command obtained in the previous step.
4. Deploy a Service:
Bash
Bash
6. Remove a Service:
Bash
docker service rm my-web
2. Kubernetes:
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.
Bash
minikube start
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)
Bash
Bash