登录
/
注册
首页
论坛
其它
首页
科技
业界
安全
程序
广播
Follow
关于
签到
每天签到奖励2-10圆
导读
排行榜
TG频道
发帖说明
登录
/
注册
账号
自动登录
找回密码
密码
登录
立即注册
搜索
搜索
关闭
CSDN热搜
程序园
精品问答
技术交流
资源下载
本版
帖子
用户
软件
问答
教程
代码
写记录
VIP申请
VIP网盘
网盘
联系我们
发帖说明
每日签到
道具
勋章
任务
淘帖
动态
分享
留言板
导读
设置
我的收藏
退出
腾讯QQ
微信登录
返回列表
首页
›
业界区
›
安全
›
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.85
master1
centos7.8
1.23.17
20.10.9
172.16.4.86
node1
centos7.8
1.23.17
20.10.9
172.16.4.87
node2
centos7.8
1.23.17
1.20.1
20.10.9
172.16.4.89
node3
centos7.8
1.23.17
1.20.1
20.10.9
172.16.4.90
node4
centos7.8
1.23.17
1.20.1
20.10.9
3.nginx集群部署
3.1 nfs csi部署
https://www.cnblogs.com/Leonardo-li/p/18813140
复制代码
3.2 创建namespace
kubectl create namespace nginx
复制代码
3.3 创建nginx configmap
# nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
namespace: nginx
data:
nginx.conf: |
user nginx;
worker_processes auto;
# 日志路径指向 /data/service/nginx/logs
error_log /data/service/nginx/logs/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/service/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream ltas-server {
server lt-algstore.ltzx.svc.cluster.local:8080;
}
upstream minio-server {
server minio-svc.minio.svc.cluster.local:9001;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
root /data/service/nginx/html;
try_files $uri $uri/ /index.html;
}
location /prod-api/ {
proxy_pass http://ltas-server/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location /prod-ws/ {
proxy_pass http://ltas-server/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_read_timeout 600s;
}
location /prod-file/ {
proxy_pass http://minio-server/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
复制代码
3.4 创建nginx html pvc
# html-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-html-pvc
namespace: nginx
spec:
accessModes:
- ReadWriteMany # 必须为多节点读写
storageClassName: nfs-csi
resources:
requests:
storage: 10Gi
复制代码
3.5 创建nginx service
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
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;'
# ng-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx
namespace: nginx
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
securityContext:
fsGroup: 1000
containers:
- name: nginx
image: 10.142.99.123:8060/public/nginx:v1.20.1
env:
- name: TZ
value: "Asia/Shanghai"
ports:
- containerPort: 39988
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;'
volumeMounts:
- name: host-timezone
mountPath: /etc/localtime
readOnly: true
- name: html-shared
mountPath: /data/service/nginx/html
- name: logs-volume
mountPath: /data/service/nginx/logs
- name: nginx-config
mountPath: /data/service/nginx/conf/nginx.conf
subPath: nginx.conf
# 关键修复:volumes 必须放在 Pod 模板内部
volumes:
- name: host-timezone
hostPath:
path: /etc/localtime
- name: html-shared
persistentVolumeClaim:
claimName: nginx-html-pvc
- name: nginx-config
configMap:
name: nginx-config
# volumeClaimTemplates 保持在 StatefulSet 顶层
volumeClaimTemplates:
- metadata:
name: logs-volume
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: nfs-csi
resources:
requests:
storage: 50Gi
复制代码
3.7 执行并创建各种资源类
kubectl apply -f ng-cm.yaml
kubectl apply -f ng-html-pvc.yaml
kubectl apply -f ng-svc.yaml
kubectl apply -f ng-sts.yaml
复制代码
3.8 验证状态
[root@master1 nginx-n6]# kubectl get pv | grep nginx
pvc-1200d1d4-6186-4629-9980-5372f3a7584c 50Gi RWO Retain Bound nginx/logs-volume-nginx-1 nfs-csi 48m
pvc-48f293ad-a6ae-4b57-883d-59e6797ce165 50Gi RWO Retain Bound nginx/logs-volume-nginx-2 nfs-csi 48m
pvc-6baae14c-0f7f-4251-8a1f-4606194677e7 10Gi RWX Retain Bound nginx/nginx-html-pvc nfs-csi 52m
pvc-dc0037af-7a9e-4547-9ea9-f3ecf692c335 50Gi RWO Retain Bound nginx/logs-volume-nginx-0 nfs-csi 48m
[root@master1 nginx-n6]# kubectl get pvc -n nginx
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
logs-volume-nginx-0 Bound pvc-dc0037af-7a9e-4547-9ea9-f3ecf692c335 50Gi RWO nfs-csi 48m
logs-volume-nginx-1 Bound pvc-1200d1d4-6186-4629-9980-5372f3a7584c 50Gi RWO nfs-csi 48m
logs-volume-nginx-2 Bound pvc-48f293ad-a6ae-4b57-883d-59e6797ce165 50Gi RWO nfs-csi 48m
nginx-html-pvc Bound pvc-6baae14c-0f7f-4251-8a1f-4606194677e7 10Gi RWX nfs-csi 52m
[root@master1 nginx-n6]# kubectl get sts -n nginx
NAME READY AGE
nginx 3/3 47m
[root@master1 nginx-n6]# kubectl get pods -n nginx
NAME READY STATUS RESTARTS AGE
nginx-0 1/1 Running 0 47m
nginx-1 1/1 Running 0 47m
nginx-2 1/1 Running 0 47m
[root@master1 nginx-n6]# kubectl get svc -n nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
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前端了。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
k8s
部署
nginx
集群
相关帖子
Kubernetes Ingress:管理集群外部访问的入口网关
读发布!设计与部署稳定的分布式系统(第2版)
docker部署showdoc以及linux网关配置
一文带你实现云上部署轻量化定制表单Docker
记录---图文并茂讲解nginx中http升级https(部署SSL证书)知识点总结
编译安装nginx
Nginx的ngx_thread_pool模块解析:提升性能与可伸缩性
nginx证书缓存功能
Nginx 缓存的工作原理
AWS EKS部署Prometheus和Grafana
vip免费申请,1年只需15美金$
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
照妖镜
相关推荐
业界
Kubernetes Ingress:管理集群外部访问的入口网关
0
763
滥眩
2025-10-09
安全
读发布!设计与部署稳定的分布式系统(第2版)
0
680
许娴广
2025-10-10
安全
docker部署showdoc以及linux网关配置
2
187
咫噎
2025-10-10
安全
一文带你实现云上部署轻量化定制表单Docker
0
756
刃减胸
2025-10-11
代码
记录---图文并茂讲解nginx中http升级https(部署SSL证书)知识点总结
0
118
甄婉丽
2025-10-11
安全
编译安装nginx
0
402
坠矜
2025-10-13
安全
Nginx的ngx_thread_pool模块解析:提升性能与可伸缩性
1
262
云卦逾
2025-10-16
业界
nginx证书缓存功能
0
384
梁丘艷蕙
2025-10-17
安全
Nginx 缓存的工作原理
0
706
雨角
2025-10-17
业界
AWS EKS部署Prometheus和Grafana
0
73
殳世英
2025-10-19
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
回复
本版积分规则
回帖并转播
回帖后跳转到最后一页
签约作者
程序园优秀签约作者
发帖
每捎京
2025-6-1 18:23:31
关注
0
粉丝关注
21
主题发布
板块介绍填写区域,请于后台编辑
财富榜{圆}
anyue1937
9994893
dage888
999994
3934307807
992122
4
富账慕
9977
5
邹语彤
9979
6
二艰糖
9997
7
刎唇
9993
8
匝抽
9986
9
聚怪闩
9960
10
孙淼淼
9977
查看更多