Linux,Database,nginx,MySQL,http,web

LNMP HA Cluster

LNMP


  如下图,次数实现MySQL主从读写分离,通过Keepalived监控服务状态,HAProxy进行调度,实现负载均衡

LB节点安装相关软件包

yum install -y keppalived haproxy

Keepalived

  • 绑定VIP172.20.35.99,以及haproxy状态监控,VIP转移通知
cat <<EOF | tee /etc/keepalived/notify.sh
#!/bin/bash
contact='122922679@qq.com'
notify() {
    mailsubject="\$(hostname) to be $1, vip 转移"
    mailbody="\$(date +'%F %T'): vrrp transition, \$(hostname) changed to be \$1"
    echo "\$mailbody" | mail -s "\$mailsubject" \$contact
}
case \$1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: \$(basename \$0) {master|backup|fault}"
    exit 1
    ;;
esac
EOF
  • 配置邮件发送账号
yum install -y mailx
echo """
set from=122922679@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=122922679@qq.com
set smtp-auth-password=XXXXXXXXXXXXXXXXXXXXX
set smtp-auth=login
set ss-verify=ignore
""" >> /etc/mail.rc
  • 创建监控HAProxy服务脚本
cat <<EOF | tee /etc/keepalived/chk_ha.sh
#!/bin/bash
kill -0 \$(cat /var/run/haproxy.pid)
if [ \$? -ne 0 ]; then
    systemctl restart haproxy
    if [ \$? -ne 0 ]; then
        systemctl stop keepalived
    fi
fi
EOF
  • 配置keepalived
cat <<EOF | tee /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka-180
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/chk_ha.sh"
    interval 2 
    weight 2 
}

vrrp_instance VIP1 {
    state MASTER
    interface eth0
    virtual_router_id 180
    priority 100
    advert_int 1
    unicast_src_ip 172.20.35.180
    unicast_peer {
        172.20.35.181
    }
    authentication {
        auth_type PASS
        auth_pass ce639edc
    }
    virtual_ipaddress {
        172.20.35.99 dev eth0 label eth0:0
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
EOF

HAProxy

  • 允许监听非本机IP地址
cat <<EOF | tee /etc/sysctl.d/ha.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
EOF

sysctl --system
  • HAProxy
cat <<EOF | tee /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /usr/local/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    uid 88
    gid 88
    daemon
    nbproc 4
    cpu-map 1 0
    cpu-map 2 1
    cpu-map 3 2
    cpu-map 4 3
    nbthread 2
    pidfile /usr/local/haproxy/run/haproxy.pid
    log 127.0.0.1 local3 info
defaults
    option http-keep-alive
    option  forwardfor
    maxconn 100000
    mode http
    option redispatch
    option http-keep-alive
    option forwardfor
    timeout http-keep-alive 120s
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms
listen stats
    mode http
    bind 0.0.0.0:8000
    stats enable
    stats hide-version
    log global
    stats uri     /haproxy-status
    stats realm "This is for admin"
    stats auth    admin:123456
    stats admin if TRUE
listen web_port_80
    bind 172.20.35.99:80
    mode http
    balance roundrobin
    server web1 172.20.35.70:80 check port 80 inter 3s fall 3 rise 5
    server web2 172.20.35.71:80 check port 80 inter 3s fall 3 rise 5
listen mysql_port_6033
    bind 172.20.35.99:6033
    mode tcp
    server mysql 172.20.35.77:6033 check inter 3s fall 3 rise 5
EOF

MySQL

Master

  • 编译MySQL
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/var/lib/mysql \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DSYSCONFDIR=/etc/mysql \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 -DWITH_READLINE=1  \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1

make -j 8 && make install

echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

cd /usr/local/mysql
scripts/mysql_install_db --user=mysql --datadir=/var/lib/mysql

cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
/etc/rc.d/init.d/mysqld start
  • 配置MySQL
cat <<EOF | tee >etc/my.cnf
[mysqld]
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
innodb_file_per_table = on
skip_name_resolve = on
character-set-server = utf8mb4
default_storage_engine = InnoDB
log_bin
binlog_format = ROW
log-basename = master
server_id = 20
EOF
  • 配置主从复制用户和读写,监控用户
grant replication slave on *.* to repluser@'172.20.35.%' identified by 'manunkind';
grant replication slave on *.* to 'monitor'@'172.20.35.%' identified by 'manunkind';
grant all on *.* to sqluser@'172.20.35.%' identified by 'manunkind';
  • 创建应用数据库
create database wpdb;

Slave

cat <<EOF | tee >etc/my.cnf
[mysqld]
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
symbolic-links=0
innodb_file_per_table = on
skip_name_resolve = on
character-set-server = utf8mb4
default_storage_engine = InnoDB
server_id = 75
read_only = on
EOF
CHANGE MASTER TO
 MASTER_HOST='172.20.35.74',
 MASTER_USER='repluser',
 MASTER_PASSWORD='manunkind',
 MASTER_PORT=3306,
 MASTER_LOG_FILE='master-bin.000003',
 MASTER_LOG_POS=245;

start slave;
show slave status\G

ProxySQL

cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=https://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
gpgcheck=1
gpgkey=https://repo.proxysql.com/ProxySQL/repo_pub_key
EOF
yum repolist
yum install -y proxysql

service proxysql start
  • 配置读写分离
mysql -uadmin -padmin -P6032 -h127.0.0.1
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(10,'172.20.35.74',3306,1,'Write Group'),(20,'172.20.35.75',3306,1,'Read Group'),(20,'172.20.35.76',3306,1,'Read Group');
load mysql servers to runtime;
save mysql servers to disk;

set mysql-monitor_username='monitor';
set mysql-monitor_password='manunkind';
load mysql variables to runtime;
save mysql variables to disk;

insert into mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup) values (10,20);
load mysql servers to runtime;
save mysql variables to disk;

insert into mysql_users(username,password,default_hostgroup) values('sqluser','manunkind',10);
load mysql users to runtime;
save mysql users to disk;
  • 添加读写分离规则
insert into mysql_query_rules (rule_id,active,match_digest,destination_hostgroup,apply)VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
load mysql query rules to runtime;
save mysql query rules to disk;
  • 确认读写分离
mysql -usqluser -pmanunkind -h127.0.0.1 -P6033  -e 'start transaction;select @@server_id;commit;select @@server_id'

NFS

mkdir /data/app -pv
cat <<EOF | tee /etc/exports
/data/app 172.20.35.0/24(rw,sync,no_root_squash)
EOF

systemctl enable --now nfs-server

Nginx + PHP

  • 挂载NFS
yum install -y nfs-utils
echo "172.20.35.73:/data/app    /app/ngx/html    nfs    defaults,_netdev    0 0" >> /etc/fstab
mount -a

安装Nginx

  • 编译
yum install -y gcc gcc-c++ perl-devel bzip2 perl-ExtUtils-Embed openssl-devel libxml2-devel libxslt-devel pcre pcre-devel
git clone https://github.com/openresty/echo-nginx-module.git
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar nginx-1.16.0.tar.gz
cd nginx-1.16.0

./configure --prefix=/app/ngx \
--add-dynamic-module=/root/echo-nginx-module \
--with-stream \
--with-pcre \
--with-http_gzip_static_module \
--with-stream_ssl_preread_module \
--with-stream_ssl_module \
--with-http_ssl_module \
--user=nginx \
--group=nginx

make -j 8 && make install

groupadd -r nginx
useradd nginx -g nginx -r -s /sbin/nologin
  • 配置
cat /app/ngx/server/wp.conf

 #启用下述配置
 server {
    listen 80;
    server_name www.s4lm0x.org;
    index index.php index.html;
    location ~ \.php$ {
        root           html;
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi.conf;
    }
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
    }
}

