基于nginx+keepalived的高可用web集群

965 阅读14分钟

项目名称

基于nginx+keepalived的负载均衡、高可用web集群

项目架构图

项目架构图

项目环境

软件环境

  • CentOS:CentOS Linux release 7.9.2009 (Core)
  • Nginx:nginx/1.23.3
  • DNS:BIND 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13
  • Ansible:ansible 2.9.27
  • Keepalived: Keepalived v1.3.5 (03/19,2017)
  • Prometheus: prometheus, version 2.43.0
  • Grafana: grafana 9.4.7
  • NFS: nfs v4
  • ab:ApacheBench, Version 2.3

硬件环境

  • CentOS 7.9(10台1核512MB)

项目描述

简介

模拟企业构建一个高可用并且高性能的web集群项目,能处理大并发的web业务。使用Ansible实现软件环境的部署,Nginx实现7层负载均衡和搭建web框架,Keepalived搭建双vip架构实现高可用,Prometheus+Grafana实现对web集群以及负载均衡器的系统资源监控,NFS实现web集群的数据同源,DNS搭建主域名服务器实现vip地址的解析。

IP地址规划

主机名IP地址
windows客户机192.168.31.68
lb1192.168.31.110
lb2192.168.31.120
web1192.168.31.200
web2192.168.31.201
web3192.168.31.202
dns192.168.31.153
ansible192.168.31.111
nfs192.168.31.250
prometheus192.168.31.180
ab192.168.31.100

项目步骤

1.准备10台全新虚拟机,按照IP规划配置好静态IP。建立免密通道,使用Ansible自动化批量部署软件环境

生成密钥对

[root@ansible ~]# ssh-keygen
[root@ansible ~]# cd /root/.ssh/
[root@ansible .ssh]# ls
id_rsa  id_rsa.pub

上传公钥到远程主机

