linux之slb七层负载

slb参数配置

  • slb七层负载

1.错误代码

后端业务服务器是集群模式情况下,通过slb访问,其中一台或多台业务服务器挂掉,通过在slb主机配置proxy_next_upstream参数,实现报错,而转向其它正常业务服务器。

# 停止掉web01 php-fpm 当用户通过slb访问时,会出现502,为解决此问题,在slb配置参数proxy_next_upstream

[root@proxy conf.d]# cat slb.conf 
upstream webs {
	server 10.0.0.7;
	server 10.0.0.8;	
}

#wp
server {
	listen 80;
	server_name www.wp.com;
	location / {
		proxy_pass http://webs;
		include proxy_params;
	}
}

[root@proxy conf.d]# cat ../proxy_params 
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504; # 配置此参数 或直接写入server模块

2.负载均衡调度算法

负载均衡一共有五种调度算法
1. rr轮询            默认使用的调度算法(客户端请求平均分配上游业务服务器)
2. 加权轮询(weight) 通过weight参数加重目标服务器响应占比,与目标主机的硬件配置性能相关
3. ip_hash          保持请求源IP能够每次定向到同一台业务服务器,(容易导致业务服务器负载过高)
4. url_hash         以url来转发到不同的web服务器
5. least_conn       最少链接数,把请求转发至建立连接比较少的业务服务器(个别客户端与服务器交互时间长)

2.1 weight 默认值是1,不能为负数或0

[root@proxy conf.d]# cat slb.conf 
upstream webs {
	server 10.0.0.7 weight=1;
	server 10.0.0.8 weight=10;	
}

2.2 ip_hash

[root@proxy conf.d]# cat slb.conf 
upstream webs {
	ip_hash;
	server 10.0.0.7 ;
	server 10.0.0.8 ;	
}

3.nginx server状态两种模式(bakcup、down)

[root@proxy conf.d]# cat slb.conf 
upstream webs {
	ip_hash;
	server 10.0.0.7 down;
	server 10.0.0.8 ;	
}


[root@proxy conf.d]# cat slb.conf 
upstream webs {
	ip_hash;
	server 10.0.0.7 ;
	server 10.0.0.8 backup;	
}

4.编译安装nginx(slb主机)

#nginx_upstream_check_module 此模块是第三方开发的 需要编译安装到nginx里

# a. 安装编译环境依赖包
[root@proxy ~]#  yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

# b. 下载nginx源码包(与现有安装的nginx版本同步)和第三方nginx_upstream_check_module包
[root@proxy ~]# wget http://nginx.org/download/nginx-1.26.1.tar.gz
[root@proxy ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# c. 解压源码包
tar zxvf nginx-1.26.1.tar.gz
unzip master.zip

# d. cd切换到nginx源码包中 ,执行 将新的模块添加进默认的模块中
[root@proxy nginx-1.26.1]# patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch

[root@proxy nginx-1.26.1]# nginx -V 查询已安装模块,并添加 --add-module=/root/nginx_upstream_check_module-master
#到下面模块中 注意,这里的nginx_upstream_check_module的绝对路径

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

# e. configure配置安装 
[root@proxy nginx-1.26.1]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

# f. make && make install 编译安装
[root@proxy nginx-1.26.1]# make && make install

# g. 查看是否成功安装新的模块
[root@proxy nginx-1.26.1]# nginx -V
nginx version: nginx/1.26.1
built by gcc 7.3.0 (GCC) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

5.获取客户端真实的IP地址

# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	# 携带客户端信息

[root@proxy ~]# cd /etc/nginx/
[root@proxy nginx]# cat proxy_params 
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	# 获取客户端真实IP ,即时抓包伪造,也是能获取到请求源IP

6.phpmyadmin会话保持连接(redis存储)

#在web01服务器部署
1.下载phpmyadmin程序包
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.zip

2.创建代码目录并拷贝phpmysqladmin源码,修改权限,并修改配置文件数据库连接信息
mkdir /admin 
[root@web01 admin]# unzip phpMyAdmin-5.2.1-all-languages.zip

2.1 修改配置文件mv config.sample.inc.php   config.inc.php 并修改30行 mysql数据库IP地址
[root@web01 admin]# grep -n host config.inc.php 
30:$cfg['Servers'][$i]['host'] = '172.16.1.51';

3.创建phpmyadmin网站配置
[root@web01 admin]# cat /etc/nginx/conf.d/admin.conf 
server {
    listen       80;
    server_name  www.admin.com;

    access_log  /var/log/nginx/www.admin.access.log  main;

    location / {
        root   /admin;
        index  index.php index.html index.htm;
    }   

    location ~ \.php$ {
    		root           /admin;
    		fastcgi_pass   127.0.0.1:9000;
    		fastcgi_index  index.php;
    		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    		include        fastcgi_params;
    }

}

4.修改windows 的hosts文件
10.0.0.7 www.admin.com
ipconfig.exe /flushdns # 清除本地dns解析缓存

5.浏览器访问www.admin.com 报错如下 ,是因为用户登录之后会产生session信息,需要写入服务器端的/var/lib/php/session/目录下,服务端PHP程序以nginx用户启动,session目录默认是root权限才能写入,所以需要修改/var/lib/php/session/权限为nginx
#报错
phpMyAdmin - Error
Error during session start; please check your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.

session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13)

