redis、LVS、nginx的基本使用方法

打印 上一主题 下一主题

主题 523|帖子 523|积分 1569

redis、LVS、nginx

redis搭建哨兵原理

哨兵搭建至少要有3个机器,且必须为奇数个

redis搭建哨兵之前要先实现主从复制;master的配置文件中的masterauth和slave都必须相同
实现主从复制之所有从节点配置文件
  1. [root@slave ~]# yum install -y redis
  2. [root@slave ~]# vim /etc/redis.conf
  3. replicaof 10.0.0.8 6379
  4. masterauth "123456"
  5. [root@server2 ~]# systemctl enable redis --now
复制代码
所有主从节点配置
  1. [root@centos8 ~]# vim /etc/redis.conf
  2. bind 0.0.0.0
  3. masterauth "123456"
  4. requirepass "123456"
复制代码
主从复制实现
  1. [root@centos8 ~]# redis-cli
  2. 127.0.0.1:6379> auth 123456
  3. OK
  4. 127.0.0.1:6379> info replication
  5. # Replication
  6. role:master
  7. connected_slaves:2
  8. slave0:ip=10.0.0.12,port=6379,state=online,offset=24332,lag=1
  9. slave1:ip=10.0.0.9,port=6379,state=online,offset=24332,lag=1
  10. master_replid:a2bbc2342854b7f8fb2f7b15623cd0645a946c0c
  11. master_replid2:0000000000000000000000000000000000000000
  12. master_repl_offset:24332
  13. second_repl_offset:-1
  14. repl_backlog_active:1
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:1
  17. repl_backlog_histlen:24332
复制代码
哨兵配置
所有redis节点使用相同的配置文件
  1. [root@server1 ~]# grep -vE "^$|^#" /etc/redis-sentinel.conf
  2. sentinel monitor mymaster 10.0.0.8 6379 2
  3. sentinel down-after-milliseconds mymaster 3000
  4. sentinel auth-pass mymaster 123456
  5. [root@centos8 ~]# scp /etc/redis-sentinel.conf 10.0.0.9:/etc/
  6. [root@centos8 ~]# scp /etc/redis-sentinel.conf 10.0.0.12:/etc/
  7. [root@centos8 ~]# systemctl enable --now redis-sentinel.service
  8. [root@slave1 ~]# systemctl enable --now redis-sentinel.service
  9. [root@slave2 ~]# systemctl enable --now redis-sentinel.service
复制代码
查看sentinel状态
  1. [root@server1 ~]# redis-cli -p 26379
  2. 127.0.0.1:26379> INFO sentinel
  3. # Sentinel
  4. sentinel_masters:1
  5. sentinel_tilt:0
  6. sentinel_running_scripts:0
  7. sentinel_scripts_queue_length:0
  8. sentinel_simulate_failure_flags:0
  9. master0:name=mymaster,status=ok,address=10.0.0.8:6379,slaves=2,sentinels=3
复制代码
制止主节点实现故障转移
  1. [root@server1 ~]# killall redis-server
  2. [root@server1 ~]# redis-cli -a 123456 -p 26379
  3. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  4. 127.0.0.1:26379> INFO sentinel
  5. # Sentinel
  6. sentinel_masters:1
  7. sentinel_tilt:0
  8. sentinel_running_scripts:0
  9. sentinel_scripts_queue_length:0
  10. sentinel_simulate_failure_flags:0
  11. master0:name=mymaster,status=ok,address=10.0.0.12:6379,slaves=2,sentinels=3
复制代码
日志跟踪
  1. [root@centos8 ~]# tail  -f /var/log/redis/sentinel.log
  2. 87189:X 09 Jan 2024 20:41:45.783 # Configuration loaded
  3. 87189:X 09 Jan 2024 20:41:45.783 * supervised by systemd, will signal readiness
  4. 87189:X 09 Jan 2024 20:41:45.785 * Running mode=sentinel, port=26379.
  5. 87189:X 09 Jan 2024 20:41:45.785 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  6. 87189:X 09 Jan 2024 20:41:45.789 # Sentinel ID is 33700c2c21a86f0e15975e23e0ab04409e0f744c
  7. 87189:X 09 Jan 2024 20:41:45.789 # +monitor master mymaster 10.0.0.8 6379 quorum 2
  8. 87189:X 09 Jan 2024 20:41:45.791 * +slave slave 10.0.0.12:6379 10.0.0.12 6379 @ mymaster 10.0.0.8 6379
  9. 87189:X 09 Jan 2024 20:41:45.792 * +slave slave 10.0.0.9:6379 10.0.0.9 6379 @ mymaster 10.0.0.8 6379
  10. 87189:X 09 Jan 2024 20:42:06.649 * +sentinel sentinel 777f3b239c0c58b3a997366fa3d8f67c30b1d2ca 10.0.0.9 26379 @ mymaster 10.0.0.8 6379
  11. 87189:X 09 Jan 2024 20:42:10.529 * +sentinel sentinel aa648c3792f52f620ab0c9e34365177ead8adec6 10.0.0.12 26379 @ mymaster 10.0.0.8 6379
  12. 87189:X 09 Jan 2024 21:39:19.980 # +new-epoch 1
  13. 87189:X 09 Jan 2024 21:39:19.982 # +vote-for-leader aa648c3792f52f620ab0c9e34365177ead8adec6 1
  14. 87189:X 09 Jan 2024 21:39:19.991 # +sdown master mymaster 10.0.0.8 6379
  15. 87189:X 09 Jan 2024 21:39:20.063 # +odown master mymaster 10.0.0.8 6379 #quorum 3/2
  16. 87189:X 09 Jan 2024 21:39:20.063 # Next failover delay: I will not start a failover before Tue Jan  9 21:45:20 2024
  17. 87189:X 09 Jan 2024 21:39:21.068 # +config-update-from sentinel aa648c3792f52f620ab0c9e34365177ead8adec6 10.0.0.12 26379 @ mymaster 10.0.0.8 6379
  18. 87189:X 09 Jan 2024 21:39:21.068 # +switch-master mymaster 10.0.0.8 6379 10.0.0.12 6379
  19. 87189:X 09 Jan 2024 21:39:21.069 * +slave slave 10.0.0.9:6379 10.0.0.9 6379 @ mymaster 10.0.0.12 6379
  20. 87189:X 09 Jan 2024 21:39:21.069 * +slave slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.12 6379
  21. 87189:X 09 Jan 2024 21:39:24.133 # +sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.12 6379
