【frontend】前端frontend的安装与配置

一、实验环境


操作系统:CentOS7.2 Mininal

serverA:192.168.1.104

serverB:192.168.1.109

VIP:       192.168.1.110

test:      192.168.1.120

二、软件安装

在serverA 和 serverB 上

# yum  -y install  nginx bind  ntp  keepalived 

# systemctl  enable  named  ntpd  nginx keepalived

三、特殊配置

在serverA 和 serverB 上

# sysctl -w net.ipv4.ip_nonlocal_bind=1

# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf

注:更改Linux系统控制文件,使得端口即使监听在不存在的IP上,也不报错

# setenforce 0

# sed -i 's/^SELINUX=.*/SELINUX=permissive/g'   /etc/selinux/config


# systemctl stop   firewalld

# systemctl diable firewalld


三、serverA服务配置

# vim  /etc/keepalived/keepalived.conf

##############################

! Configuration File for keepalived

global_defs {

   router_id LVS_DEVEL

}

vrrp_script check { 

    script "/etc/keepalived/check.sh" 

    interval 5     

}   

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 100

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {   

      check

    }   

    virtual_ipaddress {

        192.168.1.110

    }

}

##############################

注意: vrrp_script{}中的interval时间需大于脚本中的sleep时间!

#  vim /etc/keepalived/check.sh

##############################

#!/bin/bash

nginx_status1=$(ps -C nginx --no-heading|wc -l)

