Home Backend Development PHP Tutorial keepalived+nginx realizes the high availability of nginx

keepalived+nginx realizes the high availability of nginx

Aug 08, 2016 am 09:22 AM
keepalived nbsp nginx quot root

keepalived+nginx realizes the high availability of nginx

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

nginx high availability

nginx realizes load balancing of back-end realserver

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


Experimental environment: OS: Centos 6.4( redhat 6.4)yum source:

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


Topology map:

Planning of topology map:


IPAddress

Software

Master

172.16.22.1(VIP : 172.16.22.100)

keepalived+nginx

Backup

172.16.22.2 (VIP 172.16.22.100)

keepalived+ nginx

apache1

172.16.22.3

httpd

apache2

172. 16.22.4

httpd

Issues to consider with this architecture

1), Master is not down, then Master occupies VIP and nginx runs on Master

2), Master is down , the backup seizes the VIP and runs the nginx service on the backup

3). If the nginx service on the master server hangs, the VIP resources are transferred to the backup server

4), detection The health status of the backend server

Both Master and Backup have nginx services enabled. Regardless of Master or Backup, when one of the keepalived services stops, the VIP will drift to the node where the keepalived service is still there,

If you want the nginx service to hang up and the vip to drift to another node, you must use a script or use a shell command in the configuration file to control it.

First of all, it must be clear that keepalived cannot detect the health status of the backend server on this architecture. The health status detection of the backend server is judged by nginx, but the detection mechanism of nginx has certain flaws. , after a certain backend server goes down, nginx will still distribute requests to it. If the backend service cannot respond within a certain period of time, nginx will send it to another server. Then when the customer's request comes, nginx will wait for a while. The request will not be distributed to the down server, but after a period of time, nginx will still send the distribution request to the down server.

1. Install keepalived+nginx

Master:

1. Install keepalived and compile and install ngin x

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. Provide nginx system V service script file

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

6 3

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"$1"in

start)

rh_status_q && exit0

$1

;;

stop)

rh_status_q || exit0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit0

;;

*)

echo$"Usage: $0 {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:                                                                                                                       nginx-1.4.2]

# scp -p /etc/rc.d/init.d/nginx 172.16.22.2:/etc/rc.d/init.d #Copy the nginx service script to the backup, -p means to maintain the original permissions

3. Modify configuration file

12

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

See all articles