[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.31.200 #web1
...
略

ssh-copy-id -i ~/.ssh/id_rsa.pub +所需部署软件的远程主机ip地址

安装Ansible

[root@ansible ~]# yum install -y epel-release #安装epel源
[root@ansible ~]# yum install -y ansible #安装ansible
[root@ansible ~]# ansible --version #查看ansible版本

编写host inventory(主机清单)--要远程控制的主机ip

配置文件/etc/ansible/hosts

[root@ansible ansible]# vim /etc/ansible/hosts
## [webservers] 
## alpha.example.org
## beta.example.org 
## 192.168.1.100
## 192.168.1.110
[web_servers]
192.168.31.200 #web1 
192.168.31.201 #web2
192.168.31.202 #web3
[dns_server]
192.168.31.153 #dns
[lb_servers]
192.168.31.110 #lb1
192.168.31.120 #lb2
[nfs_server]
192.168.31.250 #NFS_server
# If you have multiple hosts following a pattern you can specify

编写playbook批量部署nginx、keepalived、node_exporters、dns等软件

playbook yaml脚本

[root@ansible playbooks]# cat software_install.yaml
- hosts: web_servers #web集群
  remote_user: root
  tasks:
  #web主机组中编译安装部署nginx集群
  - name: install nginx
    script:  /etc/ansible/nginx/one_key_install_nginx.sh #调用本地一键安装部署nginx脚本,在远程主机上编译安装
    #web主机组中安装nfs,访问nfs服务器,实现数据同源
  - name: install nfs
    yum: name=nfs-utils state=installed
- hosts: dns_server #dns服务器
  remote_user: root
  tasks:
  - name: install dns
    yum: name=bind.* state=installed
- hosts: lb_servers #负载均衡服务器
  remote_user: root
  tasks:
    #lb主机组中编译安装nginx
  - name: install nginx
    script:  /etc/ansible/nginx/one_key_install_nginx.sh
    #lb主机组中安装keepalived,实现高可用
  - name: install keepalived
    yum: name=keepalived state=installed
- hosts: nfs_server #NFS服务器
  remote_user: root
  tasks:
  - name: install nfs
    yum: name=nfs-utils state=installed
#调用本地onekey_install_node_exporter脚本,批量安装部署node_exporter,为prometheus采集数据
- hosts: web_servers lb_servers
  remote_user: root
  tasks: 
  - name: install node_exporters
    script: /etc/ansible/node_exporter/onekey_install_node_exporter.sh
    tags: install_exporter
  - name: start node_exporters #后台运行node_exporters
    shell: nohup node_exporter --web.listen-address 0.0.0.0:8090 &
    tags: start_exporters #打标签,方便后面直接跳转到此处批量启动node_exporters

一键安装部署nginx脚本

[root@ansible nginx]# cat one_key_install_nginx.sh 
mkdir -p /my_nginx
cd /my_nginx

# 下载nginx压缩包
curl -O http://nginx.org/download/nginx-1.23.3.tar.gz
# 或者用wget
#wget http://nginx.org/download/nginx-1.23.3.tar.gz
# 解压
tar xf nginx-1.23.3.tar.gz
# 进入文件夹
cd nginx-1.23.3

# 新建用户,用于启动nginx进程,名字自拟
useradd -s /sbin/nologin mynginx99

# 安装依赖包
# ssh相关、gcc为编译需要、pcre正则相关、make编译相关
yum install -y openssl openssl-devel gcc pcre pcre-devel automake make net-tools vim

# configure是一个配置的脚本文件,会根据指定的配置生成一个Makefile文件,这个文件会影响后面make命令的编译,相当于图纸
# configure可配置参数可以参考官方文档:http://nginx.org/en/docs/configure.html
# 常用选项:
# --with-*:开启某个功能,默认不安装	--without-*:禁用某个功能,默认安装
# --prefix=path:指定路径			--conf-path=path:指定配置文件路径,不指定会放到prefix路径下
# --user=name:指定启动nginx worker进程的用户
# --with-http_ssl_moudle 开启https的功能,下载ssl来进行公钥和私钥的认证
# --without-http——memcached_moudle 禁用http_memcached
# --with-http_realip_module 启用realip功能,让后端知道通过代理访问的用户的ip
# --with-http_v2_module:对http2.0版本的支持
# --with-threads:开启线程池功能		--with-http_stub_status_moudle:开启nginx状态统计功能,可以知道多少访问
# --with-stream 支持tcp/udp反向代理,即4层负载均衡
./configure --prefix=/usr/local/mynginx99 --user=mynginx99 --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-threads --with-http_stub_status_module --with-stream

# 编译
# make是按照Makefile的配置去编译程序为二进制文件,二进制文件就是执行可以运行的程序
# -j:指定编译进程数,多点速度快些,可以使用top后按1查看虚拟机配有几个核心
make -j2 
# 将编译好的二进制文件复制到指定安装路径目录下
make install

# 启动nginx
/usr/local/mynginx99/sbin/nginx

# 修改PATH变量
PATH=$PATH:/usr/local/mynginx99/sbin
echo "PATH=$PATH" >>/root/.bashrc

# 设置nginx的开机启动,rc.local是指向rc.d/rc.local的链接,后者需要拥有执行权限才能开机自启
echo "/usr/local/mynginx99/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local

# selinux和firewalld防火墙都关闭
# selinux临时和永久关闭
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

# 防火墙临时和永久关闭
service firewalld stop
systemctl disable firewalld

一键安装部署node_exporters脚本

#!/bin/bash
cd ~
tar xf node_exporter-1.5.0.linux-amd64.tar.gz #解压node_exporters源码包
mv  node_exporter-1.5.0.linux-amd64 /node_exporter
cd /node_exporter
PATH=/node_exporter:$PATH #加入PATH环境变量
echo "PATH=/node_exporter:$PATH" >>/root/.bashrc #加入开机启动
nohup node_exporter --web.listen-address 0.0.0.0:8090 & #后台运行,监听8090端口

prometheus、grafana、ab为手动安装

2.模拟中台转发系统,两台负载均衡器上使用nginx实现7层负载均衡,后端nginx集群通过访问NFS服务实现数据同源。

负载均衡的实现

在两台负载均衡器(lb1、lb2)上编辑nginx配置文件,增加负载均衡的功能

[root@lb1 conf]# vim /usr/local/mynginx99/conf/nginx.conf
#使用7层负载均衡
    upstream my_lb {
        server 192.168.31.200; #web1     
        server 192.168.31.201; #web2     
        server 192.168.31.202; #web3     
 }
    server {
        listen       80;
        server_name  localhost;
        #转发到后端服务器 
        location / {
                proxy_pass http://my_lb;
        }

lb2负载均衡配置同上 效果测试: 在这里插入图片描述 在这里插入图片描述

数据同源的实现

nfs服务器的安装

[root@nfs ~]# yum install -y nfs-utils #安装nfs
[root@nfs ~]# service nfs start  #启动nfs服务

web集群安装nfs

[root@web1 ~]# yum install -y nfs-utils #安装nfs
[root@web1 ~]# service nfs start  #启动nfs服务

web2、web3同上

nfs服务器编辑共享文件

31网段可以共享文件/web下的内容

[root@nfs ~]# cat /etc/exports
/web 192.168.31.0/255.255.255.0(ro,all_squash,async)

创建共享出去的文件夹

[root@nfs ~]# mkdir /web
[root@nfs web]# cd /web
[root@nfs web]# touch index.html
[root@nfs web]# ls
index.html

刷新配置文件

[root@nfs web]# service nfs restart
Redirecting to /bin/systemctl restart nfs.service
[root@nfs web]# exportfs -rv
exporting 192.168.31.0/255.255.255.0:/web

web集群挂载使用共享目录

[root@web1 ~]# mount 192.168.31.250:/web /usr/local/mynginx99/html/

效果测试

在这里插入图片描述 刷新页面,仍然访问nfs-server,实现了数据一致性

3.使用nginx的https、隐藏版本、身份认证、realip等模块

https模块/http自动跳转到https

上传域名绑定的ssl证书到/usr/local/mynginx99/conf/下

ssl证书 阿里云上购买域名可以免费申请ssl证书

修改本地hosts文件

C:\Windows\System32\drivers\etc\hosts

192.168.31.110 www.chenlb666.top
192.168.31.120 www.chenlb666.top

编辑nginx配置文件

[root@lb1 conf]# vim /usr/local/mynginx99/conf/nginx.conf
     #HTTPS server

    server {
        listen       443 ssl;
        server_name  www.chenlb666.top; #域名

        ssl_certificate      9560502_chenlb666.top.pem; #私钥
        ssl_certificate_key  9560502_chenlb666.top.key; #公钥

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
                proxy_pass http://my_lb;
                proxy_set_header X-Real-IP $remote_addr;
        }
    }

效果测试

在这里插入图片描述

实现http自动跳转到https

server{}中加入

return 301 https://www.chenlb666.top;

隐藏版本

负载均衡器上编辑nginx配置文件

server_tokens off; #nginx.conf配置文件中的http{}中加入

效果测试

在这里插入图片描述 server由server: nginx 1.23/2 变成了 nginx

realip实现

后端服务器如何知道真正访问自己的客户的ip?而不只是负载均衡器的ip?

负载均衡器编辑nginx配置文件

https server{}中加入proxy_set_header X-Real-IP $remote_addr,读取remote_addr变量,获取真实客户机的IP地址

        location / {
                proxy_pass http://my_lb;
                proxy_set_header X-Real-IP $remote_addr;
        }

web集群编辑nginx配置文件

server{}中的日志格式中加入$http_x_real_ip 获取真实客户的ip地址,在日志中显示

    log_format  main  '$remote_addr - $http_x_real_ip - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main; #指定访问日志

效果测试

在这里插入图片描述 192.168.31.68---windows客户机的IP地址

状态统计

web集群编辑nginx配置文件

server{}中定义一个路由,其中加入stub_status on;

 location = /info {
            # 开启状态统计
            stub_status on;
            # 查看状态的访问不计入日志
            access_log off;
            # 启用身份认证
            auth_basic "my_website";
            # 指定密码文件,默认在conf目录下查找
            auth_basic_user_file htpasswd;
        }

效果测试

在这里插入图片描述

身份认证

web集群编辑nginx配置文件

server{}中定义一个路由,其中加入auth_basic "my_website",并指定账号,密码;

 location = /info {
            # 开启状态统计
            stub_status on;
            # 查看状态的访问不计入日志
            access_log off;
            # 启用身份认证
            auth_basic "my_website";
            # 指定密码文件,默认在conf目录下查找
            auth_basic_user_file htpasswd;
        }

生成htpasswd

[root@web1 conf]# yum install -y httpd-tools
[root@rs1 conf]# http -c /usr/local/mynginx99/conf/htpasswd felix

效果测试

在这里插入图片描述 输入账号密码后: 在这里插入图片描述

4.在负载均衡器上使用keepalived搭建双vip双master高可用架构,并使用健康检测、notify功能监控本机nginx进程。

安装部署(已使用playbook批量安装完成)

两个负载均衡器上启用两个arrp实例

LB1:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict #记得要注释,否则可能会无法收到vrrp广播
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

#定义监控脚本chk_nginx
vrrp_script chk_nginx {
        #判断nginx是否在运行,若服务已挂,则返回值为1,执行下面的命令,使其priority值减80,小于backup的priority的值,使其成为backup状态
        script "/etc/keepalived/check_nginx.sh"
	interval 1
	weight -80
}

vrrp_instance VI_1 {
    state MASTER #设置为MASTER角色
    interface ens33 #收听vrrp广播的网卡为ens33
    virtual_router_id 88 #虚拟路由器id
    priority 150 #优先级,MASTER
    advert_int 1 #vrrp广播延迟
    authentication {
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.188 #vip地址
    }
#调用监控脚本
track_script {
	chk_nginx
}
#定义notify_backup,如果本机负载均衡器lb1的nginx挂了,就关闭keepalived
notify_backup "/etc/keepalived/stop_keepalived"
}

vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 99
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.199
    }
}

