Linux——Docker实现服务发现

Docker实现服务发现

环境:

IP 主机名 服务
192.168.1.40 Master Consul、Nginx
192.168.1.41 node01 registrator
192.168.1.42 node02 registrator

PS:关闭每台防火墙、SELinux、主机名不能冲突

实现步骤:

一、启动Consul服务

1.下载consul包并启动

[root@master ~]# wget https://releases.hashicorp.com/consul/1.8.4/consul_1.8.4_linux_amd64.zip
[root@master ~]# unzip consul_1.8.4_linux_amd64.zip 
Archive:  consul_1.8.4_linux_amd64.zip
  inflating: consul                  
[root@master ~]# mv consul /usr/local/bin/
[root@master ~]# chmod  +x /usr/local/bin/consul 
[root@master ~]# nohup consul  agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.1.40 -client=0.0.0.0 -node=master &

命令解释:

  • nohup和&:保持在后台运行此命令
  • -bootstrap:加入这个选项时,一般都在server单节点的时候用,自选举为leader.
  • -ui :开启内部的web页面
  • -data-dir:key/volume数据存储位置
  • -bind: 指定开启服务的IP
  • –client: 指定访问的客户端
  • -node:指定集群内通信使用的名称。默认是主机名

Consul开启的端口

  • 8300:集群节点
  • 8301:集群内部的访问
  • 8302:夸数据中心的通信
  • 8500:web ui 界面
  • 8600:使用dns协议查看节点信息的端口

2.查看consul的信息

[root@master ~]# consul  info
agent:
	check_monitors = 0
	check_ttls = 0
	checks = 0
	services = 0
build:
	prerelease = 
	revision = 12b16df3
	version = 1.8.4
consul:
	acl = disabled
	bootstrap = true
	known_datacenters = 1
	leader = true
	leader_addr = 192.168.1.40:8300
raft:
	applied_index = 7
	commit_index = 7
	fsm_pending = 0
	last_contact = 0
	last_log_index = 7
	last_log_term = 2
	last_snapshot_index = 0
	last_snapshot_term = 0
	latest_configuration = [{
    
    Suffrage:Voter ID:6759c618-5dbd-69b1-a3b1-201e1bf9625b Address:192.168.1.40:8300}]
	latest_configuration_index = 0
	num_peers = 0
	protocol_version = 3
	protocol_version_max = 3
	protocol_version_min = 0
	snapshot_version_max = 1
	snapshot_version_min = 0
	state = Leader
	term = 2
runtime:
	arch = amd64
	cpu_count = 2
	goroutines = 86
	max_procs = 2
	os = linux
	version = go1.14.6
serf_lan:
	coordinate_resets = 0
	encrypted = false
	event_queue = 1
	event_time = 2
	failed = 0
	health_score = 0
	intent_queue = 0
	left = 0
	member_time = 1
	members = 1
	query_queue = 0
	query_time = 1
serf_wan:
	coordinate_resets = 0
	encrypted = false
	event_queue = 0
	event_time = 1
	failed = 0
	health_score = 0
	intent_queue = 0
	left = 0
	member_time = 1
	members = 1
	query_queue = 0
	query_time = 1

3.查看consul集群内成员的信息

[root@master ~]# consul  members
Node    Address            Status  Type    Build  Protocol  DC   Segment
master  192.168.1.40:8301  alive   server  1.8.4  2         dc1  <all>

二、加入Consul集群

PS:node01和node02加入consul集群

1.容器运行consul服务

node01

[root@node01 ~]# docker run -d --name consul -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.1.40 -advertise 192.168.1.41 -client 0.0.0.0 --node=node01

node02

[root@node01 ~]# docker run -d --name consul -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.1.40 -advertise 192.168.1.42 -client 0.0.0.0 --node=node02

2.访问consul服务,验证群集信息
在这里插入图片描述

三、部署registrator服务

registrator是一个能自动发现docker container提供的服务,并在后端服务注册中心注册服务或取消服务的工具,后端注册中心支持conusl、etcd、skydns2、zookeeper等。

node01

[root@node01 ~]# docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.41:8500

node02

[root@node02 ~]# docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.42:8500

四、部署Nginx服务

Nginx官网

Master

[root@master ~]# yum -y install gcc openssl openss-devel zlib zlib-devel pcre pcre-devel
[root@master ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@master ~]# cd nginx-1.18.0/
[root@master nginx-1.18.0]# useradd -M -s /sbin/nologin nginx
[root@master nginx-1.18.0]# ./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module 
[root@master nginx-1.18.0]# make && make install
[root@master nginx-1.18.0]# make && make install^C
[root@master nginx-1.18.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@master nginx-1.18.0]# nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@master nginx-1.18.0]# nginx

node01

