負載均衡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.提醒,在實驗結束後記得將內核參數改回原值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章