LB2:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
#定义监控脚本chk_nginx
vrrp_script chk_nginx {
	#判断nginx是否在运行,若服务已挂,则返回值为1,执行下面的命令,使其priority值减80,小于backup的priority的值,使其成为backup状态
	script "/etc/keepalived/check_nginx.sh"
	interval 1
        weight -80

}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 88
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.188
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 99
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.199
    }
#调用监控脚本
track_script {
	chk_nginx
}
#定义notify_backup,如果本机负载均衡器lb2的nginx挂了,就关闭keepalived
notify_backup "/etc/keepalived/stop_keepalived"
}

定义健康检测监控脚本,notify_backup,实现如果本机nginx挂了,就由MASTER状态进入BACKUP状态,并且关闭keepalived。

定义监控脚本,当nginx挂了,优先级-80

#定义监控脚本chk_nginx
vrrp_script chk_nginx {
        #判断nginx是否在运行,若服务已挂,则返回值为1,执行下面的命令,使其priority值减80,小于backup的priority的值,使其成为backup状态
        script "/etc/keepalived/check_nginx.sh"
	interval 1
	weight -80
}

外部check_nginx.sh判断nginx是否挂了的脚本

[root@lb2 keepalived]# cat check_nginx.sh 
#!/bin/bash
if /usr/sbin/pidof nginx &>/dev/null;then
	exit 0
