【2022年版】中年エンジニアのための、UbuntuでDockerでZabbix構築

## はじめに

* Ubuntu上でZabbixによる監視環境を構築
* OSバージョン

```bash
$ uname -a
Linux nouer 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 x86_64 x86_64 
GNU/Linux
```

```bash
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"
```

```bash
$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
```

## Docker, Docker Composeのインストール

### Dockerのインストール

```bash
$ sudo apt-get update
```

```bash
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
```

```bash
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
```

```bash
$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
```

```bash
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
```

```bash
$ sudo docker --version
Docker version 20.10.14, build a224086
```

### Dockerの起動テスト

```bash
sudo docker run hello-world
```

## Docker Compose (1.29.2) のインストール

```bash
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```

```bash
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c
```

## Zabbix (6.0) のインストール（コンテナ）

```bash
$ git clone https://github.com/zabbix/zabbix-docker.git
$ cd zabbix-docker
$ git branch
* 6.0
$ cp docker-compose_v3_ubuntu_mysql_latest.yaml docker-compose.yaml
$ sudo docker-compose pull
$ sudo docker images
REPOSITORY                      TAG                 IMAGE ID       CREATED        SIZE
mysql                           8.0                 667ee8fb158e   4 days ago     521MB
zabbix/zabbix-web-nginx-mysql   ubuntu-6.0-latest   23f3ef468273   2 weeks ago    384MB
zabbix/zabbix-server-mysql      ubuntu-6.0-latest   b9741218740d   2 weeks ago    289MB
busybox                         latest              2fb6fc2d97e1   3 weeks ago    1.24MB
hello-world                     latest              feb5d9fea6a5   6 months ago   13.3kB
```

## Zabibxの起動

```bash
$ sudo docker-compose up -d
Creating network "zabbix-docker_zbx_net_backend" with driver "bridge"
Creating network "zabbix-docker_zbx_net_frontend" with driver "bridge"
Creating network "zabbix-docker_default" with the default driver
Creating volume "zabbix-docker_snmptraps" with default driver
Creating zabbix-docker_mysql-server_1  ... done
Creating zabbix-docker_db_data_mysql_1 ... done
Creating zabbix-docker_zabbix-server_1 ... done
Creating zabbix-docker_zabbix-web-nginx-mysql_1 ... done
$ sudo docker-compose ps
                 Name                               Command                  State                                          Ports                                    
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
zabbix-docker_db_data_mysql_1            sh                               Exit 0                                                                                     
zabbix-docker_mysql-server_1             docker-entrypoint.sh mysql ...   Up                                                                                         
zabbix-docker_zabbix-server_1            /usr/bin/tini -- /usr/bin/ ...   Up             0.0.0.0:10051->10051/tcp,:::10051->10051/tcp                                
zabbix-docker_zabbix-web-nginx-mysql_1   docker-entrypoint.sh             Up (healthy)   0.0.0.0:80->8080/tcp,:::80->8080/tcp, 0.0.0.0:443->8443/tcp,:::443->8443/tcp
```

## 起動ログの確認

```bash
$ docker-compose logs -f
```

## ブラウザでアクセス

```http://<IPアドレスまたはホスト名>```

* 初期ユーザIDとパスワードは、Admin/zabbix

## 設定

* Hostの設定が、IP address で Agent に接続できないので、IP address を空に、DNS name を ```zabbix-agent``` に Connect to を ```DNS``` に変更する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/143127/b372359d-3068-ccc5-ebac-57b4cf1cf87a.png)

## データベースの削除

* コンテナを削除してもデータベースが残るので、以下を削除

```bash
$ sudo rm -rf zbx_env/var/lib/mysql/*
```

## 終わりに

かんたんでしたね