nginx
主页 > 服务器 > nginx >

Nginx+keepalived配置的实现步骤

2025-05-13 | 佚名 | 点击:

一、需要的服务

Nginx、keepalived、epel-release -y

二、安装命令

1

2

3

4

5

6

7

yum install -y keepalived

 

#安装nginx以及拓展源

 

yum install epel-release -y

 

yum install -y nginx

三、配置web服务

1、web1的nginx配置

1

2

3

4

5

6

7

8

[root@nginx1 ~]# vim /etc/nginx/conf.d/web.conf

server{

        listen 8080;

        root         /usr/share/nginx/html;

        index test.html;

}

 

[root@nginx1 ~]# echo "<h1>This is web1</h1>"  > /usr/share/nginx/html/test.html

2、web2的nginx配置

1

2

3

4

5

6

7

8

[root@nginx2 ~]# vim /etc/nginx/conf.d/web.conf

server{

        listen 8080;

        root         /usr/share/nginx/html;

        index test.html;

}

 

[root@nginx2 ~]# echo "<h1>This is web2</h1>"  > /usr/share/nginx/html/test.html

3、#启动

1

2

nginx -t

nginx

四、 配置keepalived

以192.168.95.128 作为web1的master,192.168.95.129 作为web2为例

 web1配置keepalived

1

vim /etc/keepalived/keepalived.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

! 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.95.128

   smtp_connect_timeout 30

   router_id LVS_DEVEL

   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 {

        192.168.95.128

    }

}

 

virtual_server 192.168.95.128 443 {

    delay_loop 6

    lb_algo rr

    lb_kind NAT

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.95.128 443 {

        weight 1

        SSL_GET {

            url {

              path /

              digest ff20ad2481f97b1754ef3e12ecd3a9cc

            }

            url {

              path /mrtg/

              digest 9b3a0c85a887a256d6939da88aabd8cd

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

web2配置keepalived 

1

vim /etc/keepalived/keepalived.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

! 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.95.129

   smtp_connect_timeout 30

   router_id LVS_DEVEL

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.95.129

    }

}

 

virtual_server 192.168.95.129 443 {

    delay_loop 6

    lb_algo rr

    lb_kind NAT

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.95.129 443 {

        weight 1

        SSL_GET {

            url {

              path /

              digest ff20ad2481f97b1754ef3e12ecd3a9cc

            }

            url {

              path /mrtg/

              digest 9b3a0c85a887a256d6939da88aabd8cd

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

五、脚本编写

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

mkdir /tools

cd /tools

# keepalived通知脚本

vim master.sh

ip=$(hostname -I | awk '{print $1}')

dt=$(date+'%Y%m%d %H:%M:%S')

echo "$0--${ip}--${dt}" >> /tmp/kp.log

 

vim backup.sh

ip=$(hostname -I | awk '{print $1}')

dt=$(date+'%Y%m%d %H:%M:%S')

echo "$0--${ip}--${dt}" >> /tmp/kp.log

 

vim fault.sh

ip=$(ip addr|grep inet| grep 192.168 |awk '{print $2}')

dt=$(date +'%Y%m%d %H:%M:%S')

echo "$0--${ip}--${dt}" >> /tmp/kp.log

 

vim stop.sh

ip=$(ip addr|grep inet| grep 192.168| awk '{print $2}')

dt=$(date +'%Y%m%d %H:%M:%S')

echo "$0--${ip}--${dt}" >> /tmp/kp.log

 

## keepalived健康检查脚本

vim nginx_check.sh

#!/bin/bash

result=`pidof nginx`

if [ ! -z "${result}" ];

then

    exit 0

else

    exit 1

fi

 

# 注意脚本授权,重启keepalived

cd /tools/ && chmod +x *.sh

systemctl restart keepalived.service

六、放在浏览器测试

原文链接:
相关文章
最新更新