负载均衡haproxy+keepalived、lvs+keepalived后端php+nginx框架

101、102机器负载均衡haproxy+keepalived、lvs+keepalived后端php+nginx框架

haproxy设置

1. 解压文件并进入解压文件夹

tar xvf haproxy-1.9.8.tar.gz
cd haproxy-1.9.8

2. 编译安装haproxy,编译参数

make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

4. 拷贝二进制执行文件入PATH路径

cp haproxy /usr/sbin/

5. 修改systemctl启动文件

vim /usr/lib/systemd/system/haproxy.service

[Unit]                                                                                                                  
Description=HAProxy Load Banlancer  
After=syslog.target network.target  

[Service]  
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q  
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid  
ExecReload=/bin/kill -USR2 $MAINPID  

[Install]  
WantedBy=multi-user.target

6. 创建配置和pid文件夹,并授权

mkdir /etc/haproxy

useradd -r -s /sbin/nologin haproxy

mkdir /var/lib/haproxy

chown haproxy.haproxy /var/lib/haproxy -R

7. 修改配置文件,如下

vim /etc/haproxy/haproxy.cfg

global                              
maxconn 65536  
chroot /usr/local/haproxy  
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin (打开socket文件,也可以不打开注释掉)  
uid 1000  
gid 1000  
daemon  
nbproc 1  
cpu-map 1 0  
#cpu-map 2 1  
#cpu-map 3 2  
#cpu-map 4 3  
spread-checks 5  
pidfile /run/haproxy.pid  
#log 127.0.0.1 local3 info  

defaults  
option http-keep-alive  
option forwardfor  
maxconn 65536  
mode http  
timeout connect 300000ms  
timeout client 300000ms  
timeout server 300000ms  

#HTTP访问入口===============================================================  
listen web_http  
bind 192.168.66.101:80  
mode tcp  
#log global  
balance roundrobin  
server web1 192.168.66.103:80 check inter 3000 fall 2 rise 5  
server web2 192.168.66.104:80 check inter 3000 fall 2 rise 5  

keepalived设置

1. 安装keepalived软件

yum install keepalived

2. 修改配置文件

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived  

global_defs {  
   notification_email {  
     [email protected]  
   }  
   notification_email_from [email protected]  
   smtp_server 127.0.0.1  
   smtp_connect_timeout 30  
   router_id s2.pengbo.com (防止名字冲突,建议两台机器改为不同名的机器)  
   vrrp_skip_check_adv_addr  
   #vrrp_strict   
   vrrp_iptables  
   vrrp_garp_interval 0  
   vrrp_gna_interval 0  
}  

vrrp_instance VIP1 {  
    state MASTER   (102机器改为BACKUP)  
    interface eth0  
    virtual_router_id 66 (在同一网段内,要保证该值不冲突)  
    priority 100 (102机器应改为低于100的值)  
    advert_int 2  
    unicast_src_ip 192.168.66.102  (这个地方填写本机地址)  
    unicast_peer {  
        192.168.66.101  (填写一起监听的keepalive机器)  
    }  
    authentication {  
        auth_type PASS  
        auth_pass linux36  
    }  
    virtual_ipaddress {  
        192.168.66.200 dev eth0 label eth0:0 (设置一个虚拟vip地址)  
    }  
}  

修改内核参数

1. 修改配置文件

vim /etc/sysctl.conf

   net.ipv4.ip_forward = 1  (打开转发功能)  
   net.ipv4.ip_nonlocal_bind = 1 (使其能够监听自身没有的IP地址)  

2. 使配置文件参数起效

sysctl -p

3. 启动服务

systemctl start haproxy keepalived

103、104机器

编译安装nginx

1. 编译安装nginx

tar xf nginx-1.16.0.tar.gz
./configure --prefix=/apps/nginx --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
make install

2. 创建系统用户nginx

useradd -r -s /usr/sbin/nologin nginx

3. 修改nginx的systemctl服务文件

vim nginx.service

[Unit]                                                                                                                  
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

mv nginx.service /usr/lib/systemd/system/

4. 创建软链接,将nginx二进制文件加入到PATH变量中

ln -sv /apps/nginx/sbin/nginx /usr/sbin/

5. 修改nginx配置文件

vim /apps/nginx/conf/nginx.conf

user  nginx;
worker_processes  auto;
pid        logs/nginx.pid;
给wordpress单独建一个访问路径
location /wordpress {
            root   html;
            index  index.php index.htm;
        }
