Nginx配置管理平台

软件环境

centos7 python2.7.6 etcd3.2.18 confd 0.16 nginx1.12.1

效果演示

拓扑图

涉及软件

软件部署

1)安装 etcd(这里安装的单机,集群环境根据自己的需求选取)

 # yum install etcd -y
 # sed -i  's/localhost/0.0.0.0/g'  /etc/etcd/etcd.conf  #配置监听地址
 # systemctl   start  etcd  &&  systemctl  enable  etcd  #启动服务设置开机动

2)安装 nginx

 #yum install  python-devel gcc gcc-c++ pcre  pcre-devel   patch   unzip   zlib  zlib-devel  openssl openssl-devel  git  -y  #依赖包
 #cd  /usr/local/src
 #wget  http://nginx.org/download/nginx-1.12.1.tar.gz
 #git clone https://github.com/yaoweibin/nginx_upstream_check_module.git  
 #tar  -zxvf  nginx-1.12.1.tar.gz 
 #cd nginx-1.12.1
 #patch  -p1 </usr/local/src/nginx_upstream_check_module/check_1.12.1+.patch
 #./configure   --prefix=/usr/local/nginx --add-module=/usr/local/src/nginx_upstream_check_module/
 # make -j4 && make install
 #mkdir  /usr/local/nginx/conf/vhost/
 Nginx主配置文件修改为这个样子,增加include目录配置
 #vi  /usr/local/nginx/conf/nginx.conf
 
 
   #user  nobody;
   worker_processes  1;

   #error_log  logs/error.log;
   #error_log  logs/error.log  notice;
   #error_log  logs/error.log  info;

   #pid        logs/nginx.pid;


   events {
     worker_connections  1024;
   }


   http {
     include       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  logs/access.log  main;

     sendfile        on;
     #tcp_nopush     on;

     #keepalive_timeout  0;
     keepalive_timeout  65;

     #gzip  on;

   include   vhost/*.conf;
   }

3)安装 confd

下载地址https://github.com/kelseyhightower/confd/releases

下载完毕丢到系统里面

 # cp confd  /usr/bin/confd 
 # which  confd
 /usr/bin/confd

4)创建配置文件目录

 # mkdir -p /etc/confd/{conf.d,templates}
 conf.d          # 资源模板,下面文件必须以toml后缀
 templates       # 配置文件模板,下面文件必须以tmpl后缀

5)创建 confd 配置文件

 # vi /etc/confd/conf.d/app01.conf.toml

 [template]
 src = "app01.conf.tmpl"                              #默认在/etc/confd/templates目录下
 dest = "/usr/local/nginx/conf/vhost/app01.conf"      #要更新的配置文件
 keys = [
   "/Shopping",                                      #监测的key
 ]
 reload_cmd ="/usr/local/nginx/sbin/nginx -s reload"   #最后执行的命令

6)创建 confd 模板

  # vi  /etc/confd/templates/app01.conf.tmpl 
   
   upstream {{getv "/Shopping/nginx/cluster1/proxy_name"}} {
     {{range getvs "/Shopping/nginx/cluster1/upstream/*"}}
       server {{.}};
     {{end}}

     check interval=5000 rise=1 fall=5 timeout=4000 type=http;
     check_http_send "HEAD / HTTP/1.0\r\n\r\n";
     check_http_expect_alive http_2xx http_3xx;

   }
     
   server {
      server_name   {{range getvs "/Shopping/nginx/cluster1/server_name/*"}} {{.}} {{end}};
      location / {
        proxy_pass        http://{{getv  "/Shopping/nginx/cluster1/proxy_name"}};
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
       location /status {
           check_status;
           access_log   off;
          }
   }

7)启动 confd 并设置开机启动

开机启动脚本会给大家分享

拷贝至 /etc/init.d/confd ,只需要更改 etcd 的连接地址即可

#/etc/init.d/confd  start  && chkconfig  --add  confd  && chkconfig  confd on

配置平台部署

1)Github 克隆平台代码安装平台依赖

 # git  clone  https://github.com/1032231418/Conf_Web.git
 # cd Conf_Web/ospweb/
 #yum install python-pip -y         #安装pip
 #mkdir   /root/.pip/               #创建pip源配置文件目录
 # vi  /root/.pip/pip.conf          #修改为阿里云的pip源
 
   [global]
   trusted-host=mirrors.aliyun.com
   index-url=http://mirrors.aliyun.com/pypi/simple/
   [list]
   format=columns
   
 #pip install   virtualenv          #安装沙盒工具
 #virtualenv   env                  #建议创建一个沙盒环境跑该平台
 # source  env/bin/activate         #使用沙盒环境
 # pip install -r requirement.txt   #安装相关软件

2)创建数据库并将表刷入数据库

 #  mysql  -p          #登录数据库为平台创建一个数据库
 #CREATE DATABASE  opsweb   CHARACTER SET utf8 COLLATE utf8_general_ci;      #创建数据库opsweb
 
 # vi opsweb/settings.py   #这里数据库信息改为自己的数据库信息
     DATABASES = {
         'default': {
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'opsweb',
         'HOST': 'localhost',
         'USER': 'root',
         'PASSWORD': '123456',
         'PORT': 3306,
       }
     }
     
     ETCD_Server = "192.168.0.221"        #这里改为自己etcd 的ip地址
     ETCD_Port = 2379
     
 # python manage.py   migrate          #提交迁移文件至数据库,将表刷入数据库

3)创建超级管理员账号

# python manage.py    createsuperuser

4)运行平台

 # python manage.py  runserver 0:8000
 访问地址就是 http://ip:8000   账号密码就是上一步创建的超级管理员账号密码

5)登录平台为 nginx 创建 key/value 

例子:  Shopping 平台为例

项目创建

1.创建商城项目  /Shopping

2.创建商城项目里面的 /Shopping/nginx   nginx 服务

3.创建nginx 集群目录  /Shopping/nginx/cluster1

4.给我们的商城 nginx 集群1项目创建配置文件

5.域名 和 节点名称可能是多个,这里我们需要创建目录 /Shopping/nginx/cluster1/server_name 和 /Shopping/nginx/cluster1/upstream

image

etcd 里面存储的值

image

		配置创建:
			1.反向代理        /Shopping/nginx/cluster1/proxy_name  
			2.绑定一个域名     /Shopping/nginx/cluster1/server_name/1	
			3.创建一个集群节点 /Shopping/nginx/cluster1/upstream/web1	

image

etcd 里面存储的值

image

生成的配置文件

image

通过hosts 文件我们可以查看节点状态(虽然这个节点不是up 状态但是由此可见,我们可以动态添加节点)

image

五.nginx + uwsgi + django 项目部署

1)uwsgi  部署

	#source  env/bin/activate      #使用沙盒
	#pip install uwsgi             #安装 uwsgi


	#vi   uwsgi.ini 

		[uwsgi]
		# 配置服务器的监听ip和端口,让uWSGI作为nginx的支持服务器的话,设置socke就行;如果要让uWSGI作为单独的web-server,用http
		http = 127.0.0.1:8000
		#socket = 127.0.0.1:3309
		# 配置项目目录(此处设置为项目的根目录)
		chdir =  /home/web/opsweb
		# 配置入口模块 (django的入口函数的模块,即setting同级目录下的wsgi.py)
		wsgi-file =  opsweb/wsgi.py
		# 开启master, 将会多开一个管理进程, 管理其他服务进程
		master = True
		# 服务器开启的进程数量
		processes = 8
		# 以守护进程方式提供服, 输出信息将会打印到log中
		daemonize = wsgi.log
		# 服务器进程开启的线程数量
		threads = 4
		# 退出的时候清空环境变量
		vacuum = true
		# 进程pid
		pidfile = uwsgi.pid
		# 配uWSGI搜索静态文件目录(及django项目下我们存放static文件的目录,用uWSGI作为单独服务器时才需要设置,此时我们是用nginx处理静态文件)
		# check-static =  /home/web/opsweb/static/



	#/home/env/bin/uwsgi   --ini uwsgi.ini   #启动服务

2)nginx  反向代理配置

#vi  /usr/local/nginx/conf/vhost/ops.conf

		upstream  ops_web {
		
			server  127.0.0.1:8000;
	}
	  
	server {
	   server_name    ops.xxx.com;       #改为你平台的域名
	   location / {
		   proxy_pass        http://ops_web;
		   proxy_redirect off;
		   proxy_set_header Host $host;
		   proxy_set_header X-Real-IP $remote_addr;
		   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}

		location /static {
					alias  /home/web/opsweb/static/;
			}
	}

#/usr/local/nginx/sbin/nginx  -s  reload  #重新加载配置文件

参考

https://github.com/fungitive/Conf_Web

https://github.com/Qihoo360/QConf/blob/master/README_ZH.md

猜你喜欢

转载自blog.csdn.net/bbwangj/article/details/82806466
今日推荐