目录

  • 多机Nomad+Consul+consul-template+Nginx反向代理
    • 1 Nomad与Consul连接
      • 1.1 Nomad
      • 1.2 Consul
      • 1.3 Nomad Job(http-echo容器)
    • 2 consul-template
    • 3 Nginx反向代理

多机Nomad+Consul+consul-template+Nginx反向代理

虚拟机1:Ubuntu18.04,IP:192.168.10.11
虚拟机2:Ubuntu18.04,IP:192.168.10.22

测试过程:Nomad起2个job容器分别运行在2个虚拟机,consul通过check脚本检查2个容器,并将其IP和端口自动更新到consul-template中,虚拟机1输出一个文件。最后利用虚拟机1的Nginx反向代理进行域名访问2个容器。

对比单机上的测试,目的在于验证consul是否能发现非本机所运行的服务,并且通过template渲染IP端口到nginx配置文件。

此外,可以通过访问不同的域名,这些域名指向同一IP(虚拟机1),但反向代理到不同的IP和端口上。即只对外暴露虚拟机1的IP即可。

准备工作:修改docker用户权限,修改主机名以区分nomad结点

1 Nomad与Consul连接

1.1 Nomad

虚拟机1创建/home/.../nomad.d/nomad.hcl

datacenter = "dc1"
data_dir = "/home/.../nomad/data"server {enabled = truebootstrap_expect = 2server_join {retry_join = ["192.168.10.11:4648","192.168.10.22:4648"]}
}client {enabled = trueservers = ["192.168.10.11:4647"]
}

虚拟机2创建/home/.../nomad.d/nomad.hcl

datacenter = "dc1"
data_dir = "/home/.../nomad/data"server {enabled = truebootstrap_expect = 2server_join {retry_join = ["192.168.10.11:4648","192.168.10.22:4648"]}
}client {enabled = trueservers = ["192.168.10.22:4647"]
}

分别执行nomad agent -config /home/.../nomad.d启动nomad agent。

nomad server membersnomad node status可以查看状态。

1.2 Consul

虚拟机1创建/home/.../consul.d/consul.hcl

{"datacenter": "dc1","data_dir": "/home/.../consul/data","log_level": "INFO","node_name": "consul_server1","server": true,"bind_addr": "192.168.10.11","client_addr": "0.0.0.0","bootstrap_expect":2,"ui": true,"enable_script_checks": true,
}

虚拟机2创建/home/.../consul.d/consul.hcl

{"datacenter": "dc1","data_dir": "/home/.../consul/data","log_level": "INFO","node_name": "consul_server2","server": true,"bind_addr": "192.168.10.22","client_addr": "0.0.0.0","bootstrap_expect":2,"ui": true,"enable_script_checks": true,
}

虚拟机1创建/home/.../consul.d/http-echo.hcl

service {name = "http-echo"port = 5678check {args = ["/home/.../consul/check.sh"]interval = "3s"}
}

虚拟机2创建/home/.../consul.d/http-echo2.hcl

service {name = "http-echo2"port = 8765check {args = ["/home/.../consul/check2.sh"]interval = "3s"}
}

虚拟机1创建/home/.../consul/check.sh文件作为consul检测http-echo的脚本,注意chmod给权限:

#!/bin/bash
http=`docker ps | grep "http-echo" | grep "5678->5678"`if [ -n "$http" ];#非空thenexit 0elseexit 2fi

虚拟机2创建/home/.../consul/check2.sh文件作为consul检测http-echo2的脚本,注意chmod给权限:

#!/bin/bash
http=`docker ps | grep "http-echo" | grep "8765->8765"`if [ -n "$http" ];#非空thenexit 0elseexit 2fi

exit 0表示检测成功,1表示警告,其他表示失败。

分别执行consul agent -config-dir /home/.../consul.d启动consul agent。

consul members或浏览器访问192.168.10.11:8500查看状态。

1.3 Nomad Job(http-echo容器)

虚拟机1创建http-echo.nomad以备之后用:

job "http-echo" {datacenters = ["dc1"]constraint {attribute = "${node.unique.name}"value     = "ubuntu1"}group "example" {count = 1network {port "http" {static = "5678"}}task "server" {driver = "docker"config {image = "hashicorp/http-echo"ports = ["http"]args = ["-listen",":5678","-text","hello world",]}}}
}

虚拟机2创建http-echo2.nomad以备之后用:

