8-1 鏡像haproxy
docker run -it --privileged --name haproxy docker.io/centos:latest
8-2
wget http://www.haproxy.org/download/2.0/src/haproxy-2.0.4.tar.gz
8-3
[root@localhost ~]# yum install -y gcc
[root@localhost ~]# tar -xzvf haproxy-2.0.4.tar.gz -C /usr/local
8-4 uname -r #查看系統內核版本號
#數說明:
#TARGET=linux3100
#使用uname -r查看內核,如:2.6.18-371.el5,此時該參數就爲linux26
#kernel 大於2.6.28的用:TARGET=linux2628
#CPU=x86_64 #使用uname -r查看系統信息,如x86_64 x86_64 x86_64 GNU/Linux,此時該參數就爲x86_64
8-5 編譯/安裝
cd haproxy-2.0.4
#PREFIX=/usr/local/haprpxy #/usr/local/haprpxy爲haprpxy安裝路徑
[root@localhost ~]# yum install make
#編譯
[root@A local]# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy
#安裝
[root@A local]# make install PREFIX=/usr/local/haproxy
8-6設置HAProxy
mkdir -p /usr/local/haproxy/conf #建立配置文件目錄
mkdir -p /etc/haproxy #建立配置文件目錄
touch /usr/local/haproxy/conf/haproxy.cfg #建立配置文件
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件軟鏈接
cp -r /usr/local/haproxy-2.0.4/examples/errorfiles /usr/local/haproxy/errorfiles #拷貝錯誤頁面
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加軟鏈接
mkdir -p /usr/local/haproxy/log #建立日誌文件目錄
touch /usr/local/haproxy/log/haproxy.log #建立日誌文件
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加軟鏈接
cp /usr/local/haproxy-2.0.4/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷貝開機啓動文件
chmod +x /etc/rc.d/init.d/haproxy #添加腳本執行權限
chkconfig haproxy on #設置開機啓動
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加軟鏈接
8-7配置haproxy.cfg參數
cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg-bak #備份
vi /usr/local/haproxy/conf/haproxy.cfg #編輯,修改
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/local/haproxy
#pidfile /usr/data/haproxy/haproxy.pid
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode tcp
option abortonclose
option redispatch
retries 3
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000
listen proxy_status
bind :58066
mode tcp
balance roundrobin
server mycat_masert1 172.17.0.14:8066 check inter 10s
server mycat_masert2 172.17.0.15:8066 check inter 10s
server mycat_masert3 172.17.0.16:8066 check inter 10s
#server mycat_2 192.168.1.60:8066 check inter 10s
frontend admin_stats
bind :8888
mode http
stats enable
option httplog
maxconn 10
stats refresh 30s
stats uri /admin
stats auth admin:admin #登陸用戶
stats hide-version
stats admin if TRUE
8-8haproxy 啓動
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
ps -ef |grep haproxy |grep -v grepnode
8-9 haproxy的監控頁面
curl http://127.0.0.1:8888/admin //說明:8888 haproxy配置監聽端口,stats haproxy配置監聽名稱
8-10 生成鏡像
docker commit haproxy haproxy:1.0
8-11 mycat-haproxy容器mysql
docker run -it --privileged --name mycat-haproxy -p 18888:8888 -P 58066:58066 haproxy:1.0
#ps -ef |grep haproxy |grep -v grep
#curl http://127.0.0.1:8888/admin
#service haproxy start
8-12 自動啓動
[root@node3 ~]# chmod +x /etc/rc.d/init.d/haproxy
[root@node3 ~]# chkconfig --add haproxy
cat /etc/rc.d/init.d/haproxy
8-13 主機、外網訪問
外網:http://192.168.157.128:18888/admin
#mycat-haproxy2 172.17.0.20
主機: mysql -uroot -proot -h 172.17.0.20 -P 58066linux
參考筆記 nginx