標籤:lvs-nat 負載平衡 discuz
寫在前面:如果此文有幸被某位朋友看見並發現有錯的地方,希望批評指正。如有不明白的地方,願可一起探討。
案例拓撲圖
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/6B/wKioL1Qr7wKQvEOsAAGJSJ4upk0858.jpg" title="圖1" alt="wKioL1Qr7wKQvEOsAAGJSJ4upk0858.jpg" />
配置主機1
安裝ipvsadm
# yum -y install ipvsadm# ipvsadm -A -t 10.170.2.80:80 -s rr# ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.101 -m# ipvsadm -a -t 10.170.2.80:80 -r 192.168.3.102 -m# echo 1 > /proc/sys/net/ipv4/ip_forward
配置主語2和3
編譯安裝apr-1.5.0
# tar xf apr-1.5.0.tar.bz2# cd apr-1.5.0# ./configure --prefix=/usr/local/apr# make && make install
編譯安裝apr-util-1.5.3
# tar xf apr-util-1.5.3.tar.bz2 # cd apr-util-1.5.3 # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ # make && make install
編譯安裝httpd-2.4.9
# tar xf httpd-2.4.9.tar.bz2 # cd httpd-2.4.9# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event# make && make install
修改httpd主設定檔,設定Pid檔案的路徑
# vim /etc/httpd24/httpd.conf
在ServerRoot "/usr/local/apache"行下面添加一行:
PidFile "/var/run/httpd.pid"
提供SysV服務指令碼/etc/rc.d/init.d/httpd24,這個檔案需要建立
# vim /etc/rc.d/init.d/httpd24
#!/bin/bash## httpd Startup script for the Apache HTTP Server## chkconfig: - 85 15# description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI.# processname: httpd# config: /etc/httpd/conf/httpd.conf# config: /etc/sysconfig/httpd# pidfile: /var/run/httpd.pid# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpdfi# Start httpd in the C locale by default.HTTPD_LANG=${HTTPD_LANG-"C"}# This will prevent initlog from swallowing up a pass-phrase prompt if# mod_ssl needs a pass-phrase from the user.INITLOG_ARGS=""# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server# with the thread-based "worker" MPM; BE WARNED that some modules may not# work correctly with a thread-based MPM; notably PHP will refuse to start.# Path to the apachectl script, server binary, and short-form for messages.apachectl=/usr/local/apache/bin/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL}stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo}# See how we were called.case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl [email protected] RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1esacexit $RETVAL
# chmod +x /etc/rc.d/init.d/httpd24 # chkconfig --add httpd24# chkconfig httpd24 on
啟動http24服務並測試
# service httpd24 start
在主機2和主機3上添加不同的頁面
主機2
# vim /usr/local/apache/htdocs/test.html <h1>Hello,WEB1</h1>
主機3
# vim /usr/local/apache/htdocs/test.html <h1>Hello,WEB2</h1>
在瀏覽器鍵入10.170.2.80/test.html,並重新整理可以得到不同的結果
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4B/6B/wKioL1Qr7UCxB_xVAAC5Sn8-TVw411.jpg" title="圖2" alt="wKioL1Qr7UCxB_xVAAC5Sn8-TVw411.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4B/6B/wKioL1Qr7VGDDAnWAAC6xwTd4C0262.jpg" title="圖3" alt="wKioL1Qr7VGDDAnWAAC6xwTd4C0262.jpg" />
編譯安裝php-5.4.26
# tar xf php-5.4.26.tar.bz2# cd php-5.4.26# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts# make && make install
為php提供設定檔
# cp php.ini-production /etc/php.ini
編輯httpd設定檔,以支援php
# vim /etc/httpd24/httpd.conf
在AddType application/x-gzip .gz .tgz行下面添加下面兩行:AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps將DirectoryIndex index.html這一行改為:DirectoryIndex index.php index.html
給主機2和主機3添加測試頁
主機2
# vim /usr/local/apache/htdocs/index.php <h1>Hello,WEB1</h1> <?php phpinfo(); ?>
主機3
# vim /usr/local/apache/htdocs/index.php <h1>Hello,WEB2</h1> <?php phpinfo(); ?>
重啟主機2和主機3的httpd24服務
# service httpd24 restart
在瀏覽器中鍵入10.170.2.80,重新整理可以得到不同的結果
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/69/wKiom1Qr8n-DnJGHAAEFGjzRLDk932.jpg" title="圖4" alt="wKiom1Qr8n-DnJGHAAEFGjzRLDk932.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4B/69/wKiom1Qr8ryxE35bAAENfyx4ht4669.jpg" title="圖5" alt="wKiom1Qr8ryxE35bAAENfyx4ht4669.jpg" />
配置主機4
編譯安裝mariadb-5.5.36
# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local# cd /usr/local/# ln -sv mariadb-5.5.36-linux-x86_64/ mysql# mkdir -pv /mysql/data# groupadd -r mysql# useradd -g mysql -s /sbin/nologin -M -d /mysql/data -r mysql# chown -R mysql:mysql /mysql# chown -R mysql:mysql /mysql/data
為資料庫提供設定檔:
# cd mysql# mkdir /etc/mysql# chown -R root.mysql ./*# cp support-files/my-large.cnf /etc/mysql/my.cnf修改檔案/etc/mysql/my.cnf檔案內容,在thread_concurrency = 8行下添加一行:datadir = /mysql/data
為資料庫提供SysV啟動指令碼,並設定為開機啟動:
# cp support-files/mysql.server /etc/init.d/mysqld# chkconfig --add mysqld# chkconfig mysqld on
初始化資料庫並啟動資料庫:
# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh# source /etc/profile.d/mysql.sh # echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf# ldconfig# ln -sv /usr/local/mysql/include/ /usr/include/mysql# scripts/mysql_install_db --user=mysql --datadir=/mysql/data/# /etc/init.d/mysqld start
建立資料庫並授權:
MariaDB [(none)]> CREATE DATABASE discuz;MariaDB [(none)]> GRANT ALL ON discuz.* TO [email protected]‘192.168.3.%‘ IDENTIFIED BY ‘dscpass‘;MariaDB [(none)]> FLUSH PRIVILEGES;
啟動nfs共用服務
# useradd -u 600 discuz# vim /etc/exports /var/www/html 192.168.3.0/24(rw,no_root_squash)# /etc/init.d/rpcbind start# /etc/init.d/nfs start# /etc/init.d/nfslock start
將Discuz_X2.5_SC_GBK解壓到/var/www/html下並修改許可權
# unzip Discuz_X2.5_SC_GBK.zip .# cd upload# chmod -R go+w config# chmod -R go+w data# chmod -R go+w uc_*
搭建測試環境並進行測試
在主機2和主機3上進行相關配置
# useradd -u 600 discuz# mount -f nfs 192.168.3.36:/var/www/html /usr/local/apache/htdocs
在瀏覽器中鍵入10.170.2.80/upload,可以得到安裝discuz論壇頁面
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4B/69/wKiom1Qr9grC4-qbAAP1ZIPKE04192.jpg" title="圖6" alt="wKiom1Qr9grC4-qbAAP1ZIPKE04192.jpg" />
點擊“我同意”-->點擊“下一步”-->點擊“下一步”-->根據資料庫授權資訊,填寫“安裝資料庫內容”,然後點擊“下一步”-->資料庫安裝成功後,在顯示的頁面上點擊“暫不開通”即可得到論壇頁面
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4B/69/wKiom1Qr97ODB0QjAAGWxZRhRn4356.jpg" title="圖7" alt="wKiom1Qr97ODB0QjAAGWxZRhRn4356.jpg" />
利用lvs-nat實現兩台web伺服器負載平衡的簡單案例