7层负载均衡
负载均衡
Nginx负载均衡调度算法
轮询
就是web01,web02 一人一次,轮流刷页面
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
[root@web01 /etc/nginx/conf.d]# cat web01.conf
server {
listen 8080;
server_name web.oldboy.com;
root /web;
location / {
index index.html;
}
}
[root@web02 /etc/nginx/conf.d]# cat web02.conf
server {
listen 8080;
server_name web.oldboy.com;
root /web;
location / {
index index.html;
}
}
`这几个配置出来就是web01,web02轮询的页面
`记得重启nginx 还有域名解析
加权轮询
web01轮流5次,web02轮流1次
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:8080 weight=5;
server 172.16.1.8:8080 weight=1;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
ip_hash
ip_hash不能加权轮询一起使用
ip_hash 能解决会话登录,会造成负载不均衡,导致某一台主机流量过大,而另一台没什么流量
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
ip_hash;
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
Nginx负载均衡后端状态
`一般用于停机维护时使用
`配置后只能显示web02,没有web01
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:8080 down;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
`当所有的server不可用时,才会启用backup
`web02 down机了或者stop nginx 页面出现web01
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 backup;
server 172.16.1.8:80;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
`健康检查,但无法查看到具体的指标,不够形象
`连接失败之后,连续两次失败,停留10秒,重新连接
[root@lb01 /etc/nginx/conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
nginx负载均衡健康检查
1.安装依赖包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下载nginx源码包以及nginx_upstream_check模块第三方模块
`先官方现在nginx
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3.解压nginx源码包以及第三方模块
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4.进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)
[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
`老规矩编译参数 但是多加了一条
./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 --add-module=/root/nginx_upstream_check_module-master --with-stream_ssl_preread_module --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'
[root@lb02 nginx-1.14.2]# make && make install
5.在已有的负载均衡上增加健康检查的功能
`这是新服务器,别忘了配置proxy_parmas
`后面要用incloud引用
[root@lb02 /etc/nginx]# cat proxy_params
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;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:8080 max_fails=2 fail_timeout=10s;
server 172.16.1.8:8080 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp; #时间是毫秒
}
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check(可以随便定义名字) {
check_status;
}
}
[root@lb02 /etc/nginx/conf.d]# nginx -t
[root@lb02 /etc/nginx/conf.d]# systemctl start nginx
down掉一台主机 根据配置标红,等待时间
session共享
1.下载phpmyadmin(web01 web02 都装)
[root@web01 conf.d]# cd /code
[root@web01 code]# wget https://files.phpmyadmin.net/phpMyAdmin/4.9.1/phpMyAdmin-4.9.1-all-languages.zip
[root@web01 /code]# unzip phpMyAdmin-4.9.1-all-languages.zip
2.改名
[root@web01 /code]# cd phpMyAdmin-4.9.1-all-languages/
[root@web01 /code/phpMyAdmin-4.9.1-all-languages]# cp config.sample.inc.php config.inc.php
[root@web02 /code]# cd phpMyAdmin-4.9.1-all-languages/
[root@web02 /code/phpMyAdmin-4.9.1-all-languages]# cp config.sample.inc.php config.inc.php
3.配置phpmyadmin连接远程的数据库
[root@web01 /code/phpMyAdmin-4.9.1-all-languages]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
本机ip改数据库ip
[root@web02 /code/phpMyAdmin-4.9.1-all-languages]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
本机ip改数据库ip
4.配置Nginx(web01 web02)
[root@web01 conf.d]# cat php.conf
server {
listen 80;
server_name php.oldboy.com;
root /code/phpMyAdmin-4.9.1-all-languages;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# systemctl restart nginx
5.配置授权
[root@web01 conf.d]# chown -R www.www /var/lib/php/
[root@web02 conf.d]# chown -R www.www /var/lib/php/
'可以只配置一台 然后推过去
[root@web01 code]# scp -rp phpMyAdmin-4.9.1-all-languages root@172.16.1.8:/code/
[root@web01 code]# scp /etc/nginx/conf.d/php.conf root@172.16.1.8:/etc/nginx/conf.d/
6.域名解析
10.0.0.5
登录数据库用户名密码就行 ,之前自己配置的 oldboy msy123
7.接入负载均衡
[root@lb01 conf.d]# vim proxy_php.com.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name php.oldboy.com;
location / {
proxy_pass http://php;
include proxy_params;
}
}
[root@lb01 conf.d]# nginx -t
[root@lb01 conf.d]# systemctl restart nginx
redis
使用redis解决会话登录问题
1.安装redis内存数据库
[root@db01 ~]# yum install redis -y
2.配置redis监听在172.16.1.0网段上
[root@db01 ~]# vim /etc/redis.conf
找到监听网段 发现是本机ip 在后面加上51
也可以这么做
[root@db01 ~]# sed -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf
3.启动redis
[root@db01 ~]# systemctl start redis
[root@db01 ~]# systemctl enable redis
4.php配置session连接redis
#1.修改/etc/php.ini文件
[root@web01~]# vim /etc/php.ini
搜索session
改这些
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123" #如果redis存在密码,则使用该方式
session.auto_start = 1
#2.注释php-fpm.d/www.conf里面的两条内容,否则session内容会一直写入/var/lib/php/session目录中
[root@web01 /code]# vim /etc/php-fpm.d/www.conf
搜索session
用分号注释掉
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
#3.重启php-fpm
[root@web01 code]# systemctl restart php-fpm
#4.将web01上配置好的文件推送到web02
[root@web01 code]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini
[root@web01 code]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf
#5.redis查看数据
[root@db01 ~]# redis-cli
127.0.0.1:6379> keys *