复制代码
文件的内容会被自动修改
  1. [root@slave1 ~]# grep ^replicaof /etc/redis.conf
  2. replicaof 10.0.0.12 6379
复制代码
哨兵文件也会被修改
  1. [root@slave1 ~]# grep "^[a-z]"  /etc/redis-sentinel.conf
  2. port 26379
  3. daemonize no
  4. pidfile "/var/run/redis-sentinel.pid"
  5. logfile "/var/log/redis/sentinel.log"
  6. dir "/tmp"
  7. sentinel myid 777f3b239c0c58b3a997366fa3d8f67c30b1d2ca
  8. sentinel deny-scripts-reconfig yes
  9. sentinel monitor mymaster 10.0.0.12 6379 2
  10. sentinel down-after-milliseconds mymaster 3000
  11. sentinel auth-pass mymaster 123456
  12. sentinel config-epoch mymaster 1
  13. protected-mode no
  14. supervised systemd
  15. sentinel leader-epoch mymaster 1
  16. sentinel known-replica mymaster 10.0.0.8 6379
  17. sentinel known-replica mymaster 10.0.0.9 6379
  18. sentinel known-sentinel mymaster 10.0.0.12 26379 aa648c3792f52f620ab0c9e34365177ead8adec6
  19. sentinel known-sentinel mymaster 10.0.0.8 26379 33700c2c21a86f0e15975e23e0ab04409e0f744c
  20. sentinel current-epoch 1
复制代码
  1. [root@slave2 ~]# grep "^[a-z]" /etc/redis-sentinel.conf
  2. port 26379
  3. daemonize no
  4. pidfile "/var/run/redis-sentinel.pid"
  5. logfile "/var/log/redis/sentinel.log"
  6. dir "/tmp"
  7. sentinel myid aa648c3792f52f620ab0c9e34365177ead8adec6
  8. sentinel deny-scripts-reconfig yes
  9. sentinel monitor mymaster 10.0.0.12 6379 2
  10. sentinel down-after-milliseconds mymaster 3000
  11. sentinel auth-pass mymaster 123456
  12. sentinel config-epoch mymaster 1
  13. protected-mode no
  14. supervised systemd
  15. sentinel leader-epoch mymaster 1
  16. sentinel known-replica mymaster 10.0.0.8 6379
  17. sentinel known-replica mymaster 10.0.0.9 6379
  18. sentinel known-sentinel mymaster 10.0.0.9 26379 777f3b239c0c58b3a997366fa3d8f67c30b1d2ca
  19. sentinel known-sentinel mymaster 10.0.0.8 26379 33700c2c21a86f0e15975e23e0ab04409e0f744c
  20. sentinel current-epoch 1
复制代码
新的master状态
  1. [root@slave2 ~]# redis-cli -a 123456
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. 127.0.0.1:6379> INFO replication
  4. # Replication
  5. role:master
  6. connected_slaves:1
  7. slave0:ip=10.0.0.9,port=6379,state=online,offset=921399,lag=0
  8. master_replid:a529afbe029c99f25d442dd97046ae03ea369c6c
  9. master_replid2:a2bbc2342854b7f8fb2f7b15623cd0645a946c0c
  10. master_repl_offset:921399
  11. second_repl_offset:694285
  12. repl_backlog_active:1
  13. repl_backlog_size:1048576
  14. repl_backlog_first_byte_offset:309
  15. repl_backlog_histlen:921091
复制代码
redis集群实现


6个集群节点
  1. 10.0.0.8
  2. 10.0.0.9
  3. 10.0.0.10
  4. 10.0.0.11
  5. 10.0.0.12
  6. 10.0.0.13
复制代码
创建集群
  1. [root@centos8 ~]# redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.9:6379 10.0.0.10:6379 10.0.0.11:6379 10.0.0.12:6379 10.0.0.13:6379 --cluster-replicas 1
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Master[0] -> Slots 0 - 5460
  5. Master[1] -> Slots 5461 - 10922
  6. Master[2] -> Slots 10923 - 16383
  7. Adding replica 10.0.0.11:6379 to 10.0.0.8:6379
  8. Adding replica 10.0.0.12:6379 to 10.0.0.9:6379
  9. Adding replica 10.0.0.13:6379 to 10.0.0.10:6379
  10. M: b2672437f224c869af7f2634e2edcf39c419b9d8 10.0.0.8:6379
  11.    slots:[0-5460] (5461 slots) master
  12. M: 67de6b7274ce80052a7ef44511568ccc4f031859 10.0.0.9:6379
  13.    slots:[5461-10922] (5462 slots) master
  14. M: a126ea9c3b933c448c32a30a5bac9ab2635b446f 10.0.0.10:6379
  15.    slots:[10923-16383] (5461 slots) master
  16. S: 9e28728a72a83ed93112162803beb9ea0773cb23 10.0.0.11:6379
  17.    replicates b2672437f224c869af7f2634e2edcf39c419b9d8
  18. S: 12f7df4f822a27b23458103a7a63534d0e9f8af4 10.0.0.12:6379
  19.    replicates 67de6b7274ce80052a7ef44511568ccc4f031859
  20. S: 17361c4bf708f52830bce1969618105027a34042 10.0.0.13:6379
  21.    replicates a126ea9c3b933c448c32a30a5bac9ab2635b446f
  22. Can I set the above configuration? (type 'yes' to accept): yes
  23. >>> Nodes configuration updated
  24. >>> Assign a different config epoch to each node
  25. >>> Sending CLUSTER MEET messages to join the cluster
  26. Failed to send CLUSTER MEET command.
