簡單負載配置

簡單的負載均衡站點配置

資料從網上收集的,但是有實測。給新手用的,老手可以參考並完善一下。

一、 實現目標:負載均衡,數據熱備

二、 網絡top圖

clip_image002

Server1 192.168.94.236

Server2 192.168.94.237

Server3 192.168.94.238

三、 Server2 Server3基本安裝 (Server1 不需要安裝LNMP包)

1、 操作系統安裝 CentOS 5.5 32位操作系統。

2、 磁盤分區 /boot 100M /swat 4G /home 20G /usr/local 60G /web

3、 安裝後注意事項

A、建立一個普通權限的用戶
因爲root用戶對系統具有全權的操作權限,爲了避免一些失誤的操作,在一般情況下,以一般用戶登錄系統,必要的時候需要root操作權限時,再通過“su -”命令來登錄爲root用戶進行操作。

useradd anniuser
passwd anniuser
usermod -G wheel anniuser

修改pam配置,使非wheel組用戶不能使用su命令登錄爲root:

vi /etc/pam.d/su
找到

#auth required /lib/security/$ISA/pam_wheel.so use_uid
將行首的 # 去掉。

然後

vi /etc/login.defs
在文件末尾加上

SU_WHEEL_ONLY yes

B、root郵件的轉送

Vi /etc/aliases

C、停止不必要的服務

[root@ ~]# /etc/rc.d/init.d/cups stop ← 停止打印服務
Stopping cups: [ OK ] ← 停止服務成功,出現“OK”
[root@ ~]# chkconfig cups off ← 禁止打印服務自動啓動
[root@ ~]# chkconfig --list cups ← 確認打印服務自啓動設置狀態
cups 0:off 1:off 2:off 3:off 4:off 5:off 6:off ← 0-6都爲off的狀態就OK(當前打印服務自啓動被禁止中)

除了以下服務以外,其他服務默認的都可以採用剛纔的方法關閉,根據需要自己選擇:

atd
crond
irqbalance
lvm2-monitor
microcode_ctl
network
sendmail
sshd
syslog

D、關閉SELinux

vi /etc/selinux/config
將其中的

SELINUX=enforcing
改爲

SELINUX=disabled

E、安裝sudo工具

yum install sudo -y
安裝好了以後,修改sudo的配置

vi /etc/sudousers

# %wheel ALL=(ALL) NOPASSWORD:ALL
去掉前面的 # , 然後保存文件。
這樣修改了以後,只有所有屬於wheel組的用戶能執行sudo命令,並且執行sudo命令時只需要輸入自己的密碼即可。

F、修改SSH配置

vi /etc/ssh/sshd_config
Port 22 #修改默認端口

#ServerKeyBits 768改爲ServerKeyBits 1024#增加ServerKey的強度找到
#PermitRootLogin Yes改爲PermitRootLogin no #不允許root用戶直接登錄

#PermitEmptyPasswords no #禁止空密碼登錄 去掉前面的 #

全部修改完了以後,重啓服務器

G、定義yum非官方庫(使用網易163的yum庫更新)

先關閉fastestmirror

“vi /etc/yum/pluginconf.d/fastestmirror.conf”     and set “enable=0″

cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS-Base-163.repo
yum makecache  //生成緩存
yum –y update

H、定時校對時間

crontab –e

00 00 * * * ntpdate 210.72.154.44 #每日24點校準時間,頻率自己設置

4、 LNMP安裝

安裝步驟:

1、下載LNMP一鍵安裝包:

wget -c http://soft.vpser.net/lnmp/lnmp0.5.tar.gz,如果使用完整版

wget -c http://soft.vpser.net/lnmp/lnmp0.5-full.tar.gz,執行上述命令後LNMP一鍵安裝包就會被下載到服務器上。
2、解壓LNMP一鍵安裝包:

· 執行tar zxvf lnmp0.5.tar.gz 或者tar zxvf lnmp0.5-full.tar.gz 就會將LNMP一鍵安裝包解壓縮。
3、安裝步驟

· 下載版執行命令 cd lnmp0.5/ ,完整版執行命令:cd lnmp0.5-full/

· 然後再執行./centos.sh ,輸入要綁定的域名,回車,再輸入要設置的MySQL root的密碼,再次回車確認。程序會自動安裝編譯Nginx、PHP、MySQL、phpMyAdmin、Zend這幾個軟件。

