如何在Ubuntu 18.04上安装和配置Graphite

时间:2020-02-23 14:44:34  来源:igfitidea点击:

本教程将介绍Ubuntu 18.04均质BEAVER Linux上Graphite的安装和配置。
Graphite是一个高可用的时序监控和数据存储,支持数据可视化。
理想情况下,我们将在系统上安装其他工具来监视,这将收集指标并将其运送到Graphite E.g Statsd。

Graphite架构

Graphite由3个软件组件组成:碳 - 一种扭曲的守护程序,可以侦听时间序列数据的数据库 - 一个简单的数据库库,用于存储时间序列数据(类似于RRD的设计)Graphite WebApp - 一种Django Webapp,使得按需推出图表的Django WebApp开罗

将数据送至Graphite是容易的部分。
发送到碳的数据立即用于在WebApp中进行图形。
从WEBAPP中,我们可以创建和显示图形,包括一个简单的URL API,用于呈现,这使得可以轻松地在其他网页中嵌入图形。

方法1:使用Docker(推荐)在Ubuntu 18.04上安装Graphite(推荐)

在Ubuntu 18.04上安装Straphite的第一个和简单的方法使用Docker。

第1步:更新系统和已安装的软件包。

sudo apt-get update
sudo apt-get upgrade

第2步:安装Docker引擎

在Ubuntu 18.04上安装Docker Engine,请使用我们的教程:

如何在Ubuntu/debian/fedora/arch/centos上安装Docker CE

确认服务在继续之前运行:

$systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2016-07-11 07:45:15 UTC; 43s ago
     Docs: https://docs.docker.com
 Main PID: 8898 (dockerd)
    Tasks: 16
   CGroup: /system.slice/docker.service
           ├─8898 /usr/bin/dockerd -H fd://
           └─8937 docker-containerd --config /var/run/docker/containerd/containerd.toml

第3步:安装Graphite和Statsd容器

安装和运行Docker引擎后,是时候启动用于Graphite和Statsd的Docker容器了。
我们将使用官方Docker Repo forGraphite:

为持久数据存储创建主机系统上的目录:

sudo mkdir -p /data/graphite/{statsd,configs,data}

Graphite&Statsd可以复杂。
提供的Docker图像将在几分钟内运行和收集统计信息。

sudo docker run -d \
 --name graphite \
 --restart=always \
 -p 80:80 \
 -p 2003-2004:2003-2004 \
 -p 2023-2024:2023-2024 \
 -p 8125:8125/udp \
 -p 8126:8126 \
 -v /data/graphite/configs:/opt/graphite/conf \
 -v /data/graphite/data:/opt/graphite/storage \
 -v /data/graphite/statsd:/opt/statsd \
 graphiteapp/graphite-statsd

等待Docker引擎下载并启动Docker容器。
取决于互联网速度可能需要一段时间。
完成后,我们应该能够看到运行的Docker容器:

$docker ps
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS                                                                                                                                                        NAMES
959fe7ab2abd        graphiteapp/graphite-statsd   "/sbin/my_init"     11 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp, 0.0.0.0:2003-2004->2003-2004/tcp, 8080/tcp, 0.0.0.0:2023-2024->2023-2024/tcp, 0.0.0.0:8126->8126/tcp, 8125/tcp, 0.0.0.0:8125->8125/udp   graphite

这首先启动Docker容器命名:Graphite。
另请注意,如果在主机上已占用,则可以自由重新映射到任何主机端口的任何主机端口。

包括以下组件

nginx - 反向代理Graphite仪表板 - 前端DashboardCarbon - 后端售货机 - 基于UDP的后端代理

映射端口

||||
| --- - | --- | - - |
| 80 | 80 | nginx |
| 2003 | 2003 |碳接收器 - 明文|
| 2004 | 2004 |碳接收器 - 泡菜|
| 2023 | 2023 |碳聚合器 - 明文|
| 2024 | 2024 |碳聚合器 - 泡菜|
| 8080 | 8080 |Graphite内部凝血端口(没有nginx代理)。 |
| 8125 | 8125 | statsd |
| 8126 | 8126 | statsd admin |

默认情况下,statsd侦听UDP端口8125.如果要它侦听TCP端口8125,则可以设置环境变量 STATSD_INTERFACEtcp运行容器时。

第3步:访问GraphiteWeb界面

当一切正常运行时,通过使用服务器IP地址或者端口号和端口号和主机上映射到容器端口80的端口,访问GraphiteWeb仪表板:

默认的登录信誉是:

Username: root
Password: root

在http://ip-address/admin/auth/user/1 /的首次登录后更改此登录凭据

提供所需的root密码并确认。

我们现在可以注销并重新登录以确认新密码正在运行。

