美文网首页
docker基于bind9搭建dns&Prometheus+Gr

docker基于bind9搭建dns&Prometheus+Gr

作者: 李戴桃江 | 来源:发表于2024-07-15 15:13 被阅读0次

    1.安装

    1.1.Dockerfile

    这个镜像自带webmin,不需要手动安装;手动安装webmin参考官网:https://webmin.com/download/

    FROM sameersbn/bind
    
    # 复制自定义的配置文件
    COPY named.conf.options /etc/bind/named.conf.options
    COPY named.conf.local /etc/bind/named.conf.local
    
    # 暴露 DNS 服务端口
    EXPOSE 53
    
    # 启动 BIND 服务
    CMD ["/usr/sbin/named", "-g"]
    
    1.2 named.conf.options
    options {
        directory "/var/cache/bind";
        dnssec-validation auto;
        recursion yes;
        allow-query { any; };
        # allow-query-cache { any; };
        listen-on { any; };
        listen-on-v6 { any; };
        max-cache-ttl 3600;
        forwarders {
            8.8.8.8;
            };
    };
    
    1.3 named.conf.local
    zone "test.com" {
        type master;
        file "/var/lib/bind/test.com.hosts";
        };
    zone "test.net" {
        type forward;
        forward only;
        forwarders { 8.8.8.8; };
        };
    
    1.4启动
    docker build -t bind9 .
    docker run --name bind9 -d --restart=always \
    --publish 9000:53/tcp --publish 9000:53/udp --publish 10000:10000/tcp \
    bind9
    检查启动情况:docker ps
    如果一直在重启,查看日志:docker logs bind9
    

    2.配置

    2.1控制台

    启动成功后,访问端口10000可进入控制台。初始账号密码:root/password
    参考:https://juejin.cn/post/7147536857298993189

    image.png
    2.2反向解析

    在/etc/bind/named.conf.default-zones下添加配置

    zone "2.0.192.in-addr.arpa" IN {  
        type master;  
        file "/etc/bind/2.0.192.in-addr.arpa";  
        allow-update { none; };  
    };
    
    

    在上面配置的file路径下创建文件,也可以docker打包时复制到容器中。

    $TTL    604800  
    @       IN      SOA     ns1.example.com. admin.example.com. (  
                                  3         ; Serial  
                             604800         ; Refresh  
                              86400         ; Retry  
                            2419200         ; Expire  
                             604800 )       ; Negative Cache TTL  
    ;  
    @       IN      NS      ns1.example.com.  
    ;  
    $ORIGIN 2.0.192.in-addr.arpa.  
      
    2       IN      PTR     host2.example.com.  
    100     IN      PTR     server100.example.com.  
    ;
    

    3.测试

    替换成启动时配置的实际ip和port
    nslookup -port=9000 b.test.com 0.0.0.0
    dig @0.0.0.0 -p 9000 b.test.com
    
    nslookup -port=9000 192.0.2.100 0.0.0.0
    dig @0.0.0.0 -p 9000 -x 192.0.2.100
    

    4.监控

    4.1 bind_exporter

    下载bind_exporter
    curl -s https://api.github.com/repos/prometheus-community/bind_exporter/releases/latest | grep browser_download_url | grep linux-amd64 |  cut -d '"' -f 4 | wget -qi -
    
    解压&移动
    tar xvf bind_exporter*.tar.gz
    sudo mv bind_exporter-*/bind_exporter /usr/local/bin
    
    新增bind9配置并重启
    (centos)sudo vim /etc/named.conf
    (ubuntu)sudo vim /etc/bind/named.conf.options
    statistics-channels {
      inet 127.0.0.1 port 8053 allow { any; };
    };
    sudo systemctl restart named
    
    创建Prometheus用户组和用户
    sudo groupadd --system prometheus
    sudo useradd -s /sbin/nologin --system -g prometheus prometheus
    
    创建systemd配置
    sudo tee /etc/systemd/system/bind_exporter.service<<EOF
    [Unit]
    Description=Prometheus
    Documentation=https://github.com/digitalocean/bind_exporter
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    Type=simple
    User=prometheus
    Group=prometheus
    ExecReload=/bin/kill -HUP \$MAINPID
    ExecStart=/usr/local/bin/bind_exporter \
      --bind.pid-file=/var/run/named/named.pid \
      --bind.timeout=20s \
      --web.listen-address=0.0.0.0:9153 \
      --web.telemetry-path=/metrics \
      --bind.stats-url=http://localhost:8053/ \
      --bind.stats-groups=server,view,tasks
    
    SyslogIdentifier=prometheus
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    
    重启&启动
    sudo systemctl daemon-reload
    sudo systemctl restart bind_exporter.service
    sudo systemctl enable bind_exporter.service
    
    检查
    systemctl status bind_exporter.service
    sudo ss -tunelp | grep 9153
    

    4.2 Prometheus

    创建用户组和用户
    sudo groupadd --system prometheus
    sudo useradd -s /sbin/nologin --system -g prometheus prometheus
    
    创建数据目录
    sudo mkdir /var/lib/prometheus
    
    创建配置目录
    for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done
    
    下载安装包
    mkdir -p /tmp/prometheus && cd /tmp/prometheus
    curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest \
      | grep browser_download_url \
      | grep linux-amd64 \
      | cut -d '"' -f 4 \
      | wget -qi -
    
    解压&移动数据
    tar xvf prometheus*.tar.gz
    cd prometheus*/
    sudo mv prometheus promtool /usr/local/bin/
    sudo mv prometheus.yml  /etc/prometheus/prometheus.yml
    sudo mv consoles/ console_libraries/ /etc/prometheus/
    cd ~/
    rm -rf /tmp/prometheus
    
    新增job配置
    sudo vim /etc/prometheus/prometheus.yml
    - job_name: dns-master
        static_configs:
          - targets: ['0.0.0.0:9153']
            labels:
              alias: dns-master
    
    创建systemd配置
    sudo vim /etc/systemd/system/prometheus.service
    [Unit]
    Description=Prometheus
    Documentation=https://prometheus.io/docs/introduction/overview/
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    Type=simple
    Environment="GOMAXPROCS=1" #这里修改为CPU核数
    User=prometheus
    Group=prometheus
    ExecReload=/bin/kill -HUP $MAINPID
    ExecStart=/usr/local/bin/prometheus \
      --config.file=/etc/prometheus/prometheus.yml \
      --storage.tsdb.path=/var/lib/prometheus \
      --web.console.templates=/etc/prometheus/consoles \
      --web.console.libraries=/etc/prometheus/console_libraries \
      --web.listen-address=0.0.0.0:9090 \
      --web.external-url=
    
    SyslogIdentifier=prometheus
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    
    设置权限
    for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
    for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
    sudo chown -R prometheus:prometheus /var/lib/prometheus/
    
    启动
    sudo systemctl daemon-reload
    sudo systemctl start prometheus
    sudo systemctl enable prometheus
    
    检查
    systemctl status prometheus
    

    4.3 Grafana

    添加Data Sources

    1.登录到 Grafana 界面。
    2.在左侧菜单栏中,点击“Configuration”(配置),然后选择“Data Sources”(数据源)。
    3.点击“Add data source”(添加数据源)按钮。
    4.在“Type”(类型)下拉菜单中,选择“Prometheus”。
    5.在“Name”(名称)字段中,为数据源输入一个有意义的名称,例如“Prometheus Server”。
    6.在“URL”字段中,输入 Prometheus 服务器的地址,通常是 http://your-prometheus-server:9090 ,请将 your-prometheus-server 替换为您实际的 Prometheus 服务器的主机名或 IP 地址。
    7.(可选)根据您的需求设置其他选项,如访问模式、HTTP 配置等。
    8.点击“Save & Test”(保存并测试)按钮。如果配置正确,您应该看到“Data source is working”(数据源正常工作)的消息。

    导入

    1.在左侧菜单栏仪表板中选择“导入”。
    2.输入模板ID 1666,选择上面导入的数据。

    5.参考

    https://bind9.readthedocs.io/en/latest/chapter1.html
    https://cn.linux-console.net/?p=22001
    https://computingforgeeks.com/monitor-bind-dns-server-with-prometheus-grafana/
    https://computingforgeeks.com/install-prometheus-server-on-centos-rhel/
    https://computingforgeeks.com/install-grafana-and-influxdb-on-centos-rhel/

    相关文章

      网友评论

          本文标题:docker基于bind9搭建dns&Prometheus+Gr

          本文链接:https://www.haomeiwen.com/subject/iqehhjtx.html