《打造高可用监控系统》之——CentOS在Docker环境下安装Grafana及Grafana Image Renderer(附Docker环境安装步骤)

前言

写完《Grafana Alert通过Ceph的S3兼容接口在推送webhook报警时同时渲染图片并带上imageUrl参数》这篇文章后,我想很多像我一样的新手都会开始恐惧安装Docker的繁琐;其实装好之后会感觉到实际一点儿都不难
所以另外写这篇文章,用于跟大家交流一下自己在一台机器上安装docker环境的过程

环境

  • CentOS Linux release 7.4.1708 (Core)
[root@01 ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 

流程

Created with Raphaël 2.2.0 开始 安装Docker-CE 安装 Docker Compose 添加DockerHub国内源 结束

安装Docker-ce

最开始向借花献佛,直接使用DaoCloud的脚本,结果失败了。
找到了清华大学开源软件站的docker-ce镜像帮助文档,手动来执行

安装依赖

[root@01 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
……省略部分内容
Updated:
  device-mapper-persistent-data.x86_64 0:0.8.5-1.el7                                                                           lvm2.x86_64 7:2.02.185-2.el7_7.2                                                                          

Dependency Updated:
  device-mapper.x86_64 7:1.02.158-2.el7_7.2 device-mapper-event.x86_64 7:1.02.158-2.el7_7.2 device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2 device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 lvm2-libs.x86_64 7:2.02.185-2.el7_7.2

Complete!

下载repo文件

[root@01 ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
--2020-04-27 16:38:38--  https://download.docker.com/linux/centos/docker-ce.repo
Resolving download.docker.com (download.docker.com)... 13.32.53.11, 13.32.53.119, 13.32.53.78, ...
Connecting to download.docker.com (download.docker.com)|13.32.53.11|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2424 (2.4K) [binary/octet-stream]
Saving to: ‘/etc/yum.repos.d/docker-ce.repo’

100%[===============================================================================================================================================================================================>] 2,424       --.-K/s   in 0.1s    

2020-04-27 16:38:40 (20.3 KB/s) - ‘/etc/yum.repos.d/docker-ce.repo’ saved [2424/2424]

[root@01 ~]# sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo

yum 安装docker-ce

到了这一步,碰到了问题,yum报错了(可能这个也是使用DaoCloud的脚本报错的原因)

[root@01 ~]# yum install docker-ce -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:19.03.8-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-19.03.8-3.el7.x86_64
--> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-19.03.8-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-19.03.8-3.el7.x86_64
--> Running transaction check
---> Package containerd.io.x86_64 0:1.2.13-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.2.13-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:19.03.8-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-19.03.8-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:19.03.8-3.el7 will be installed
--> Finished Dependency Resolution
Error: Package: 3:docker-ce-19.03.8-3.el7.x86_64 (docker-ce-stable)
           Requires: container-selinux >= 2:2.74
           Available: 2:container-selinux-2.21-1.el7.noarch (xxx)
               container-selinux = 2:2.21-1.el7
           Available: 2:container-selinux-2.21-2.gitba103ac.el7.noarch (xxx)
               container-selinux = 2:2.21-2.gitba103ac.el7
Error: Package: containerd.io-1.2.13-3.1.el7.x86_64 (docker-ce-stable)
           Requires: container-selinux >= 2:2.74
           Available: 2:container-selinux-2.21-1.el7.noarch (xxx)
               container-selinux = 2:2.21-1.el7
           Available: 2:container-selinux-2.21-2.gitba103ac.el7.noarch (xxx)
               container-selinux = 2:2.21-2.gitba103ac.el7
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

后来发现是公司内部yum的镜像服务器内版本的原因。故更换了清华大学的CentOS yum镜像。详见:清华大学开源软件镜像站:CentOS 镜像使用帮助
再次尝试安装

[root@01 yum.repos.d]# yum install docker-ce -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies

……省略过程

Installed:
  docker-ce.x86_64 3:19.03.8-3.el7                                                                                                                                                                                                       

Dependency Installed:
  container-selinux.noarch 2:2.107-3.el7                                        containerd.io.x86_64 0:1.2.13-3.1.el7                                        docker-ce-cli.x86_64 1:19.03.8-3.el7                                       

Complete!

验证

一般都用hallow-world这个镜像来验证安装情况

[root@01 yum.repos.d]# docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

一如既往地报错了。
百度一下,原来少了几个步骤

[root@01 yum.repos.d]# systemctl daemon-reload    
[root@01 yum.repos.d]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@01 yum.repos.d]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-04-27 17:05:20 CST; 8s ago
     Docs: https://docs.docker.com
 Main PID: 676733 (dockerd)
   Memory: 44.6M
   CGroup: /system.slice/docker.service
           └─676733 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.669872420+08:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.669900342+08:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock 0  <nil>}] <nil>}" module=grpc
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.669915636+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.695406910+08:00" level=info msg="Loading containers: start."
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.794767825+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be use...red IP address"
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.825618838+08:00" level=info msg="Loading containers: done."
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.841630471+08:00" level=info msg="Docker daemon" commit=afacb8b graphdriver(s)=overlay2 version=19.03.8
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.841706220+08:00" level=info msg="Daemon has completed initialization"
Apr 27 17:05:20 tv1-ops-prometheus-01 systemd[1]: Started Docker Application Container Engine.
Apr 27 17:05:20 tv1-ops-prometheus-01 dockerd[676733]: time="2020-04-27T17:05:20.854713675+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[root@01 yum.repos.d]# docker run hello-world 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

