1. 程式人生 > >http高可用+負載均衡 corosync + pacemaker + pcs

http高可用+負載均衡 corosync + pacemaker + pcs

http高可用+負載均衡 corosync + pacemaker + pcs
openstack pike 部署 目錄彙總 http://www.cnblogs.com/elvi/p/7613861.html

複製程式碼

#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  檢視