文章目錄
- 前言
- 一 :consul 理論介紹
- 1.1consul具有如下特性:
- 1.2:基于nginx和consul構建高可用及自動發現的Docker服務架構
- 二 :Docker consul自動發現服務架構的構建
- 三 . 具體配置:
- 3.1 環境部署
- 安裝環境
- 設定阿里云鏡像
- 優化
- 加速優化 阿里云鏡像加速
- 準備軟體包
- 下載安裝docker
- 3.2 代理服務器安裝相關軟體
- 部署consul
- consul常用操作查看命令
- 關閉規則,不能關閉防火墻
- 3.3節點服務器操作
- 節點服務器安裝 registrator 容器
- 在節點創建容器進行測驗:
- 3.4 宿主機可訪問 192.168.100.8:8500 測驗
- 3.5 代理服務器原始碼編譯安裝Nginx
- 3.6 代理服務器安裝 consul-template
- 代理服務器 安裝consul-template,準備模板檔案
- 編輯 template nginx 模板檔案
- 修改nginx 配置
- 代理服務器 啟動 template
- 3.7 這時開一臺終端 ,查看組態檔,發現配置以及自動寫入
- 3.8 然后訪問測驗,檢查日志,發現在輪詢訪問容器節點
- 訪問 192.168.100.8:83
- 利用 docker logs -f test2 查看容器 test2 日志
- 3.9 在 節點操作 新增一個容器,測驗 發現功能
- 查看組態檔
- 四 . 發散思維 ,如何 通過配置 將 apache 和 nginx 容器 一起進行輪詢訪問操作
- 方案一 ,修改模板檔案
- 方案二 (騷操作,慎用)
前言
一 :consul 理論介紹
consul是HashiCorp公司推出使用go語言撰寫的開源工具,用于實作分布式系統的服務發現與配置
1.1consul具有如下特性:
1、consul支持健康檢查,允許存盤鍵值對
2、一致性協議采用Raft演算法,用來保證服務的高可用
3、成員管理和訊息廣播采用GOSSIP協議,支持ACL訪問控制
4、方便部署,與Docker等輕量級容器可無縫配合
1.2:基于nginx和consul構建高可用及自動發現的Docker服務架構

上方拓撲圖是基于Docker完成的,然后將consul、consul template、registrator和nginx組裝成一個值得信任且可擴展的服務框架,此架構可以靈活的、不需要重啟任何服務、不需要重寫任何配置的添加和移除服務
拓撲圖解釋
consul template:組態檔模板
registrator:注冊機制
consul server:consul服務
例如:當后方增加了一個容器時,容器會注冊registrator,registrator發現增加了一個容器的時候,會通知consul server要更新,consul server使用consul template模板更新
二 :Docker consul自動發現服務架構的構建
1、每個提供服務的節點上都要部署和運行consul的agent
2、consul agent有兩種運行模式:server和client
3、server和client只是consul集群層面的區分,與搭建在cluster之上的應用服務無關
4、兩個節點都安裝Docker-ce,注意防火墻不要關閉,
5、在代理服務器上部署consul
主機 IP地址 需要安裝的軟體

三 . 具體配置:
3.1 環境部署
安裝環境
[root@server 11 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
設定阿里云鏡像
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加載插件:fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@server 11 ~]# vim /etc/sysctl/conf
net.ipv4.ip_forward=1
優化
開啟路由功能
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@server 11 ~]# sysctl -p
net.ipv4.ip_forward = 1
加速優化 阿里云鏡像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://******.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
準備軟體包