job "http-echo2" {datacenters = ["dc1"]constraint {attribute = "${node.unique.name}"value     = "ubuntu2"}group "example2" {count = 1network {port "http2" {static = "8765"}}task "server2" {driver = "docker"config {image = "hashicorp/http-echo"ports = ["http2"]args = ["-listen",":8765","-text","hello world222",]}}}
}

constraint是限定参数,${node.unique.name}表示限定节点名唯一,即只能在特定结点运行job。目的是保证两个Job分别在两个虚拟机运行。

2 consul-template

源码:https://github.com/hashicorp/consul-template

二进制文件:https://releases.hashicorp.com/consul-template/

参考:configuration-file,Templating Language

虚拟机分别给权限:

sudo cp ./consul-template /usr/bin/consul-template
sudo chmod 777 /usr/bin/consul-template

虚拟机1创建两个template配置文件consul-template-config.hclconsul-template-config2.hcl

consul {address = "192.168.10.11:8500"retry {enabled  = trueattempts = 12backoff  = "1s"}
}
template {source      = "/home/.../consul/http-echo.ctmpl"destination = "/home/.../consul/ip_port.txt"perms       = 0600command     = "/home/.../consul/template.sh"
}
consul {address = "192.168.10.22:8500"retry {enabled  = trueattempts = 12backoff  = "1s"}
}
template {source      = "/home/.../consul/http-echo2.ctmpl"destination = "/home/.../consul/ip_port2.txt"perms       = 0600command     = "/home/.../consul/template2.sh"
}

source表示模版文件,destination表示输出文件,perms表示文件访问权限,command表示每次变化执行的命令。

虚拟机1创建2个模版文件/home/.../consul/http-echo.ctmpl/home/.../consul/http-echo2.ctmpl

{{range service "http-echo"}}server  {{.Address}}:{{.Port}};{{end}}
{{range service "http-echo2"}}server  {{.Address}}:{{.Port}};{{end}}

创建空文件/home/.../consul/ip_port.txt/home/.../consul/ip_port2.txt/home/.../consul/ng.conf/home/.../consul/ng2.conf,或者perms改成0644允许自动创建。

虚拟机1创建两个template脚本/home/.../consul/template.sh/home/.../consul/template2.sh如下,注意chmod给权限:

