1.基礎(chǔ)安裝
2.本機(jī)IP:192.168.56.15
[root@bogon examples]# cat /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /usr/local/haproxy uid 99 gid 99 daemon nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid log 127.0.0.1 local0 info defaults option http-keep-alive maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:123456 listen web_port bind 0.0.0.0:80 mode tcp log global [root@bogon examples]# [root@bogon examples]# /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ OK ] [root@bogon examples]#http://192.168.56.15:9999/haproxy-status
3.對(duì)后端代理
查看負(fù)載狀況
################################################
haproxy調(diào)度算法
HAProxy的算法有如下8種: roundrobin,表示簡(jiǎn)單的輪詢(xún), static-rr,表示根據(jù)權(quán)重,建議關(guān)注; leastconn,表示最少連接者先處理,建議關(guān)注; source,表示根據(jù)請(qǐng)求源IP,建議關(guān)注; uri,表示根據(jù)請(qǐng)求的URI; url_param,表示根據(jù)請(qǐng)求的URl參數(shù)'balanceurl_param' requires an URL parameter name hdr(name),表示根據(jù)HTTP請(qǐng)求頭來(lái)鎖定每一次HTTP請(qǐng)求; rdp-cookie(name),表示根據(jù)據(jù)cookie(name)來(lái)鎖定并哈希每一次TCP請(qǐng)求。haproxy的摘除節(jié)點(diǎn)的 yum安裝的
global maxconn 100000 chroot /var/lib/haproxy user haproxy group haproxy daemon nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level admin log 127.0.0.1 local10 info defaults option http-keep-alive maxconn 100000 mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen stats mode http bind 0.0.0.0:8888 stats enable stats uri /haproxy-status stats auth haproxy:saltstack frontend frontend_www_example_com bind 192.168.56.21:80 mode http option httplog log global default_backend backend_www_example_com backend backend_www_example_com option forwardfor header X-REAL-IP option httpchk HEAD / HTTP/1.0 balance roundrobin server web-node1 192.168.56.11:8080 check inter 2000 rise 30 fall 15 server web-node2 192.168.56.12:8080 check inter 2000 rise 30 fall 15 [root@web-node1 ~]#這樣就能吧haproxy的節(jié)點(diǎn)刪除或者增加
echo "disable server backend_www_example_com/web-node1" | socat /var/lib/haproxy.sock stdio echo "enable server backend_www_example_com/web-node1" | socat /var/lib/haproxy.sock stdiohaproxy增加日志的功能
mkdir /var/log/haproxy chmod a+w /var/log/haproxy /etc/rsyslog.conf 打開(kāi)選項(xiàng): $ModLoad imudp $UDPServerRun 514 # Save haproxy log local0.* /var/log/haproxy/haproxy.log vim /etc/sysconfig/rsyslog [root@web-node1 nginx]# cat /etc/sysconfig/rsyslog # Options for rsyslogd # Syslogd options are deprecated since rsyslog v3. # If you want to use them, switch to compatibility mode 2 by "-c 2" See rsyslogd(8) for more details SYSLOGD_OPTIONS="-r -m 0 -c 2" #修改此處 [root@web-node1 nginx]#haproxy日志里面的localhost 和配置文件有關(guān) 這個(gè)IP可以修改
log 127.0.0.1 local10 info3.集群配置
192.168.56.11 nginx 反向代理 keepalived haproxy
192.168.56.12 nginx 反向代理 keepalived haproxy
192.168.56.13 nginx 模擬tomcat
192.168.56.14 nginx 模擬tomcat
keepalived的VIP為192.168.56.21
問(wèn)題:haproxy 不會(huì)綁定不存在的VIP 所以啟動(dòng)不起來(lái) so,這樣可以綁定不存在的VIP
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind
vi /etc/sysctl.conf sysctl -p
net.ipv4.ip_nonlocal_bind = 1
開(kāi)啟允許綁定非本機(jī)的IP
查看keepalived的配置文件192.168.56.11(主)
[root@web-node1 nginx]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { saltstack@example.com } notification_email_from keepalived@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id haproxy_ha } vrrp_instance haproxy_ha { state MASTER interface eth0 virtual_router_id 36 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.21 } } [root@web-node1 nginx]#查看keepalived的配置文件192.168.56.12(備)
[root@web-node2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { saltstack@example.com } notification_email_from keepalived@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id haproxy_ha } vrrp_instance haproxy_ha { state BACKUP interface eth0 virtual_router_id 36 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.21 } } [root@web-node2 ~]#查看192.168.56.11 haproxy的配置文件
[root@web-node2 ~]# cat /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /var/lib/haproxy user haproxy group haproxy daemon nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level admin log 127.0.0.1 local3 info defaults option http-keep-alive maxconn 100000 mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen stats mode http bind 0.0.0.0:8888 stats enable stats uri /haproxy-status stats auth haproxy:saltstack frontend frontend_www_example_com bind 192.168.56.21:80 mode tcp option httplog log global default_backend backend_www_example_com backend backend_www_example_com option forwardfor header X-REAL-IP option httpchk HEAD / HTTP/1.0 balance roundrobin server web-node1 192.168.56.11:8080 check inter 2000 rise 30 fall 15 server web-node2 192.168.56.12:8080 check inter 2000 rise 30 fall 15 [root@web-node2 ~]#查看192.168.56.12 haproxy的配置文件
[root@web-node1 nginx]# cat /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /var/lib/haproxy user haproxy group haproxy daemon nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level admin #log 127.0.0.1 local3 info log 192.168.56.21 local0 debug defaults option http-keep-alive maxconn 100000 mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms listen stats mode http bind 0.0.0.0:8888 stats enable stats uri /haproxy-status stats auth haproxy:saltstack frontend frontend_www_example_com bind 192.168.56.21:80 mode http option httplog log global default_backend backend_www_example_com backend backend_www_example_com option forwardfor header X-REAL-IP option httpchk HEAD / HTTP/1.0 balance roundrobin server web-node1 192.168.56.11:8080 check inter 2000 rise 30 fall 15 server web-node2 192.168.56.12:8080 check inter 2000 rise 30 fall 15 [root@web-node1 nginx]#查看192.168.56.11的nginx反向代理的配置文件(最簡(jiǎn)單的配置 demo)
[root@web-node1 nginx]# cat /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 192.168.56.13:8080 weight=1; server 192.168.56.14:8080 weight=1; } server { listen 192.168.56.11:8080; server_name blog.liuhaixiao.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } [root@web-node1 nginx]#查看192.168.56.12的nginx反向代理的配置文件(最簡(jiǎn)單的配置 demo)
[root@web-node2 ~]# cat /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 192.168.56.13:8080 weight=1; server 192.168.56.14:8080 weight=1; } server { listen 192.168.56.12:8080; server_name blog.liuhaixiao.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } [root@web-node2 ~]#查看192.168.56.13的nginx web容器 [root@web-node3 html]# cat /usr/share/nginx/html/index.html node-2 查看192.168.56.14的nginx web容器 [root@web-node4 html]# cat /usr/share/nginx/html/index.html node-1檢查VIP zai 192.168.56.11上面
[root@web-node1 nginx]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:10:66:98 brd ff:ff:ff:ff:ff:ff inet 192.168.56.11/24 brd 192.168.56.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.56.21/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe10:6698/64 scope link valid_lft forever preferred_lft forever [root@web-node1 nginx]#現(xiàn)在把本地的電腦blog.liuhaixiao.com 解析到192.168.56.21上面去
####################################################
新添加:192.168.56.15 增加一個(gè)nginx 域名
haproxy 監(jiān)聽(tīng)80端口 nginx 監(jiān)聽(tīng)的端口千萬(wàn)不能和后端tomcat一樣 否則回包回不來(lái)
[root@web-node1 ~]# cat /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 192.168.56.13:8080 weight=1; server 192.168.56.14:8080 weight=1; } upstream server_tt { server 192.168.56.15:8080 weight=1; } server { listen 81; server_name blog.liuhaixiao.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 81; server_name blog.sanlang.com; location / { proxy_pass http://server_tt; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } [root@web-node1 ~]#3是 另外一臺(tái)服務(wù)器的192.168.56.15 tomcat/nginx 的頁(yè)面
haproxy具體參數(shù)請(qǐng)參考:
http://www.cnblogs.com/zhang789/p/6057402.html
haproxy:多域名參考
基于域名負(fù)載均衡的Haproxy配置
http://blog.csdn.net/youyudehexie/article/details/7606504
#####################
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
標(biāo)題名稱(chēng):haproxy的安裝-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://chinadenli.net/article46/dgghhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、標(biāo)簽優(yōu)化、微信小程序、商城網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容