安装Docker Compose

这里借花献佛,直接用了DaoCloud的脚本

[root@tv1-ops-prometheus-01 yum.repos.d]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100   423  100   423    0     0    487      0 --:--:-- --:--:-- --:--:--   486
100 16.7M  100 16.7M    0     0   488k      0  0:00:35  0:00:35 --:--:-- 3775k
[root@tv1-ops-prometheus-01 yum.repos.d]# chmod +x /usr/local/bin/docker-compose

添加(更换)DockerHub国内源

由于DockerHub的服务器在国外,国内访问巨慢。所以这里要换一个国内源。
尝试过阿里、DaoCloud的加速器,但是速度上堪忧。这里推荐中科大的加速器(帮助文档
依旧借花献佛,用了DaoCloud的脚本

 [root@01 yum.repos.d]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s https://docker.mirrors.ustc.edu.cn/
docker version >= 1.12
{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]}
Success.
You need to restart docker to take effect: sudo systemctl restart docker 
[root@01 ~]# systemctl restart docker

这个脚本后面的url地址可以更换为任意你想使用的国内源(加速器)地址
注意:更新源之后一定要重启一下docker服务使得配置生效

验证

让我们来拉一个Grafana的镜像验证一下。先来看看最新的Grafana镜像是哪个版本的

Grafana镜像版本

有2个官方渠道可以查看:

拉取镜像

[root@tv1-ops-prometheus-01 ~]# docker pull grafana/grafana:6.7.3
6.7.3: Pulling from grafana/grafana
4167d3e14976: Pull complete 
dd2bf2ad25d9: Pull complete 
bc3026833a3a: Pull complete 
8789bc1f4250: Pull complete 
fc931efc1e71: Pull complete 
09de0f0f5c91: Pull complete 
b9833e14e8a2: Pull complete 
763366917f49: Pull complete 
Digest: sha256:b409a69218ca5990b61e33fe00121c94b2f9b5a7ec2f3118c703454c26067020
Status: Downloaded newer image for grafana/grafana:6.7.3
docker.io/grafana/grafana:6.7.3

顺带把grafana-image-renderer的镜像也一起拉过来(吐槽一下,这个包真心很大。原因是组件使用了Chrome的Linux内核来渲染图片。所以一起打包进去了)

