#http高可用+負載均衡pacemaker+haproxy
#http+corosync+pacemaker+pcs+haproxy
#ssh免密驗證,必須設置http://www.cnblogs.com/elvi/p/7736521.html
########################
#全部控制節點
#安裝Pacemake Corosync
yum install corosync pacemaker pcs fence-agents resource-agents -y
yum install haproxy httpd -y
#啓動pcsd
systemctl enable pcsd.service
systemctl start pcsd.service
#修改羣集用戶hacluster密碼
echo centos | passwd --stdin hacluster
#http設置
cp /etc/httpd/conf/httpd.conf{,.bak}
sed -i 's#^Listen 80#Listen 8080#' /etc/httpd/conf/httpd.conf
echo "ServerName `hostname`:8080">>/etc/httpd/conf/httpd.conf
tail -1 /etc/httpd/conf/httpd.conf
systemctl start httpd.service
netstat -antp|grep httpd
echo `hostname`>/var/www/html/index.html #測試主頁
########################
#其中一節點執行 controller1
#建立、啓動my_cluster集羣
pcs cluster auth -u hacluster -p centos controller1 controller2 controller3
pcs cluster setup --start --name my_cluster controller1 controller2 controller3
pcs cluster enable --all #集羣自啓動
#pcs cluster start --all # 啓動集羣
pcs cluster status #集羣狀態
#檢驗
corosync-cfgtool -s #驗證corosync
corosync-cmapctl| grep members #查當作員
pcs status corosync #查看corosync狀態
crm_verify -L -V #檢查配置
pcs property set stonith-enabled=false #禁用STONITH
pcs property set no-quorum-policy=ignore #無仲裁時,選擇忽略
#建立 VIP 資源
pcs resource create vip ocf:heartbeat:IPaddr2 ip=10.2.1.10 cidr_netmask=24 op monitor interval=28s
# pcs resource rsc defaults resource-stickiness=100
# ### 可選參考
# pcs resource create haproxy systemd:haproxy op monitor interval=5s
# pcs constraint colocation add vip haproxy INFINITY #HAProxy和VIP必須在同一節點
# pcs constraint order vip then haproxy #先啓動VIP,再啓動HAProxy
#添加到羣集
#pcs resource create WEB apache configfile="/etc/httpd/conf/httpd.conf" statusurl="http://127.0.0.1/server-status"
# #建立group做爲一個總體
# pcs resource group add MyGroup vip
# pcs resource group add MyGroup WEB
############haproxy配置,全部節點執行
#容許沒VIP時啓動
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
sysctl -p
#haproxy日誌
echo '
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%rawmsg% \n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
'>/etc/rsyslog.d/haproxy.conf
systemctl status rsyslog.service
systemctl restart rsyslog.service
cp /etc/haproxy/haproxy.cfg{,.bak}
#默認配置
echo '
###########全局配置#########
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
daemon
nbproc 1 #進程數量
maxconn 4096 #最大鏈接數
user haproxy #運行用戶
group haproxy #運行組
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
########默認配置############
defaults
log global
mode http #默認模式{ tcp|http|health }
option httplog #日誌類別,採用httplog
option dontlognull #不記錄健康檢查日誌信息
retries 2 #2次鏈接失敗不可用
option forwardfor #後端服務得到真實ip
option httpclose #請求完畢後主動關閉http通道
option abortonclose #服務器負載很高,自動結束比較久的連接
maxconn 4096 #最大鏈接數
timeout connect 5m #鏈接超時
timeout client 1m #客戶端超時
timeout server 31m #服務器超時
timeout check 10s #心跳檢測超時
balance roundrobin #負載均衡方式,輪詢
########統計頁面配置########
listen stats
bind 0.0.0.0:1080
mode http
option httplog
log 127.0.0.1 local0 err
maxconn 10 #最大鏈接數
stats refresh 30s
stats uri /admin #狀態頁面 http//ip:1080/admin訪問
stats realm Haproxy\ Statistics
stats auth admin:admin #用戶和密碼:admin
stats hide-version #隱藏版本信息
stats admin if TRUE #設置手工啓動/禁用
'>/etc/haproxy/haproxy.cfg
#haproxy web代理配置
echo '
########WEB############
listen dashboard_cluster
bind controller:80
balance roundrobin
option tcpka
option httpchk
option tcplog
server controller1 controller1:8080 check port 8080 inter 2000 rise 2 fall 5
server controller2 controller2:8080 check port 8080 inter 2000 rise 2 fall 5
server controller3 controller3:8080 check port 8080 inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg
#重啓haproxy
systemctl restart haproxy.service
systemctl status haproxy.service
#systemctl disable haproxy.service
#登陸狀態頁面 http//ip:1080/admin 查看