phpinfo : http://前面輸入的域名或IP/phpinfo.php
phpMyAdmin : http://前面輸入的域名或IP/phpmyadmin/
探針 : http://前面輸入的域名或IP/p.php
MySQL root密碼:如果不輸入直接回車爲root,否則爲你輸入的密碼。
LNMP相關目錄:
mysql : /usr/local/mysql
php : /usr/local/php
nginx : /usr/local/nginx
網站目錄: /home/wwwroot

基本安裝至此完成

五、配置

文件實時同步配置說明

文件實時同步軟件:sersync

實現過程:

在server3上配置rsync服務器(serync客戶端) /etc/rsyncd.conf

uid = root

use chroot = no

max connections = 20

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[web_tongbu] #模塊名稱

path = /home/wwwroot/anni/ #同步目錄

ignore errors

read only = no #

hosts allow = 192.168.94.237/24

hosts deny = 0.0.0.0/32

auth users = root

secrets file = /etc/rsyncd.pas

#exclude = xxx/ yyy/ #把xxx和yyy目錄排除在外

[nginx_tongbu]

path = /usr/local/nginx/conf/

ignore errors

read only = no

hosts allow = 192.168.94.237/24

hosts deny = 0.0.0.0/32

auth users = root

secrets file = /etc/rsyncd.pas

#exclude = xxx/ yyy/ #把xxx和yyy目錄排除在外

Pass文件配置 /etc/rsyncd.pas (名字隨便) 格式 : 用戶名:密碼 比如 root:12345

並將rsyncd.pas權限設置爲600

chmod 600 /etc/rsyncd.pas

開啓rsync

rsync –daemon

加入開機啓動

echo "rsync --daemon" >> /etc/rc.d/rc.local

配置sersync服務端(rsync客戶端)

rsync pass 文件配置 /etc/rsyncd.pas 格式:密碼 比如: 12345 (這裏的密碼對應rsync服務器配置的用戶密碼)並將rsyncd.pas權限設置爲600

chmod 600 /etc/rsyncd.pas

sersync 安裝配置(安裝於server2)

新版本下載地址http://code.google.com/p/sersync/downloads/list

解壓sersync後生成GNU-Linux-x86文件夾,以當前最新2.5版本爲例:

tar zxf sersync2.5_32bit_binary_stable_final.tar.gz

mv GNU-Linux-x86 sersync

cd sersync

vi confxml.xml

修改如下兩部分:

<sersync>

<localpath watch="/home/tongbu">

<remote ip="192.168.94.238" name="web_tongbu"/>

</localpath>

表明要將主服務器上本地的/home/tongbu 路徑下的文件,同步到遠程服務器 192.168.94.238 上的web_tongbu 模塊。

<rsync>

<commonParams params="-artuz"/>

<auth start="true" users="root" passwordfile="/etc/rsyncd.pas"/>

<userDefinedPort start="false" port="874"/><!-- port=874 -->

<timeout start="false" time="100"/><!-- timeout=100 -->

<ssh start="false"/>

表示使用rsync密碼認證方式

啓動sersync

. /sersync2 -r -d /usr/local/sersync/confxml.xml

加入開機自啓動

echo "/usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml" &gt;&gt; /etc/rc.d/rc.local

這裏參數-o表示指定配置文件爲/usr/local/sersync/confxml.xml

實時同步配置完成

Mysql 主主複製配置

主主配置過程

1、創建同步用戶:

Server 2:

grant replication slave,file on *.* to 'replication'@'192.168.94.238' identified by '123456';

flush privileges;

Server 3:

grant replication slave,file on *.* to 'replication'@'192.168.94.237' identified by '123456';

flush privileges;

2、修改mysql配置文件 /etc/my.cnf

Server 2:

log-bin=mysql-bin

server-id = 1

binlog-do-db=test

binlog-ignore-db=mysql

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

Server 3:

log-bin=mysql-bin

server-id = 2

binlog-do-db=test

binlog-ignore-db=mysql

replicate-do-db=test

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=2

3、重啓mysql服務後,進入mysql命令行,執行操作如下:

Server A:

mysql&gt; flush tables with read lock;

mysql&gt; show master status\G

************************** 1. row ***************************

File: mysql-bin.000028

Position: 866

Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

mysql&gt; unlock tables;

mysql&gt; stop slave;

mysql&gt; change master to master_host='192.168.94.238',master_user='replication',master_password='123456',master_log_file='mysql-bin.000014', master_log_pos=704;

mysql&gt; start slave;

Server B:

mysql&gt; flush tables with read lock;

mysql&gt; show master status\G

*************************** 1. row ***************************

File: mysql-bin.000014

Position: 704

Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