下載安裝docker
[root@server 11 ~]# yum install -y docker-ce
sudo systemctl restart docker
3.2 代理服務器安裝相關軟體
安裝 軟體 docker-compose
chmod +x docker-compose
cp -p docker-compose /usr/local/bin/
[root@docker2 compose_nginx]# yum install tree -y
部署consul
[root@pc-8 dockersoft]# ls
consul_0.9.2_linux_amd64.zip debian-7.0-x86-minimal.tar.gz
consul-template_0.19.3_linux_amd64.zip docker-compose
mkdir -r /root/consul
cp consul_0.9.2_linux_amd64.zip /root/consul/
cd /root/consul/
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
安裝consul
[root@pc-8 bin]#
consul agent \
-server \ //服務功能'
-bootstrap \ //參與選舉'
-ui \ //提供web界面'
-data-dir=/var/lib/consul-data \ //提供一個代理存盤資料目錄'
-bind=192.168.100.8 \ //系結本地地址'
-client=0.0.0.0 \ 監控的地址,所有網段
-node=consul-server01 &> /var/log/consul.log & //定義節點名稱,日志混合輸出到log,并且放到后臺運行'
[1] 77128
consul常用操作查看命令
consul members '//查看集群資訊'
consul info |grep leader '//查看管理資訊'
curl 127.0.0.1:8500/v1/status/peers '/查看集群server成員'
curl 127.0.0.1:8500/v1/status/leaders '//查看集群Raf leader'
curl 127.0.0.1:8500/v1/catalog/services '//查看注冊的所有服務'
curl 127.0.0.1:8500/v1/catalog/nginx '//查看nginx服務的資訊'
curl 127.0.0.1:8500/v1/catalog/nodes '//集群節點詳細資訊'
關閉規則,不能關閉防火墻
setenforce 0
iptables -F 清空規則
3.3節點服務器操作
節點服務器安裝 registrator 容器
docker run -d \
--name=registrator \ '//定義容器名稱'
--net=host \ '定義網路'
-v /var/run/docker.sock:/tmp/docker.sock \ '指定資料卷,存盤資訊'
--restart=always \
gliderlabs/registrator:latest \ '定義鏡像'
-ip=192.168.100.9 \ '指定本地地址
consul://192.168.100.8:8500 '指定consul管理節點地址、埠'
在節點創建容器進行測驗:
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
3.4 宿主機可訪問 192.168.100.8:8500 測驗