/app/ngx/sbin/nginx -s reload

安装PHP

  • 编译
yum install -y bzip2-devel libcurl-devel gd-devel gmp-devel libmcrypt-devel libzip libzip-devel

wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar xf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make -j 4 && make install
wget https://www.php.net/distributions/php-7.3.6.tar.bz2
tar xf php-7.3.6.tar.bz2
cd php-7.3.6

./configure --prefix=/usr/local/php73 \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-config-file-path=/usr/local/php73/conf \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-mcrypt \
--with-mhash \
--with-gmp \
--with-curl \
--with-bz2 \
--with-libxml-dir \
--with-xmlrpc \
--with-zlib-dir \
--with-gd \
--with-pcre-dir \
--enable-soap \
--with-gettext \
--with-config-file-scan-dir=/usr/local/php73/php.d \
--enable-mbstring \
--enable-xml \
--enable-fpm \
--enable-sockets \
--enable-exif \
--enable-mbstring \
--enable-bcmath \
--enable-maintainer-zts \
--disable-fileinfo \
--enable-ftp \
--enable-zip \
--enable-gd-native-ttf \
--enable-inline-optimization

make -j 8 && make install
  • 配置
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
cp php.ini-production /usr/local/php73/etc/php.ini
cp /usr/local/php73/etc/php-fpm.conf.default /usr/local/php73/etc/php-fpm.conf
cp /usr/local/php73/etc/php-fpm.d/www.conf.default /usr/local/php73/etc/php-fpm.d/www.conf
sed -i 's@user = nobody@user = nginx@' /usr/local/php73/etc/php-fpm.d/www.conf
sed -i 's@group = nobody@group = nginx@' /usr/local/php73/etc/php-fpm.d/www.conf
sed -i 's@;listen.mode = 0660@listen.mode = 0666@' /usr/local/php73/etc/php-fpm.d/www.conf
sed -i 's@listen = 127.0.0.1:9000@listen = /var/run/php-fpm.sock@' /usr/local/php73/etc/php-fpm.d/www.conf

systemctl start php-fpm
  • 配置wordpress

添加数据库配置信息即可

cp wp-config-sample.php wp-config.php
vim wp-config.php
define('DB_NAME', 'wpdb');
define('DB_USER', 'sqluser');
define('DB_PASSWORD', 'manunkind');
define('DB_HOST', '172.20.35.99:6033');
微信扫一扫,向我赞赏

微信扫一扫,向我赞赏

微信扫一扫,向我赞赏

支付宝扫一扫,向我赞赏

回复

This is just a placeholder img.