找回密码
 立即注册
首页 业界区 安全 k8s部署nginx集群

k8s部署nginx集群

每捎京 2025-6-1 18:23:31
1.nginx集群介绍

    在Kubernetes(k8s)中部署Nginx集群,是通过声明式配置实现高可用、可扩展的Web服务。其核心是通过​​Deployment​​管理Nginx容器副本的自动扩缩容和故障恢复,并借助​​Service​​提供负载均衡和统一的访问入口。Kubernetes的调度机制保障了集群的弹性与稳定性,支持滚动更新、资源限制、健康检查等关键特性,适用于生产环境的大规模流量分发与业务托管。
2.部署环境

IP节点操作系统k8s版本nginx版本
docker版本
172.16.4.85master1centos7.81.23.17 20.10.9
172.16.4.86node1centos7.81.23.17 20.10.9
172.16.4.87node2centos7.81.23.171.20.120.10.9
172.16.4.89node3centos7.81.23.171.20.120.10.9
172.16.4.90node4centos7.81.23.171.20.120.10.9
3.nginx集群部署

3.1 nfs csi部署 
  1. https://www.cnblogs.com/Leonardo-li/p/18813140
复制代码
3.2 创建namespace
  1. kubectl create namespace nginx
复制代码
3.3 创建nginx configmap
  1. # nginx-configmap.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5.   name: nginx-config
  6.   namespace: nginx
  7. data:
  8.   nginx.conf: |
  9.     user  nginx;
  10.     worker_processes  auto;
  11.     # 日志路径指向 /data/service/nginx/logs
  12.     error_log  /data/service/nginx/logs/error.log warn;
  13.     pid        /var/run/nginx.pid;
  14.     events {
  15.         worker_connections  1024;
  16.     }
  17.     http {
  18.         server_tokens off;
  19.         include       mime.types;
  20.         default_type  application/octet-stream;
  21.         log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  22.                         '$status $body_bytes_sent "$http_referer" '
  23.                         '"$http_user_agent" "$http_x_forwarded_for"';
  24.         access_log  /data/service/nginx/logs/access.log main;
  25.         sendfile        on;
  26.         keepalive_timeout  65;
  27.         map $http_upgrade $connection_upgrade {
  28.             default upgrade;
  29.             '' close;
  30.         }
  31.         upstream ltas-server {
  32.             server lt-algstore.ltzx.svc.cluster.local:8080;
  33.         }
  34.         upstream minio-server {
  35.             server minio-svc.minio.svc.cluster.local:9001;
  36.         }
  37.         server {
  38.             listen 80;
  39.             server_name localhost;
  40.             client_max_body_size 1024M;
  41.             location / {
  42.                 root   /data/service/nginx/html;
  43.                 try_files $uri $uri/ /index.html;
  44.             }
  45.             location /prod-api/ {
  46.                 proxy_pass http://ltas-server/;
  47.                 proxy_http_version 1.1;
  48.                 proxy_set_header Upgrade $http_upgrade;
  49.                 proxy_set_header Connection $connection_upgrade;
  50.             }
  51.             location /prod-ws/ {
  52.                 proxy_pass http://ltas-server/;
  53.                 proxy_http_version 1.1;
  54.                 proxy_set_header Host $host;
  55.                 proxy_set_header Upgrade $http_upgrade;
  56.                 proxy_set_header Connection 'upgrade';
  57.                 proxy_read_timeout 600s;
  58.             }
  59.             location /prod-file/ {
  60.                 proxy_pass http://minio-server/;
  61.                 proxy_set_header Host $http_host;
  62.                 proxy_set_header X-Real-IP $remote_addr;
  63.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  64.             }
  65.         }
  66.     }
复制代码
3.4 创建nginx html pvc
  1. # html-pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5.   name: nginx-html-pvc
  6.   namespace: nginx
  7. spec:
  8.   accessModes:
  9.     - ReadWriteMany  # 必须为多节点读写
  10.   storageClassName: nfs-csi
  11.   resources:
  12.     requests:
  13.       storage: 10Gi
复制代码
3.5 创建nginx service
  1. # nginx-service.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5.   name: nginx
  6.   namespace: nginx
  7. spec:
  8.   type: NodePort
  9.   selector:
  10.     app: nginx
  11.   ports:
  12.   - protocol: TCP
  13.     port: 80
  14.     targetPort: 80
  15.     nodePort: 30080  # 根据需求调整端口范围(30000-32767)