if [ "${nginx_status1}" = "0" ]; then

  systemctl start nginx.service

  sleep 3

  nginx_status2=$(ps -C nginx --no-heading|wc -l)

  if [ "${nginx_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

named_status1=$(ps -C named --no-heading|wc -l)

if [ "${named_status1}" = "0" ]; then

  systemctl start named.service

  sleep 3

  named_status2=$(ps -C named --no-heading|wc -l)

  if [ "${named_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

ntpd_status1=$(ps -C ntpd --no-heading|wc -l)

if [ "${ntpd_status1}" = "0" ]; then

  systemctl start ntpd.service

  sleep 3

  ntpd_status2=$(ps -C ntpd --no-heading|wc -l)

  if [ "${ntpd_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

#######################################

# chmod +x  /etc/keepalived/check.sh

# vim  /etc/ntp.conf

########################################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

restrict 192.168.1.0 mask 255..255.255.0 nomodify notrap

server 192.168.1.110 iburst

server 127.127.1.0

fudge 127.127.1.0 stratum 10

interface ignore  wildcard

interface listen  192.168.1.110

interface listen  127.0.0.1

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

##########################################

# vim /etc/named.conf

##########################################

options {

        listen-on port 53 { 192.168.1.110; };

        listen-on-v6 port 53 { ::1; };

        directory       "/var/named";

        dump-file       "/var/named/data/cache_dump.db";

        statistics-file "/var/named/data/named_stats.txt";

        memstatistics-file "/var/named/data/named_mem_stats.txt";

        allow-query     { any; };

        recursion yes;

        dnssec-enable yes;

        dnssec-validation yes;

        pid-file "/run/named/named.pid";

};

zone "test.com" IN {

        type master;

        file "test.com.zone";

};

###############################################

# cp  -p  /var/named/named.localhost    /var/named/test.com.zone

# vim  /var/named/test.com.zone



# vim /etc/nginx/nginx.conf

#########################################

#    For more information on configuration, see:

#   * Official English Documentation:http://nginx.org/en/docs/

#   * Official Russian Documentation:http://nginx.org/ru/docs/

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections  1024;

}

# stream转发

stream {

#    hash $remote_addr consistent;

    proxy_connect_timeout 3s;

    include /etc/nginx/conf.d/stream_proxy.conf;

}

# http转发

http {

    client_max_body_size     500M;

    include                             mime.types;

    default_type                     application/octet-stream;

    server_tokens                   off;

    sendfile                            on;

    keepalive_timeout           65;

    include /etc/nginx/conf.d/http_proxy.conf;

}

############################################

#  vim  /etc/nginx/conf.d/stream_proxy.conf

#############################################

upstream stream_service {

    hash $remote_addr consistent;

    server192.168.1.103:12345        max_fails=1 fail_timeout=180s;

    server 192.168.1.104:12345       max_fails=1 fail_timeout=180s;


}

server {

    listen 192.168.1.110:54321;

    proxy_pass stream_service;

}

#####################################################

#  vim /etc/nginx/conf.d/http_proxy.conf

#####################################################

upstream http_service {

    server 192.168.1.107:443      max_fails=1 fail_timeout=180s;

    server 192.168.1.108:443       max_fails=1 fail_timeout=180s;


  }

server {

    listen 192.168.1.110:443 ssl;

    ssl_certificate          /etc/nginx/ssl/nginx-selfsigned.crt;

    ssl_certificate_key  /etc/nginx/ssl/nginx-selfsigned.key;

    location / {

        proxy_connect_timeout     3;

        proxy_send_timeout         600;

        proxy_read_timeout          600;

        send_timeout                    600;

        proxy_set_header            X-Real-IP $remote_addr;

        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

         proxy_pass  https://http_service;

    }

}

#################################################################

# mkdir  /etc/nginx/ssl

# openssl req  -x509  -nodes \

    -newkey rsa:2048 \

    -days 365 \

    -subj "/C=CN/ST=Gunagdong/L=Shenzhen/O=TEST/OU=TEST/CN=www.test.com" \

    -keyout /etc/nginx/ssl/nginx-selfsigned.key \

    -out /etc/nginx/ssl/nginx-selfsigned.crt

四、serverB服务配置

# vim  /etc/keepalived/keepalived.conf

##########################

! Configuration File for keepalived

global_defs {

   router_id LVS_DEVEL

}

vrrp_script check { 

    script "/etc/keepalived/check.sh" 

    interval 5     

}   

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 100

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {   

      check

    }   

    virtual_ipaddress {

        192.168.1.110

    }

}

##############################

注意: vrrp_script{}中的interval时间需大于脚本中的sleep时间!

#  vim /etc/keepalived/check.sh


##############################

#!/bin/bash

nginx_status1=$(ps -C nginx --no-heading|wc -l)

if [ "${nginx_status1}" = "0" ]; then

  systemctl start nginx.service

  sleep 3

  nginx_status2=$(ps -C nginx --no-heading|wc -l)

  if [ "${nginx_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

named_status1=$(ps -C named --no-heading|wc -l)

if [ "${named_status1}" = "0" ]; then

  systemctl start named.service

  sleep 3

  named_status2=$(ps -C named --no-heading|wc -l)

  if [ "${named_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

ntpd_status1=$(ps -C ntpd --no-heading|wc -l)

if [ "${ntpd_status1}" = "0" ]; then

  systemctl start ntpd.service

  sleep 3

  ntpd_status2=$(ps -C ntpd --no-heading|wc -l)

  if [ "${ntpd_status2}" = "0" ]; then

    systemctl stop keepalived.service

  fi

fi

#######################################

# chmod +x  /etc/keepalived/check.sh

# vim  /etc/ntp.conf

########################################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

restrict 192.168.1.0 mask 255..255.255.0 nomodify notrap

server 192.168.1.110 iburst

server 127.127.1.0

fudge 127.127.1.0 stratum 10

interface ignore  wildcard

interface listen  192.168.1.110

interface listen  127.0.0.1

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

##########################################

# vim /etc/named.conf

##########################################

options {

        listen-on port 53 { 192.168.1.110; };

        listen-on-v6 port 53 { ::1; };

        directory       "/var/named";

        dump-file       "/var/named/data/cache_dump.db";

        statistics-file "/var/named/data/named_stats.txt";

        memstatistics-file "/var/named/data/named_mem_stats.txt";

        allow-query     { any; };

        recursion yes;

        dnssec-enable yes;

        dnssec-validation yes;

        pid-file "/run/named/named.pid";

};

zone "test.com" IN {

        type master;

        file "test.com.zone";

};

###############################################

# cp  -p  /var/named/named.localhost    /var/named/test.com.zone

# vim  /var/named/test.com.zone


# vim /etc/nginx/nginx.conf

#########################################

#    For more information on configuration, see:

#   * Official English Documentation:http://nginx.org/en/docs/

#   * Official Russian Documentation:http://nginx.org/ru/docs/

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections  1024;

}

# stream转发

stream {

#    hash $remote_addr consistent;

    proxy_connect_timeout 3s;

    include /etc/nginx/conf.d/stream_proxy.conf;

}

# http转发

http {

    client_max_body_size     500M;

    include                             mime.types;

    default_type                     application/octet-stream;

    server_tokens                   off;

    sendfile                            on;

    keepalive_timeout           65;

    include /etc/nginx/conf.d/http_proxy.conf;

}

############################################

#  vim  /etc/nginx/conf.d/stream_proxy.conf

#############################################

upstream stream_service {

    hash $remote_addr consistent;

    server192.168.1.103:12345        max_fails=1 fail_timeout=180s;

    server 192.168.1.104:12345       max_fails=1 fail_timeout=180s;

}

server {

    listen 192.168.1.110:54321;

    proxy_pass stream_service;

}

#####################################################

#  vim /etc/nginx/conf.d/http_proxy.conf

#####################################################

upstream http_service {

    server 192.168.1.107:443      max_fails=1 fail_timeout=180s;

    server 192.168.1.108:443       max_fails=1 fail_timeout=180s;

  }

server {

    listen 192.168.1.110:443 ssl;

    ssl_certificate          /etc/nginx/ssl/nginx-selfsigned.crt;

    ssl_certificate_key  /etc/nginx/ssl/nginx-selfsigned.key;

    location / {

        proxy_connect_timeout     3;

        proxy_send_timeout         600;

        proxy_read_timeout          600;

        send_timeout                    600;

        proxy_set_header            X-Real-IP $remote_addr;

        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass  https://http_service;

    }

}

#################################################################

# mkdir  /etc/nginx/ssl

# openssl req  -x509  -nodes \

    -newkey rsa:2048 \

    -days 365 \

    -subj "/C=CN/ST=Gunagdong/L=Shenzhen/O=TEST/OU=TEST/CN=www.test.com" \

    -keyout /etc/nginx/ssl/nginx-selfsigned.key \

    -out /etc/nginx/ssl/nginx-selfsigned.crt

五、启动服务

在serverA 和 serveB上

# systemctl  start named  ntpd  nginx keepalived

六、查看服务状态

在serverA

在serverB


七、在test服务器上测试

反向代理测试:

https://192.168.1.110:443

DNS测试:

# vim   /etc/resolv.conf

######################

nameserver 192.168.1.110

# Generated by NetworkManager

nameserver 202.96.128.166

nameserver 202.96.134.133

#####################

# ping www.test.com

# ping mysql.test.com

NTP测试:

# ntpdate 192.168.1.110

# vim  /etc/ntp.conf


#########################

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict ::1

server 192.168.1.110 iburst

restrict 192.168.1.110 nomodify notrap noquery

server 127.127.1.0

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

#########################

# systemctl start   ntpd

# systemctl enable ntpd

八、前端的高可用性测试

在 serverA

# systemctl  restart keepalived

# systemctl  status keepalived

# ip addr list

在 serverB

# systemctl  status keepalived

# ip addr list

可以看到,重启serverA的keepalived,VIP成功漂移了,实际上,VIP所在的服务器上的 nginx、named 、ntpd任何一个服务出问题,keepalived的检测脚本就会停其keepalived服务,使得VIP漂移,服务基本不受影响,实现高可用!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容