容器自动化(六):容器服务的监控,注册与发现

一,容器监控系统介绍
1.1 Influxdb

InfluxDB是一个开源的没有外部依赖的时间序列数据库。适用于记录度量,事件及执行分析。

特性
内置HTTP API,所以不用再写服务端代码来启动和运行
数据可以被标记,允许非常灵活的查询
类似SQL的查询语句
安装和管理简单,数据输入和输出速度块
它旨在实时响应查询。这意味着point数据写入即被索引并立即可供响应时间应小于100ms的查询使用。
容器自动化(六):容器服务的监控,注册与发现
1.2 cAdvisor

cAdvisor是一款google开源的数据收集工具。但是它默认只显示实时数据,不储存历史数据。因此,为了存储和显示历史数
容器自动化(六):容器服务的监控,注册与发现

1.3 Grafana

Grafana是一个开源的度量分析与可视化套件。经常被用作基础设施的时间序列数据和应用程序分析的可视化,它在其他领域也被广泛的使用包括工业传感器,家庭自动化,天气和过程控制等。
Grafana支持许多不同的数据源。每个数据源都有一个特定的查询编辑器,该编辑器定制的特性和功能是公开的特定数据来源。
官方支持以下数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB。
每个数据源的查询语言和能力都是不同的。你可以把来自多个数据源的数据组合到一个仪表板,但每一个面板被绑到一个特定的数据源,它就属于一个特定的组织

容器自动化(六):容器服务的监控,注册与发现
1.4 Influxdb+cAdvisor+Grafana组合

   段落引用

二,构建容器监控系统入门
2.1 安装及配置docker-ce
主机名 IP 用途
registrator-server 192.168.200.155 容器监控服务器