复制代码
出现了错误,由于哨兵模式sentinel在运行,杀掉该进程。
  1. [root@centos8 ~]# ps -aux | grep redis
  2. redis       1086  0.2  0.5 263696  4700 ?        Ssl  15:57   0:01 /usr/bin/redis-sentinel *:26379 [sentinel]
  3. redis       2778  0.1  1.1 266256  8768 ?        Ssl  15:59   0:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
  4. root        2843  0.0  0.1 221940  1096 pts/0    S+   16:05   0:00 grep --color=auto redis
  5. [root@centos8 ~]# kill -9 26379
  6. -bash: kill: (26379) - No such process
  7. [root@centos8 ~]# kill -9 1086
复制代码
继续
  1. [root@centos8 ~]# redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.9:6379 10.0.0.10:6379 10.0.0.11:6379 10.0.0.12:6379 10.0.0.13:6379 --cluster-replicas 1
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Master[0] -> Slots 0 - 5460
  5. Master[1] -> Slots 5461 - 10922
  6. Master[2] -> Slots 10923 - 16383
  7. Adding replica 10.0.0.11:6379 to 10.0.0.8:6379
  8. Adding replica 10.0.0.12:6379 to 10.0.0.9:6379
  9. Adding replica 10.0.0.13:6379 to 10.0.0.10:6379
  10. M: b2672437f224c869af7f2634e2edcf39c419b9d8 10.0.0.8:6379
  11.    slots:[0-5460] (5461 slots) master
  12. M: 67de6b7274ce80052a7ef44511568ccc4f031859 10.0.0.9:6379
  13.    slots:[5461-10922] (5462 slots) master
  14. M: a126ea9c3b933c448c32a30a5bac9ab2635b446f 10.0.0.10:6379
  15.    slots:[10923-16383] (5461 slots) master
  16. S: 9e28728a72a83ed93112162803beb9ea0773cb23 10.0.0.11:6379
  17.    replicates b2672437f224c869af7f2634e2edcf39c419b9d8
  18. S: 12f7df4f822a27b23458103a7a63534d0e9f8af4 10.0.0.12:6379
  19.    replicates 67de6b7274ce80052a7ef44511568ccc4f031859
  20. S: 17361c4bf708f52830bce1969618105027a34042 10.0.0.13:6379
  21.    replicates a126ea9c3b933c448c32a30a5bac9ab2635b446f
  22. Can I set the above configuration? (type 'yes' to accept): yes
  23. >>> Nodes configuration updated
  24. >>> Assign a different config epoch to each node
  25. >>> Sending CLUSTER MEET messages to join the cluster
  26. Waiting for the cluster to join
  27. .....
  28. >>> Performing Cluster Check (using node 10.0.0.8:6379)
  29. M: b2672437f224c869af7f2634e2edcf39c419b9d8 10.0.0.8:6379
  30.    slots:[0-5460] (5461 slots) master
  31.    1 additional replica(s)
  32. M: a126ea9c3b933c448c32a30a5bac9ab2635b446f 10.0.0.10:6379
  33.    slots:[10923-16383] (5461 slots) master
  34.    1 additional replica(s)
  35. M: 67de6b7274ce80052a7ef44511568ccc4f031859 10.0.0.9:6379
  36.    slots:[5461-10922] (5462 slots) master
  37.    1 additional replica(s)
  38. S: 17361c4bf708f52830bce1969618105027a34042 10.0.0.13:6379
  39.    slots: (0 slots) slave
  40.    replicates a126ea9c3b933c448c32a30a5bac9ab2635b446f
  41. S: 9e28728a72a83ed93112162803beb9ea0773cb23 10.0.0.11:6379
  42.    slots: (0 slots) slave
  43.    replicates b2672437f224c869af7f2634e2edcf39c419b9d8
  44. S: 12f7df4f822a27b23458103a7a63534d0e9f8af4 10.0.0.12:6379
  45.    slots: (0 slots) slave
  46.    replicates 67de6b7274ce80052a7ef44511568ccc4f031859
  47. [OK] All nodes agree about slots configuration.
  48. >>> Check for open slots...
  49. >>> Check slots coverage...
  50. [OK] All 16384 slots covered.
复制代码
查看指定master节点的slave节点信息
  1. [root@centos8 ~]# redis-cli -a 123456 cluster nodes
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. a126ea9c3b933c448c32a30a5bac9ab2635b446f 10.0.0.10:6379@16379 master - 0 1704984060648 3 connected 10923-16383
  4. 67de6b7274ce80052a7ef44511568ccc4f031859 10.0.0.9:6379@16379 master - 0 1704984062664 2 connected 5461-10922
  5. 17361c4bf708f52830bce1969618105027a34042 10.0.0.13:6379@16379 slave a126ea9c3b933c448c32a30a5bac9ab2635b446f 0 1704984061654 6 connected
  6. 9e28728a72a83ed93112162803beb9ea0773cb23 10.0.0.11:6379@16379 slave b2672437f224c869af7f2634e2edcf39c419b9d8 0 1704984060000 4 connected
  7. 12f7df4f822a27b23458103a7a63534d0e9f8af4 10.0.0.12:6379@16379 slave 67de6b7274ce80052a7ef44511568ccc4f031859 0 1704984059639 5 connected
  8. b2672437f224c869af7f2634e2edcf39c419b9d8 10.0.0.8:6379@16379 myself,master - 0 1704984061000 1 connected 0-5460
