天天看点

基于Corosync + LNMP + NFS 服务实现高可用

一、试验拓扑:

基于Corosync + LNMP + NFS 服务实现高可用
二、配置File Server

  1. # pvcreate /dev/sda5  
  2. # vgcreate vg01 /dev/sda5  
  3. # lvcreate -n mydata -L 10G vg01  
  4. # mke2fs -j /dev/vg01/mydata  
  5. # cat /etc/fstab  
  6. /dev/vg01/mydata /share   ext3 defaults 0 0  
  7. # mkdir /share  
  8. # mount -a  
  9. (这里要注意的是node1,node2,File Server三台的mysql用户UID要一样)  
  10. # useradd -r -u 200 mysql  
  11. # chown -R mysql.mysql /share/  
  12. # vim /etc/exports   
  13. /share  172.16.15.20(rw,no_root_squash) 172.16.15.30(rw,no_root_squash)  
  14. # service nfs restart  
  15. # chkconfig nfs on 

三、配置RS1和RS2

1、基本配置

  1. # vim /etc/sysconfig/network  
  2. HOSTNAME=rs1.peace.com  
  3. # vim /etc/hosts  
  4. 192.168.80.145  rs1.peace.com rs1  
  5. 192.168.80.146  rs2.peace.com rs2  
  6. # ssh-keygen -t rsa  
  7. # ssh-copy-id -i .ssh/id_rsa.pub  root@rs2 

2、配置LNMP

环境需求:

# yum -y install pcre-devel

# yum -y groupinstall "Development Tools"

# yum -y groupinstall "Development Libraries"

# yum -y groupinstall "X Software Development"

1)配置Nginx

  1. # groupadd -r nginx  
  2. # useradd -r -g nginx -s /sbin/nologin -M nginx  
  3. # tar xf nginx-1.2.2.tar.gz   
  4. # cd  nginx-1.2.2  
  5. # ./configure --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log   --http-log-path=/var/log/nginx/access.log   --pid-path=/var/run/nginx/nginx.pid    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre   
  6. # make && make install   
  7. # cd /root  
  8. # cp nginx /etc/rc.d/init.d/nginx   
  9. # chmod +x /etc/rc.d/init.d/nginx  
  10. # service nginx start   

2)配置Mysql

  1. # useradd -r -u 200 mysql  
  2. # mkdir /data/mydata -p  
  3. # mount -t nfs 192.168.80.142:/share /data/mydata/   
  4. # tar -xf mysql-5.5.24-linux2.6-i686.tar.gz  -C /usr/local/   
  5. # cd /usr/local/  
  6. # ln -s mysql-5.5.24-linux2.6-i686/ mysql   
  7. # cd mysql  
  8. (注意:这里的RS1是第一次初始化Mysql所以使用以下2条命令,而RS2只需执行“chown -R root.mysql ."即可,不用再重新初始化)  
  9. # chown -R mysql.mysql .  
  10. # scripts/mysql_install_db --user=mysql --datadir=/data/mydata/  
  11. # cp support-files/my-large.cnf /etc/my.cnf  
  12. # sed -i 's/^\(thread_concurrency\).*/\1 = 2/' /etc/my.cnf  
  13. # sed -i '/^thread_concurrency.*/a\datadir = /data/mydata/' /etc/my.cnf  
  14. # cp support-files/mysql.server /etc/rc.d/init.d/mysqld  
  15. # chmod +x /etc/rc.d/init.d/mysqld  
  16. # cd /root  
  17. # echo '/usr/local/mysql/lib/' >> /etc/ld.so.conf  
  18. # ldconfig   
  19. # service mysqld start  

3)配置php

  1. 升级安装如下软件包:  
  2. libmcrypt-2.5.7-5.el5.i386.rpm  
  3. libmcrypt-devel-2.5.7-5.el5.i386.rpm  
  4. mhash-0.9.2-6.el5.i386.rpm  
  5. mhash-devel-0.9.2-6.el5.i386.rpm  
  6. # cd php_rpm  
  7. # rpm -Uvh libmcrypt-* mhash-* && cd /root  
  8. # tar xf php-5.4.4.tar.bz2   
  9. # cd php-5.4.4  
  10. # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring  --enable-sockets --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl   
  11. # make && make install  
  12. # cp php.ini-production /etc/php.ini  
  13. # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm  
  14. # chmod +x /etc/rc.d/init.d/php-fpm   
  15. # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  
  16. # sed -i 's/^;\(pid.*\)/\1/' /usr/local/php/etc/php-fpm.conf  
  17. # sed -i 's/^\(pm.max_children\).*/\1 = 50/' /usr/local/php/etc/php-fpm.conf  
  18. # sed -i 's/^\(pm.start_servers\).*/\1 = 5/' /usr/local/php/etc/php-fpm.conf  
  19. # sed -i 's/^\(pm.min_spare_servers\).*/\1 = 2/' /usr/local/php/etc/php-fpm.conf  
  20. # sed -i 's/^\(pm.max_spare_servers\).*/\1 = 8/' /usr/local/php/etc/php-fpm.conf  
  21. # cd /root  
  22. # service php-fpm start 