else
	exit 1
fi

记得给脚本chmod +x check_nginx.sh增加可执行权限 master实例调用监控脚本

#调用监控脚本
track_script {
	chk_nginx
}

master实例中定义notify_backu实现本机nginx挂了,就关闭keepalived进程

notify_backup "/etc/keepalived/stop_keepalived"

外部关闭keepalived脚本:

[root@lb2 keepalived]# cat stop_keepalived 
#!/bin/bash
service keepalived stop

记得给脚本stop_keepalived.sh增加可执行权限

效果测试:

vip地址192.168.31.188漂移到了lb1上 在这里插入图片描述

vip地址192.168.31.199漂移到了lb2上 在这里插入图片描述 关闭LB1的nginx进程

[root@lb1 keepalived]# nginx -s stop

在这里插入图片描述 在这里插入图片描述

LB1的vip地址192.168.31.188漂移到了LB2上 查看keepalived进程是否启动? 在这里插入图片描述 keepalived也挂了,测试成功

5.搭建DNS主域名服务器,增添两条负载均衡记录,实现基于DNS的负载均衡;访问同一URL,解析出双vip地址。

安装

[root@dns ~]# systemctl disable firewalld #关闭防火墙,防止windows客户机无法访问dns服务器 
[root@dns ~]# systemctl disable NetworkManager #关闭NetworkManager
[root@dns ~]# yum install bind* #安装dns服务的软件包
[root@dns ~]# service named start #启动dns服务
[root@dns ~]# systemctl enable named  #开机启动dns服务