打开PHP功能
location ~ \.php$ {
            root           /apps/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

6. 启动服务

nginx

7. 创建一个挂载nfs的文件夹

mkdir /apps/nginx/html/wordpress

编译安装php-fpm

1. 安装开发软件包

yum install libxml2-devel bzip2-devel libmcrypt-devel bzip2

2. 编译安装参数

tar xvf php-7.3.5
./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
make && make install

3. 拷贝配置文件

cp php.ini-production /etc/php.ini

4. 修改配置文件时区

vim /etc/php.ini

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Shanghai"

5. 移动service启动脚本

cp php-7.3.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
或者 cp php-7.3.5/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

6. 拷贝主配置文件

cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf
cp /app/php/etc/php-fpm.d/www.conf.default /app/php/etc/php-fpm.d/www.conf

7. 修改主配置文件中的所属者所属组

vim /app/php/etc/php-fpm.d/www.conf

   user = nginx  
   group = nginx

8. 启动服务

systemctl start php-fpm 或者 service php-fpm start

105机器

wordpress文件解压

1. 解压(将解压包放在/data/目录下)

unzip wordpress-5.2.1.zip
cd wordpress

2. 修改主配置文件模板,连接数据库

cp wp-config-sample.php wp-config.php
vim wp-config.php

define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wordpress' );

/** MySQL database password */
define( 'DB_PASSWORD', '123456' );

/** MySQL hostname */
define( 'DB_HOST', '192.168.66.106' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

存储服务器nfs

1. 启动服务(一般系统都默认安装)

systemctl start nfs

2. 修改配置文件

vim /etc/exports

/data/wordpress *(rw,no_root_squash)

3. 重新加载配置文件

exportfs -r

挂载文件

mount 192.168.66.105:/data/wordpress /apps/nginx/html/wordpress/ (在103、104机器上操作)
chown -R nginx.nginx /apps/nginx/html/wordpress/ (在103、104机器上操作)

106机器

二进制编译安装mariadb

1. 安装环境开发包

yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel libevent-devel libaio-devel

2. 解压二进制文件

tar xvf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/

3. 进入解压文件夹,创建软链接和修改权限

cd /usr/local
ln -sv mariadb-10.2.23-linux-x86_64/ mysql
chown -R root.root mysql/

4. 创建mysql数据使用的系统账户

useradd -r -s /sbin/nologin mysql -d /data/mysql
mkdir /data/mysql
chown mysql.mysql /data/mysql

5. 进入mysql主文件夹,执行安装脚本

cd mysql/
scripts/mysql_install_db --user=mysql --datadir=/data/mysql

6. 创建配置文件夹,并修改配置文件

mkdir /etc/mysql
cp support-files/my-huge.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf

[mysqld]
    datadir=/data/mysql
    port            = 3306
    socket          = /tmp/mysql.sock
    skip-external-locking
    key_buffer_size = 384M
    max_allowed_packet = 1M
    table_open_cache = 512
    sort_buffer_size = 2M
    read_buffer_size = 2M
    read_rnd_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    thread_cache_size = 8
    query_cache_size = 32M

7. 将mysql加入到启动服务脚本

cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --list

8. 设置环境变量文件,并起效

vim /etc/profile.d/lamp.sh
PATH=/usr/local/mysql/bin:$PATH
. /etc/profile.d/lamp.sh

9. 启动服务

service mysqld start

10. 创建wordpress数据库和创建一个wordpress使用的账户

mysql

create database wordpress;  
grant all on wordpress.* to wordpress@'192.168.66.%' identified by '123456';

107、108机器

1. 安装keepalived

yum install keepalived

2. 修改配置文件

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived                                        

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id s3.pengbo.com
   vrrp_skip_check_adv_addr
   vrrp_iptables
  #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VIP2 {
    state MASTER (区分主从)
    interface eth0
    virtual_router_id 99
    priority 100   (主和从优先级要不一样)
    advert_int 2
    unicast_src_ip 192.168.66.107  (这个地方填写本机地址)
    unicast_peer {                  
        192.168.66.108              (这个地方填写另外一个监视地址)
    }
    authentication {
        auth_type PASS
        auth_pass linux36
    }
    virtual_ipaddress {
        192.168.66.202 dev eth0 label eth0:0
    }
}
include /etc/keepalived/conf/*.conf (导入子配置文件)

3. 创建子配置文件,并写规则

mkdir /etc/keepalived/conf
vim /etc/keepalived/conf/lvs.conf

virtual_server 192.168.66.202 80 {                                                                                    
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 20
    protocol TCP
    sorry_server 192.168.66.105 80

  real_server 192.168.66.103 80 {
      weight 1
      TCP_CHECK {
      connect_timeout 5
      nb_get_retry
      delay_before_retry 3
      connect_port 80
      }
   }

   real_server 192.168.66.104 80 {
       weight 1
       TCP_CHECK {
       connect_timeout 5
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
   }
}

4. 启动服务,并查看规则

systemctl start keepalived
ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)  
Prot LocalAddress:Port Scheduler Flags  
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn  
TCP  192.168.66.202:80 wrr  
  -> 192.168.66.103:80            Route   1      0          0           
  -> 192.168.66.104:80            Route   1      0          0   

5.在103、104机器上的回环网卡绑定vip地址,并修改内核参数

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:1 192.168.66.202 netmask 255.255.255.255

6.提醒,在实验结束后记得将内核参数改回原值

猜你喜欢

转载自blog.51cto.com/14234933/2406987