This docs talk about high-availability service by Keepalived, HAproxy define port listen at the front-end and forward request into web server backend.
I have a topology include:
- Two server running HAproxy, KeepAlived service with a virtual IP address.
- Two server running Nginx - web server backend.
Server information:
- HAproxy01:
OS: CentOS 6.8 64bit final.
IP addr: 192.168.22.54/24
Port: TCP 80.
- HAproxy02:
OS: CentOS 6.8 64bit final.
IP addr: 192.168.22.56/24
Port: TCP 80.
Virtual IP addr:
192.168.22.55
port: TCP 80
- Webserver01
OS: CentOS 6.8 64bit final
IP addr: 192.168.22.59/24
Port: TCP: 8080
- Webserver02
OS: CentOS 6.8 64bit final
IP addr: 192.168.22.60/24
Port: TCP: 8080
First, i config nginx running for a php site, so i will config php-fpm more. Here is nginx config:
# vi /etx/nginx/nginx.conf
server {
listen 8080;
server_name example-site.org;
index index.php;
root /home/nginx/example-site.org;
access_log off;
error_log /var/log/nginx/example-site-error.log error crit;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_send_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 32k;
fastcgi_busy_buffers_size 32k;
fastcgi_buffers 8 16k;
fastcgi_temp_file_write_size 32k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
php-fpm config:
# vi /etc/php-fpm.d/www.conf
}
php-fpm config:
# vi /etc/php-fpm.d/www.conf
[www]
listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
pm = dynamic
pm.max_children = 13
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500
pm.status_path = /php_status
request_terminate_timeout = 120s
request_slowlog_timeout = 4s
slowlog = /home/nginx/logs/php-fpm-slow.log
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = \$HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
php_admin_value[error_log] = /home/nginx/logs/php-fpm-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
Nginx use unix socket in order to improve performance, faster than call IP:port.
Next, i config HAproxy service on the HAPROXY01 and HAPROXY02 in order to forward request from client.
HAproxy config:
listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
pm = dynamic
pm.max_children = 13
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500
pm.status_path = /php_status
request_terminate_timeout = 120s
request_slowlog_timeout = 4s
slowlog = /home/nginx/logs/php-fpm-slow.log
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = \$HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
php_admin_value[error_log] = /home/nginx/logs/php-fpm-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
Nginx use unix socket in order to improve performance, faster than call IP:port.
Next, i config HAproxy service on the HAPROXY01 and HAPROXY02 in order to forward request from client.
HAproxy config:
defaults
mode http
option http-server-close
timeout client 20s
timeout server 20s
timeout connect 4s
frontend ft_app
bind 192.168.22.54:80 name app
default_backend bk_app
backend bk_app
server s1 192.168.22.59:80 check
server s2 192.168.22.60:80 check backup
Finally, i config KeepAlived service for HA two server running HAproxy. I need a virtual IP address, between two server HAPROXY have channel called "heartbeat" with responsibility communicate servers. And when server HAProxy is down, or service HAproxy stop, it will make HAPROXY02 become MASTER state.
KeepAlived config:
# vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/usr/local/bin/check_nginx.sh"
interval 2
fall 2
rise 2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on master, 100 on backup
nopreempt
unicast_src_ip 192.168.22.54
unicast_peer {
192.168.22.56
}
virtual_ipaddress {
192.168.22.55
}
track_script {
chk_nginx
}
notify /usr/local/bin/keepalived.state.sh
}
# vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/usr/local/bin/check_nginx.sh"
interval 2
fall 2
rise 2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on master, 100 on backup
nopreempt
unicast_src_ip 192.168.22.54
unicast_peer {
192.168.22.56
}
virtual_ipaddress {
192.168.22.55
}
track_script {
chk_nginx
}
notify /usr/local/bin/keepalived.state.sh
}
# vi /usr/local/bin/check_nginx.sh
#!/bin/bash
HAPROXY_STATUS=$(/bin/ps ax | grep -w [n]ginx)
if [ "$NGINX_STATUS" != "" ]
then
exit 0
else
logger "Nginx is NOT running. Setting keepalived state to FAULT."
exit 1
fi
# vi /usr/local/bin/keepalived.state.sh
#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
echo $STATE > /var/run/keepalived.state
# ip addr show
This command is show server hold virtual ip: 192.168.22.55
If you get error about php-fpm, nginx, maybe you forget set permission for session php, try:
# mkdir -p /var/log/nginx
# chown -R nginx:nginx /var/log/nginx
# chown -R nginx:nginx /var/lib/php/session
0 comments:
Post a Comment