Linux云计算学习笔记day50
nginx负载均衡 高可用
[root@lb01 /server/scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script chk_ngx {
script "/server/scripts/chk_ngx.sh"
interval 2
weight 1
}
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
}
track_script {
chk_ngx
}
}
[root@lb01 /server/scripts]# systemctl reestart nginx keepalived.service
[root@lb01 /server/scripts]# ps -ef |grep nginx
root 9378 1 0 09:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 9380 9378 0 09:21 ? 00:00:00 nginx: worker process
root 9391 8995 0 09:21 pts/0 00:00:00 grep --color=auto nginx
[root@lb01 /server/scripts]# ps -ef |grep keep
root 9379 1 0 09:21 ? 00:00:00 /usr/sbin/keepalived -D
root 9379 1 0 09:21 ? 00:00:00 /usr/sbin/keepalived -D
root 9381 9379 0 09:21 ? 00:00:00 /usr/sbin/keepalived -D
root 9382 9379 0 09:21 ? 00:00:00 /usr/sbin/keepalived -D
root 9428 8995 0 09:22 pts/0 00:00:00 grep --color=auto keep
[root@lb01 /server/scripts]# ip a |grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 /server/scripts]# pkill nginx
[root@lb01 /server/scripts]# ip a |grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 /server/scripts]# ip a |grep 0.3
[root@lb01 /server/scripts]# ps -ef |grep keep
root 9517 8995 0 09:22 pts/0 00:00:00 grep --color=auto keep
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
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
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
nginx: [emerg] bind() to 10.0.0.4:80 failed (99: Cannot assign requested address)
把这个10.0.0.4的ip绑定到这台机器 失败了
nginx无法把本地不存在的ip地址进行绑定
修改内核参数:
/etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
生效
sysctl -p
[root@lb01 ~]# #net.ipv4.ip_nonlocal_bind
[root@lb01 ~]# # /proc/sys/
[root@lb01 ~]# #net.ipv4.ip_nonlocal_bind
[root@lb01 ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
[root@lb01 ~]# echo 0 >/proc/sys/net/ipv4/ip_nonlocal_bind
[root@lb01 ~]# cat /server/scripts/chk_vip.sh
while true
do
date
sleep 2;
done
[root@lb01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream web_pools {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
include /etc/nginx/conf.d/*.conf;
server {
listen 10.0.0.3:80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}