修改dns配置文件,任意ip可以访问本机的53端口,并且允许dns解析。

[root@dns ~]# vim /etc/named.conf
listen-on port 53 { any; };#允许任意ip访问53端口
        listen-on-v6 port 53 { any; }; 
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; }; #允许任意dns解析

搭建主域名服务器

编辑dns次要配置文件/etc/named.rfc1912.zones,增加一条主域名记录

zone "chenlb666.top" IN {
        type master; #类型为主域名
        file "chenlb666.top.zone"; #chenlb666.top域名的数据文件,需要去/var/named/下创建
        allow-update { none; }; 
};

创建chenlb666.top主域名的数据文件

[root@dns ~]# cd /var/named/
[root@dns named]# cp -a named.localhost chenlb666.top.zone

编辑数据文件,增加两条负载均衡记录。

[root@dns named]# vim chenlb666.top.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       192.168.31.153 
www IN  A       192.168.31.188 #负载均衡记录指向LB1的vip地址
www IN  A       192.168.31.199 #负载均衡记录指向LB2的vip地址

刷新dns服务

[root@dns named]# service named restart

修改windows客户机的dns服务器的地址为搭建的dns服务器192.168.31.153。

在这里插入图片描述

效果测试

windows客户机分别解析出了双vip地址 在这里插入图片描述 在这里插入图片描述

6.安装部署prometheus+grafana实现对backend服务器和load balance服务器的监控和出图

prometheus服务器安装prometheus,客户机安装node_exporters采集数据。

安装部署prometheus成一个服务

官网下载源码包,上传到/prometheus里,解压 在这里插入图片描述 解压,加入环境变量 /root/.bashrc下修改环境变量 PATH=/prometheus/prometheus:$PATH

在/var/lib/systemd/system下vim一个prometheus.service [unit] Description=prometheus [Service] ExecStart=/prometheus/prometheus/prometheus --config.file=/prometheus/prometheus/prometheus.yml ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target

重载systemd相关的服务 systemctl daemon-reload 启动prometheus service prometheus start 查看端口 netstat -anplut|grep prometheus

测试访问prometheus页面 在这里插入图片描述

web集群安装node_exporters采集数据

官网下载源码包在ansible机上,使用copy模块上传到web集群

[root@ansible ~]# ansible web_servers -m copy -a "src ~/node_exporter-1.5.0.linux-amd64.tar.gz dest=~"

编写playbook安装,并启动。

#调用本地onekey_install_node_exporter脚本,批量安装部署node_exporter,为prometheus采集数据
- hosts: web_servers lb_servers
  remote_user: root
  tasks: 
  - name: install node_exporters
    script: /etc/ansible/node_exporter/onekey_install_node_exporter.sh
    tags: install_exporter
  - name: start node_exporters #后台运行node_exporters
    shell: nohup node_exporter --web.listen-address 0.0.0.0:8090 &
    tags: start_exporters #打标签,方便后面直接跳转到此处批量启动node_exporters

本地onekey_install_node_exporter脚本:

#!/bin/bash
cd ~
tar xf node_exporter-1.5.0.linux-amd64.tar.gz
mv  node_exporter-1.5.0.linux-amd64 /node_exporter
cd /node_exporter
PATH=/node_exporter:$PATH
echo "PATH=/node_exporter:$PATH" >>/root/.bashrc
nohup node_exporter --web.listen-address 0.0.0.0:8090 &

访问测试 在这里插入图片描述

prometheus server里添加安装了exporter的web集群

[root@prometheus prometheus-2.43.0]# vim /prometheus/prometheus/prometheus.yml
 - job_name: "LB1"
    static_configs:
        - targets: ["192.168.31.110:8090"]
  - job_name: "LB2"
    static_configs:
        - targets: ["192.168.31.120:8090"]
  - job_name: "Web1"
    static_configs:
        - targets: ["192.168.31.200:8090"]
  - job_name: "Web2"
    static_configs:
        - targets: ["192.168.31.201:8090"]
  - job_name: "Web3"
    static_configs:
        - targets: ["192.168.31.202:8090"]

访问prometheus,查看效果

在这里插入图片描述

安装部署grafana,展示出图。

官网下载源码包