4)整合nginx,php

  1. # echo "fastcgi_param  SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;"  >> /etc/nginx/fastcgi_params  
  2. 去掉nginx.conf中的如下行的#号,使用sed即可  
  3.         location ~ \.php$ {  
  4.             root           html;  
  5.             fastcgi_pass   127.0.0.1:9000;  
  6.             fastcgi_index  index.php;  
  7.             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  8.             include        fastcgi_params;  
  9.         }  
  10. # sed -i '65,71s/#//' /etc/nginx/nginx.conf  
  11. 添加支持index.php页面  
  12. location / {  
  13.             root   html;  
  14.             index  index.php index.html index.htm;  
  15.         }  
  16. # sed -i '45s/\<\(index.html\)\>/index.php \1/' /etc/nginx/nginx.conf  
  17. # service nginx restar 

5)测试访问下RS1和RS2

分别在两台添加测试页面

  1. # vim /usr/html/index.php  
  2. <?php 
  3. $link=mysql_connect('localhost','root','');  
  4. if ($link)  
  5.         echo "RS1 Sucess!!";  
  6. else  
  7.         echo "RS1 Failuser!!";  
  8. mysql_close();  
  9. ?> 

访问结果如下:

基于Corosync + LNMP + NFS 服务实现高可用
基于Corosync + LNMP + NFS 服务实现高可用

之后一定要关闭所有的服务,并且卸载数据目录

# service nginx stop

# service mysqld stop

# service php-fpm stop

# umount /data/mydata

四、配置corosync

1、安装如下软件包:

  1. cluster-glue-1.0.6-1.6.el5.i386.rpm  
  2. cluster-glue-libs-1.0.6-1.6.el5.i386.rpm  
  3. corosync-1.2.7-1.1.el5.i386.rpm  
  4. corosynclib-1.2.7-1.1.el5.i386.rpm  
  5. heartbeat-3.0.3-2.3.el5.i386.rpm  
  6. heartbeat-libs-3.0.3-2.3.el5.i386.rpm  
  7. libesmtp-1.0.4-5.el5.i386.rpm  
  8. pacemaker-1.1.5-1.1.el5.i386.rpm  
  9. pacemaker-cts-1.1.5-1.1.el5.i386.rpm  
  10. pacemaker-libs-1.1.5-1.1.el5.i386.rpm  
  11. resource-agents-1.0.4-1.1.el5.i386.rpm  
  12. # yum -y --nogpgcheck localinstall *.rpm  
  13. # cd /etc/corosync/  
  14. # cp corosync.conf.example corosync.conf 

2、修改配置文件,具体详细信息可以参考之前文章

  1. # vim corosync.conf  
  2. compatibility: whitetank      
  3. totem {         
  4.         version: 2       
  5.         secauth: on        
  6.         threads: 0       
  7.         interface {       
  8.                 ringnumber: 0    
  9.                 bindnetaddr: 192.168.80.0     
  10.                 mcastaddr: 226.94.1.1     
  11.                 mcastport: 5405      
  12.         }  
  13. }  
  14. logging {        
  15.         fileline: off  
  16.         to_stderr: no       
  17.         to_logfile: yes       
  18.         to_syslog: no       
  19.         logfile: /var/log/corosync.log     
  20.         debug: off       
  21.         timestamp: on       
  22.         logger_subsys {  
  23.                 subsys: AMF  
  24.                 debug: off  
  25.         }  
  26. }  
  27. amf {      
  28.         mode: disabled  
  29. }  
  30. (添加以下信息)  
  31. service {  
  32.         ver: 0  
  33.         name: pacemaker    
  34. }  
  35. aisexec {     
  36.         user:   root  
  37.         group:  root  

3、其他配置

  1. # corosync-keygen   //生成authkey密钥  
  2. # scp -p authkey corosync.conf node2:/etc/corosync/  
  3. # service corosync start  
  4. # ssh rs2 '/etc/rc.d/init.d/corosync start' 

4、建立资源

  1. crm(live)configure# primitive mysql_vip ocf:heartbeat:IPaddr params ip=192.168.80.100  
  2. crm(live)configure# primitive mynfs ocf:heartbeat:Filesystem params device="192.168.80.142:/share" directory="/data/mydata" fstype="nfs" op start timeout=60s op stop timeout=60s 
  3. crm(live)configure# primitive mysql lsb:mysqld   
  4. crm(live)configure# primitive php-fpm lsb:php-fpm   
  5. crm(live)configure# primitive nginx lsb:nginx  
  6. crm(live)configure# colocation lnmp inf: nginx php-fpm mysql_vip mynfs mysql  
  7. crm(live)configure# order mysql_after_mynfs mandatory: mynfs mysql  
  8. crm(live)configure# property stonith-enabled=false 
  9. crm(live)configure# commit  
  10. crm(live)configure# quit 

五、测试验证

通过crm status 查看状态信息,发现已经在RS1上了,并且有相应的进程:

基于Corosync + LNMP + NFS 服务实现高可用

访问下看看:

基于Corosync + LNMP + NFS 服务实现高可用

下面模拟RS1挂掉,看是否正常,在rs1上执行crm node standby即可,也已看到已经切换到RS2上了:

基于Corosync + LNMP + NFS 服务实现高可用

访问下,已经切换到RS2上了:

基于Corosync + LNMP + NFS 服务实现高可用

并且RS2进程也已经启动了:

继续阅读