3.5 代理服務器原始碼編譯安裝Nginx
yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel \
pcre
[root@localhost nginx-1.12.2]# tar -zxvf nginx-1.12.2.tar.gz
[root@localhost LNMP]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
3.6 代理服務器安裝 consul-template
compose-template介紹
compose-template是一個守護行程,用于實時查詢consul集群資訊
并且運維人員可以在consul服務器上,定制任意的模板,生成組態檔
compose可以查詢consul中的服務目錄、key、key-values等
這種強大的抽象功能和查詢語言模板使得consul-template特別適合動態的創建組態檔,也就是自動化增加服務
例如:創建apache/nginx proxy balacers、haproxy backends
代理服務器 安裝consul-template,準備模板檔案
[root@pc-8 dockersoft]# ls
consul_0.9.2_linux_amd64.zip debian-7.0-x86-minimal.tar.gz
consul-template_0.19.3_linux_amd64.zip docker-compose
[root@pc-8 dockersoft]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@pc-8 dockersoft]# ls
consul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip docker-compose
consul-template debian-7.0-x86-minimal.tar.gz
[root@pc-8 dockersoft]# cp -p consul
consul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip
consul-template
[root@pc-8 dockersoft]# cp -p consul
consul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip
consul-template
[root@pc-8 dockersoft]# cp -p consul-template /usr/local/bin/
編輯 template nginx 模板檔案
此模板用于nginx反向代理模板
nginx.ctmpl跟nginx沒有直接關系,
consul是docker的一種自動管理機制
nginx.ctmpl中的引數以變數的形式寫入
[root@pc-8 dockersoft]# vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}} // 根據鏡像名過濾包含nginx 的 地址和埠
server {{.Address}}:{{.Port}};
{{end}}
}
server { // 反向代理
listen 83;
server_name localhost 192.168.100.8;
access_log /var/log/nginx/nginx-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
修改nginx 配置
[root@pc-8 dockersoft]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; 添加組態檔路徑可識別
default_type application/octet-stream;
創建虛擬主機目錄
[root@pc-8 dockersoft]# mkdir /usr/local/nginx/conf/vhost
創建日志檔案目錄
[root@pc-8 dockersoft]# mkdir /var/log/nginx
[root@pc-8 dockersoft]# /usr/local/nginx/sbin/nginx
[root@pc-8 dockersoft]# netstat -ntap
代理服務器 啟動 template
//指定模板路徑,/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/sha.conf,然后多載nginx -s reload'
接下來會進入監控狀態
consul-template -consul-addr 192.168.100.8:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/sha.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
3.7 這時開一臺終端 ,查看組態檔,發現配置以及自動寫入
[root@pc-8 vhost]# cat sha.conf
upstream http_backend {
server 192.168.100.9:83;
server 192.168.100.9:84;
}
server {
listen 83;
server_name localhost 192.168.100.8;
access_log /var/log/nginx/nginx-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
3.8 然后訪問測驗,檢查日志,發現在輪詢訪問容器節點

訪問 192.168.100.8:83

利用 docker logs -f test2 查看容器 test2 日志

3.9 在 節點操作 新增一個容器,測驗 發現功能
docker run -itd --name test-05 -p 85:80 -h test05 nginx

查看組態檔

四 . 發散思維 ,如何 通過配置 將 apache 和 nginx 容器 一起進行輪詢訪問操作
這里有兩個實作方案
方案一 ,修改模板檔案
實作同時輪詢的方法,設定兩個upstream 進行分別匹配 nginx 和 httpd
[root@docker vhost]# vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
upstream http_httpd {
{{range service "httpd"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 1111;
server_name localhost 192.168.100.8;
access_log /var/log/nginx/cllt.cn-access.log;
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
upstream http_httpd {
{{range service "httpd"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 1111;
server_name localhost 192.168.100.8;
access_log /var/log/nginx/cllt.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
server {
listen 2222;
server_name localhost 192.168.100.8;
access_log /var/log/httpd/cllt.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_httpd;
}
}
結果可自行嘗試
方案二 (騷操作,慎用)
將httpd像匯出,重命名為 nginx:new ,因為 它的過濾是 基于鏡像名來進行的,所以很有效果,
然后基于此鏡像創建容器,即可達到目的,
[root@server-9 opt]# docker run -itd -p 91:80 --name test-08 -h test08 nginx:new
cce37a01c5cd1bf1fa7b665eade83c78c7ee00574e118fe9a618c0f9ce26e582
[root@server-9 opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cce37a01c5cd nginx:new "httpd-foreground" 5 seconds ago Up 5 seconds 0.0.0.0:91->80/tcp test-08
6d9c14e57792 nginx "/docker-entrypoint.…" 50 minutes ago Up 50 minutes 0.0.0.0:85->80/tcp test-05
19f647594be7 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:84->80/tcp test-02
c7704ab68f10 httpd "httpd-foreground" 2 hours ago Up 2 hours 0.0.0.0:89->80/tcp test-04
d0fdaa6ae1f0 httpd "httpd-foreground" 2 hours ago Up 2 hours 0.0.0.0:88->80/tcp test-03
9b7dd5493a95 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:83->80/tcp test-01
16f497d3290b gliderlabs/registrator:latest "/bin/registrator -i…" 4 hours ago Up 4 hours registrator
[root@server-9 opt]#
查看日志檔案
[root@server-9 ~]# docker logs -f test-08
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.7. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.7. Set the 'ServerName' directive globally to suppress this message
[Wed Sep 23 11:50:23.484954 2020] [mpm_event:notice] [pid 1:tid 139876988019840] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Wed Sep 23 11:50:23.485102 2020] [core:notice] [pid 1:tid 139876988019840] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.100.8 - - [23/Sep/2020:11:51:18 +0000] "GET / HTTP/1.0" 200 45
192.168.100.8 - - [23/Sep/2020:11:51:19 +0000] "GET / HTTP/1.0" 200 45
192.168.100.8 - - [23/Sep/2020:11:52:02 +0000] "GET / HTTP/1.0" 200 45
192.168.100.8 - - [23/Sep/2020:11:52:02 +0000] "GET / HTTP/1.0" 200 45
192.168.100.8 - - [23/Sep/2020:11:52:03 +0000] "GET / HTTP/1.0" 200 45
192.168.100.8 - - [23/Sep/2020:11:52:04 +0000] "GET / HTTP/1.0" 200 45



轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128740.html
標籤:其他
上一篇:求infragistics原始碼,哪位大神有?小弟不勝感激!謝謝啊!
下一篇:常用的FTP軟體及優缺點分析
