一、试验拓扑:
二、配置File Server
- # pvcreate /dev/sda5
- # vgcreate vg01 /dev/sda5
- # lvcreate -n mydata -L 10G vg01
- # mke2fs -j /dev/vg01/mydata
- # cat /etc/fstab
- /dev/vg01/mydata /share ext3 defaults 0 0
- # mkdir /share
- # mount -a
- (这里要注意的是node1,node2,File Server三台的mysql用户UID要一样)
- # useradd -r -u 200 mysql
- # chown -R mysql.mysql /share/
- # vim /etc/exports
- /share 172.16.15.20(rw,no_root_squash) 172.16.15.30(rw,no_root_squash)
- # service nfs restart
- # chkconfig nfs on
三、配置RS1和RS2
1、基本配置
- # vim /etc/sysconfig/network
- HOSTNAME=rs1.peace.com
- # vim /etc/hosts
- 192.168.80.145 rs1.peace.com rs1
- 192.168.80.146 rs2.peace.com rs2
- # ssh-keygen -t rsa
- # 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
- # groupadd -r nginx
- # useradd -r -g nginx -s /sbin/nologin -M nginx
- # tar xf nginx-1.2.2.tar.gz
- # cd nginx-1.2.2
- # ./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
- # make && make install
- # cd /root
- # cp nginx /etc/rc.d/init.d/nginx
- # chmod +x /etc/rc.d/init.d/nginx
- # service nginx start
2)配置Mysql
- # useradd -r -u 200 mysql
- # mkdir /data/mydata -p
- # mount -t nfs 192.168.80.142:/share /data/mydata/
- # tar -xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/
- # cd /usr/local/
- # ln -s mysql-5.5.24-linux2.6-i686/ mysql
- # cd mysql
- (注意:这里的RS1是第一次初始化Mysql所以使用以下2条命令,而RS2只需执行“chown -R root.mysql ."即可,不用再重新初始化)
- # chown -R mysql.mysql .
- # scripts/mysql_install_db --user=mysql --datadir=/data/mydata/
- # cp support-files/my-large.cnf /etc/my.cnf
- # sed -i 's/^\(thread_concurrency\).*/\1 = 2/' /etc/my.cnf
- # sed -i '/^thread_concurrency.*/a\datadir = /data/mydata/' /etc/my.cnf
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- # chmod +x /etc/rc.d/init.d/mysqld
- # cd /root
- # echo '/usr/local/mysql/lib/' >> /etc/ld.so.conf
- # ldconfig
- # service mysqld start
3)配置php
- 升级安装如下软件包:
- libmcrypt-2.5.7-5.el5.i386.rpm
- libmcrypt-devel-2.5.7-5.el5.i386.rpm
- mhash-0.9.2-6.el5.i386.rpm
- mhash-devel-0.9.2-6.el5.i386.rpm
- # cd php_rpm
- # rpm -Uvh libmcrypt-* mhash-* && cd /root
- # tar xf php-5.4.4.tar.bz2
- # cd php-5.4.4
- # ./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
- # make && make install
- # cp php.ini-production /etc/php.ini
- # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
- # chmod +x /etc/rc.d/init.d/php-fpm
- # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- # sed -i 's/^;\(pid.*\)/\1/' /usr/local/php/etc/php-fpm.conf
- # sed -i 's/^\(pm.max_children\).*/\1 = 50/' /usr/local/php/etc/php-fpm.conf
- # sed -i 's/^\(pm.start_servers\).*/\1 = 5/' /usr/local/php/etc/php-fpm.conf
- # sed -i 's/^\(pm.min_spare_servers\).*/\1 = 2/' /usr/local/php/etc/php-fpm.conf
- # sed -i 's/^\(pm.max_spare_servers\).*/\1 = 8/' /usr/local/php/etc/php-fpm.conf
- # cd /root
- # service php-fpm start
4)整合nginx,php
- # echo "fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;" >> /etc/nginx/fastcgi_params
- 去掉nginx.conf中的如下行的#号,使用sed即可
- location ~ \.php$ {
- root html;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;
- }
- # sed -i '65,71s/#//' /etc/nginx/nginx.conf
- 添加支持index.php页面
- location / {
- root html;
- index index.php index.html index.htm;
- }
- # sed -i '45s/\<\(index.html\)\>/index.php \1/' /etc/nginx/nginx.conf
- # service nginx restar
5)测试访问下RS1和RS2分别在两台添加测试页面
- # vim /usr/html/index.php
- <?php
- $link=mysql_connect('localhost','root','');
- if ($link)
- echo "RS1 Sucess!!";
- else
- echo "RS1 Failuser!!";
- mysql_close();
- ?>
访问结果如下:
之后一定要关闭所有的服务,并且卸载数据目录 # service nginx stop # service mysqld stop # service php-fpm stop # umount /data/mydata 四、配置corosync
1、安装如下软件包:
- cluster-glue-1.0.6-1.6.el5.i386.rpm
- cluster-glue-libs-1.0.6-1.6.el5.i386.rpm
- corosync-1.2.7-1.1.el5.i386.rpm
- corosynclib-1.2.7-1.1.el5.i386.rpm
- heartbeat-3.0.3-2.3.el5.i386.rpm
- heartbeat-libs-3.0.3-2.3.el5.i386.rpm
- libesmtp-1.0.4-5.el5.i386.rpm
- pacemaker-1.1.5-1.1.el5.i386.rpm
- pacemaker-cts-1.1.5-1.1.el5.i386.rpm
- pacemaker-libs-1.1.5-1.1.el5.i386.rpm
- resource-agents-1.0.4-1.1.el5.i386.rpm
- # yum -y --nogpgcheck localinstall *.rpm
- # cd /etc/corosync/
- # cp corosync.conf.example corosync.conf
2、修改配置文件,具体详细信息可以参考之前文章
- # vim corosync.conf
- compatibility: whitetank
- totem {
- version: 2
- secauth: on
- threads: 0
- interface {
- ringnumber: 0
- bindnetaddr: 192.168.80.0
- mcastaddr: 226.94.1.1
- mcastport: 5405
- }
- }
- logging {
- fileline: off
- to_stderr: no
- to_logfile: yes
- to_syslog: no
- logfile: /var/log/corosync.log
- debug: off
- timestamp: on
- logger_subsys {
- subsys: AMF
- debug: off
- }
- }
- amf {
- mode: disabled
- }
- (添加以下信息)
- service {
- ver: 0
- name: pacemaker
- }
- aisexec {
- user: root
- group: root
- }
3、其他配置
- # corosync-keygen //生成authkey密钥
- # scp -p authkey corosync.conf node2:/etc/corosync/
- # service corosync start
- # ssh rs2 '/etc/rc.d/init.d/corosync start'
4、建立资源
- crm(live)configure# primitive mysql_vip ocf:heartbeat:IPaddr params ip=192.168.80.100
- 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
- crm(live)configure# primitive mysql lsb:mysqld
- crm(live)configure# primitive php-fpm lsb:php-fpm
- crm(live)configure# primitive nginx lsb:nginx
- crm(live)configure# colocation lnmp inf: nginx php-fpm mysql_vip mynfs mysql
- crm(live)configure# order mysql_after_mynfs mandatory: mynfs mysql
- crm(live)configure# property stonith-enabled=false
- crm(live)configure# commit
- crm(live)configure# quit
访问下看看:
下面模拟RS1挂掉,看是否正常,在rs1上执行crm node standby即可,也已看到已经切换到RS2上了: 访问下,已经切换到RS2上了: 并且RS2进程也已经启动了: 至此试验已经成功了,不过这里莫怪小弟愚钝,没法解决单个服务挂了也进行切换,想只能脚本监控了。