mysql&gt; unlock tables;

mysql&gt; stop slave;

mysql&gt;change master to master_host='192.168.94.237',master_user='replication',master_password='123456',master_log_file='mysql-bin.000028', master_log_pos=866;

mysql&gt; start slave;

4、查看各服務器的狀態:

mysql&gt; show slave status\G;

出現:Slave_IO_Running: Yes

Slave_SQL_Running: Yes

則狀態正常,直接測試數據能否同步就OK了!

mysql-mmm高可用配置(及mysql自動切換)

MMM簡介:

MMM即Master-Master Replication Manager for MySQL(mysql主主複製管理器)關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫入),這個套件也能對居於標準的主從配置的任意數量的從服務器進行讀負載均衡,所以你可以用它來在一組居於複製的服務器啓動虛擬ip,除此之外,它還有實現數據備份、節點之間重新同步功能的腳本。

MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。

MMM項目來自 Google:http://code.google.com/p/mysql-master-master

官方網站爲:http://mysql-mmm.org

Mmm主要功能由下面三個腳本提供

mmm_mond 負責所有的監控工作的監控守護進程,決定節點的移除等等

mmm_agentd 運行在mysql服務器上的代理守護進程,通過簡單遠程服務集提供給監控節點

mmm_control 通過命令行管理mmm_mond進程

mysql-mmm架構的搭建

1、 安裝環境:

系統環境:CentOS 5.5(32bit)

Server1 ip: 192.168.94.236 virtual write ip: 192.168.1.113

Server2 ip: 192.168.94.237 virtual read ip:192.168.1.111

Server3 ip: 192.168.94.238 virtual read ip:192.168.1.112

2、 mysql-mmm架構配置簡介:

在server2、server3上安裝mysql,並配置爲master-master架構(即上面的主主複製)

在server1、server2,server3上安裝mmm,並配置:mmm_common.conf、mmm_agent.conf以及mmm_mon.conf文件

3、 Mysql-mmm實戰

前提:server2和server3上已經配置好mysql主主同步

安裝mysql-mmm

CentOS軟件倉庫默認是不含這些軟件的,必須要有epel這個包的支持。故我們必須先安裝epel:

wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

rpm -Uvh epel-release-5-4.noarch.rpm

yum -y install mysql-mmm*

配置mmm代理和監控賬號的權限

在server2和server3上分別執行:

GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.94.%' IDENTIFIED BY 'monitor_password';

GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.94.%' IDENTIFIED BY 'agent_password';

flush privileges;

配置mysql-mmm

所有的配置選項都集合在了一個叫/etc/mysql-mmm/mmm_common.conf的單獨文件中,系統中所有主機的該文件內容都是一樣的, 配置完後不要忘記了拷貝這個文件到所有的主機(包括監控主機)!,內容如下:

active_master_role writer

<host default>

cluster_interface eth0

pid_path /var/run/mysql-mmm/mmm_agentd.pid

bin_path /usr/libexec/mysql-mmm/

#同步的帳號(這些要和前面設置的保持一致!)

replication_user replication

replication_password 123456 #同步的密碼

agent_user mmm_agent #mmm-agent用戶名

agent_password agent_password #mmm-agent用戶密碼

</host>

<host db1>

ip 192.168.94.237 #db1的ip

mode master

peer db2

</host>

<host db2>

ip 192.168.94.238 #db2的ip

mode master

peer db1

</host>

<role writer>

hosts db1, db2

ips 192.168.1.113 #設置寫入的虛擬IP

mode exclusive

</role>

<role reader>

hosts db1, db2

ips 192.168.1.111, 192.168.1.112 #設置讀取的虛擬IP

mode balanced

</role>

在數據庫主機上我們需要編輯/etc/mysql-mmm/mmm_agent.conf文件,根據其他主機的不同更改db1的值(db2就將db1更改成db2):

include mmm_common.conf

this db1

在監控主機server 1上我們需要編輯/etc/mysql-mmm/mmm_mon.conf文件:

include mmm_common.conf

<monitor>

ip 127.0.0.1

pid_path /var/run/mysql-mmm/mmm_mond.pid

bin_path /usr/libexec/mysql-mmm

status_path /var/lib/mysql-mmm/mmm_mond.status

ping_ips 192.168.94.237,192.168.94.238 #監控服務器ip

auto_set_online 60

# The kill_host_bin does not exist by default, though the monitor will

# throw a warning about it missing. See the section 5.10 "Kill Host

# Functionality" in the PDF documentation.

#

