環境 CentOS 6/7 x64 Python:2 .7.6 Etcd: 3.2.18 Confd:0 .16.0 Nginx: 1.12.1 效果演示 一,拓撲圖: 二、涉及軟體 ETD: 。分散式KV存儲系統,一般用於共用配置和服務註冊與發現是ETOS存儲格式類似於文件系統,以根“/”開始下麵 ...
環境
CentOS 6/7 x64
Python:2 .7.6
Etcd: 3.2.18
Confd:0 .16.0
Nginx: 1.12.1
效果演示
一,拓撲圖:
二、涉及軟體
ETD: 。分散式KV存儲系統,一般用於共用配置和服務註冊與發現是ETOS存儲格式類似於文件系統,以根“/”開始下麵一級級目錄,最後一個是重點,一個關鍵對應一個值。
ETCD 集群:使用筏協議保證每個節點數據一致,由多個節點對外提供服務這裡只用單台。
confd:管理本地應用配置文件,使用etcd或consul存儲的數據渲染模板,還支持redis,zookeeper等.confd有一個手錶功能,通過HTTP API定期監測對應的etcd中目錄變化,獲取最新的價值,然後竟染模板
Nginx: Nginx是一款輕量級的Web伺服器/反向代理伺服器以及電子郵件代理伺服器,併在一個BSD-like協議下發行。來自俄羅斯的程式設計師l gor Sysoev所開發,供俄國大型的入口網站及搜索引擎聯繫Rambler使用。其特點是占有記憶體少,併發能力強,事實上nginx的的併發能力確實在同類型的網頁伺服器中表現較好。
三、軟體部署
環境說明:建議使用 Cento7.X X64
1)安裝 etcd(這裡安裝的單機,集群環境根據自己的需求選取)
1 2 3 |
#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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#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 && make install
#mkdir / usr / local / nginx / conf / vhost /
Nginx的主配置文件修改為這個樣子,增加包括目錄配置
#vi /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1 ;
#error_log logs / error.log;
#error_log logs / error.log通知;
#error_log logs / error.log info;
#pid logs / nginx.pid;
事件{
worker_connections 1024 ;
}
http {
包括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;
包括vhost / * .conf;
}
|
3)安裝 confd
地址下載https://github.com/kelseyhightower/confd/releases
下載完畢丟到系統裡面
1 2 3 |
#cp confd / usr / bin / confd
#哪個confd
在 / usr / bin 中 / confd
|
4)創建配置文件目錄
#mkdir -p /etc/confd/{conf.d,templates}
conf.d#資源模板,下麵文件必須以toml尾碼
templates#配置文件模板,下麵文件必須以tmpl尾碼
5)創建 confd 配置文件
1 2 3 4 5 6 7 |
#vi /etc/confd/conf.d/app01.conf.toml
src = “app01.conf.tmpl”#預設在 / etc / confd / templates目錄下
dest = “ / usr / local / nginx / conf / vhost / app01.conf”#要更新的配置文件
鍵 = [
“ / Shopping”,#監測的關鍵
]
reload_cmd = “ / usr / local / nginx / sbin / nginx - s reload ”#最後執行的命令
|
6)創建 confd 模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#vi /etc/confd/templates/app01.conf.tmpl
upstream {{getv“ / Shopping / nginx / cluster1 / proxy_name”}} {
{{ range getvs“ / Shopping / nginx / cluster1 / upstream / * ”}}
server {{。}};
{{結束}}
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_name {{ range getvs“ / Shopping / nginx / cluster1 / server_name / * ”}} {{。}} {{end}};
位置 / {
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;
}
位置 / 狀態{
檢查狀態;
access_log off;
}
}
|
7)啟動 confd 並設置開機啟動
開機啟動腳本會隨文檔附帶
拷貝至 /etc/init.d/confd,只需要更改等改為連接地址即可
1 |
# / etc / init.d / confd start && chkconfig - - add confd && chkconfig confd on
|
四、配置平臺部署
1)Github克隆平臺代碼安裝平臺依賴
1 2 3 4 5 |
#git clone https://github.com/1032231418/Conf_Web.git
#cd Conf_Web / ospweb /
#virtualenv env#建議創建一個沙盒環境跑該平臺
#source env / bin / activate#使用沙盒環境
#pip install -r requirement.txt#安裝相關軟體
|
2)創建資料庫並將表刷入資料庫
1 |
#vi opsweb / settings.py#這裡資料庫信息改為自己的資料庫信息DATABASES = {'default':{'ENGINE':'django.db.backends.mysql','NAME':'confd','HOST' :'192.168.8.114','USER':'root','PASSWORD':'123456','PORT':3306,}} ETCD_Server =“192.168.0.221”#這裡改為自己etcd的ip地址ETCD_Port = 2379 #python manage.py migrate#提交遷移文件至資料庫,將表刷入資料庫
|
3)創建超級管理員賬號
1 |
#python manage.py createsuperuser
|
4)運行平臺
1 |
#python manage.py runserver 0:8000
|
訪問地址就是http:// ip:8000賬號密碼就是上一步創建的超級管理員賬號密碼5)登錄平臺為nginx創建鍵/值
例子:購物平臺為例
項目創建:
1.創建商城項目/購物
2.創建商城項目裡面的/ Shopping / nginx nginx服務
3.創建nginx集群目錄/ Shopping / nginx / cluster1
4.給我們的商城nginx的集群1項目創建配置文件
5.功能變數名稱和節點名稱可能是多個,這裡我們需要創建目錄/ Shopping / nginx / cluster1 / server_name和/ Shopping / nginx / cluster1 / upstream
etcd裡面存儲的值
配置創建:
1.反向代理/ Shopping / nginx / cluster1 / proxy_name
2.綁定一個功能變數名稱/ Shopping / nginx / cluster1 / server_name / 1
3.創建一個集群節點/ Shopping / nginx / cluster1 / upstream / web1
etcd 裡面存儲的值
生成的配置文件
通過主機文件我們可以查看節點狀態(雖然這個節點不是最高狀態但是由此可見,我們可以動態添加節點)