[[email protected] ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[[email protected] ~]# curl https://download.docker.com/linux/centos/docker-    ce.repo -o /etc/yum.repos.d/docker-ce.repo
[[email protected] ~]# yum -y install docker-ce
[[email protected] ~]# systemctl start docker
[[email protected] ~]# systemctl enable docker
[[email protected] ~]# cat /etc/docker/daemon.json
{
"registry-mirrors":[ "https://registry.docker-cn.com" ]
}
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl restart docker

2.2 下载和启动Influxdb

 [[email protected] ~]# docker pull tutum/influxdb
[[email protected] ~]# docker network create monitor
 45fb9fc751dc3d9a037523a0e873301df336f42687ea96e4847089b8e0399a27
[[email protected] ~]# docker network ls
   NETWORK ID          NAME                DRIVER              SCOPE
  ff764a696839        bridge              bridge              local
 f90ce5fee1ea        host                host                local
45fb9fc751dc        monitor             bridge              local
1fc63d273d63        none                null                local
[[email protected] ~]# docker run -d --name influxdb --net monitor -p 8083:8083 -p 8086:8086 tutum/influxdb
 [[email protected] ~]# docker ps -a
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                PORTS                                            NAMES
7978e4313360        tutum/influxdb      "/run.sh"           7 seconds ago       Up 7 seconds        0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp   influxdb

通过浏览器访问http://192.168.200.155:8083
进入Influxdb的管理界面,如下:

容器自动化(六):容器服务的监控,注册与发现
8083端口是默认的Influxdb的图形界面管理接口
8086端口是默认的Influxdb的数据连接端口
容器自动化(六):容器服务的监控,注册与发现
2.3 下载和启动cAdvisor

[[email protected] ~]# docker pull google/cadvisor
    Using default tag: latest
    latest: Pulling from google/cadvisor
    ab7e51e37a18: Pull complete 
    a2dc2f1bce51: Pull complete 
    3b017de60d4f: Pull complete 
     Digest:           sha256:9e347affc725efd3bfe95aa69362cf833aa810f84e6cb9eed1cb65c35216632a
    Status: Downloaded newer image for google/cadvisor:latest
[[email protected] ~]# docker images
     REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
     google/cadvisor     latest              75f88e3ec333        7 months ago        62.2MB
     tutum/influxdb      latest              c061e5808198        21 months ago       290MB
[[email protected] ~]# docker run -d --name=cadvisor --net monitor -p 8081:8080 --mount        type=bind,src=/,dst=/rootfs,ro --mount type=bind,src=/var/run,dst=/var/run --mount type=bind,src=/sys,dst=/sys,ro --mount type=bind,src=/var/lib/docker/,dst=/var/lib/docker,ro google/cadvisor -storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_host=influxdb:8086
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                PORTS                                            NAMES
a5ac810be58a        google/cadvisor     "/usr/bin/cadvisor -…"   44 seconds ago      Up 43 seconds       0.0.0.0:8081->8080/tcp                           cadvisor
7978e4313360        tutum/influxdb      "/run.sh"                33 minutes ago      Up 33 minutes       0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp   influxdb

通过浏览器访问http://192.168.200.155:8081
进入cAdvisor管理界面,如下:

容器自动化(六):容器服务的监控,注册与发现
2.4 下载和启动Grafana

[[email protected] ~]# docker pull grafana/grafana
    Using default tag: latest
    latest: Pulling from grafana/grafana
    be8881be8156: Pull complete 
    728ffd1b8130: Pull complete 
    426111690cea: Pull complete 
    Digest:      sha256:b5591419cfa3a930cecdddff0a338c03296d29b617d9f340dc72ee839dd1c5be
    Status: Downloaded newer image for grafana/grafana:latest
[[email protected] ~]# docker run -d --name grafana --net monitor -p 3000:3000 grafana/grafana
    24c13bd99f1a01cb05f87f41e124f2fc8bbc1af5d9375b69d735b914d070c55f
[[email protected] ~]# docker ps -a
     CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
     24c13bd99f1a        grafana/grafana     "/run.sh"                8 seconds ago       Up 7      seconds        0.0.0.0:3000->3000/tcp                           grafana
     a5ac810be58a        google/cadvisor     "/usr/bin/cadvisor -…"   2 hours ago         Up 2 hours          0.0.0.0:8081->8080/tcp                           cadvisor
     7978e4313360        tutum/influxdb      "/run.sh"                2 hours ago         Up 2 hours          0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp   influxdb

通过浏览器访问http://192.168.200.155:3000
默认用户:admin 默认密码:admin
进入Grafana管理界面,如下:

容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
三,配置容器监控系统
3.1 向Influxdb中创建一个库"cadvisor",用来给cadvisor来存储数据
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
3.2 向Influxdb中创建一个用户,用来给cadvisor来访问

用户:grafana
密码:grafana
容器自动化(六):容器服务的监控,注册与发现
3.3 在Grafana中添加数据库源地址
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
3.4 创建一个仪表盘
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
四,容器服务的自动注册与发现
4.1 什么是微服务
微服务是众多能够独立运行,独立部署,独立提供访问的服务程序。
这些独立的程序可以单独运行提供某方面的服务,也可以通过分布式的方式调用各自提供的API接口进行集群组合式服务。
就如同之前我们安装的容器监控系统,它是通过InfluxDB+cAdvisor+Grafana组合来实现的。这三个软件服务都可以独立部署,独立运行,并都有独立提供外部访问的web界面。可以分拆来使用(搭配别的微服务软件进行组合),也可以通过各自的API接口进行串联集群式访问。

容器自动化(六):容器服务的监控,注册与发现
4.2 什么是服务发现

微服务的框架体系中,服务发现是不能不提的一个模块。我们来看下图:
容器自动化(六):容器服务的监控,注册与发现
**上图中,左图的客户端的一个借口,需要调用服务A-D。客户端必须要知道所有服务的网络位置的,以往的做法是配置在配置文件中,或者有些配置在数据库中。这里就带出几个问题:
需要配置N个服务的网络位置,加大配置的复杂性
服务的网络位置变化,都需要改变每个调用者的配置
集群的情况下,难以做负载(反向代理的方式除外)

总结起来一句话:服务多了,配置很麻烦,问题多多

上图中,右图多加了个服务发现模块。服务A-D把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录下,K一般是服务名,V就是IP:PORT。服务发现模块定时的轮询查看这些服务能不能访问的了(这就是健康检查)。客户端在调用服务A-D的时候,就跑去服务发现模块问下他们的网络位置,然后再调用他们的服务。这样的方式是不是就可以解决之前的问题了呢?客户端完全不需要记录这些服务网络位置,客户端和服务端完全解耦!**

4.3 Consul 是什么?
Consul是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由HashiCorp公司用Go语言开发,基于Mozilla Public License 2.0的协议进行开源。Consul支持健康检查,并允许HTTP和DNS协议调用API存储键值对。
4.4 Consul的使用场景

docker实例的注册与配置共享
coreos实例的注册与配置共享
vitess集群
SaaS应用的配置共享
与confd服务集成,动态生成nginx和haproxy配置文件

4.5 Consul的优势:
使用Raft算法来保证一致性,比复杂的Paxos算法更直接。相比较而言,zookeeper采用的是Paxos,而etcd使用的则是Raft
支持多数据中心,内外网的服务采用不同的端口进行监听。多数据中心集群可以避免单数据中心的单点故障,而其部署则需要考虑网络延迟,分片等情况。zookeeper和etcd均不提供多数据中心功能的支持。
支持健康检查,etcd不提供此功能。
支持http和dns协议接口。zookeeper的集成较为复杂,etcd只支持http协议。
官方提供web管理界面,etcd无此功能。

综合比较,Consul作为服务注册和配置管理的新星,比较值得关注和研究.

4.6 Consul服务器端的安装和部署
consul下载地址:https://www.consul.io/downloads.html
主机名 IP 用途
registrator-server 192.168.200.155 consul注册服务器
#解压安装consul
[[email protected] ~]# tar xf consul_1.2.1_linux_amd64.tar.gz
[[email protected] ~]# mv consul /usr/bin/
[[email protected] ~]# ll /usr/bin/consul
-rw-r–r-- 1 root root 56272838 7月 28 23:57 /usr/bin/consul
[[email protected] ~]# chmod +x /usr/bin/consul
#部署并启动consul
[[email protected] ~]# consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.200.155 -client=0.0.0.0 -node=server01 &>/var/log/consul.log &
[[email protected] ~]# netstat -antup | grep consul
tcp 0 0 192.168.200.155:8300 0.0.0.0:* LISTEN 18866/consul
tcp 0 0 192.168.200.155:8301 0.0.0.0:* LISTEN 18866/consul
tcp 0 0 192.168.200.155:8302 0.0.0.0:* LISTEN 18866/consul
tcp6 0 0 :::8500 ::???? LISTEN 18866/consul #这是对外访问端口
tcp6 0 0 :::8600 ::???? LISTEN 18866/consul
udp 0 0 192.168.200.155:8301 0.0.0.0:* 18866/consul
udp 0 0 192.168.200.155:8302 0.0.0.0:* 18866/consul
udp6 0 0 :::8600 ::???? 18866/consul

浏览器访问192.168.200.155:8500
容器自动化(六):容器服务的监控,注册与发现
4.7 consul的基本使用

(1)通过Linux命令获取信息
1)通过Linux命令获取信息

