构建环境:centos7.1
Diretor server:172.16.15.131 ( VIP:172.16.15.138 )
Real server:172.16.15.132/133 配置163源
拓扑结构如图:
1.构建LVS-DR 实现
下面我们VS/DR介绍详细的配置过程。
Diretor server上面的配置:
//首先在Director Server上绑定一个虚拟IP(也叫VIP),此IP用于对外提供服务: ifconfig eth0:0 172.16.15.138 broadcast 172.16.15.138 netmask 255.255.255.255 up //给设备eth0:0指定一条路由 route add -host 172.16.15.138 dev eth0:0 //启用系统的包转发功能 echo "1">/proc/sys/net/ipv4/ip_forward //安装ipvsadm yum install -y ipvsadm //清楚ipvsadm以前的设置 ipvsadm -C //添加一个新的虚拟IP记录192.168.132.254,其持续服务之间是120秒 ipvsadm -A -t 172.16.15.138:80 -s rr -p 120 //在新增的虚拟IP记录中新增两天real server记录,-g即为使用VS/DR模式 ipvsadm -a -t 172.16.15.138:80 -r 172.16.15.132:80 -g ipvsadm -a -t 172.16.15.138:80 -r 172.16.15.133:80 -g 脚本如下 #!/bin/bash VIP=172.16.15.138 RIP1=172.16.15.131 RIP2=172.16.15.132 case "$1" in start) echo "开始配置LVS Director Server" ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev eth0:0 echo "1">/proc/sys/net/ipv4/ip_forward ipvsadm -C ipvsadm -A -t $VIP:80 -s rr -p 120 ipvsadm -a -t $VIP:80 -r $RIP1:80 -g ipvsadm -a -t $VIP:80 -r $RIP2:80 -g ipvsadm echo "配置成功" ;; stop) echo "正在关闭LVS Director Server" echo "0">/proc/sys/net/ipv4/ip_forward ipvsadm -C ifconfig eth0:0 down echo "关闭成功!" ;; *) echo "用法:$0 {start|stop}" exit 1 esac //查看LVS状态 ipvsadm -Ln |
两台real server上的配置:
/*在回环设备上绑定了一个虚拟IP地址,并设定其子网掩码为255.255.255.255,与Director Server上的虚拟IP保持互通*/ ifconfig lo:0 172.16.15.138 broadcast 172.16.15.138 netmask 255.255.255.255 up route add -host172.16.15.138 dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce |
编辑为如下脚本lvsRS.sh
#!/bin/bash VIP=172.16.15.138 case "$1" in start) echo "配置lvs Real Server开始..." ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up route add -host $VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce ;; stop) echo "正在关闭lvs Real server" ifconfig lo:0 down echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce ;; *) echo "用法:$0 {start|stop}" exit 1 esac
LVS-DR模型已搭建成功。
第二步,两台realserver上搭建nginx
#编译nginx环境前需要先把gcc等开发库之类提前装好;
[root@realserver1 ~]# yum -y install gcc gcc-c++ automake autoconf libtool make #安装pcre是为了支持rewrite, #zlib是为了支持gzip压缩 #openssl是为了支持https; [root@realserver1 ~]# yum install -y pcre-devel.x86_64 pcre-devel.x86_64 zlib.x86_64 zlib-devel.x86_64 openssl-devel.x86_64 openssl.x86_64 [root@realserver1 ~]# groupadd www [root@realserver1 ~]# useradd -r -g www www -s /sbin/nologin #运行nginx的用户和用户组 [root@realserver1 nginx-1.8.1]# mkdir -pv /opt/application/nginx mkdir: created directory `/opt/application' mkdir: created directory `/opt/application/nginx' [root@realserver1 tools]# tar -xf nginx-1.8.1.tar.gz [root@realserver1 tools]# cd nginx-1.8.1 [root@realserver1 nginx-1.8.1]# ./configure --prefix=/opt/application/nginx/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-ipv6 # --with-http_stub_status_module 启用nginx状态监控 # --with-http_ssl_module 启用HTTPS加密 # --with-http_spdy_module 启用spdy支持,缩短为网页加载时间 # --with-http_gzip_static_module 启用静态压缩 # --with-http_realip_module 做代理时获取客户端真实IP # --with-ipv6 支持ipv6 [root@realserver1 nginx-1.8.1]# make ;make install
在realserver上安装percona (mysql)
1.解压包 tar xvf Percona-Server-5.7.10-3-r63dafaf-el7-x86_64-bundle.tar 2.删除无用的rpm包 rm Percona-Server-test-57-5.7.10-3.1.el7.x86_64.rpm rm Percona-Server-tokudb-57-5.7.10-3.1.el7.x86_64.rpm 3.安装相关联数据包/卸载冲突mariadb.lib yum install -y perl yum -y install libaio yum install -y net-tools rpm -qa|grep -i mariadb rpm -e mariadb-libs-5.5.44-1.el7_1.x86_64 --nodeps //强制卸载 4.安装rpm包 rpm -ivh Percona-Server-*.rpm 5.启动mysql service mysql start 6.查看初始密码 grep 'temporary password' /var/log/mysqld.log 7.验证登录 mysql -uroot -p{passwd} 8.修改初始密码 ALTER USER 'root'@'localhost' IDENTIFIED BY '*******'; flush privileges;
[root@php-server ~]# yum -y install libmcrypt libmcrypt-devel mhash mhash-devel install # libxml2-devel openssl openssl-devel bzip2-devel libcurl-devel gd gd-devel.x86_64 [root@php-server ~]# groupadd www [root@php-server ~]# useradd -r -g www www -s /sbin/nologin #运行php-fpm的用户和用户组 [root@php-server ~]# wget http://cn.php.net/distributions/php-5.6.24.tar.gz [root@php-server ~]# tar -zxf php-5.6.24.tar.gz [root@php-server ~]# cd php-5.6.24
mkdir: created directory `/opt/application' mkdir: created directory `/opt/application/php' [root@php-server php-5.6.24]# ./configure \ --prefix=/opt/application/php \ --with-config-file-path=/etc \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-fpm \ --enable-ftp \ --enable-zip \ --enable-soap \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-exif \ --enable-pcntl \ --enable-opcache \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-sockets \ --enable-gd-native-ttf \ --enable-inline-optimization \ --with-zlib \ --with-curl \ --with-gd \ --with-mcrypt \ --with-openssl \ --with-mhash \ --with-xmlrpc \ --with-gettext \ --with-iconv-dir \ --with-freetype-dir \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-libxml-dir=/usr \ --with-pdo-mysql=mysqlnd \ --disable-rpath \ --disable-ipv6 \ --disable-debug \ --disable-fileinfo \
[root@php-server php-5.6.24]# make install [root@php-server php-5.6.24]# cp -a sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@php-server php-5.6.24]# chmod +x /etc/init.d/php-fpm [root@php-server php-5.6.24]# cp -a php.ini-production /etc/php.ini cp: overwrite `/etc/php.ini'? y [root@php-server ~]# cd /opt/application/php/etc/ [root@php-server etc]# cp php-fpm.conf.default php-fpm.conf [root@php-server etc]# vim php-fpm.conf #将164行注释掉,修改为 164 ;listen = 127.0.0.1:9000 165 listen = 172.16.120.240:9000 [root@php-server etc]# service php-fpm start [root@php-server etc]# chkconfig --add php-fpm [root@php-server etc]# chkconfig php-fpm on [root@php-server etc]# chkconfig --list php-fpm php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
修改nginx和php的配置文件
# vi /opt/application/nginx/conf/nginx.conf location / { root /opt/application/nginx/html/www; index index.html index.htm; } location ~ \.php$ { root /opt/application/nginx/html/www; fastcgi_pass 172.16.120.240:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
在172.16.120.240上安装NFS挂载wordpress
[root@php-server ~]# yum install -y nfs-utils.x86_64 nfs-utils-lib.x86_64 nfs-utils-lib-devel.x86_64 # yum安装nfs程序 [root@php-server ~]# /etc/init.d/rpcbind start [root@php-server ~]# /etc/init.d/nfs start [root@php-server data]# mkdir -pv /data/www mkdir: created directory `/data' mkdir: created directory `/data/www' [root@php-server data]# cat /etc/exports /data/www *(rw,no_root_squash) [root@php-server data]# ll -d /data/www/ drwxrwxrwx 2 root root 4096 Aug 13 04:02 /data/www/ [root@php-server data]# service nfs restart [root@php-server data]# showmount -e 172.16.120.240 Export list for 172.16.120.240: /data/www * #下载wordpress到php-server并解压缩 [root@php-server www]# chown -R www.www *
在两个realserver上配置nfs客户端
####安装nfs####
[root@realserver1 html]# yum install -y nfs-utils.x86_64 nfs-utils-lib.x86_64 nfs-utils-lib-devel.x86_64 #把172.16.120.240上的程序挂载到realserver1、realserver2对应目录上 [root@realserver1 html]# mount -t nfs 172.16.120.240:/data/www/ /opt/application/nginx/html/wordpress/
mysql进行如下设
mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'172.16.120.240' IDENTIFIED BY '123456'; mysql> flush privileges;
####wordpress 设置####
[root@mytest-120-240 html]# cat wp-config.php /** WordPress数据库的名称 */ define('DB_NAME', 'wordpress'); /** MySQL数据库用户名 */ define('DB_USER', 'root'); /** MySQL数据库密码 */ define('DB_PASSWORD', '12345'); /** MySQL主机 */ define('DB_HOST', '172.16.15.132'); /** 创建数据表时默认的文字编码 */ define('DB_CHARSET', 'utf8');
至此基于LVS-DR的一套高性能的服务已构建成功。!!!
原创文章,作者:heilinux,如若转载,请注明出处:http://www.178linux.com/35847