In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Suppose the code we write invokes the service of REST API or Thrift API. To complete a request, the code needs to know the network location (IP address and port) of the service instance.
In traditional applications running on physical hardware, the network location of the service instance is relatively fixed, and the code can read the network location from an occasionally updated configuration file.
For cloud-based, modern micro-service applications, this is a big problem. When a container application is deployed to a cluster, its service address is dynamically assigned by the cluster system. So, when we need to access this service, how do we determine its address? At this point, service discovery (Service Discovery) is needed.
Docker+Consul+registrator implements service discovery
Experimental environment
Docker01 192.168.2.10
Docker02 192.168.2.20
Docker03 192.168.2.30
Turn off the firewall and selinux. Consul_1.5.1_linux_amd64.zip
[root@docker01 ~] # unzip consul_1.5.1_linux_amd64.zip
[root@docker01 ~] # mv consul / usr/local/bin/
[root@docker01 ~] # chmod + x / usr/local/bin/consul
[root@docker01 ~] # consul-- help # # confirm that the command is available
1) start the consul service on docker01
/ / start consul
[root@docker01 ~] # consul agent-server-bootstrap\
-ui-data-dir=/var/lib/consul-data\
-bind=192.168.2.10\
-client=0.0.0.0\
-node=master
Background operation
[root@docker01] # nohup consul agent-server-bootstrap-ui-data-dir=/var/lib/consul-data-bind=192.168.2.10-client=0.0.0.0-node=master &
[1] 17633
[root@docker01 ~] # nohup: ignore input and append output to "nohup.out"
PS://-bootstrap: when this option is added, it is usually used when server is single node, and it is elected as leader since.
-ui: open the internal web page-data-dir:key/volum data storage location
-bind: specify the IP to enable the service
-client: specify the client to access
-node: specifies the name used for communication within the cluster. The default is the hostname
PS: open port
8300 Cluster nodes
8301 access within the cluster
8302 Communication across data centers
8500 web ui interface
8600 Port that uses dns protocol to view node information
/ / View the information of consul
[root@docker01 ~] # consul info
Leader_addr = 192.168.2.10 purl 8300
/ / View the information of the members in the cluster
[root@docker01 ~] # consul members
Node Address Status Type Build Protocol DC Segment
Master 192.168.2.10:8301 alive server 1.5.1 2 dc1
2) docker02, docker03, join the consul cluster
Here we use a container to run the consul service. Myprogrium-consul.tar
Docker load
< myprogrium-consul.tar 报错重启docker: Systemctl restart docker [root@docker02 ~]# docker run -d --name consul -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.2.10 -advertise 192.168.2.20 -client 0.0.0.0 -node=node01 [root@docker03 ~]# docker run -d --name consul -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.2.10 -advertise 192.168.2.30 -client 0.0.0.0 -node=node02 [root@docker01 ~]# consul members ##可查询到3台主机 浏览器访问本机的8500端口 3)下载部署consul-template consul-template_0.19.5_linux_amd64.zip [root@docker01 ~]# unzip consul-template_0.19.5_linux_amd64.zip [root@docker01 ~]# mv consul-template /usr/local/bin/ [root@docker01 ~]# chmod +x /usr/local/bin/consul-template 4)docker02、docker03上部署registrator服务 registrator是一个能自动发现docker container提供的服务,并在后端服务注册中心注册服务或取消服务的工具,后端注册中心支持conusl、etcd、skydns2、zookeeper等。 myregistrator.tar [root@docker02 ~]# docker load < myregistrator.tar [root@docker02 ~]# docker run -d \ --name registrator \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart always \ gliderlabs/registrator \ consul://192.168.2.20:8500 [root@docker03 ~]# docker load < myregistrator.tar [root@docker03 ~]# docker run -d \ --name registrator \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart always \ gliderlabs/registrator \ consul://192.168.2.30:8500 Docker2上创建一个nginx容器 [root@docker02 ~]# docker run -d -P --name test nginx 验证conusl上的nginx服务 5)docker01部署一个ngixn服务 [root@docker01 ~]# yum -y install gcc openssl openssl-devel zlib zlib-devel pcre pcre-devel [root@docker01 ~]# useradd -M -s /sbin/nologin nginx [root@docker01 ~]# tar zxf nginx-1.14.0.tar.gz [root@docker01 ~]# cd nginx-1.14.0/ nginx-1.14.0.tar.gz [root@docker01 nginx-1.14.0]# ./configure --user=nginx --group=nginx \ --with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module [root@docker01 nginx-1.14.0]# make && make install [root@docker01 nginx-1.14.0]# ln -s /usr/local/nginx/sbin/* /usr/local/bin/ [root@docker01 nginx-1.14.0]# nginx -t [root@docker01 nginx-1.14.0]# nginx PS:这里nginx作为反向代理,代理后端docker02、docker03上nginx的容器服务,所以我们先去docker02、docker03上部署一些服务,为了方便等会回看到负载的效果,所以,我们运行完成容器之后,做一个主界面内容的区分 Docker02:web01 web02 [root@docker02 ~]# docker run -itd --name web01 -P nginx:latest [root@docker02 ~]# docker exec -it web01 /bin/bash root@b47619f3f7ae:/# echo The web container in docker02-web01 >/ usr/share/nginx/html/index.html
[root@docker02] # docker run-itd-- name web02-P nginx:latest
[root@docker02 ~] # docker exec-it web02 / bin/bash
Root@89cc41040e33:/# echo The web container in docker02-web02 > / usr/share/nginx/html/index.html
Docker03:web03 web04
[root@docker03] # docker run-itd-- name web03-P nginx:latest
[root@docker03 ~] # docker exec-it web03 / bin/bash
Root@3f0d20853b0b:/# echo The web container in docker03-web03 > / usr/share/nginx/html/index.html
[root@docker03] # docker run-itd-- name web04-P nginx:latest
[root@docker03 ~] # docker exec-it web04 / bin/bash
Root@79168d0aa77f:/# echo The web container in docker03-web04 > / usr/share/nginx/html/index.html
Change the profile of the nginx service
[root@docker01 ~] # cd / usr/local/nginx/
[root@docker01 nginx] # mkdir consul
[root@docker01 nginx] # cd consul/
[root@docker01 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;
}
}
[root@docker01 nginx] # cd.. / conf/
[root@docker01 conf] # vim nginx.conf
Include / usr/local/nginx/consul/*.conf
/ / the main configuration file of nginx can recognize the new
[root@docker01 conf] # consul-template-consul-addr 192.168.2.10 template-template "/ usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/bin/nginx-s reload"
Background operation
[root@docker01 conf] # nohup consul-template-consul-addr 192.168.2.10 template-template "/ usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/bin/nginx-s reload" &
[root@docker01 conf] # nohup: ignore input and append output to "nohup.out"
[root@docker01 consul] # pwd
/ usr/local/nginx/consul
[root@docker01 consul] # ls # # generate a vhost.conf file
Nginx.ctmpl vhost.conf
[root@docker01 consul] # cat vhost.conf
Check to see if port 8000 is up
[root@docker01 consul] # ss-lnt
Did not get up, restart nginx
[root@docker01 consul] # nginx-s reload
Access port 8000 of this machine
[root@docker01 ~] # curl 127.0.0.1 purl 8000
Of course: no matter whether the backend is a newly added nginx web container or deleted, the newly produced configuration file will be updated in real time. This is the template we are adding by running the command consul-template.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.