欢迎光临!
若无相欠,怎会相见

Netdata 部署与 Linux 服务器集群监控

序言

我自己买了好几台 VPS, 但是都没有完整的监控起来,最近有需要,因此打算用 Netdata 将这几台 Linux 服务器监控起来。

安装 Netdata

我自己推荐使用静态二进制包的形式安装,最好不要编译源码安装,这都是我自己踩过的坑。

一个下午了,使用官方的脚本一直出错 bash <(curl -Ss https://my-netdata.io/kickstart.sh),各种软件包无法安装,当然我的系统是 CentOS 7,安装的时候被强制升级到 CentOS 8。因此我推荐使用如下脚本安装,也是官方脚本:

bash <(curl -Ss https://my-netdata.io/kickstart-static64.sh)

使用这种方式安装后,程序所在位置为: /opt/netdata

节点服务器配置

我打算使用 Stream 方式配置 Netdata , 使用 Stream 方式配置的节点服务器 19999 端口算是废弃了,所有的数据汇总到主服务器上。

vim /opt/netdata/etc/netdata/netdata.conf

[global]
    memory mode = none
    hostname = [按照自己的想法修改相应的名称]
[web]
    mode = none

同时在 netdata.conf 同级目录新建一个 stream.conf,内容如下:

vim /opt/netdata/etc/netdata/stream.conf

[stream]
        enabled = yes
        destination = 173.254.231.139:19999
        api key = [使用 uuidgen 生成一个]

这个 api key 后面再主服务器上会用到,配置完成后,在节点服务器上重启 netdata 服务:

systemctl restart netdata

主服务器配置

切换到 netdata 程序配置目录里面 :cd /opt/netdata/etc/netdata , 然后修改 netdata.conf 文件,就修改了一个 hostname 的名称,这个名称可以随便取:

[global]
        hostname = liangz.org

同时在 netdata.conf 同级目录新建 stream.conf 文件:

vim stream.conf

[xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx]
    enabled = yes
    default history = 3600
    default memory mode = save
    health enabled by default = auto
    allow from = *

注意上面的代码中的一串 xxx 就是你在节点服务器上设置的 api key。allow from 可以直接设置为节点服务器的 IP 。

如果有多台服务器的话,继续在 stream.conf 中添加就可以了 。

然后重启 netdata 服务:

systemctl restart netdata

重新访问主服务器的 IP:19999 就可以看到主服务器和节点服务器的状态了。

Nginx 代理

如果只是这样配置的话,只要别人知道你的 IP,就可以访问到服务器的状态信息了,感觉不安全,因此我们设置一些代理,以 Nginx 为例:

修改 Nginx server 的 vhost 配置:

upstream backend {
        server xxx.xxx.xxx.xxx:19999;
        keepalive 64;
}

server
    {
        listen 80;
        #listen [::]:80;
        server_name xxx.liangz.org ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/xxx.liangz.org;

        include rewrite/other.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location / {
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://backend;
                proxy_http_version 1.1;
                proxy_pass_request_headers on;
                proxy_set_header Connection "keep-alive";
                proxy_store off;
        }
        access_log  /home/wwwlogs/xxx.liangz.org.log;
    }

注意将上面的代码中的 server 和 server_name 改成自己的。

此处修改参考官方文档 : https://learn.netdata.cloud/docs/agent/running-behind-nginx#as-a-virtual-host

修改完成后,重启一下 Nginx 服务器。

为了安全起见,我们设置一个访问密码,不能让每个人都能访问,在 vhost 目录,也就是你设置的 nginx 配置文件同级目录执行如下命令:

printf "yourusername:$(openssl passwd -apr1)" > /usr/local/nginx/conf/passwords

然后自己设置密码,最后会生成一个 passwords 文件,用户名就是 printf 之后的 yourusername ,然后在 nginx 配置文件中添加如下:

auth_basic "Protected";
auth_basic_user_file passwords;

server
    {
        listen 80;
        #listen [::]:80;
        server_name xxx.liangz.org ;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/xxx.liangz.org;

        auth_basic "Protected";
        auth_basic_user_file passwords;

        include rewrite/other.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location / {
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://backend;
                proxy_http_version 1.1;
                proxy_pass_request_headers on;
                proxy_set_header Connection "keep-alive";
                proxy_store off;
        }
        access_log  /home/wwwlogs/xxx.liangz.org.log;
    }

重启 Nginx 服务器,在打开 Web 就需要输入账号密码了 ,加了一道安全防线 。

结语

可以把多台 Linux 服务器集中监控了,不用每台都登录到 Web 管理平台查看了,Good!

如有错误,敬请指出,感谢指正!     — 2021-05-23  23:39:21

赞(0) 打赏
转载请注明:飘零博客 » Netdata 部署与 Linux 服务器集群监控
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临