[root@01 ~]# docker pull grafana/grafana-image-renderer:1.0.12
1.0.12: Pulling from grafana/grafana-image-renderer
aad63a933944: Pull complete 
edd41271d385: Pull complete 
dd731a721451: Pull complete 
495807fcdd37: Pull complete 
bb3e333813be: Pull complete 
0cf57668099a: Pull complete 
438d1b53e50f: Pull complete 
d7e6981d81c0: Pull complete 
ac1670af7e51: Pull complete 
d72a20b4fdf8: Pull complete 
Digest: sha256:e2e3aede3ef31e37289983eb22cc8c8049f4d76e8f5901180759312140e926a7
Status: Downloaded newer image for grafana/grafana-image-renderer:1.0.12
docker.io/grafana/grafana-image-renderer:1.0.12

Docker环境到此部署结束

部署Grafana

从这里开始,我会介绍Grafana和附加组件Grafana Image Renderer的高可用部署。大家可以参考一下下面2篇原创博文,里面有探讨一些研究细节
《Grafana Alert通过Ceph的S3兼容接口在推送webhook报警时同时渲染图片并带上imageUrl参数》
《Grafana内部配置库sqlite3迁移至MySQL(面板迁移,同时将数据从sqlite3平移至MySQL)》

最佳实践

  • 使用Docker部署最新版本Grafana镜像,并在宿主机上保留下面3个文件夹
    • conf/用于存储配置文件,方便进行灵活的数据调整
    • data/用于Grafana数据存储(如果使用外部数据库,如MySQL则不需要)
    • log/用于写日志文件
  • 使用外部存储用于存储内部数据(如图表数据),方便搭建集群及迁移
  • 使用Docker容器部署Grafana Image Rendere,并通过http方式访问

准备宿主机目录

根据“最佳实践”中第一条中所述,在宿主机内准备好如下3个目录

cd /usr/local/
mkdir grafana-docker
cd grafana-docker/
mkdir conf
mkdir data
mkdir log

由于这些目录是要映射到Grafana的容器内的,为保障读写正常,需要将用户组更改为Grafana的用户ID
ps:472:472为Grafana官方Docker镜像所使用的用户/组

chown -R 472:472 ../grafana-docker/

配置文件default.ini

在conf文件夹中新建配置文件

vim default.ini

由于我的配置文件中有很多公司内部的配置,另外网上配置文件的全文范例特别的多。我就不在这里贴全文了。有需要的伙伴可以参考如下文章
Grafana配置官方文档
Grafana配置文件default.ini全文

这里配置时注意一下grafana-image-renderer的配置,可见我的另一篇博文
《Grafana Alert通过Ceph的S3兼容接口在推送webhook报警时同时渲染图片并带上imageUrl参数》

[rendering]
# Options to configure external image rendering server like https://github.com/grafana/grafana-image-renderer
server_url = http://renderer:8081/render/
#callback_url = {grafana的根地址}
#注意:经验证callback_url 这项不填同样也可以正常使用

Docker Compose

version: '2'
services:
  grafana:
    image: index.docker.io/grafana/grafana:6.7.2
    restart: always
    ports:
    - 3000:3000
    volumes:
    #将刚刚准备好的defaults.ini直接映射到/usr/share/grafana/conf/defaults.ini路径
    - /usr/local/grafana-docker/conf/defaults.ini:/usr/share/grafana/conf/defaults.ini
    #data目录,如果使用了默认的sqlite3数据库,则文件会存在这边
    - /usr/local/grafana-docker/data:/var/lib/grafana
    #log目录,后期会写入log文件
    - /usr/local/grafana-docker/log:/var/log/grafana
    container_name: grafana
  renderer:
    image: index.docker.io/grafana/grafana-image-renderer:1.0.12
    restart: always
    ports:
    - 8081:8081
    container_name: renderer
    environment:
    - GF_RENDERER_PLUGIN_TZ=Asia/Shanghai
    - GF_RENDERER_PLUGIN_IGNORE_HTTPS_ERRORS=true

最后通过Docker Compose进行部署即可

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术流奶爸奶爸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值