nginx 역방향 에이전트 설정, 부하 균형 및 nginx 사용 가능

1. nginx 역방향 대 리 를 설정 하여 api. x. com 도 메 인 네 임 에이전트 로 컬 9001 포트 실현
1.1 설치 nginx
1.1.1 설치 의존 패키지
[root@c1 nginx]# yum install gcc pcre-devel openssl-devel zlib-devel -y

1.1.2 nginx 사용자 만 들 기
[root@c1 ~]# useradd -r -s /sbin/nologin nginx

1.1.3 홈 페이지 에서 nginx 소스 패 키 지 를 다운로드 하고 압축 을 풀 고 컴 파일 하여 설치 합 니 다.
[root@c1 src]# pwd
/usr/local/src
[root@c1 src]# ls
nginx-1.16.1.tar.gz
[root@c1 src]# tar xf nginx-1.16.1.tar.gz 
[root@c1 src]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz
[root@c1 src]# mv nginx-1.16.1 nginx
[root@c1 src]# cd nginx/
[root@c1 nginx]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@c1 nginx]# ./configure --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
[root@c1 nginx]# make -j 4 && make install

1.1.4 환경 변 수 를 설정 하여 nginx 를 시작 하기에 편리 합 니 다.
[root@c1 sbin]# export PATH="/usr/local/nginx/sbin:$PATH"

