文章目錄
前言
最近學習高可用,但手上只有一臺服務器,所以使用docker容器、keepalived和Nginx模擬場景。目標:從最開始的安裝docker到最後實現高可用。
開始
安裝docker
可參考我以前的博客:centos7中安裝docker
下載鏡像
docker pull centos:7
容器準備
生成容器並進入
docker run -itd --privileged=true --name=centos_kn centos:7 init
docker exec -it centos_kn bash
安裝工具
yum install vim net-tools rsyslog ipvsadm initscripts libnl3-devel ipset-devel -y
安裝keepalived
yum install -y gcc openssl-devel popt-devel
yum install keepalived -y
安裝nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
#啓動nginx
nginx
#設置開機啓動
systemctl enable nginx
打包容器
docker commit -a lwh -m 'centos with keepalived nginx' centos_kn centos_kn
進入容器
宿主機也需要安裝keepalived並運行
yum install -y gcc openssl-devel popt-devel
yum install keepalived -y
systemctl start keepalived
#運行完成後可以停止
systemctl stop keepalived
master容器
#進入master容器
docker run -itd --privileged=true --name=centos_master centos_kn init
docker exec -it centos_master bash
#設置keepalived配置
cd /etc/keepalived
#備份keepalived.conf
cp keepalived.conf keepalived.conf.bak
#更改配置,配置見下邊master配置
#啓動keepalived
systemctl start keepalived
#設置開啓啓動
systemctl enable keepalived
#設置nginx頁面顯示
echo master > /usr/share/nginx/html/index.html
master配置
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.100.100
}
}
virtual_server 172.17.100.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 0
protocol TCP
real_server 172.17.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.17.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
slave容器
#進入backup容器
docker run -itd --privileged=true --name=centos_slave centos_kn init
docker exec -it centos_slave bash
#設置keepalived配置
cd /etc/keepalived
#備份keepalived.conf
cp keepalived.conf keepalived.conf.bak
#更改配置,配置見下邊master配置
#啓動keepalived
systemctl start keepalived
#設置開啓啓動
systemctl enable keepalived
#設置nginx頁面顯示
echo slave> /usr/share/nginx/html/index.html
slave配置
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_SLAVE
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.100.100
}
}
virtual_server 172.17.100.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 0
protocol TCP
real_server 172.17.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.17.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
容器配置
兩個容器都需要執行,否則無法輪詢
#!/bin/bash
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig lo:0 172.17.100.100 broadcast 172.17.100.100 netmask 255.255.255.255 up
route add -host 172.17.100.100 dev lo:0
測試是否配置成功
進入宿主機
首先使用curl訪問虛擬ip:
curl 172.17.100.100
能看到返回的值說明配置成功了
使用公網訪問
#開啓防火牆
systemctl start firewalld.service
#查看防火牆狀態,顯示爲running
firewall-cmd --state
#開通80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#設置端口轉發
firewall-cmd --permanent --zone=public --add-forward-port=port=80:proto=tcp:toaddr=172.17.100.100:toport=80
#刷新防火牆
firewall-cmd --reload
測試
使用瀏覽器訪問會發現達不到輪詢的效果,使用jmeter壓測可以看到輪詢的效果