這裏使用haproxy構建一個動靜分離的集羣,而且靜態數據請求使用varnish作緩存。本打算作圖1.1的集羣,可是手頭機器不夠,只好委曲求全把動態服務器和靜態服務器和到一塊兒(主要是懶),數據庫和共享文件和到一塊兒如圖1.2javascript
圖1.1php
圖1.2css
file服務器的配置html
#安裝mysqld和nfs服務器 yum install mysql-server nfs-utils -y #提供網頁文件 mkdir /wordpress wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip unzip Discuz_X3.2_SC_UTF8.zip -d /wordpress chown -R apache:apache /wordpress cat > /etc/exports <<eof /wordpress 172.16.0.0/16(rw,no_root_squash) eof service nfs start #提供數據庫 service mysqld start mysql <<eof grant all privileges on wpdb.* to wpuser@'172.16.29.%'identified by "wppass"; eof
Rs服務器的配置java
yum install nfs-utils httpd php php-mysql -y cat >> /etc/fstab <<eof 172.16.29.1:/wordpress /wordpress nfs defaults 0 0 eof mkdir /wordpress mount -a #如下是提供httpd配置文件 vim /etc/httpd/conf/httpd.conf #把DocumentRoot "/var/www/html"改爲以下 DocumentRoot "/wordpress/upload" #把<Directory "/var/www/html">改成以下內容 <Directory "/wordpress"> #把DirectoryIndex index.html改爲以下內容 DirectoryIndex index.php index.html #啓動服務 systemctl start httpd.service
varnish服務器的配置node
yum install varnish -y vim /etc/varnish/varnish.params #把VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"改成以下內容,意思是使用512m的內存進行緩存數據 VARNISH_STORAGE="malloc,512m"
cat > /etc/varnish/default.vcl <<eof #提供以下配置文件 vcl 4.0; import directors; backend default { .host = "127.0.0.1"; .port = "8080"; } #定義後端服務器狀態檢測機制 probe check { .url = "/robots.txt"; .window = 5; .threshold = 3; .interval = 2s; .timeout = 1s; } #定義兩個服務器 backend server1 { .host = "172.16.29.10"; .port = "80"; .probe = check; } backend server2 { .host = "172.16.29.20"; .port = "80"; .probe = check; } #定義兩個服務器的調度算法 sub vcl_init { new static = directors.round_robin(); static.add_backend(server1); static.add_backend(server2); } #定義命中 sub vcl_recv { set req.backend_hint = static.backend(); } sub vcl_backend_response { } sub vcl_deliver { } eof
haproxy服務器通用配置配置mysql
兩個haproxy服務器的keepalived的配置文件有細微的差異,注意註釋信息
redis
yum install haproxy keepalived -y cat > /etc/haproxy/haproxy.cfg <<eof global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main *:80 #靜態數據訪問重定向到動態服務器,其餘的動態服務器 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend static if url_static default_backend app #狀態頁 listen admin_stats bind *:8080 stats enable stats uri /haproxy11 acl hastats src 172.16.0.0/16 block unless hastats #靜態服務器 backend static balance roundrobin server static 172.16.29.40:6081 check #動態服務器 backend app balance source server rs1 172.16.29.10:80 check server rs2 172.16.29.20:80 check eof systemctl restart haproxy.service
cat > /etc/keepalived/keepalived.conf <<eof #keepalived的配置文件 ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group 224.0.29.29 #這個腳本是爲了維護代理服務器Haproxy時使用的 vrrp_script chk_down { script "[[ -f /etc/haproxy/down ]] && exit 1 || exit 0" interval 1 weight -20 } #這個腳本是爲了檢測代理服務器Haproxy的狀態 vrrp_script chk_haproxy { script "killall -0 haproxy && exit 0 || exit 1" interval 1 weight -20 } } vrrp_instance VI_1 { #另外一臺主機的下一行改成MASTER state BACKUP interface eno16777736 virtual_router_id 51 #另外一臺主機的下一行改成100 priority 90 advert_int 1 authentication { auth_type PASS auth_pass oldking } virtual_ipaddress { 172.16.29.11/16 dev eno16777736 label eno16777736:0 } track_script { chk_down chk_haproxy } } eof
我這裏haproxy介紹的不夠詳細,參考這個博客http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html,或者官方文檔算法