方法2:从APT包中安装Ubuntu 18.04上的Graphite

现在我们已经涵盖了Graphite的基础知识,让我们继续安装。
按照以下步骤获取在Ubuntu 18.04服务器上运行的Graphite。

第1步:更新系统包:

安装中的第一步是确保更新所有系统包:

sudo apt update
sudo apt -y upgrade

考虑在升级后重新启动服务器

sudo reboot

第2步:在Ubuntu 18.04上安装Graphite

从重新启动中服务器后,继续安装Graphite软件包。
我们将安装GraphiteWeb应用程序和碳,将数据存储在Graphite的专用数据库中。

sudo apt-get install graphite-web graphite-carbon

等待包完成安装,然后继续下一步。

$apt policy graphite-web graphite-carbon 
graphite-web:
  Installed: 1.0.2+debian-2
  Candidate: 1.0.2+debian-2
  Version table:
.................................
graphite-carbon:
  Installed: 1.0.2-1
  Candidate: 1.0.2-1
  Version table:
..................................

第3步:安装和配置MariaDB数据库服务器(可选)

默认情况下,Graphites使用SQLite数据库来存储Django模型,例如保存的图形,仪表板,用户偏好等。
请注意,在此处不存储公制数据。
在本教程中,我们将使用MariaDB数据库服务器。
检查我们的教程,了解如何在Ubuntu 18.04上安装MariaDB。

一旦数据库服务器启动并准备就绪,为Graphite创建数据库。
以root用户身份登录数据库服务器并为Graphite创建数据库:

$mysql -u root -p
CREATE DATABASE graphite;
GRANT ALL PRIVILEGES ON graphite.* TO graphite IDENTIFIED BY "Hyman@theitroad";
FLUSH PRIVILEGES;
QUIT;

一旦数据库准备就绪,请继续执行步骤4.

第4步:配置Graphite和碳

安装Graphite软件包后,请继续配置:

sudo vim /etc/graphite/local_settings.py

设置时区:

TIME_ZONE = "Africa/Nairobi"

设置用于盐的秘密键,用于验证令牌中使用的哈希。
我们可以使用一个使用 uuidgen

SECRET_KEY = 'eihi8dee4Otae0i'

请将其设置为长时间随机唯一的字符串,用作秘密密钥。
对于数据库部分,以下部分注释:

DATABASES = {
    'default': {
        'NAME': '/var/lib/graphite/graphite.db',
        'ENGINE': 'django.db.backends.sqlite3',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': ''
    }
}

然后为mysql/mariadb添加配置

DATABASES = {
    'default': {
        'NAME': 'graphite',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'graphite',
        'PASSWORD': 'Hyman@theitroad',
        'HOST': '127.0.0.1',
        'PORT': ''
    }
}

安装pymysql python模块

sudo apt -y install python3-pymysql python-pymysql

现在同步数据库以具有正确的结构:

$sudo graphite-manage migrate auth
Operations to perform:
  Apply all migrations: auth
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
启用碳缓存:
$sudo vim /etc/default/graphite-carbon
CARBON_CACHE_ENABLED=true
启用日志旋转:
$sudo vim /etc/carbon/carbon.conf
ENABLE_LOGROTATION = True

使用以下命令启动Carbon Service/St开始启动:

sudo systemctl start carbon-cache
sudo systemctl enable carbon-cache

第5步:配置WebApp

我们已经准备好了,我们唯一需要做的是配置Web应用程序。

配置nginx.

安装nginx和gunicorn。

sudo apt -y install nginx gunicorn

在服务Graphite时为nginx创建日志文件。

sudo touch /var/log/nginx/{graphite.access.log,graphite.error.log}
sudo chmod 640 /var/log/nginx/graphite.*
sudo chown www-data:www-data /var/log/nginx/graphite.*

为Graphite创建nginx站点配置文件。

sudo nano /etc/nginx/conf.d/graphite.conf

应该添加到文件的内容是:

upstream graphite {
    server 127.0.0.1:8080 fail_timeout=0;
}
server {
    listen 80 default_server;
    server_name graphite.example.com;
    root /opt/graphite/webapp;
    access_log /var/log/nginx/graphite.access.log;
    error_log  /var/log/nginx/graphite.error.log;
    location = /favicon.ico {
        return 204;
    }
    # serve static content from the "content" directory
    location /static {
        alias /opt/graphite/webapp/content;
        expires max;
    }
    location/{
        try_files $uri @graphite;
    }
    location @graphite {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://graphite;
    }
}

更改Graphite.example.com以匹配Graphite Server HostName。

验证nginx配置。

$nginx  -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

启动nginx服务。

sudo systemctl restart nginx