复制代码
验证集群状态
  1. [root@centos8 ~]# redis-cli -a 123456 cluster info
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. cluster_state:ok
  4. cluster_slots_assigned:16384
  5. cluster_slots_ok:16384
  6. cluster_slots_pfail:0
  7. cluster_slots_fail:0
  8. cluster_known_nodes:6
  9. cluster_size:3
  10. cluster_current_epoch:6
  11. cluster_my_epoch:1
  12. cluster_stats_messages_ping_sent:6247
  13. cluster_stats_messages_pong_sent:6168
  14. cluster_stats_messages_sent:12415
  15. cluster_stats_messages_ping_received:6163
  16. cluster_stats_messages_pong_received:6247
  17. cluster_stats_messages_meet_received:5
  18. cluster_stats_messages_received:12415
复制代码
尝试写入数据
  1. [root@centos8 ~]# redis-cli -a 123456 -h 10.0.0.9 set car bwm
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. OK
  4. [root@centos8 ~]# redis-cli -a 123456 -h 10.0.0.10 set bb cc
  5. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  6. (error) MOVED 8620 10.0.0.9:6379   #经过算法计算,需要写入指定的槽位对应node
  7. [root@centos8 ~]# redis-cli -a 123456 -h 10.0.0.9 set bb cc
  8. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  9. OK
复制代码
LVS集群的工作模式

nat:修改请求报文的目标IP,多目标IP的DNAT
本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发
  1. 1、RIP和DIP应在同一个IP网络,且应使用私网地址;RS的网关要指向DIP
  2. 2、请求报文和响应报文都必须有Diretor转发,Diretor易于成为系统瓶颈
  3. 3、支持端口映射,可修改请求报文的目标PORT
  4. 4、VS必须是Linux系统,RS可以是任意OS系统
复制代码

dr:操纵封装新的MAC地址
直接路由,LVS默认模式,应用最广泛,通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变。

tun:在原请求IP报文之外新加一个IP首部
转发方式:不修改请求报文的IP首部(源IP为CIP,目标IP为VIP),而在原IP报文之外再封装一个IP首部(源IP是DIP,目标为RIP),将报文发往挑选出的目标RS;RS直接相应给客户端(源IP是VIP,目标IP是CIP)

fullnat:修改请求报文的源和目标IP,默认内核不支持
通过同时修改请求报文的源IP地址和目标地址进行转发
CIP --> DIP
VIP --> RIP

LVS调度算法

ipvs scheduler:根据其调度时是否思量各RS当前的负载状态
分为两种:静态方法和动态方法
静态方法


  • RR:roundrobin,轮询,较常用,雨露均沾
  • WRR:weighted RR,加权轮询,较常用
  • SH:Source Hashing,实现session sticky,源IP地址hash;将来自于同一个IP地址的请求始终发往第一次挑中的RS,从而实现会话绑定
  • DH:Destination Hashing;目标地址哈希,第一次轮询调度至RS,后续将发往同一个目标地址的请求始终转发至第一次挑中的RS,典型使用场景是正向署理缓存场景中的负载均衡,如:Web缓存
动态方法


  • LC:least connections 适用于长连接应用
  • WLC:Weighted LC,默认调度方法,较常用
  • SED:Shortest Expection Delay,初始连接高权重优先,只检查活动连接,而不思量非活动连接
  • NQ:Never Queue,第一轮匀称分配,后续SED
  • LBLC:Locality-Based LC,动态的DH算法,使用场景:根据负载状态实现正向署理,实现Web、Cache等
  • LBLCR:LBLC with Replication,带复制功能的LBLC,解决LBLC负载不均衡问题,从负载重的复制到负载轻的RS,实现Web Cache等
LVS-NAT


注意·:rs-server只配置一个IP地址,不要多配会造成访问失败。
准备四台主机
  1. client:192.168.0.130/24
  2. lvs:
  3.         eth0:192.168.0.132/24
  4.         eth1:10.0.0.13/24
  5. rs1:10.0.0.9/24  GW:10.0.0.13
  6. rs2:10.0.0.11/24  GW:10.0.0.13
