# 🗺️ DevOps Roadmap for Fullstack Developers

Dành cho Fullstack Developer chuyển hướng sang DevOps. Tài liệu tập trung vào các nền tảng cốt lõi, ngắn gọn, đi kèm câu lệnh/code thực hành.

---

## 📌 Tổng quan Lộ trình Ưu tiên (Priority Core)

```
[1. Linux Fundamentals] ---> [2. Networking] ---> [3. Docker / Containers] ---> [4. Kubernetes (K8s)]
                                                                                       |
                                                                                       v
                                                                             [5. CI/CD & IaC]
```

---

<details>
<summary><h2>1. Linux Basics & Shell Scripting</h2></summary>

Linux là nền tảng của Hầu hết môi trường Server và Container.

### 1.1. File System & Permissions
- `chmod`: Thay đổi quyền truy cập (`chmod 755 script.sh`).
- `chown`: Thay đổi chủ sở hữu file (`chown user:group file.txt`).

```bash
# Phân quyền thực thi cho file shell
chmod +x deploy.sh

# Kiểm tra quyền truy cập chi tiết
ls -la /var/log
```

### 1.2. Process Management & Monitoring
- `ps aux`, `top`, `htop`: Kiểm tra tiến trình đang chạy.
- `systemctl`: Quản lý các dịch vụ (services).

```bash
# Kiểm tra trạng thái dịch vụ Nginx
sudo systemctl status nginx

# Khởi động lại service
sudo systemctl restart nginx
```

### 1.3. Shell Scripting Cơ Bản
Tự động hóa các tác vụ lặp đi lặp lại.

```bash
#!/usr/bin/env bash
set -e # Dừng script ngay khi có lỗi

APP_DIR="/var/www/app"

if [ -d "$APP_DIR" ]; then
    echo "Thu muc tồn tại. Tiến hành pull code..."
    cd $APP_DIR && git pull origin main
else
    echo "Thu muc không tồn tại!"
    exit 1
fi
```
</details>

---

<details>
<summary><h2>2. Networking Fundamentals</h2></summary>

Hiểu cách các dịch vụ giao tiếp với nhau trong mạng máy tính và cloud.

### 2.1. OSI Model & Core Protocols
- **TCP/IP**: Giao thức truyền tải tin cậy.
- **HTTP/HTTPS**: Cổng mặc định 80 / 443.
- **DNS**: Ánh ánh Domain name -> IP Address.

### 2.2. Command Line Tools
Các lệnh debug mạng thông dụng:

```bash
# Kiểm tra kết nối IP/Domain
ping -c 4 google.com

# Kiểm tra kết nối cổng cụ thể (Port Test)
nc -zv 127.0.0.1 8080

# Xem các port đang mở trên máy
netstat -tulpn
# hoặc dùng ss
ss -tulpn
```

### 2.3. Reverse Proxy & Web Server (Nginx)
Cấu hình Nginx làm Reverse Proxy tới ứng dụng Node.js/Python:

```nginx
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```
</details>

---

<details>
<summary><h2>3. Containerization (Docker)</h2></summary>

Đóng gói ứng dụng cùng môi trường thực thi chuẩn hóa.

### 3.1. Dockerfile Best Practices
Viết Dockerfile chuẩn Multi-stage build để tối ưu dung lượng image.

```dockerfile
# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Production Run
FROM node:18-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --only=production

EXPOSE 3000
CMD ["node", "dist/main.js"]
```

### 3.2. Docker Compose
Chạy multi-container application cho môi trường local.

```yaml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DB_HOST=db
    depends_on:
      - db

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: appdb
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
```

### 3.3. Câu lệnh Docker Căn Bản
```bash
# Build & Run
docker build -t my-app:v1 .
docker run -d -p 8080:3000 --name app_instance my-app:v1

# Debugging
docker logs -f app_instance
docker exec -it app_instance /bin/sh
```
</details>

---

<details>
<summary><h2>4. Container Orchestration (Kubernetes - K8s)</h2></summary>

Quản lý, điều phối và tự động hóa việc mở rộng container ở môi trường production.

### 4.1. Kiến trúc & Khái niệm cốt lõi
- **Pod**: Đơn vị nhỏ nhất trong K8s (chứa 1 hoặc nhiều container).
- **Deployment**: Quản lý việc cập nhật, scale Pod.
- **Service**: Đầu mối giao tiếp (ClusterIP, NodePort, LoadBalancer).
- **Ingress**: Quản lý truy cập từ bên ngoài vào Service (L7 Routing).

### 4.2. Kubernetes Manifests (YAML)

#### Deployment Config
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: node-app
        image: my-app:v1
        ports:
        - containerPort: 3000
```

#### Service Config
```yaml
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: ClusterIP
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 3000
```

### 4.3. Các lệnh `kubectl` thiết yếu
```bash
# Xem trạng thái các tài nguyên
kubectl get pods
kubectl get deployments
kubectl get services

# Xem log và Debug
kubectl logs -f <pod-name>
kubectl describe pod <pod-name>

# Apply cấu hình
kubectl apply -f deployment.yaml
```
</details>

---

<details>
<summary><h2>5. CI/CD & Infrastructure as Code (Các bước tiếp theo)</h2></summary>

Tự động hóa tích hợp/triển khai và quản lý hạ tầng bằng code.

### 5.1. CI/CD (GitHub Actions Example)
```yaml
name: CI/CD Pipeline

on:
  push:
    branches: [ "main" ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
      - run: npm ci
      - run: npm test
```

### 5.2. Infrastructure as Code (Terraform)
Khởi tạo tài nguyên trên Cloud bằng code.
</details>