>  기사  >  백엔드 개발  >  keepalived+nginx는 nginx의 고가용성을 실현합니다.

keepalived+nginx는 nginx의 고가용성을 실현합니다.

WBOY
WBOY원래의
2016-08-08 09:22:551018검색

keepalived+nginx는 nginx의 고가용성을 실현합니다

===== = ==========================

nginx 고가용성

nginx는 백엔드 실제 서버의 로드 밸런싱을 구현합니다

====== = ===========================


실험 환경: OS: Centos 6.4(redhat 6.4)yum 소스:

1

2

3

4

5

6

7

8

9

10

11

[centos]

name=sohu-centos

baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch

gpgcheck=1

enable=0

gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6

[epel]

name=sohu-epel

baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/

enable=1

gpgcheck=0


拓扑图:

拓扑图的规划:


IP地址

软件

Master

172.16.22.1(VIP172.16.22.100)

keepalived+nginx

Backup

172.16.22.2(VIP172.16.22.100)

keepalived+nginx

apache1

172.16.22.3

httpd

apache2

172.16.22.4

httpd

IP地址 软件

마스터

172.16.22.1(VIP172.16.22.100)

keepalived+nginx

백업 172.16.22.2(VIP172.16.22.100)

keepalived+nginx

apache1

172.16.22.3

httpd

apache2

172.16.22.4

httpd

이 아키텍처에서 고려해야 할 문제

1) Master가 다운되지 않은 경우 Master가 VIP를 점유하고 nginx가 Master에서 실행됩니다

2) Master가 다운된 경우 , 백업은 VIP를 점유하고 백업에서 nginx 서비스를 실행합니다

3). 마스터 서버의 nginx 서비스가 중단되면 VIP 리소스가 중단됩니다.

4) 백엔드 서버의 상태를 확인하세요

마스터 또는 백업에 관계없이 마스터와 백업 모두에서 nginx 서비스를 활성화합니다. 연결 유지 서비스 중 하나가 중지되면 VIP는 연결 유지 서비스가 아직 있는 노드로 이동합니다.

nginx를 사용하려는 경우 서비스가 중단되고 vip가 다른 노드로 이동하는 경우 구성 파일의 스크립트 또는 쉘 명령을 사용하여 제어해야 합니다.

우선, keepalived는 이 아키텍처에서 백엔드 서버의 상태를 감지할 수 없다는 점을 분명히 해야 합니다. 예, 하지만 nginx의 감지 메커니즘에는 특정 결함이 있습니다. 백엔드 서버가 다운된 후에도 nginx는 백엔드 서비스가 일정 기간 내에 응답할 수 없는 경우 계속해서 해당 서버에 요청을 배포합니다. 그런 다음 고객의 요청이 오면 nginx는 일정 기간 동안 해당 요청을 다운 서버로 배포하지 않지만 일정 시간이 지난 후에도 nginx는 여전히 다운 서버로 배포 요청을 보냅니다.

1. keepalived+nginx 설치

마스터:

1. keepalived를 설치하고 nginx를 컴파일하고 설치합니다

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

[root@jie1 ~]# yum -y install keepalived

[root@jie1 ~]#tar xf nginx-1.4.2.tar.gz

[root@jie1 ~]#yum -y groupinstall "Development tools" "Server  Platform Development"

[root@jie1 ~]#yum -y install pcre-devel

[root@jie1 ~]# cd nginx-1.4.2

[root@jie1 nginx-1.4.2]# groupadd nginx

[root@jie1 nginx-1.4.2]# useradd -r -g nginx nginx

[root@jie1 nginx-1.4.2]#./configure

--prefix=/usr

--sbin-path=/usr/sbin/nginx

--conf-path=/etc/nginx/nginx.conf

--error-log-path=/var/log/nginx/error.log

--http-log-path=/var/log/nginx/access.log

--pid-path=/var/run/nginx/nginx.pid 

--lock-path=/var/lock/nginx.lock

--user=nginx

--group=nginx

--with-http_ssl_module

--with-http_flv_module

--with-http_stub_status_module

--with-http_gzip_static_module

--http-client-body-temp-path=/var/tmp/nginx/client/

--http-proxy-temp-path=/var/tmp/nginx/proxy/

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi

--http-scgi-temp-path=/var/tmp/nginx/scgi

--with-pcre

[root@jie1 nginx-1.4.2]# make && make install

2. nginx 시스템 V 서비스 스크립트 파일 제공

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

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

[root@jie1 nginx-1.4.2]# vim /etc/rc.d/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse

#               proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /etc/nginx/nginx.conf

# config:      /etc/sysconfig/nginx

# pidfile:     /var/run/nginx.pid

# Source function library.

/etc/rc.d/init.d/functions

# Source networking configuration.

/etc/sysconfig/network

# Check that networking is up.

"$NETWORKING""no"] && exit0

nginx="/usr/sbin/nginx"

prog=$(basename$nginx)

NGINX_C/code>"/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories

user=`nginx -V 2>&1 | grep"configure arguments:"sed's/[^*]*--user=([^ ]*).*/1/g'-`

opti -V 2>&1 | grep'configure arguments:'`

foropt in$options; do

if[ `echo$opt | grep'.*-temp-path'` ]; then

value=`echo$opt | cut-d "="-f 2`

if[ ! -d "$value"]; then

# echo "creating" $value

mkdir-p $value && chown-R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit5

[ -f $NGINX_CONF_FILE ] || exit6

make_dirs

echo-n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq0 ] && touch$lockfile

return$retval

}

stop() {

echo-n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq0 ] && rm-f $lockfile

return$retval

}

restart() {

configtest || return$?

stop

sleep1

start

}

reload() {

configtest || return$?

echo-n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null2>&1

}

case""in

start)

rh_status_q && exit0

;;

stop)

rh_status_q || exit0

;;

restart|configtest)

;;

reload)

rh_status_q || exit7

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit0

;;

*)

echo $"Usage:

{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit2

esac

[root@jie1 nginx-1.4.2]# chmod +x /etc/rc.d/init.d/nginx

[root@jie1 nginx-1.4.2]# service nginx start

Starting nginx:                                            [  OK  ]

[root@jie1 nginx-1.4.2]# scp -p /etc/rc.d/init.d/nginx  172.16.22.2:/etc/rc.d/init.d    #把nginx的服务脚本复制到backup上,-p是保持原有的权限

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

[root@jie1 ~]# cd /etc/keepalived/

[root@jie1 keepalived]# vim keepalived.conf

global_defs {

   notification_email {

     root@localhost

   

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.