[root@nfs graf]# ls
grafana-enterprise-9.4.7-1.x86_64.rpm
[root@nfs graf]# yum install grafana-enterprise-9.1.2-1.x86_64.rpm -y #安装grafana
[root@prometheus graf]# service grafana-server start #启动grafana
Starting grafana-server (via systemctl):                   [  确定  ]
[root@prometheus graf]# systemctl enable grafana-server #开机启动grafana

登录grafana

在这里插入图片描述 账号密码默认为admin

配置prometheus数据源

在这里插入图片描述 导入使用8919的中文化模块 在这里插入图片描述

效果测试

在这里插入图片描述

7.ab测试机对web集群和负载均衡器进行压力测试,了解系统性能的瓶颈。

安装ab命令,yum install -y httpd-tools 压力测试

[root@ab ~]# ab -c 1500 -n 20000 http://www.chenlb666.top/
结果为
Requests per second:    2618.15 [#/sec] (mean)
Time per request:       852.403 [ms] (mean)
Time per request:       0.852 [ms] (mean, across all concurrent requests)
Transfer rate:          400.98 [Kbytes/sec] received

多次修改并发数和请求数,每秒请求数最大值约为2600左右。

8.对系统性能资源(如内核参数、nginx参数)进行调优,提升系统性能

内核参数调优

[root@lb1 ~]# ulimit -n 65535
[root@lb1 ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7190
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7190
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

交换分区调优,当内存使用率为0时在使用交换分区资源,提高内存的利用率。

[root@lb1 ~]# cat /proc/sys/vm/swappiness 30 [root@lb1 ~]# echo 0 > /proc/sys/vm/swappiness [root@lb1 ~]# cat /proc/sys/vm/swappiness 0

nginx参数调优

#user  nobody;
worker_processes  2; #增加worker进程数量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events { 
    worker_connections  2048; #增加每个worker进程的最大并发连接数
}


在进行多次压力测试

[root@ab ~]# ab -c 3000 -n 30000 http://www.chenlb666.top/
结果为
Requests per second:    3014.15 [#/sec] (mean)
Time per request:       852.403 [ms] (mean)
Time per request:       0.852 [ms] (mean, across all concurrent requests)
Transfer rate:          400.98 [Kbytes/sec] received

发现每秒请求数增大为了3014,所以进行内核参数调优和nginx参数调优,效果还是有的。只不过不是很显著。

项目遇到的问题

  • 静态配置ip的时候,有几台虚拟机未设置桥接,导致无法上网
  • dns服务器上忘记关闭firewalld和NetworkManager导致windows客户机域名解析不出来。
  • 使用keepalived中的notify_backup功能并调用外部脚本时,由于脚本命名与配置中的脚本名不符,导致一致没有效果
  • 搭建双vip高可用架构的时候出现了脑裂现象,发现是因为虚拟路由器id设置错误了。
  • 未完全理解ansible中script模块的使用,以为是要将脚本上传到远程主机才可以执行脚本,其实完全可以在在中控机上编写好脚本,远程机上执行。
  • realip获取真实客户机的ip地址时,代码放错了位置,导致没有出现效果。
  • nginx前期编译时没加入realip模块,可以考虑热升级。
  • 一键安装nginx脚本中修改PATH变量时,修改的是子进程中的环境变量,即使export PATH变量也只是让子进程以及子进程的子进程可以使用,无法影响到父进程的环境变量,导致安装完nginx后无法在PATH变量中找到nginx命令,简单的解决办法有二个:一是重启,二是在ansibleplaybook中不使用script模块,而是先cp脚本过去后通过shell模块用
  • 安装速度慢,可以选择配置yum源

项目心得

1.提前规划好整个集群的架构,可以提高项目开展时效率。 2.对基于Nginx的web集群和高可用、高性能有了深入的理解,同时对脑裂和vip漂移现象也有了更加深刻的体会和分析。 3.加强了对7层负载均衡和dns负载均衡的认识 4.认识到了系统性能资源的重要性,对压力测试下整个集群的瓶颈有了一个整体概念。 5.对监控也有了的更进一步的认识,监控可以提前看到问题,做好预警 6.对很多软件之间的配合有了一定的理解,如Grafana、prometheus、ansible、nginx、nfs等 7.troubleshooting的能力得到了提升