环境:CenOS 6.5
Nginx-Master:10.10.10.128
Nginx-Backup:10.10.10.129
Tomcat1:10.10.10.130
Tomcat2:10.10.10.131
VIP:10.10.10.100

集群规划图片

一、环境基础配置
1、更换国内yum源
2、关闭防火墙、SELinux
3、时间同步

一、Nginx的安装过程

二、Web端安装Web服务

1.下载Nginx安装包,安装依赖环境包

1、查看是否安装JDK
[root@Tomcat1 ~]# java -version
java version “1.8.0_171”
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
2、安装JDK
官网下载二进制包
解压到/usr/local/java
    tar zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java/
修改环境变量/etc/profile
    JAVA_HOME=/usr/local/java
    PATH=$JAVA_HOME/bin:$PATH
    CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    export PATH JAVA_HOME CLASSPATH
使环境变量生效:
    source /etc/profile
3、下载Tomcat源码包:
    wget -O /opt/apache-tomcat-9.0.7.tar.gz

4、解压到/usr/local/tomcat
    tar zxvf /opt/apache-tomcat-9.0.7.tar.gz -C /usr/local/
5、修改Tomcat的主页
    rm -rf /usr/local/apache-tomcat-9.0.7/webapps/ROOT/*
    echo “Tomcat1”
>/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat1
    echo “Tomcat2”
>/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat2
6、测试Tomcat能否正常启动
[root@Tomcat1 bin]# ./usr/local/apache-tomcat-9.0.7/bin
/startup.sh
Using CATALINA_BASE:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_HOME:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:     
/usr/local/apache-tomcat-9.0.7/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.7/bin/tomcat-juli.jar
Tomcat started.

(1)安装 C++编译环境

yum  -y install gcc   #C++

curl 10.10.10.130:8080  #返回Tomcat1
curl 10.10.10.131:8080  #返回Tomcat2

(2)安装pcre

yum  -y install pcre-devel

至此web端配置完成。

(3)安装zlib

yum  -y install  zlib-devel

二、Nginx反向代理安装

(4)安装Nginx

定位到nginx 解压文件位置,执行编译安装命令

[root@localhost nginx-1.12.2]# pwd
/usr/local/nginx/nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure  && make && make install

1、安装依赖软件
    yum install -y gcc gcc-c++
    yum install -y pcre pcre-devel openssl openssl-devel zlib
zlib-devel
2、官网下载源码包
3、解压源码包
    tar zxvf nginx-1.14.0.tar.gz -C /tmp/
4、编译安装
    useradd -s /bin/false -M nginx
    ./configure –user=nginx –group=nginx
–prefix=/usr/local/nginx-1.14.0/ –with-http_v2_module
–with-http_ssl_module –with-http_sub_module
–with-http_stub_status_module –with-http_gzip_static_module
–with-pcre
    make && make install
5、配置反向代理/usr/local/nginx/conf/nginx.conf
worker_processes  1;
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
  use epoll;
  worker_connections  51200;
}
http {
  include      mime.types;
  default_type  application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile        on;
  tcp_nopush      on;
  keepalive_timeout  65;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types    test/plain application/x-javascript test/css
application/xml;
  gzip_vary on;

(5)启动Nginx

安装完成后先寻找那安装完成的目录位置

[root@localhost nginx-1.12.2]# whereis nginx
nginx: /usr/local/nginx
[root@localhost nginx-1.12.2]# 

进入Nginx子目录sbin启动Nginx

[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx &
[1] 5768
[root@localhost sbin]# 

查看Nginx是否启动

Niginx启动成功截图

或通过进程查看Nginx启动情况

[root@localhost sbin]# ps -aux|grep nginx
root       5769  0.0  0.0  20484   608 ?        Ss   14:03   0:00 nginx: master process ./nginx
nobody     5770  0.0  0.0  23012  1620 ?        S    14:03   0:00 nginx: worker process
root       5796  0.0  0.0 112668   972 pts/0    R+   14:07   0:00 grep --color=auto nginx
[1]+  完成                  ./nginx
[root@localhost sbin]# 

到此Nginx安装完成并启动成功。

  upstream backend {
        server 10.10.10.130:8080;
        server 10.10.10.131:8080;

(6)Nginx快捷启动和开机启动配置

编辑Nginx快捷启动脚本【注意Nginx安装路径需要根据自己的NGINX路径进行改动

[root@localhost init.d]# 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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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' -`
    if [ -z "`grep $user /etc/passwd`" ]; then
    useradd -M -s /bin/nologin $user
    fi
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    for opt 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 ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    #configtest || return $?
    stop
    sleep 1
    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/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)

rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

为启动脚本授权 并加入开机启动

[root@localhost init.d]# chmod -R 777 /etc/rc.d/init.d/nginx 
[root@localhost init.d]# chkconfig  nginx 

启动Nginx

[root@localhost init.d]# ./nginx start

将Nginx加入系统环境变量

[root@localhost init.d]# echo 'export PATH=$PATH:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile

Nginx命令 [ service nginx (start|stop|restart) ]

[root@localhost init.d]# service nginx start
Starting nginx (via systemctl):                            [  确定  ]

Tips:快捷命令

service nginx (start|stop|restart)

  }
  server {
      listen      80;
      server_name  10.10.10.128;  #Nginx2改为:10.10.10.129
      location / {
          root  /var/www/html;
          index  index.php index.html index.htm;
          proxy_pass ;
      }
  }
}     
先测试再启动:
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.14.0//conf/nginx.conf
syntax is ok
nginx: configuration file /usr/local/nginx-1.14.0//conf/nginx.conf test
is successful
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx
[root@Nginx1 ~]# lsof -i:80
COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx  4896  root    6u  IPv4  18439      0t0  TCP *:http (LISTEN)
nginx  4897 nginx    6u  IPv4  18439      0t0  TCP *:http (LISTEN)

二、KeepAlived安装和配置

curl 10.10.10.128  #轮询返回Tomcat1 Tomcat2
Nginx2同样的搭建。
至此Nginx反向代理搭建完成。

1.安装Keepalived依赖环境

yum install -y popt-devel     
yum install  -y ipvsadm
yum install -y libnl*
yum install -y libnf*
yum install -y openssl-devel

三、使用Keepalived实现高可用

2.编译Keepalived并安装

[root@localhost keepalived-1.3.9]# ./configure
[root@localhost keepalived-1.3.9]#  make && make install

1、安装:
    yum install keepalived -y
2、修改配置文件/etc/keepalived/keepalived.conf
MASTER 端:
! Configuration File for keepalived
vrrp_script check_nginx {
        script “/etc/keepalived/check_nginx.sh”
        interval 2
        weight 2
}
global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL1
}

3.将Keepalive 安装成系统服务

[root@localhost etc]# mkdir /etc/keepalived
[root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/

手动复制默认的配置文件到默认路径

[root@localhost etc]#  mkdir /etc/keepalived
[root@localhost etc]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
[root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

为keepalived 创建软链接

[root@localhost sysconfig]# ln -s /usr/local/keepalived/sbin/keepalived  /usr/sbin/

设置Keepalived开机自启动

[root@localhost sysconfig]# chkconfig keepalived  on
注意:正在将请求转发到“systemctl enable keepalived.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service

启动Keepalived服务

[root@localhost keepalived]# keepalived -D  -f /etc/keepalived/keepalived.conf

关闭Keepalived服务

[root@localhost keepalived]# killall keepalived

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100/24 dev eth0
  }
 track_script {
        check_nginx
  }
}
BACKUP端:
修改:
router_id LVS_DEVEL2
state BACKUP
priority 90

三、集群规划和搭建

集群规划图片

环境准备:

CentOS 7.2

Keepalived   Version 1.4.0 – December 29, 2017

Nginx           Version: nginx/1.12.2

Tomcat         Version:8


集群规划清单

虚拟机 IP 说明
Keepalived+Nginx1[Master] 192.168.43.101 Nginx Server 01
Keeepalived+Nginx[Backup] 192.168.43.102 Nginx Server 02
Tomcat01 192.168.43.103 Tomcat Web Server01
Tomcat02 192.168.43.104 Tomcat Web Server02
VIP 192.168.43.150 虚拟漂移IP

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图