# kill_host_bin /usr/libexec/mysql-mmm/monitor/kill_host

#

</monitor>

<host default>

monitor_user mmm_monitor #mmm_monitor用戶名

monitor_password monitor_password #mmm_monitor密碼

</host>

debug 0

啓動MMM

啓動代理:

(在數據庫服務器上server2、3)編輯/etc/default/mysql-mmm-agent來開啓:

ENABLED=1

然後啓動它:

/etc/init.d/mysql-mmm-agent start

啓動監控(在監控機server 1上):

/etc/init.d/mysql-mmm-monitor start

利用mmm_control監控mysql服務器狀態:

[root@server1 mysql-mmm]# mmm_control show

db1(192.168.94.237) master/ONLINE. Roles: reader(192.168.1.112), writer(192.168.1.113)

db2(192.168.94.238) master/ONLINE. Roles: reader(192.168.1.111)

測試看兩個mysql服務器能否實現故障自動切換

停掉作爲寫的db1上的mysql,查看寫的服務器會不會自動轉移到db2上去

停掉幾秒鐘後用mmm_control show查看:

[root@server1 mysql-mmm]# mmm_control show

db1(192.168.94.237) master/HARD_OFFLINE. Roles:

db2(192.168.94.238) master/ONLINE. Roles: reader(192.168.1.111), reader(192.168.1.112), writer(192.168.1.113)

我們可以看到已經把db2當作主寫服務器

再來看看db1恢復後會是什麼情況:

[root@server1 mysql-mmm]# mmm_control show

db1(192.168.94.237) master/ONLINE. Roles: reader(192.168.1.111)

db2(192.168.94.238) master/ONLINE. Roles: reader(192.168.1.112), writer(192.168.1.113)

我們可以看到當db1恢復後就充當slave的角色了!只有當db2掛了以後db1又會擔當起主服務器的寫入功能

HAProxy 安裝部署(Server 1上)

1、 haproxy的編譯安裝

獲取haproxy的源代碼

官方地址:http://haproxy.1wt.eu/

目前最新的版本:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.9.tar.gz

命令:

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.9.tar.gz考慮到版本的更新,具體的安裝屬性可能會稍有改變,建議在安裝前可以大致瀏覽下haproxy目錄下的README和Makefile兩個文件。

編譯haproxy

進入解壓後的haproxy的目錄,用下列命令進行編譯:

make TARGET=linux26 CPU=generic

安裝

make install PREFIX=/usr/local/haproxy

2、配置文件 /usr/local/haproxy/haproxy.cfg

global

maxconn 5120

chroot /usr/local/haproxy

uid 99

gid 99

daemon

quiet

nbproc 2

pidfile /usr/local/haproxy/haproxy.pid

defaults

log global

mode http

option httplog

option dontlognull

log 127.0.0.1 local3

retries 3

option redispatch

maxconn 2000

contimeout 5000

clitimeout 50000

srvtimeout 50000

listen webinfo :1080

mode http

balance roundrobin

option httpclose

option forwardfor

server phpinfo1 192.168.94.237:80 check weight 1 minconn 1 maxconn 3 check inter 40000

server phpinfo2 192.168.94.238:80 check weight 1 minconn 1 maxconn 3 check inter 40000

listen webmb :80

mode http

balance roundrobin

option httpclose

option forwardfor

cookie DYNSRV insert indirect nocache

server web237 192.168.94.237:80 check cookie first inter 1000

server web238 192.168.94.238:80 check cookie first inter 1000

listen stats :888

mode http

transparent

stats uri / haproxy-stats

stats realm Haproxy \ statistic

stats auth davexi:111111 #狀態頁面用戶名密碼

監測頁面 192.168.94.236:888

啓動haproxy

/usr/local/haproxy/sbin/haproxy –f /usr/local/haproxy/haproxy.cfg

加入開機啓動

echo “/usr/local/haproxy/sbin/haproxy –f /usr/local/haproxy/haproxy.cfg” &gt;&gt; /etc/rc.d/rc.local

到這裏就基本完工了,自己查看一下成果吧,你的網站訪問地址爲 192.168.94.236

各服務器端口開放說明

以下端口Server 1 與Server 2 ftp端口通過防火牆映射至外網,並ssh 與ftp端口僅公司內部訪問

Server 1

9527 ssh

80 web

Server 2

22 ssh

32 ftp

80 web

3306 mysql

874

Server 3

22 ssh

80 web

3306 mysql

874 rsync

請珍惜一下俺的小小的勞動成果,如有完善請直接上傳,並通知我刪除原文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章