Skip to content

Commit 7920157

Browse files
authored
Merge pull request #4103 from Nayanika1402/docker
New Blog Added
2 parents 1024eb0 + 31e1caf commit 7920157

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
---
2+
title: 'Containerization with Docker and Kubernetes'
3+
sidebar_label: Containerization Basics
4+
authors: [nayanika-mukherjee]
5+
tags: [containerization, docker, kubernetes, devops, ci/cd]
6+
date: 2024-07-30
7+
hide_table_of_contents: true
8+
---
9+
10+
## Introduction to Containerization
11+
12+
Containerization is a lightweight form of virtualization that allows you to run multiple isolated systems on a single host. Containers package an application and its dependencies together, ensuring consistency across different environments and simplifying deployment.
13+
14+
## Introduction to Docker
15+
16+
Docker is an open-source platform for developing, shipping, and running applications inside containers. It provides an easy-to-use CLI and APIs to manage containerized applications.
17+
18+
### Key Concepts
19+
20+
- **Images:** Read-only templates that define the contents of a container.
21+
- **Containers:** Instances of Docker images running as isolated processes.
22+
- **Dockerfile:** A script containing instructions to build a Docker image.
23+
- **Docker Hub:** A cloud-based registry service for sharing Docker images.
24+
25+
## Dockerizing Applications
26+
27+
Dockerizing an application involves creating a Dockerfile and building a Docker image.
28+
29+
### Example: Dockerizing a Node.js Application
30+
31+
```dockerfile
32+
# Use an official Node.js runtime as a parent image
33+
FROM node:14
34+
35+
# Set the working directory
36+
WORKDIR /app
37+
38+
# Copy package.json and install dependencies
39+
COPY package.json ./
40+
RUN npm install
41+
42+
# Copy the application code
43+
COPY . .
44+
45+
# Expose the application port
46+
EXPOSE 3000
47+
48+
# Run the application
49+
CMD ["node", "index.js"]
50+
```
51+
52+
## Docker Compose
53+
54+
Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file.
55+
56+
### Example: Docker Compose for a Web Application and Database
57+
58+
```yaml
59+
version: '3'
60+
services:
61+
web:
62+
image: myapp:latest
63+
ports:
64+
- "3000:3000"
65+
db:
66+
image: postgres:latest
67+
environment:
68+
POSTGRES_USER: user
69+
POSTGRES_PASSWORD: password
70+
POSTGRES_DB: mydb
71+
```
72+
73+
## Docker Networking
74+
75+
Docker networking allows containers to communicate with each other and external systems.
76+
77+
### Example: Creating a Docker Network
78+
79+
```bash
80+
docker network create my_network
81+
```
82+
83+
## Docker Volumes and Storage
84+
Docker volumes are used to persist data generated by and used by Docker containers.
85+
86+
### Example: Creating and Using a Docker Volume
87+
88+
```bash
89+
docker volume create my_volume
90+
docker run -d --name my_container -v my_volume:/data myapp:latest
91+
```
92+
93+
## Introduction to Kubernetes
94+
95+
Kubernetes is an open-source platform for automating deployment, scaling, and management of containerized applications. It groups containers into logical units called pods for easy management and discovery.
96+
97+
## Deploying Applications with Kubernetes
98+
99+
Deploying applications with Kubernetes involves creating deployment and service configurations.
100+
101+
### Example: Kubernetes Deployment
102+
103+
```yaml
104+
apiVersion: apps/v1
105+
kind: Deployment
106+
metadata:
107+
name: myapp-deployment
108+
spec:
109+
replicas: 3
110+
selector:
111+
matchLabels:
112+
app: myapp
113+
template:
114+
metadata:
115+
labels:
116+
app: myapp
117+
spec:
118+
containers:
119+
- name: myapp
120+
image: myapp:latest
121+
ports:
122+
- containerPort: 3000
123+
```
124+
125+
## Kubernetes Networking
126+
127+
Kubernetes networking handles communication between pods, services, and external systems.
128+
129+
### Example: Kubernetes Service
130+
131+
```yaml
132+
apiVersion: v1
133+
kind: Service
134+
metadata:
135+
name: myapp-service
136+
spec:
137+
selector:
138+
app: myapp
139+
ports:
140+
- protocol: TCP
141+
port: 80
142+
targetPort: 3000
143+
type: LoadBalancer
144+
```
145+
146+
## Kubernetes Storage
147+
148+
Kubernetes provides several types of storage for containers, such as persistent volumes and persistent volume claims.
149+
150+
### Example: Persistent Volume and Persistent Volume Claim
151+
152+
```yaml
153+
apiVersion: v1
154+
kind: PersistentVolume
155+
metadata:
156+
name: my-pv
157+
spec:
158+
capacity:
159+
storage: 10Gi
160+
accessModes:
161+
- ReadWriteOnce
162+
hostPath:
163+
path: /data/myapp
164+
---
165+
apiVersion: v1
166+
kind: PersistentVolumeClaim
167+
metadata:
168+
name: my-pvc
169+
spec:
170+
accessModes:
171+
- ReadWriteOnce
172+
resources:
173+
requests:
174+
storage: 10Gi
175+
```
176+
177+
## Kubernetes Configurations and Secrets
178+
179+
Kubernetes allows you to manage application configurations and secrets separately from code.
180+
181+
### Example: ConfigMap and Secret
182+
183+
```yaml
184+
apiVersion: v1
185+
kind: ConfigMap
186+
metadata:
187+
name: my-config
188+
data:
189+
APP_ENV: production
190+
---
191+
apiVersion: v1
192+
kind: Secret
193+
metadata:
194+
name: my-secret
195+
type: Opaque
196+
data:
197+
DB_PASSWORD: cGFzc3dvcmQ=
198+
```
199+
200+
## Kubernetes Security
201+
202+
Kubernetes provides several security features, including role-based access control (RBAC) and network policies.
203+
204+
### Example: RBAC
205+
206+
```yaml
207+
apiVersion: rbac.authorization.k8s.io/v1
208+
kind: Role
209+
metadata:
210+
name: pod-reader
211+
rules:
212+
- apiGroups: [""]
213+
resources: ["pods"]
214+
verbs: ["get", "watch", "list"]
215+
---
216+
apiVersion: rbac.authorization.k8s.io/v1
217+
kind: RoleBinding
218+
metadata:
219+
name: read-pods
220+
subjects:
221+
- kind: User
222+
name: jane
223+
apiGroup: rbac.authorization.k8s.io
224+
roleRef:
225+
kind: Role
226+
name: pod-reader
227+
apiGroup: rbac.authorization.k8s.io
228+
```
229+
230+
## CI/CD with Docker and Kubernetes
231+
232+
Continuous Integration and Continuous Deployment (CI/CD) pipelines can be set up using Docker and Kubernetes to automate the build, test, and deployment processes.
233+
234+
### Example: GitLab CI/CD Pipeline
235+
236+
```yaml
237+
stages:
238+
- build
239+
- deploy
240+
241+
build:
242+
stage: build
243+
script:
244+
- docker build -t myapp:latest .
245+
246+
deploy:
247+
stage: deploy
248+
script:
249+
- kubectl apply -f deployment.yaml
250+
```
251+
252+
## Advanced Topics
253+
254+
Advanced topics in Docker and Kubernetes include service mesh (e.g., Istio), serverless computing (e.g., Knative), and Kubernetes Operators.
255+
256+
## Real-World Examples and Use Cases
257+
258+
### Examples
259+
260+
- **Spotify:** Uses Docker and Kubernetes for microservices deployment.
261+
- **Airbnb:** Leverages Kubernetes for scaling their services.
262+
263+
### Use Cases
264+
265+
- **Dev/Test Environments:** Quickly spin up and tear down environments.
266+
- **Microservices:** Manage microservices architecture efficiently.
267+
- **CI/CD Pipelines:** Automate build, test, and deployment processes.
268+
269+
## Resources for Further Learning
270+
271+
- **Books:** "Kubernetes Up & Running" by Kelsey Hightower, "Docker Deep Dive" by Nigel Poulton.
272+
- **Online Courses:** Kubernetes Academy by VMware, Docker for DevOps by Bret Fisher.
273+
- **Communities:** Join Docker and Kubernetes communities on Slack, Reddit, and Stack Overflow.
274+
275+
## Conclusion
276+
277+
Containerization with Docker and Kubernetes streamlines the development, deployment, and scaling of applications. By understanding the core concepts and leveraging best practices, you can build and manage robust containerized applications.

0 commit comments

Comments
 (0)