#!/bin/bash
txt=`cat ./ip_port.txt`if [[  $txt =~ "server" ]];thenecho "upstream http_backend {" | cat > ./ng.confcat ./ip_port.txt >> ./ng.confecho "
}server {listen 80;server_name test.com;location / {proxy_pass http://http_backend;proxy_redirect off;proxy_set_header Host \$host;proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;proxy_set_header X-Real-IP \$remote_addr;}
}"  | cat >> ./ng.confecho "a" | sudo -S service nginx reloadelse> ./ng.conf
fi
#!/bin/bash
txt=`cat ./ip_port2.txt`if [[  $txt =~ "server" ]];thenecho "upstream http_backend2 {" | cat > ./ng2.confcat ./ip_port2.txt >> ./ng2.confecho "
}server {listen 80;server_name test2.com;location / {proxy_pass http://http_backend2;proxy_redirect off;proxy_set_header Host \$host;proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;proxy_set_header X-Real-IP \$remote_addr;}
}"  | cat >> ./ng2.confecho "a" | sudo -S service nginx reloadelse> ./ng2.conf
fi

因为如果upstream中没有server,service nginx reload会报错,所以需要判断一下输出文件是否有内容,没有的话就把nginx反向代理的配置文件设为空。

执行consul-template:

consul-template \-config=consul-template-config.hcl \-config=consul-template-config2.hcl

3 Nginx反向代理

安装Nginx:https://blog.csdn.net/weixin_43739110/article/details/121079232

在虚拟机1中/etc/nginx/nginx.conf加:

...
http{...include /home/.../consul/*.conf;...
}
...

service nginx restart重启Nginx。

nomad job run http-echo.nomadnomad job run http-echo2.nomad启动之前创建的两个Nomad job容器。

浏览器访问192.168.10.11:8500可以结果。

运行容器后,会看到ip_port.txtip_port2.txt中的内容变为:

server  192.168.10.11:5678;
server  192.168.10.22:8765;

/home/.../consul/ng.conf中的内容变为:

upstream http_backend {server  192.168.10.11:5678;}server {listen 80;server_name test.com;location / {proxy_pass http://http_backend;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;}
}

/home/.../consul/ng2.conf中的内容变为:

upstream http_backend2 {server  192.168.10.22:8765;}server {listen 80;server_name test2.com;location / {proxy_pass http://http_backend2;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;}
}

/etc/hosts文件中添加:

192.168.10.11  test.com
192.168.10.22  test2.com

浏览器登陆test.comtest2.com可以看到(或执行curl test.comcurl test2.com命令):

nomad job stop http-echo关闭Nomad job,会看到/home/.../consul/ip_port.txt/home/.../consul/ng.conf中的内容消失。

浏览器登陆test.com会502错误。

多机Nomad+Consul+consul-template+Nginx反向代理相关推荐

  1. 在一台win10系统的电脑里安装虚拟机运行CentOS7并实现nginx反向代理从而用域名访问本机的微服务项目

    在虚拟机里的CentOS7安装配置nginx,之前需要一些依赖库作为编译安装的条件,具体作用和命令详情跳转我之前的博客. 安装并使用VMware-workstation-full-15.5.0安装Ce ...

  2. 使用Nginx反向代理tomcat服务器

    使用Nginx反向代理tomcat服务器 2015-04-23 11:19 244人阅读 评论(0) 收藏 举报  分类: Tomcat(5)   LNMP/LAMP开发平台研究(10)  版权声明: ...

  3. Tomcat系列之服务器的基本配置及Nginx反向代理tomcat服务

    大纲 一.Tomcat 基本配置 1.为Tomcat提供SysV脚本 2.catalina 脚本讲解 3.telnet 登录管理Tomcat 4.配置Tomcat虚拟主机 5.Tomcat图形管理接口 ...

  4. 微服务架构(5):nginx反向代理cors解决跨域

    微服务架构(5):nginx反向代理&&cors解决跨域 学习目标 1.使用域名访问本地项目 1.1.统一环境 1.2.域名解析 1.3.解决域名解析问题 1.4.nginx解决端口问 ...

  5. nginx反向代理原理及配置详解

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  6. nginx反向代理原理讲解

    一 .概述                  反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果 ...

  7. 懂点 Nginx 反向代理与负载均衡,是面试加分项没有之一

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等 ...

  8. nginx反向代理,负载均衡

    nginx 反向代理(Reverse Proxy)是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给Internet上请求连接的客户 ...

  9. Nginx反向代理以及负载均衡配置

    一 .nginx 的优缺点: nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则 ...

最新文章

  1. 2022-2028年中国车载摄像头行业市场前瞻与投资战略规划分析报告
  2. CGpoint,CGSize,CGRect,NSRange
  3. Linux C:管道的实现原理,命名管道
  4. iReport中序号自增的实现
  5. 苹果市场占有率_三星、华为、苹果位列前三!外媒公布2020年Q2全球智能手机销量排行榜...
  6. 动态规划——最小路径和(Leetcode 64)
  7. JS中的Map和Set实现映射对象
  8. Java铬钼钢车架几何_车架的几何尺寸
  9. python中string什么意思_Python:string是什么意思
  10. 计算机笔记--【Redis高级】
  11. parellels desktop启动虚拟机报“操作失败”
  12. STM32F103/107 移植Freemodbus RTU
  13. MATLAB 中contour的应用
  14. viterbi算法词性标注_使用Viterbi算法深入研究词性标记
  15. 数据处理之seaborn的学习
  16. 海伦公式 (利用三边长求三角形面积)
  17. 解决linux只有ens33和lo网卡的问题
  18. WTF Solidity极简入门: 39链上随机数
  19. 嵌入式linux之buildroot(二)编译rk3399镜像
  20. 多线程调用static方法线程安全问题

热门文章

  1. 如何用python获取文献_[python]eutilities获取文献题录
  2. apache ajax 跨域访问,AJAX跨域访问(从Tomcat8到Apache/Nginx)
  3. 大数据三个特点的理解
  4. 【python】gensim corpora的简单使用
  5. gensim.corpora中Dictionaryd的用法
  6. C#工业触摸屏上位机源码 替代传统plc搭载的触摸屏
  7. python笔记 基础语法·第14课 【三局两胜角斗场小游戏,类与对象】
  8. android graphview使用
  9. 浅谈卡尔曼滤波(Kalman Filter)(一)
  10. 【学习笔记】[省选联考 2023] 填数游戏