Skip to main content

使用 Docker 和 Kubernetes 部署 Nginx 集群

下面我将介绍如何使用 Docker 和 Kubernetes (k8s) 部署高可用的 Nginx 集群。

1. 准备工作

1.1 环境要求

  • 已安装 Docker
  • 已安装 Kubernetes 集群 (Minikube 或生产环境 k8s)
  • kubectl 命令行工具

2. 创建 Nginx Docker 镜像

2.1 自定义 Nginx Dockerfile

# Dockerfile
FROM nginx:latest

# 复制自定义配置
COPY nginx.conf /etc/nginx/nginx.conf
COPY conf.d/ /etc/nginx/conf.d/

# 暴露端口
EXPOSE 80 443

# 启动命令
CMD ["nginx", "-g", "daemon off;"]

构建镜像:

docker build -t my-nginx:1.0 .

3. Kubernetes 部署 Nginx 集群

3.1 创建 Deployment

# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: my-nginx:1.0
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5

应用部署:

kubectl apply -f nginx-deployment.yaml

3.2 创建 Service

# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

应用 Service:

kubectl apply -f nginx-service.yaml

4. 高级配置

4.1 水平自动扩展 (HPA)

# 创建 HPA
kubectl autoscale deployment nginx-deployment --cpu-percent=50 --min=2 --max=10

4.2 配置 ConfigMap

# nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    user  nginx;
    worker_processes  auto;
    
    events {
      worker_connections  1024;
    }
    
    http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
      
      server {
        listen       80;
        server_name  localhost;
        
        location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
        }
      }
    }

更新 Deployment 使用 ConfigMap:

# 在 deployment 的 containers 部分添加
volumeMounts:
- name: nginx-config
  mountPath: /etc/nginx/nginx.conf
  subPath: nginx.conf
volumes:
- name: nginx-config
  configMap:
    name: nginx-config

4.3 使用 Ingress 控制器

# nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-service
            port:
              number: 80

5. 监控和维护

5.1 查看 Pod 状态

kubectl get pods -l app=nginx

5.2 查看日志

kubectl logs -f <pod-name>

5.3 监控资源使用

kubectl top pods -l app=nginx

6. 生产环境建议

  1. 使用私有镜像仓库:避免从 Docker Hub 拉取镜像的限制
  2. 配置资源限制:防止单个 Pod 占用过多资源
  3. 启用滚动更新策略
    strategy:
      type: RollingUpdate
      rollingUpdate:
        maxSurge: 1
        maxUnavailable: 0
    
  4. 使用持久化存储:对于需要持久化的日志或数据
  5. 配置网络策略:限制 Pod 间的网络通信
  6. 启用 TLS:为 Ingress 配置 HTTPS

7. 清理资源

kubectl delete deployment nginx-deployment
kubectl delete service nginx-service
kubectl delete ingress nginx-ingress
kubectl delete configmap nginx-config

通过以上步骤,您可以在 Kubernetes 集群上部署一个高可用的 Nginx 服务,并根据需要进行扩展和配置。