中小型规模网站集群架构:nginx负载均衡
: Ago linux运维群:https://hankbook.cn
前言
nginx的反向代理拥有负载均衡功能,即此处用nginx来完成
rpm包制作,根之前的安装环境一样即可
高可用用keepalived来实现,用yum安装即可
yum install -y keepalived
1.各个配置文件
nginx配置文件
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream server_pools {
server 172.16.1.7 weight=1 max_fails=3 fail_timeout=10s;
server 172.16.1.8 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log logs/access_www.log main;
}
server {
listen 10.0.0.3:80;
server_name blog.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log logs/access_blog.log main;
}
server {
listen 10.0.0.3:80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log logs/access_bbs.log main;
}
}
2.keepalived配置文件
master的配置文件,即172.16.1.5上
global_defs {
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
backup的配置文件,即172.16.1.6上
监控主的vip,防止脑裂方法之一
#!/bin/bash
#name: check_web.sh
#desc: check nginx and kill keepalived
if [ `ps -ef |grep nginx |grep -v grep |wc -l` -lt 2 ];then
/etc/init.d/keepalived stop
fi
global_defs {
router_id LVS_02
}
vrrp_script check_web {
script "/server/scripts/check_web.sh"
interval 2
weight 2
}
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 {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
check_web
}
}
2.ansible剧本走起
---
- hosts: 172.16.1.5 172.16.1.6
tasks:
- name: base
include: /server/playbook/base.yml
- name: "install nginx"
yum:
name: nginx
state: installed
- name: "install keepalived"
yum:
name: keepalived
state: installed
- name: "push nginx cfg to server"
copy:
src: /server/files/nginx_lb.cfg
dest: /application/nginx/conf/nginx.conf
mode: 0600
owner: www
group: www
- name: "sysctl"
shell: echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf && sysctl -p
ignore_errors: True
- name: "start nginx"
shell: /application/nginx/sbin/nginx
ignore_errors: True
- hosts: 172.16.1.5
tasks:
- name: "keepalived master cfg"
copy:
src: /server/files/keepalived_master.cfg
dest: /etc/keepalived/keepalived.conf
- name: "start keepalived"
service:
name: keepalived
state: started
enabled: yes
- name: "kaiji ziqi"
shell: chkconfig keepalived on
- hosts: 172.16.1.6
tasks:
- name: "keepalived backup cfg"
copy:
src: /server/files/keepalived_backup.cfg
dest: /etc/keepalived/keepalived.conf
- name: "naolie scripts"
copy:
src: /server/scripts/check_web.sh
dest: /server/scripts/check_web.sh
- name: "start keepalived"
service:
name: keepalived
state: started
enabled: yes
- name: "kaiji ziqi"
shell: chkconfig keepalived on
浙公网安备 33010602011771号