1.1.5 nginx 프로필 수정
[root@c1 nginx]# vim /usr/local/nginx/conf/nginx.conf  ###           
include       /usr/local/nginx/conf.d/*.conf;
[root@c1 conf.d]# pwd
/usr/local/nginx/conf.d
[root@c1 conf.d]# cat proxy.conf
server {
    server_name api.x.com;
    location / {
    proxy_pass http://localhost:9001; 
    }
}

server {
    listen 9001;
    server_name _;
    root /data/nginx;
    index index.html;
}

1.1.6 테스트 준비 홈 페이지
[root@c1 ~]# mkdir /data/nginx/
[root@c1 ~]# echo proxypass > /data/nginx/index.html

1.3 테스트
1.3.1 수정 / etc / hosts 파일
[root@c2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.1.242 c1 api.x.com

1.3.2 c2 서버 에서 테스트
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass

2. nginx 부하 균형 설정
2.1 계획
c3      
C2  nginx    
c1    web1
c5    web2

2.2 c2 에 nginx 설치
[root@c2 conf.d]# yum install epel-release.noarch -y  ###nginx  epel 
[root@c2 conf.d]# yum install nginx -y

2.3 c2 에서 nginx 설정 파일 수정
###      http{}       
[root@c2 nginx]# pwd
/etc/nginx
[root@c2 nginx]# cat nginx.conf
upstream httpsrvs {       ###        
    server c1;
    server c5;
    }
###      
[root@c2 conf.d]# pwd
/etc/nginx/conf.d
[root@c2 conf.d]# cat test.conf 
server {
    listen 80;
    server_name c2;
    root /data/nginx;
    index index.html;
    location / {
        proxy_pass http://httpsrvs;
    }
}
###  nginx
[root@c2 conf.d]# nginx

2.4 c1 과 c5 에 nginx 설치
참고
2.5 c1 과 c5 의 nginx 를 각각 설정 합 니 다.
###  c1 c5 nginx     default_server,    c1 c5     

[root@c5 nginx]# grep -Ev "#|^$" nginx.conf

......

server {
        listen       80;                               ###  default_server
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

......
### c1 c5   nginx           
[root@c1 conf.d]# pwd
/etc/nginx/conf.d
[root@c1 conf.d]# cat web.conf
server {
    listen 80;
    server_name c1;
    root /data/nginx/;
    index index.html;

}
[[email protected]]# mkdir /data/nginx/ -pv
[root@c1 conf.d]# echo this is c1 > /data/nginx/index.html
[root@c5 conf.d]# pwd
/etc/nginx/conf.d
[root@c5 conf.d]# cat web.conf
server {
    listen 80;
    server_name c5;
    root /data/nginx/;
    index index.html;

}
[root@c5 conf.d]# mkdir /data/nginx/ -pv
[root@c5 conf.d]# echo this is c5 > /data/nginx/index.html

2.6 c3 에서 테스트
[root@c3 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.1.242 c1
10.1.1.243 c2
10.1.1.244 c3
10.1.1.245 c4
10.1.1.246 c5
[root@c3 ~]# curl c2
this is c5
[root@c3 ~]# curl c2
this is c1
[root@c3 ~]# curl c2
this is c5
[root@c3 ~]# curl c2
this is c1
[root@c3 ~]# curl c2
this is c5
[root@c3 ~]# curl c2
this is c1
[root@c3 ~]# curl c2
this is c5
[root@c3 ~]# curl c2
this is c1

3. keepalived 기반 nginx 높 은 사용 가능
3.1 계획
1)  2      
2)c3 c2  keepalived      

3.2 c3 에 nginx 설치 및 설정
1)  2.2 2.3  
2)      c100, /etc/hosts    10.0.1.100 c100
server_name c100;  

3.3 c2 와 c3 에 keepalived 를 설치 하고 설정 파일 을 수정 합 니 다.
3.3.1 keepalived 설치
[root@c2 conf.d]# yum install keepalived -y
[root@c3 conf.d]# yum install keepalived -y

3.3.2 프로필 수정
[root@c2 keepalived]# pwd
/etc/keepalived
[root@c2 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node3
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 5
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev bond0 label bond0:0
    }
}

[root@c3 keepalived]# pwd
/etc/keepalived
[root@c3 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node3
   vrrp_mcast_group4 224.100.100.100
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 5
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev eth0 label eth0:0
    }
}

3.3.3 keepalived 서비스 시작
[root@c2 keepalived]# systemctl start keepalived
[root@c3 keepalived]# systemctl start keepalived
###  ip     bond0 
[root@c2 keepalived]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:ba:03:94 brd ff:ff:ff:ff:ff:ff
3: eth1:  mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
    link/ether 00:0c:29:ba:03:9e brd ff:ff:ff:ff:ff:ff
7: bond0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:ba:03:94 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.243/24 brd 10.0.1.255 scope global noprefixroute bond0
       valid_lft forever preferred_lft forever
    inet 10.0.1.100/24 scope global secondary bond0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feba:394/64 scope link 
       valid_lft forever preferred_lft forever

3.3.4 테스트 keepalived 중복
### c2   keepalived  
[root@c2 keepalived]# systemctl stop keepalived.service
###        ,    IP    
[root@c4 ~]# while true;do curl c100;sleep 1;done
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
[root@c3 keepalived]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/32 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:f1:37:a8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.244/24 brd 10.0.1.255 scope global noprefixroute dynamic eth0
       valid_lft 17051sec preferred_lft 17051sec
    inet 10.0.1.100/24 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet6 fe80::5025:c937:77d0:2b28/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

3.4. 설정 스 크 립 트 호출 nginx 높 은 사용 가능
3.4.1 killall 명령 설치
[root@c2 keepalived]# yum install psmisc-22.20-16.el7.x86_64 -y
[root@c3 keepalived]# yum install psmisc-22.20-16.el7.x86_64 -y

3.4.2 스 크 립 트 를 준비 하고 keepalived 설정 파일 을 수정 합 니 다.
3.4.2.1 주 검사 스 크 립 트
[root@c3 keepalived]# echo "killall -0 nginx || exit 1" > chk_nginx.sh
[root@c3 keepalived]# cat chk_nginx.sh
killall -0 nginx >/dev/null || exit 1
[root@c3 keepalived]# chmod +x chk_nginx.sh
[root@c2 keepalived]# echo "killall -0 nginx || exit 1" > chk_nginx.sh
[root@c2 keepalived]# cat chk_nginx.sh
killall -0 nginx >/dev/null || exit 1
[root@c2 keepalived]# chmod +x chk_nginx.sh

3.4.2.2 keepalived 프로필 수정
[root@c2 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_script chk_nginx {
    script "/etc/keepalived/chk_nginx.sh"
    interval 1
    weight -30
}
vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 5
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev bond0 label bond0:0
    }
    track_script {
    chk_nginx
    }
}

[root@c3 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node3
   vrrp_mcast_group4 224.100.100.100
}
vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 1
        weight -30
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 5
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev eth0 label eth0:0
    }
    track_script {
        chk_nginx
    }
}

###  keepalived  
[root@c2 keepalived]# systemctl restart keepalived.service
[root@c3 keepalived]# systemctl restart keepalived.service

3.4.3 c4 테스트
3.4.3.1 c2 의 nginx 서 비 스 를 중단 합 니 다.
[root@c2 keepalived]# nginx -s stop
[root@c4 ~]# while true;do curl c100;sleep 1;done
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
curl: (7) Failed connect to c100:80; Connection refused
curl: (7) Failed connect to c100:80; Connection refused
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1

3.4.4 nginx 자동 회복 실현
3.4.4.1 프로필 수정
[root@c2 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_mcast_group4 224.100.100.100
}

vrrp_script chk_nginx {
    script "/etc/keepalived/chk_nginx.sh"
    interval 1
    weight -30
}
vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 5
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev bond0 label bond0:0
    }
    track_script {
    chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
[root@c3 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node3
   vrrp_mcast_group4 224.100.100.100
}
vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 1
        weight -30
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 5
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.1.100/24 dev eth0 label eth0:0
    }
    track_script {
        chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

3.4.4.2 c2 와 c3 에서 notify. sh 스 크 립 트 를 준비 합 니 다.
[root@c2 keepalived]# cat notify.sh 
#!/bin/bash
#
contact='root@localhost'
notify() {
    mailsubject="$(hostname) to be $1, vip floating"
    mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    nginx -s stop
    sleep 1
    nginx   
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac
[root@c2 keepalived]#chmod +x notify.sh

3.4.4.3 keepalived 서비스 재 개
[root@c2 keepalived]# systemctl restart keepalived.service
[root@c3 keepalived]# systemctl restart keepalived.service

3.4.5 c4 테스트
3.4.5.1 c2 의 nginx 서 비 스 를 중단 합 니 다.
[root@c2 keepalived]# nginx -s stop
[root@c4 ~]# while true;do curl c100;sleep 1;done
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
curl: (7) Failed connect to c100:80; Connection refused
curl: (7) Failed connect to c100:80; Connection refused
curl: (7) Failed connect to c100:80; Connection refused
this is c5
this is c1
this is c5
this is c1
this is c5
this is c1
this is c5
[root@c2 keepalived]# ss -tnlp |grep -w 80
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=13251,fd=6),("nginx",pid=13250,fd=6))
LISTEN     0      128         :::80                      :::*                   users:(("nginx",pid=13251,fd=7),("nginx",pid=13250,fd=7))

좋은 웹페이지 즐겨찾기