nginx高可用性能部署
HA高可用
先制作镜像再创建两个容器
根据第三条来创建两个容器,你会发现有大量的重复操作,我们可以先制作一个镜像,再根据该镜像创建两个容器,从而避免了大量重复操作。
1、配置模板容器
(1)、下载centos:7.6.1810镜像
docker pull centos:7.6.1810
(2)、启动并进入容器
docker run -it centos:7.6.1810 /bin/bash
(3)、安装基础工具
yum install -y iproute yum install -y net-tools
(4)、安装nginx的依赖库和安装nginx
先安装nginx依赖
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
再安装nginx
yum install -y nginx
(5)、修改index.html的标题
cd /usr/share/nginx/html vi index.html
vi index.html
title改为Master
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_MASTER
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx {
script “/etc/keepalived/check_nginx.sh”
interval 2
weight -20
fall 3
rise 2
user root
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.66
}
track_script {
chk_nginx
}
}
(11)、退出容器,制作镜像
执行exit退出容器,查看容器
docker ps -a
结果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 07c519d6244b centos:7.6.1810 “/bin/bash” 25 minutes ago Exited (1) 2 minutes ago peaceful_fermi
使用commit命令将容器保存为镜像
docker commit peaceful_fermi keepalived_nginx:v1
此镜像的内容就是当前容器的内容,接下来你可以用此镜像再次运行新的容器
启动主容器
docker run -it --privileged=true --name keepalived_master --restart=always --net mynet --ip 192.168.0.2 keepalived_nginx:v1 /usr/sbin/init
docker run -it --privileged=true --name keepalived_slave --restart=always --net mynet --ip 192.168.0.3 keepalived_nginx:v1 /usr/sbin/init
进入备机中修改 keepalived配置
docker exec -it keepalived_slave bash cd /etc/keepalived vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_SLAVE
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx {
script “/etc/keepalived/check_nginx.sh”
interval 2
weight -20
fall 3
rise 2
user root
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.66
}
track_script {
chk_nginx
}
}
修改router_id、state、priority即可。
修改完后重启keepalived
cd /usr/local/keepalived/sbin/ ./keepalived -f /etc/keepalived/keepalived.conf
关闭keepalived
ps -ef | grep keepalived kill -9 3747
修改nginx中index.html的标题
cd /usr/share/nginx/html vi index.html
启动nginx
cd /usr/sbin/
./nginx
测试
进入主容器启动nginx和keepalived
查看主容器IP ip a
[root@b3a23e52b268 sbin]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 188: eth0@if189:
进入从容器查看IP ip a
[root@ad57dfb60f80 /]# ip a 1: lo:
主容器中通过curl命令访问192.168.0.66
[root@b3a23e52b268 sbin]# curl 192.168.0.66
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
从容器中通过curl命令访问192.168.0.66
[root@ad57dfb60f80 /]# curl 192.168.0.66
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
关闭主容器的keepalived
[root@b3a23e52b268 sbin]# ps -ef | grep keepalived root 3771 0 1 09:19 ? 00:00:04 ./keepalived -f /etc/keepalived/keepalived.conf root 3773 3771 0 09:19 ? 00:00:00 ./keepalived -f /etc/keepalived/keepalived.conf root 15815 3744 0 09:23 pts/1 00:00:00 grep --color=auto keepalived
杀死keepalived
kill -9 3771
在主容器中查看IP ip a
[root@b3a23e52b268 sbin]# ip a 1: lo:
在从容器中查看IP ip a
[root@ad57dfb60f80 /]# ip a 1: lo:
发现VIP绑定到从容器中了。
在主容器中通过curl命令访问192.168.0.66
[root@b3a23e52b268 sbin]# curl 192.168.0.66
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
在从容器中通过curl命令访问192.168.0.66
[root@ad57dfb60f80 sbin]# curl 192.168.0.66
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
主容器中启动keepalived
主容器中查看ip ip a
[root@b3a23e52b268 sbin]# ip a 1: lo:
发现VIP又绑定到主容器上了。
在主容器中通过curl命令访问192.168.0.66
[root@b3a23e52b268 sbin]# curl 192.168.0.66
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
说明keepalived实现了nginx的高可用。