复制代码
配置IP地址
  1. [root@internet network-scripts]# cat ifcfg-eth0
  2. TYPE=Ethernet
  3. PROXY_METHOD=none
  4. BROWSER_ONLY=no
  5. BOOTPROTO=static
  6. IPADDR=192.168.0.130
  7. NETMASK=255.255.255.0
  8. DEFROUTE=yes
  9. NAME=eth0
  10. DEVICE=eth0
  11. ONBOOT=yes
  12. [root@lvs-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  13. TYPE=Ethernet
  14. PROXY_METHOD=none
  15. BROWSER_ONLY=no
  16. BOOTPROTO=static
  17. DEFROUTE=yes
  18. NAME=eth0
  19. DEVICE=eth0
  20. IPADDR=192.168.0.132
  21. NETMASK=255.255.255.0
  22. DNS1=8.8.8.8
  23. DNS2=114.114.114.114
  24. ONBOOT=yes
  25. [root@lvs-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
  26. TYPE=Ethernet
  27. PROXY_METHOD=none
  28. BROWSER_ONLY=no
  29. BOOTPROTO=static
  30. DEFROUTE=yes
  31. IPADDR=10.0.0.13
  32. NETMASK=255.255.255.0
  33. NAME=eth1
  34. DEVICE=eth1
  35. ONBOOT=yes
  36. [root@rs1-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
  37. TYPE=Ethernet
  38. BOOTPROTO=static
  39. IPADDR=10.0.0.9
  40. GATEWAY=10.0.0.13
  41. NETMASK=255.255.255.0
  42. NAME=eth1
  43. DEVICE=eth1
  44. ONBOOT=yes
  45. [root@rs2-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
  46. TYPE=Ethernet
  47. BOOTPROTO=static
  48. DEVICE=eth1
  49. ONBOOT=yes
  50. NAME=eth1
  51. IPADDR=10.0.0.11
  52. GATEWAY=10.0.0.13
  53. NETMASK=255.255.255.0
复制代码
lvs配置
  1. [root@lvs-server /]# hostname -I
  2. 192.168.0.132 10.0.0.13
  3. [root@lvs-server ~]# cat  /etc/sysctl.conf
  4. net.ipv4.ip_forward = 1
  5. [root@lvs-server ~]# sysctl -p
  6. net.ipv4.ip_forward = 1
  7. [root@lvs-server /]# ipvsadm -A -t 192.168.0.132:80 -s wrr
  8. [root@lvs-server /]# ipvsadm -a -t 192.168.0.132:80 -r 10.0.0.9:80 -m
  9. [root@lvs-server /]# ipvsadm -a -t 192.168.0.132:80 -r 10.0.0.9:17 -m
  10. [root@lvs-server /]# ipvsadm -Ln
  11. IP Virtual Server version 1.2.1 (size=4096)
  12. Prot LocalAddress:Port Scheduler Flags
  13.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  14. TCP  192.168.0.132:80 wrr
  15.   -> 10.0.0.9:80                  Masq    1      0          0         
  16.   -> 10.0.0.11:80                  Masq    1      0          0
复制代码
rs服务器配置
  1. [root@rs1-server ~]# yum install httpd -y
  2. [root@rs1-server ~]# echo "`hostname` rs1 "  >> /var/www/html/index.html
  3. [root@rs1-server ~]# systemctl enable httpd --now
复制代码
  1. [root@rs2-server ~]# yum install httpd -y
  2. [root@rs2-server ~]# echo "`hostname` rs1 "  >> /var/www/html/index.html
  3. [root@rs2-server ~]# systemctl enable httpd --now
复制代码
测试
  1. [root@internet ~]# while  :; do  curl 192.168.0.132; sleep 0.5;done
  2. rs2-server rs2
  3. rs1-server rs1
  4. rs2-server rs2
  5. rs1-server rs1
  6. rs2-server rs2
  7. rs1-server rs1
  8. rs2-server rs2
  9. rs1-server rs1
  10. rs2-server rs2
  11. rs1-server rs1
  12. rs2-server rs2
复制代码
永久生存?
  1. [root@lvs-server ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
  2. [root@lvs-server ~]# systemctl enable ipvsadm --now
复制代码
LVS-DR模式单网段


准备5台主机
  1. Client:        仅主机 10.0.0.10   GW:10.0.0.8
  2. Router: eth0: NAT 192.168.0.128/24  eth1: 仅主机 10.0.0.8/24   开启IP_forward
  3. LVS:eth0: NAT 192.168.0.132/24 GW: 192.168.0.128
  4. RS1:eth0: NAT 192.168.0.129/24 GW: 192.168.0.128
  5. RS2:eth0: NAT 192.168.0.131/24 GW: 192.168.0.128
复制代码
LVS配置网络
  1. [root@internet ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
  2. TYPE=Ethernet
  3. PROXY_METHOD=none
  4. BROWSER_ONLY=no
  5. BOOTPROTO=static
  6. IPADDR=10.0.0.10
  7. GATEWAY=10.0.0.8
  8. NETMASK=255.255.255.0
  9. DEFROUTE=yes
  10. NAME=eth1
  11. DEVICE=eth1
  12. ONBOOT=yes
  13. [root@router-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  14. TYPE=Ethernet
  15. BOOTPROTO=static
  16. IPADDR=192.168.0.128
  17. NETMASK=255.255.255.0
  18. NAME=eth0
  19. DEVICE=eth0
  20. ONBOOT=yes
  21. [root@router-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
  22. TYPE=Ethernet
  23. PROXY_METHOD=none
  24. BROWSER_ONLY=no
  25. BOOTPROTO=static
  26. IPADDR=10.0.0.8
  27. NETMASK=255.255.255.0
  28. NAME=eth1
  29. DEVICE=eth1
  30. ONBOOT=yes
  31. [root@router-server ~]# cat /etc/sysctl.conf         #开启带内转发
  32. net.ipv4.ip_forward = 1
  33. [root@router-server ~]# sysctl -p
  34. net.ipv4.ip_forward = 1
  35. [root@lvs-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  36. TYPE=Ethernet
  37. PROXY_METHOD=none
  38. BROWSER_ONLY=no
  39. BOOTPROTO=static
  40. DEFROUTE=yes
  41. NAME=eth0
  42. DEVICE=eth0
  43. IPADDR=192.168.0.132
  44. NETMASK=255.255.255.0
  45. GATEWAY=192.168.0.128
  46. ONBOOT=yes
  47. [root@rs1-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  48. TYPE=Ethernet
  49. PROXY_METHOD=none
  50. BROWSER_ONLY=no
  51. BOOTPROTO=static
  52. IPADDR=192.168.0.129
  53. NETMASK=255.255.255.0
  54. GATEWAY=192.168.0.128
  55. NAME=eth0
  56. DEVICE=eth0
  57. ONBOOT=yes
  58. [root@rs2-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  59. TYPE=Ethernet
  60. PROXY_METHOD=none
  61. BROWSER_ONLY=no
  62. BOOTPROTO=dhcp
  63. NAME=eth0
  64. DEVICE=eth0
  65. IPADDR=192.168.0.131
  66. NETMASK=255.255.255.0
  67. GATEWAY=192.168.0.128
  68. ONBOOT=yes
复制代码
RS两台服务器
  1. [root@rs1-server ~]# yum install httpd -y
  2. [root@rs1-server ~]# echo "`hostname` rs1 "  >> /var/www/html/index.html
  3. [root@rs1-server ~]# systemctl enable httpd --now [root@rs1-server ~]# ping 10.0.0.10 -c1PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.64 bytes from 10.0.0.10: icmp_seq=1 ttl=63 time=0.637 ms[root@rs2-server ~]# yum install httpd -y
  4. [root@rs2-server ~]# echo "`hostname` rs1 "  >> /var/www/html/index.html
  5. [root@rs2-server ~]# systemctl enable httpd --now [root@rs2-server ~]# ping 10.0.0.10 -c1PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.64 bytes from 10.0.0.10: icmp_seq=1 ttl=63 time=0.910 ms
复制代码
RS的IPVS配置

RS1配置
  1. [root@rs1-server ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  2. [root@rs1-server ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
  3. [root@rs1-server ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  4. [root@rs1-server ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  5. [root@rs1-server ~]# hostname -I
  6. 192.168.0.129
  7. [root@rs1-server ~]# ifconfig lo:1 192.168.0.100/32
  8. [root@rs1-server ~]# ip a
  9. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  10.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  11.     inet 127.0.0.1/8 scope host lo
  12.        valid_lft forever preferred_lft forever
  13.     inet 192.168.0.100/0 scope global lo:1
  14.        valid_lft forever preferred_lft forever
  15.     inet6 ::1/128 scope host
  16.        valid_lft forever preferred_lft forever
  17. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  18.     link/ether 00:0c:29:cf:95:47 brd ff:ff:ff:ff:ff:ff
  19.     altname enp3s0
  20.     altname ens160
  21.     inet 192.168.0.129/24 brd 192.168.0.255 scope global noprefixroute eth0
  22.        valid_lft forever preferred_lft forever
  23.     inet6 fe80::20c:29ff:fecf:9547/64 scope link
  24.        valid_lft forever preferred_lft forever
复制代码
RS2配置
  1. [root@rs2-server ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
  2. [root@rs2-server ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  3. [root@rs2-server ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
  4. [root@rs2-server ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  5. [root@rs2-server ~]# ifconfig lo:1 192.168.0.100/32
  6. [root@rs2-server ~]# ip a
  7. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  8.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  9.     inet 127.0.0.1/8 scope host lo
  10.        valid_lft forever preferred_lft forever
  11.     inet 192.168.0.100/0 scope global lo:1
  12.        valid_lft forever preferred_lft forever
  13.     inet6 ::1/128 scope host
  14.        valid_lft forever preferred_lft forever
  15. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  16.     link/ether 00:0c:29:43:14:7d brd ff:ff:ff:ff:ff:ff
  17.     altname enp3s0
  18.     altname ens160
  19.     inet 192.168.0.131/24 brd 192.168.0.255 scope global noprefixroute eth0
  20.        valid_lft forever preferred_lft forever
  21.     inet6 fe80::20c:29ff:fe43:147d/64 scope link
  22.        valid_lft forever preferred_lft forever
复制代码
LVS主机配置
  1. [root@lvs-server ~]# ifconfig lo:1 192.168.0.100/32
  2. [root@lvs-server ~]# ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  4.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5.     inet 127.0.0.1/8 scope host lo
  6.        valid_lft forever preferred_lft forever
  7.     inet 192.168.0.100/0 scope global lo:1
  8.        valid_lft forever preferred_lft forever
  9.     inet6 ::1/128 scope host
  10.        valid_lft forever preferred_lft forever
  11. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  12.     link/ether 00:0c:29:b1:38:0c brd ff:ff:ff:ff:ff:ff
  13.     altname enp3s0
  14.     altname ens160
  15.     inet 192.168.0.132/24 brd 192.168.0.255 scope global noprefixroute eth0
  16.        valid_lft forever preferred_lft forever
  17.     inet6 fe80::20c:29ff:feb1:380c/64 scope link
  18.        valid_lft forever preferred_lft forever
  19. [root@lvs-server ~]# ipvsadm -A -t 192.168.0.100:80 -s rr
  20. [root@lvs-server ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.129:80 -g
  21. [root@lvs-server ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.131:80 -g
  22. [root@lvs-server ~]# ipvsadm -Ln
  23. IP Virtual Server version 1.2.1 (size=4096)
  24. Prot LocalAddress:Port Scheduler Flags
  25.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  26. TCP  192.168.0.100:80 rr
  27.   -> 192.168.0.129:80             Route   1      0          0         
  28.   -> 192.168.0.131:80             Route   1      0          0         
复制代码
测试访问
  1. [root@internet ~]# curl 192.168.0.129
  2. rs1-server rs1
  3. [root@internet ~]# curl 192.168.0.131
  4. rs2-server rs2
复制代码
  1. [root@rs1-server ~]# tail -f /var/log/httpd/access_log -n0
  2. 10.0.0.10 - - [31/Jan/2024:14:51:57 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.61.1"
复制代码
web http协议通信过程

Client                                                                                                                                                                Server
​                                                                                                                                                                                        创建新的套接字(socket)
​                                                                                                                                                                                        将套接字绑定到端口80上去(bind)
​                                                                                                                                                                                        允许套接字进行连接(listen)
​                                                                                                                                                                                        期待连接(accept)
获取IP地址和端口号
创建新的套接字(socket)
连接到服务器IP:port上去(connect)
​                                                                                                                                                                                        通知应用程序有连接到来
​                                                                                                                                                                                        开始读取请求(read)
连接成功
发送HTTP请求
期待HTTP相应

​                                                                                                                                                                                        处理HTTP请求报文
​                                                                                                                                                                                        回送HTTP相应(write)
​                                                                                                                                                                                        关闭连接(close)
处理HTTP相应
关闭连接(close)
网络I/O模型

阻塞型、非阻塞型、复用型、信号驱动型、异步
阻塞I/O模型


阻塞IO模型是最简单的I/O模型,用户线程内核进行IO操作时被阻塞
用户线程通过体系调用read发起I/O读操作,由用户空间转到内核空间。内核等到数据包到达后,然后将接收的数据拷贝到用户空间,完成read操作
用户必要期待read将数据读取到buffer后,才继续处理接收的数据。整个I/O请求的过程中,用户线程是被阻塞的,这导致用户在发起IO请求时,不能做任何事变,对CPU的资源利用率不够
非阻塞型I/O模型


用户线程发起IO请求时立即返回。但并未读取到任何数据 ,用户线程必要不停地发起IO请求,直到数据到达后,才真正读取数据,继续实验。即“轮询”机制存在两个问题:如果有大量文件描述符都要等,那么就得一个一个的read。这会带来大量的Context Switch(read是体系调用,每调用一次就得在用户态和焦点态切换一次)。轮询的时间不好把握。这里是要猜多久之后数据才气到。期待时间设的太长,程序相应延迟就过大;设的太短,就会造成过于频繁的重试,干耗CPU而已。是比较浪费CPU的方式,一样平常很少直接使用这种模型。而是在其他IO模型中使用非阻塞IO这一特性。
I/O多路复用型


多路复用IO指一个线程可以同时(实际是交替实现,即并发完成)监控和处理多个文件描述符对应各自的IO,即复用同一个线程
一个线程之所以能实现同时处理多个IO,是由于这个线程调用了内核中的SELECT,POLL或EPOLL等体系调用。从而实现多路复用IO
信号驱动I/O模型


信号驱动I/O的意思就是进程现在不用傻等,也不用去轮询。而是让内核在数据就绪时,发送信号通知进程。
调用的步调是,通过体系调用sigaction,并注册一个信号处理的回调函数,该调用会立即返回,然后主程序可以继续向下实验,当有I/O操作准备就绪时,内核会为该进程产生一个SIGIO信号,并回调注册的信号回调函数,如许就可以在信号回调函数中体系调用recvfrom获取数据,将用户进程所必要的数据从内核空间拷贝到用户空间
此模型的优势在于期待数据报到达期间进程不被阻塞。用户主程序可以继续实验,只要期待来自信号处理函数的通知。
异步I/O模型


异步I/O与信号驱动I/O最大区别在于,信号驱动是内核通知用户进程何时开始一个I/O操作,而异步I/O是由内核通知用户进程I/O操作何时完成,两者有本质区别,相当于不用去饭店场吃饭,直接点个外卖,把期待上菜的时间也给省了
相对于同步I/O,异步I/O不是顺序实验。用户进程进行aio_read体系调用之后,无论内核数据是否准备好,都会直接返回给用户进程,然后用户态进程可以去做别的事变。等到socket数据准备好了,内核直接复制数据给进程,然后从内核向进程发送通知。IO两个阶段,进程都是非阻塞的。
Nginx

架构


进程结构
web请求处理机制
多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程相应客户端,直到用户关闭连接,如许的优势是处理速率快,子进程之间相互独立,但是如果访问过大会导致服务器资源耗尽而无法提供请求。
多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程和此客户端进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程序减轻了web服务器对体系资源的要求,但是多线程也有自己的缺点,即多个线程位于同一个进程内工作的时候,可以相互访问同样的内存地址空间,所有他们相互影响,一旦主进程挂掉则所有子进程都不能工作了,IIS服务器使用多线程的方式,必要间隔一段时间就重启一次才气稳定。
Nginx是多进程组织模型,而且是一个由Master主进程和Worker工作进程组成。
焦点配置
  1. [root@centos nginx]# cat  nginx.conf
  2. #全局配置端,对全局生效,主要配置nginx的启动用户/组,启动的工作进程数量,工作模式,nginx的PID路径,日志路径
  3. user nginx;
  4. worker_processes auto;        #启动工作进程数数量
  5. error_log /var/log/nginx/error.log;
  6. pid /run/nginx.pid;
  7. # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
  8. include /usr/share/nginx/modules/*.conf;
  9. events {
  10.         #events设置块,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动型模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
  11.     worker_connections 1024;        #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections * worker_processes)/2
  12. }
  13. http {
  14.         #http块是nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,server块可以配置文件引入、MIME-Type定义、日志定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
  15.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  16.                       '$status $body_bytes_sent "$http_referer" '
  17.                       '"$http_user_agent" "$http_x_forwarded_for"';
  18.     access_log  /var/log/nginx/access.log  main;
  19.     sendfile            on;                #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer(快速拷贝到kernelsocket buffer) >> 协议栈。
  20.     tcp_nopush          on;
  21.     tcp_nodelay         on;
  22.     keepalive_timeout   65;  #长连接超时时间,单位是秒
  23.     types_hash_max_size 2048;
  24.     include             /etc/nginx/mime.types;
  25.     default_type        application/octet-stream;
  26.     # Load modular configuration files from the /etc/nginx/conf.d directory.
  27.     # See http://nginx.org/en/docs/ngx_core_module.html#include
  28.     # for more information.
  29.     include /etc/nginx/conf.d/*.conf;
  30.     server {
  31.     #设置一个虚拟主机,可以包含自己的全局块,同时也可以包含多个location模块,比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server可以使用一个端口,比如都使用80端口提供web服务。
  32.         listen       80 default_server;
  33.         listen       [::]:80 default_server;
  34.         server_name  _;  #本server的名称,当访问此名称的时候nginx会调用当前server内部的配置进程匹配。
  35.         root         /usr/share/nginx/html;                #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使用绝对路径配置。
  36.         # Load configuration files for the default server block.
  37.         include /etc/nginx/default.d/*.conf;
  38.         location / {
  39.         #location其实是server的一个指令,为nginx服务器提供比较多而灵活的指令,都是location中体现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理。
  40.         }
  41.         error_page 404 /404.html;
  42.             location = /40x.html {
  43.             #location处理对应的不同错误码的页面定义到/40x.html,这跟对应其server中定义的目录下。
  44.         }
  45.         error_page 500 502 503 504 /50x.html;
  46.             location = /50x.html {
  47.              #location处理错误码(500 502 503 504)的页面定义到/50x.html,这跟对应其server中定义的目录下。
  48.         }
  49.     }
  50. # Settings for a TLS enabled server.
  51. #
  52. #    server {
  53. #        listen       443 ssl http2 default_server;
  54. #        listen       [::]:443 ssl http2 default_server;
  55. #        server_name  _;
  56. #        root         /usr/share/nginx/html;
  57. #
  58. #        ssl_certificate "/etc/pki/nginx/server.crt";
  59. #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
  60. #        ssl_session_cache shared:SSL:1m;
  61. #        ssl_session_timeout  10m;
  62. #        ssl_ciphers PROFILE=SYSTEM;
  63. #        ssl_prefer_server_ciphers on;
  64. #
  65. #        # Load configuration files for the default server block.
  66. #        include /etc/nginx/default.d/*.conf;
  67. #
  68. #        location / {
  69. #        }
  70. #
  71. #        error_page 404 /404.html;
  72. #            location = /40x.html {
  73. #        }
  74. #
  75. #        error_page 500 502 503 504 /50x.html;
  76. #            location = /50x.html {
  77. #        }
  78. #    }
  79. }
复制代码
优化
  1. user nginx;
  2. worker_processes [number| auto];#启动nginx工作进程的数量,一般设为和CPU核心数相同
  3. worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto; #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理,因此可以有效的提升nginx服务器的性能。
  4. CPU MASk: 00000001:0号CPU
  5.                    00000010:1号CPU
  6.                    10000000:7号CPU
  7. #示例                 
  8. work_cpu_affinity 0001 0010 0100 1000;第0号---第3号CPU
  9. error_log /apps/nginx/logs/error.log error; #错误日志记录配置
  10. pid                        /apps/nginx/logs/nginx.pid; #pid文件保存路径
  11. worker_priority 0; #工作进程优先级,-20~20(19)
  12. worker_rlimit_nofile 65536; #所有worker进程能打开的文件数量上限,包括:Nginx的所有连接,而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的限制,最好与ulimit -n 或者 limits.conf的值保持一致
  13. daemon off; #前台运行Nginx服务用于测试、docker等环境。
  14. master_process off|on #是否开启Nginx的master-worker工作模式,仅用于开发调试场景,默认为on
  15. events {
  16.     worker_connections 1024;  #设置单个工作进程的最大并发连接数
  17. }
复制代码
实现nginx的高并发配置
  1. [root@centos nginx]# while true;do ab -c 5000 -n 10000 http://10.0.0.8/;sleep 0.5;done
  2. This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking 10.0.0.8 (be patient)
  6. Completed 1000 requests
  7. Completed 2000 requests
  8. Completed 3000 requests
  9. Completed 4000 requests
  10. Completed 5000 requests
  11. Completed 6000 requests
  12. Completed 7000 requests
  13. Completed 8000 requests
  14. Completed 9000 requests
  15. [root@centos nginx]# tail -f /var/log/nginx/error.log
  16. 2024/02/22 11:26:24 [crit] 40383#0: accept4() failed (24: Too many open files)
  17. 2024/02/22 11:26:24 [crit] 40382#0: accept4() failed (24: Too many open files)
  18. 2024/02/22 11:26:24 [crit] 40383#0: accept4() failed (24: Too many open files)
  19. 2024/02/22 11:26:24 [crit] 40382#0: accept4() failed (24: Too many open files)
  20. 2024/02/22 11:26:25 [crit] 40383#0: accept4() failed (24: Too many open files)
复制代码
调大并发连接数
  1. [root@centos nginx]# ulimit -n 102400
  2. [root@centos nginx]# cat  /usr/lib/systemd/system/nginx.service
  3. LimitNOFILE=100000
  4. [root@centos nginx]# systemctl daemon-reload
  5. [root@centos nginx]# cat nginx.conf
  6. worker_rlimit_nofile 102400;
  7. events {
  8.     worker_connections 102400;
  9. }
  10. [root@centos nginx]# systemctl restart  nginx
复制代码
脚本实现一键编译安装NGINX

[code][root@centos data]# cat   install.sh #!/bin/bash##*******************************************************************#Author:                yoobak#QQ:                        877441893#Date:                        2024-02-23#FileName:                instal-nginx.sh#URL:                        https://www.cnblogs.com/yoobak#Description:                The test script#Copyright (C):        2024 All rights reserved#*******************************************************************SRC_DIR=/usr/local/src/NGINX_INSTALL_DIR=/apps/nginxversion=nginx-1.24.0download=https://nginx.org/download/install() {    yum install -y gcc pcre-devel openssl-devel zlib-devel    cd /data && wget ${download}${version}.tar.gz  && tar xf ${version}.tar.gz    cd $version && ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module    make -j `lscpu | awk '/^CPU\(s\)/{print $2}'` &&  make install    } useradd(){    useradd -s /sbin/nologin nginx    chown -R nginx. /${NGINX_INSTALL_DIR}    ln -s /apps/nginx/sbin/nginx /usr/sbin/}cat > /lib/systemd/system/nginx.service

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

九天猎人

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表