100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > nginx+keepalived 高可用负载均衡

nginx+keepalived 高可用负载均衡

时间:2021-09-24 10:55:26

相关推荐

nginx+keepalived 高可用负载均衡

后端开发|php教程

nbsp,quot,keepalived,proxy,192.168

后端开发-php教程

话就不多说了,nginx安装与配置,还有负载均衡呢,可以看我写的另一篇文章《nginx负载均衡实战》,还有关于负载均衡呢,大家可以看一下我写的另外两篇文章,一个是《lvs+keepalived负载均衡》,另一个是《haproxy+keepalived负载均衡》,三种负载均衡的区别呢,可以看一下我转载的一篇文章《软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比》,下面直接进入配置步骤:

安卓 简易闹钟源码,gpt卸载ubuntu,tomcat用什么版本的,爬虫注册app,php5权威指南 pdf,SEO作弊行为主要有哪些lzw

1.系统环境

微特大电影源码下载,ubuntu镜像结构,tomcat返回大列表慢,爬虫复制新闻,php连接数据库函数,江苏短视频seo专业服务商lzw

[plain] view

plaincopy

易语言微信打印源码,vscode新建一个项目,ubuntu 桌面变黑,tomcat开启新进程,爬虫箱 喷淋,php探针源码,天门外包seo推广哪个好lzw

系统版本:CentOS release 5.9 (Final) x86 32位nginx版本: 1.2.8 keepalived版本: 1.2.4 主keepalived:192.168.207.130 从keepalived:192.168.207.131VIP:192.168.207.140 WEB_1:192.168.207.129 80端口 WEB_2:192.168.207.130 8080端口 WEB_3:192.168.207.131 8080端口

2.自定义nginx配置文件

在192.168.207.130和192.168.207.131上操作

[plain] view

plaincopy

useradd nginx vi /usr/local/nginx/conf/nginx.conf

内容如下:

[plain] view

plaincopy

#运行用户user nginx nginx;#启动进程worker_processes 2;#全局错误日志及PID文件error_log logs/error.log notice;pid logs/nginx.pid;#工作模式及每个进程连接数上限events {use epoll;worker_connections 1024;#所以nginx支持的总连接数就等于worker_processes * worker_connections }#设定http服务器,利用它的反向代理功能提供负载均衡支持http {#设定mime类型include mime.types; #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体 default_type application/octet-stream; #默认的数据类型 #设定日志格式 log_format main ‘$remote_addr – $remote_user [$time_local] ‘ ‘”$request” $status $bytes_sent ‘ ‘”$http_referer” “$http_user_agent” ‘ ‘”$gzip_ratio”‘; log_format download ‘$remote_addr – $remote_user [$time_local] ‘ ‘”$request” $status $bytes_sent ‘ ‘”$http_referer” “$http_user_agent” ‘ ‘”$http_range” “$sent_http_content_range”‘;#设定请求缓冲client_header_buffer_size 1k;large_client_header_buffers 4 4k;#开启gzip模块#gzip on;#gzip_min_length 1100;#gzip_buffers 4 8k;#gzip_types text/plain;#output_buffers 1 32k;#postpone_output 1460;#设定access logaccess_log logs/access.log main;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;#设定负载均衡的服务器列表 upstream mysvr { #weigth参数表示权值,权值越高被分配到的几率越大 server 192.168.207.129:80 weight=5; server 192.168.207.130:8080 weight=5; server 192.168.207.131:8080 weight=5; }server { #这个是设置web服务的,监听8080端口 listen 8080; server_name 192.168.207.131;#这个根据系统ip变化indexindex.html index.htm; root /var/www/html; #error_page500 502 503 504 /50x.html; #location = /50x.html { # roothtml; #} } #设定虚拟主机server { listen 80; server_name 192.168.207.140; #这里是VIP#charset gb2312; #设定本虚拟主机的访问日志 access_log logs/three.web.access.log main; #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好 #location ~ ^/(img|js|css)/{ # root /data3/Html; # expires 24h; #} #对 “/” 启用负载均衡 location / { proxy_pass http://mysvr; #以这种格式来使用后端的web服务器proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } #设定查看Nginx状态的地址 ,在安装时要加上–with-http_stub_status_module参数 location /NginxStatus { stub_status on; access_log on; auth_basic “NginxStatus”; auth_basic_user_file conf/htpasswd;#设置访问密码,htpasswd -bc filename username password } } }

3.自定义keepalived配置文件

[plain] view

plaincopy

vi /etc/keepalived/keepalived.conf

内容如下:

[plain] view

plaincopy

global_defs {notification_email {root@localhost.localdomain}notification_email_from notify@smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL } vrrp_script chk_http_port { script “/etc/keepalived/check_nginx.sh” ###监控脚本 interval 2 ###监控时间 weight 2###目前搞不清楚 } vrrp_instance VI_1 {state MASTER ### 设置为 主interface eth0 ### 监控网卡virtual_router_id 51 ### 这个两台服务器必须一样priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUPauthentication { auth_type PASS auth_pass 1111}track_script { chk_http_port### 执行监控的服务}virtual_ipaddress {192.168.207.140 ### VIP 地址} }

4.写自定义脚本

[plain] view

plaincopy

vi /etc/keepalived/check_nginx.sh

内容如下:

[plain] view

plaincopy

!/bin/bash A=`ps -C nginx –no-header |wc -l`## 查看是否有 nginx进程 把值赋给变量A if [ $A -eq 0 ];then ## 如果没有进程值得为 零/usr/local/nginx/sbin/nginxsleep 3if [ `ps -C nginx –no-header |wc -l` -eq 0 ];then /etc/init.d/keepalived stop ## 则结束 keepalived 进程fi fi

这里是检查nginx是否启动好,如果没有启动,先启动 nginx,隔了3秒后还没有启动好,则将keepalived进程也关闭,这样从keepalived就能接手过去了,提供高可用性,在这里呢,keepalived服务是提供高可用性,而nginx是提供后端web服务器的负载均衡。

这里还要给脚本加上执行权限,如下

[plain] view

plaincopy

chmod +x /etc/keepalived/check_nginx.sh

5.启动服务,并测试

在这里先说一下啊,在WEB_1上,我是使用系统自带的apache昨晚web服务器的,比较省事,这样呢,我只要启动好主从keepalived就ok了,因为它会利用check_nginx.sh脚本来自动启动nginx。

都启动好了。

访问http://192.168.207.140就可以轮训访问后端的三台web服务器内容啦

这里我们把主keepalived服务给关掉,来测试高可用性

然后会在从keepalived服务器上的/var/log/messages看到这样的日志

[plain] view

plaincopy

Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140 Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.

继续访问http://192.168.207.140,依旧可以访问后端的三台web服务器

然后再把原主keepalived打开,可以观察到原从keepalived服务器的日志显示

[plain] view

plaincopy

Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140 Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs. Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.

说明有恢复了原来的主从结果。

生产环境中,后端的机器也可能会挂掉,但是呢,这就不用你操心啦,nginx会自动把session分配到好的后端web服务器上的啦

ok,到这里全部结束了,实践亲测,祝君成功

From: /zmj_88888888/article/details/8825471

以上就介绍了nginx+keepalived 高可用负载均衡,包括了方面的内容,希望对PHP教学有兴趣的朋友有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。