session_start(): Failed to read session data: files (path: /var/lib/php/session)

[root@web01 admin]# chown nginx.nginx /var/lib/php/session/

再次访问www.admin.com 即可

4.同理 在web02上也部署phpmysqladmin,并配置slb服务器,访问正常,无法正常登录,是因为slb的rr轮询调度算法导致的,当用户请求phpmyadmin是web01服务器响应,登录提交时到web02服务器并写入到web02的/var/lib/php/session/,当再次响应请求时,web01服务器session并未记录用户提交的账号密码,又重定向到login,依次反复无法登录。

7.redis服务

解决上述问题,这里使用redis缓存数据库

5.1 在数据库服务器51安装redis
[root@mysql ~]# yum -y install redis
[root@mysql ~]# systemctl start redis

5.2 修改redis配置文件bind 允许同网段可访问 /etc/redis/redis.conf
[root@mysql ~]# grep "bind " /etc/redis/redis.conf 
bind 127.0.0.1 172.16.1.51

#编译安装php redis扩展插件
#下载redis插件源码
[root@web02 ~]# wget https://pecl.php.net/get/redis-4.0.1.tgz
[root@web02 ~]# tar xf redis-4.0.1.tgz && cd redis-4.0.1

#安装编译依赖
[root@web02 redis-4.0.1]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

#编译初始化
[root@web02 redis-4.0.1]# phpize 
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718

#配置环境变量
[root@web02 redis-4.0.1]# ./configure 

#编译安装
[root@web02 redis-4.0.1]# make && make install
Installing shared extensions:     /usr/lib64/php/modules/

[root@web02 redis-4.0.1]# php -m |grep redis
redis

5.3 配置web服务器的php.ini 关于redis参数设置,开启redis插件功能,修改session存储文件模式为redis存储

[root@web02 conf.d]# grep -n extension=redis.so /etc/php.ini 
1358:extension=redis.so         #开启redis插件

[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = redis    #session会话存入redis服务

; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; The path can be defined as:
;
session.save_path = "tcp://172.16.1.51:6379" #具体交给172.16.1.51数据库服务器的redis服务

5.4 修改php的配置文件/etc/php-fpm.d/www.conf关闭session文件存储方式
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session


5.5 重启php-fpm服务
[root@web02 redis-4.0.1]# php-fpm -t
[15-Dec-2024 11:20:34] NOTICE: configuration file /etc/php-fpm.conf test is successful

[root@web02 redis-4.0.1]# systemctl restart php-fpm
posted @ 2024-12-15 22:07  被时光移动的城市  阅读(5)  评论(0编辑  收藏  举报