#查看集群节点信息
[[email protected] ~]# consul members
Node Address Status Type Build Protocol DC Segment
server01 192.168.200.155:8301 alive server 1.2.1 2 dc1
#查看集群节点的服务端位置
[[email protected] ~]# consul info | grep leader
leader = true
leader_addr = 192.168.200.155:8300
#查看集群注册的服务信息
[[email protected] ~]# consul catalog services
consul

(2)本地模拟进行注册

[[email protected] ~]# curl -X PUT -d ‘{“id”:“jetty”,“name”:“service_name”,“adress”:“192.168.200.155”,“port”:8080,“tags”:[“test”],“checks”:[{“http”:“http://192.168.200.155:8080/",“interval”:"5s”}]}’ http://192.168.200.155:8500/v1/agent/service/register

        ![](http://i2.51cto.com/images/blog/201812/10/3af0f6c599acfb1c67465b81cd50c4f9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)  

容器自动化(六):容器服务的监控,注册与发现
(3)通过HTTP API获取集群信息

查看集群节点成员

[[email protected] ~]# curl 192.168.200.155:8500/v1/status/peers
           ["192.168.200.155:8300"]

查看集群服务端位置

[[email protected] ~]# curl 192.168.200.155:8500/v1/status/leader

"192.168.200.155:8300"
#查看注册的所有服务
[[email protected] ~]# curl 192.168.200.155:8500/v1/catalog/services
{“consul”:[]}
#查看nginx服务信息
curl 192.168.200.155:8500/v1/catalog/services/nginx
#集群节点详细信息
[[email protected] ~]# curl 192.168.200.155:8500/v1/catalog/nodes
[{“ID”:“5adbe28d-fae0-8e45-e91e-abcb7fdf468a”,“Node”:“server01”,“Address”:“192.168.200.155”,“Datacenter”:“dc1”,“TaggedAddresses”:{“lan”:“192.168.200.155”,“wan”:“192.168.200.155”},“Meta”:{“consul-network-segment”:""},“CreateIndex”:5,“ModifyIndex”:6}]

五,Docker+Registrator+Consul实现容器服务自动加入Nginx集群

consul-template:一个守护程序,用于实时查询consul集群数据,并更新文件系统上的任意数量的指定模板,生成配置文件,更新完成后可以选择运行任何Shell命令。gliderlabs/registrator:检查容器运行状态自动注册和注销Docker容器的服务到服务配置中心。目前支持Consul,etcd和SkyDNS2。

https://github.com/hashicorp/consul-template
容器自动化(六):容器服务的监控,注册与发现
主机名 IP 用途
registrator-server 192.168.200.155 consul注册服务器
nginx-LB 192.168.200.156 nginx反向代理服务器
docker-client 192.168.200.142 nginxWeb节点服务器

[[email protected] ~]# ls
   anaconda-ks.cfg  consul-template_0.19.3_linux_amd64.zip  nginx-1.10.2.tar.gz
[[email protected] ~]# unzip consul-template_0.19.3_linux_amd64.zip 
          Archive:  consul-template_0.19.3_linux_amd64.zip
          inflating: consul-template         
[[email protected] ~]# ls
    anaconda-ks.cfg  consul-template  consul-template_0.19.3_linux_amd64.zip  nginx- 1.10.2.tar.gz
[[email protected] ~]# mv consul-template /usr/bin/
[[email protected] ~]# which consul-template
           /usr/bin/consul-template

5.2 nginx-LB服务器安装nginx
#编译安装nginx
[[email protected] ~]# yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
[[email protected] ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[[email protected] src]# cd /usr/src/nginx-1.10.2/
[[email protected] nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install

5.3 nginx-LB配置consul服务支持的nginx配置文件模板
#制作consul支持的nginx配置文件模板
[[email protected] conf]# mkdir -p /consul-tml
[[email protected] conf]# cd /consul-tml/
[[email protected] conf]# cat /consul-tml/nginx.ctmpl
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http_backend {
ip_hash;
{{ range service “nginx” }} #获取服务nginx
server {{ .Address }}:{{ .Port }}; #循环罗列所属服务的IP和端口
{{ end }}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
}
#启动consul-template
[[email protected] ~]# nohup consul-template -consul-addr 192.168.200.155:8500 -template /consul-tml/nginx.ctmpl:/usr/local/nginx/conf/nginx.conf:"/usr/local/nginx/sbin/nginx -s reload" 2>&1 >/consul-tml/consul-template.log &
[1] 14174
#查看nginx配置文件
[[email protected] conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http_backend { #还没有任何容器节点注册,因此这里没东西
ip_hash;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
}
[[email protected] ~]# netstat -antup | grep nginx
#配置文件里没有web节点因此nginx没有启动成功

5.4 在Docker客户端上启动nginxWeb容器作为节点,并启动registrator进行服务注册:
#开启ipv4网卡转发
[[email protected] ~]# echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf
[[email protected] ~]# sysctl -p
net.ipv4.ip_forward = 1
#下载并启动nginxWeb01容器节点
[[email protected] ~]# docker pull nginx
[[email protected] ~]# mkdir -p /www/html
[[email protected] ~]# echo "hostname -I www.yunjisuan.com" >> /www/html/index.html
[[email protected] ~]# docker run -dit --name nginxWeb01 -p 80:80 --mount type=bind,src=/www/html,dst=/usr/share/nginx/html nginx
c04b2b3f148026b3b9a719555f9a6bdf5ee74425831e8da3843daa66da98de44
#模拟web访问
[[email protected] ~]# curl localhost
192.168.200.142 172.17.0.1 www.yunjisuan.com
#下载并启动registrator容器进行自动注册
[[email protected] ~]# docker pull gliderlabs/registrator
[[email protected] ~]# docker run -d --name=registrator -v /var/run/docker.sock:/tmp/docker.sock --restart=always gliderlabs/registrator:latest -ip=192.168.200.142 consul://192.168.200.155:8500

浏览器查看http://192.168.200.155:8500
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
容器自动化(六):容器服务的监控,注册与发现
在nginx-LB上查看nginx反向代理配置文件渲染情况
#如果没有启动nginx,需要先启动
[[email protected] consul-tml]# /usr/local/nginx/sbin/nginx
[[email protected] conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http_backend {
ip_hash;
server 192.168.200.142:80; #已经有注册的web容器地址了
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
}
[[email protected] conf]# netstat -antup | grep nginx #nginx服务也启动了
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14517/nginx: master

5.5 在docker客户端上继续增加nginxWeb容器节点,进行验证

[[email protected] ~]# docker run -dit --name nginxWeb02 -p 81:80 --mount type=bind,src=/www/html,dst=/usr/share/nginx/html nginx
[[email protected] ~]# docker run -dit --name nginxWeb03 -p 82:80 --mount type=bind,src=/www/html,dst=/usr/share/nginx/html nginx
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fcf819ad1a9 nginx “nginx -g 'daemon of…” 13 minutes ago Up 25 seconds 0.0.0.0:82->80/tcp nginxWeb03
39ebb234056f nginx “nginx -g 'daemon of…” 17 minutes ago Up 27 seconds 0.0.0.0:81->80/tcp nginxWeb02
a41c7a48a967 gliderlabs/registrator:latest “/bin/registrator -i…” 38 minutes ago Up 38 minutes registrator
829e474b4770 nginx “nginx -g 'daemon of…” 41 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp nginxWeb01

查看consul服务器端web界面
容器自动化(六):容器服务的监控,注册与发现
查看反向代理配置文件渲染情况
[[email protected] conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http_backend {
ip_hash;
server 192.168.200.142:80;
server 192.168.200.142:81;
server 192.168.200.142:82;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
}
[[email protected] conf]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14517/nginx: master

通过浏览器访问nginx-LB的80端口验证反向代理的功能是否正常
容器自动化(六):容器服务的监控,注册与发现
5.6 验证consul注册服务的健康检查功能
#停止nginxWeb02和nginxWeb03的容器进程
[[email protected] ~]# docker stop nginxWeb03
nginxWeb03
[[email protected] ~]# docker stop nginxWeb02
nginxWeb02
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a41c7a48a967 gliderlabs/registrator:latest “/bin/registrator -i…” 43 minutes ago Up 43 minutes registrator
829e474b4770 nginx “nginx -g 'daemon of…” About an hour ago Up 9 minutes 0.0.0.0:80->80/tcp nginxWeb01
#查看nginx-LB配置文件渲染情况
[[email protected] conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream http_backend {
ip_hash;
server 192.168.200.142:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
}
[[email protected] conf]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14517/nginx: master
浏览器查看consul注册服务的web界面
容器自动化(六):容器服务的监控,注册与发现