[root@node01 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
d121f8d1c412: Pull complete 
ebd81fc8c071: Pull complete 
655316c160af: Pull complete 
d15953c0e0f8: Pull complete 
2ee525c5c3cc: Pull complete 
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for nginx:latest
[root@node01 ~]# docker run  -itd --name  web01 -p 80 nginx:latest 
d1d3154f8dbf8179f20d03ffd0bb64d24485d4f92b7c8ee8523b437bfb9c1f8d
[root@node01 ~]# docker run  -itd --name  web02 -p 80 nginx:latest 
172c7c48b3ef746bceafe277320399004e3c7c7e860bfcb1d80825270dce947b
[root@node01 ~]# docker exec  -it web01 bash
root@d1d3154f8dbf:/# echo web1 > /usr/share/nginx/html/index.html 
root@d1d3154f8dbf:/# exit
exit
[root@node01 ~]# docker exec  -it web02 bash
root@172c7c48b3ef:/# echo web2 > /usr/share/nginx/html/index.html 
root@172c7c48b3ef:/# exit
exit

node02

[root@node02 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
d121f8d1c412: Pull complete 
ebd81fc8c071: Pull complete 
655316c160af: Pull complete 
d15953c0e0f8: Pull complete 
2ee525c5c3cc: Pull complete 
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for nginx:latest
[root@node02 ~]# docker run  -itd --name  web03 -p 80 nginx:latest 
71fd86364d22558d631e876af70667974e08b42906fb79408c4471260742b667
[root@node02 ~]# docker run  -itd --name  web04 -p 80 nginx:latest 
d213cf0ea9e0e208ddd1f92f386ef95ccfb7abdbcade40b5794e752aba89a9df
[root@node02 ~]# docker exec  -it web03 bash
root@71fd86364d22:/# echo web3 > /usr/share/nginx/html/index.html 
root@71fd86364d22:/# exit
exit
[root@node02 ~]# docker exec  -it web04 bash
root@d213cf0ea9e0:/# echo web4 > /usr/share/nginx/html/index.html 
root@d213cf0ea9e0:/# exit
exit

五、更改Nginx配置文件

[root@master ~]# cd /usr/local/nginx/
[root@master nginx]# mkdir consul
[root@master nginx]# cd consul/
[root@master consul]# vim nginx.ctmpl
upstream http_backend {
    
    
	{
    
    {
    
    range service "nginx"}}
	server {
    
    {
    
     .Address }}:{
    
    {
    
     .Port }};
	{
    
    {
    
     end }}
} 

server {
    
    
	listen 8000;
	server_name localhost;
	location / {
    
    
		proxy_pass http://http_backend;
	}
}
#在nginx的配置文件的最后添加
[root@master consul]# vim /usr/local/nginx/conf/nginx.conf
.......
    #    }
    #}
    include  /usr/local/nginx/consul/*.conf;
}
[root@master consul]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@master consul]# nginx -s reload

六、安装consul-template命令

下载consul-template包

[root@master ~]# wget https://releases.hashicorp.com/consul-template/0.25.1/consul-template_0.25.1_linux_amd64.zip

安装

[root@master ~]# unzip consul-template_0.25.1_linux_amd64.zip 
Archive:  consul-template_0.25.1_linux_amd64.zip
  inflating: consul-template         
[root@master ~]# mv consul-template /usr/local/bin/
[root@master ~]# chmod  +x /usr/local/bin/consul-template 

使用consul-template 命令,根据模板生产新的配置文件,并重新加载nginx的配置文件。

[root@master ~]# nohup consul-template -consul-addr 192.168.1.40:8500 -template "/usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/sbin/nginx -s reload" &

此时,能够看到,新生产的vhost.conf配置文件已经生效,访问本机8000端口可以得到不同容器提供的服务

[root@master ~]# cd /usr/local/nginx/consul/
[root@master consul]# cat vhost.conf 
upstream http_backend {
    
    
	
	server 192.168.1.41:32768;
	
	server 192.168.1.41:32769;
	
	server 192.168.1.42:32768;
	
	server 192.168.1.42:32769;
	
} 

server {
    
    
	listen 8000;
	server_name localhost;
	location / {
    
    
		proxy_pass http://http_backend;
	}
}

[root@master consul]# curl  localhost:8000
web1
[root@master consul]# curl  localhost:8000
web2
[root@master consul]# curl  localhost:8000
web3
[root@master consul]# curl  localhost:8000
web4

这时不管后端是新添加nginx的web容器,或是删除,新生产的配置文件都会自动的更新,这时我们在运行consul-template这条命令最后添加: /usr/local/sbin/nginx -s reload 它的作用。

PS:添加一个web05,看会不会自动更新?

[root@node01 ~]# docker run -itd --name  web05 -p 80 nginx:latest 
af28594aad8ff989307e27a72ded3dbdd74a7950853fb5a8a441538919cd56fd
[root@node01 ~]# docker exec  -it web05 bash
root@af28594aad8f:/# echo web05 > /usr/share/nginx/html/index.html 
root@af28594aad8f:/# exit
exit

查看:

[root@master consul]# curl  localhost:8000
web1
[root@master consul]# curl  localhost:8000
web2
[root@master consul]# curl  localhost:8000
web05
[root@master consul]# curl  localhost:8000
web3
[root@master consul]# curl  localhost:8000
web4

猜你喜欢

转载自blog.csdn.net/weixin_45191791/article/details/108734290