复制代码
3.6 创建nginx statefulset


  • 此段配置,是因为我在做nginx镜像的时候,没有将日志输出到前台,所以在执行kubectl logs的时候是没有输出的,所以在增加此段配置,来输出nginx日志
  • command: ["/bin/sh", "-c"]
            args:
              - |
                mkdir -p /data/service/nginx/logs
                ln -sf /dev/stdout /data/service/nginx/logs/access.log
                ln -sf /dev/stderr /data/service/nginx/logs/error.log
                exec nginx -g 'daemon off;'
  1. # ng-statefulset.yaml                    
  2. apiVersion: apps/v1                    
  3. kind: StatefulSet                    
  4. metadata:                    
  5.   name: nginx                    
  6.   namespace: nginx                    
  7. spec:                    
  8.   serviceName: nginx                    
  9.   replicas: 3                    
  10.   selector:                    
  11.     matchLabels:                    
  12.       app: nginx                    
  13.   template:                    
  14.     metadata:                    
  15.       labels:                    
  16.         app: nginx                    
  17.     spec:
  18.       securityContext:
  19.         fsGroup: 1000
  20.       containers:
  21.       - name: nginx
  22.         image: 10.142.99.123:8060/public/nginx:v1.20.1
  23.         env:
  24.         - name: TZ
  25.           value: "Asia/Shanghai"
  26.         ports:
  27.         - containerPort: 39988
  28.         command: ["/bin/sh", "-c"]
  29.         args:
  30.           - |
  31.             mkdir -p /data/service/nginx/logs
  32.             ln -sf /dev/stdout /data/service/nginx/logs/access.log
  33.             ln -sf /dev/stderr /data/service/nginx/logs/error.log
  34.             exec nginx -g 'daemon off;'
  35.         volumeMounts:
  36.         - name: host-timezone
  37.           mountPath: /etc/localtime
  38.           readOnly: true
  39.         - name: html-shared
  40.           mountPath: /data/service/nginx/html
  41.         - name: logs-volume
  42.           mountPath: /data/service/nginx/logs
  43.         - name: nginx-config
  44.           mountPath: /data/service/nginx/conf/nginx.conf
  45.           subPath: nginx.conf
  46.       # 关键修复:volumes 必须放在 Pod 模板内部
  47.       volumes:
  48.       - name: host-timezone
  49.         hostPath:
  50.           path: /etc/localtime
  51.       - name: html-shared
  52.         persistentVolumeClaim:
  53.           claimName: nginx-html-pvc
  54.       - name: nginx-config
  55.         configMap:
  56.           name: nginx-config
  57.   # volumeClaimTemplates 保持在 StatefulSet 顶层
  58.   volumeClaimTemplates:
  59.   - metadata:
  60.       name: logs-volume
  61.     spec:
  62.       accessModes: [ "ReadWriteOnce" ]
  63.       storageClassName: nfs-csi
  64.       resources:
  65.         requests:
  66.           storage: 50Gi
复制代码
3.7 执行并创建各种资源类
  1. kubectl apply -f ng-cm.yaml  
  2. kubectl apply -f ng-html-pvc.yaml  
  3. kubectl apply -f ng-svc.yaml
  4. kubectl apply -f ng-sts.yaml  
复制代码
3.8 验证状态
  1. [root@master1 nginx-n6]# kubectl get pv | grep nginx
  2. pvc-1200d1d4-6186-4629-9980-5372f3a7584c   50Gi       RWO            Retain           Bound    nginx/logs-volume-nginx-1                nfs-csi                 48m
  3. pvc-48f293ad-a6ae-4b57-883d-59e6797ce165   50Gi       RWO            Retain           Bound    nginx/logs-volume-nginx-2                nfs-csi                 48m
  4. pvc-6baae14c-0f7f-4251-8a1f-4606194677e7   10Gi       RWX            Retain           Bound    nginx/nginx-html-pvc                     nfs-csi                 52m
  5. pvc-dc0037af-7a9e-4547-9ea9-f3ecf692c335   50Gi       RWO            Retain           Bound    nginx/logs-volume-nginx-0                nfs-csi                 48m
  6. [root@master1 nginx-n6]# kubectl get pvc -n nginx
  7. NAME                  STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
  8. logs-volume-nginx-0   Bound    pvc-dc0037af-7a9e-4547-9ea9-f3ecf692c335   50Gi       RWO            nfs-csi        48m
  9. logs-volume-nginx-1   Bound    pvc-1200d1d4-6186-4629-9980-5372f3a7584c   50Gi       RWO            nfs-csi        48m
  10. logs-volume-nginx-2   Bound    pvc-48f293ad-a6ae-4b57-883d-59e6797ce165   50Gi       RWO            nfs-csi        48m
  11. nginx-html-pvc        Bound    pvc-6baae14c-0f7f-4251-8a1f-4606194677e7   10Gi       RWX            nfs-csi        52m
  12. [root@master1 nginx-n6]# kubectl get sts -n nginx
  13. NAME    READY   AGE
  14. nginx   3/3     47m
  15. [root@master1 nginx-n6]# kubectl get pods -n nginx
  16. NAME      READY   STATUS    RESTARTS   AGE
  17. nginx-0   1/1     Running   0          47m
  18. nginx-1   1/1     Running   0          47m
  19. nginx-2   1/1     Running   0          47m
  20. [root@master1 nginx-n6]# kubectl get svc -n nginx
  21. NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE
  22. nginx   NodePort   10.102.171.84   <none>        80:30080/TCP   88m
复制代码
3.9 前端页面目录


  • 因为我的nginx是需要做web服务器的,所以将业务的前端放到nfs csi的自动创建的pvc(nginx-html-pvc)中,它对应的pv是挂载到容器中的html目录的,这样就可以正常访问了nginx发布的web前端了。
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册