Home Creating stateful Zabbix Docker pods
Post
Cancel

Creating stateful Zabbix Docker pods

This script will create a stateful Zabbix series of pod. The only pod that can be accessed is the Apache web server pod and only on port 80/TCP. The database files are stored on a user-defined local path. Therefore, this script can be run multiple times without losing your data.

The first time you run this script, the web login will not work for 1-2 minutes as the database contents are being built.

This script creates three pods:

  • A stock MySQL database pod
  • A Zabbix “server” pod handling all of the monitoring duties
  • An Apache pod tuned for Zabbix

Zabbix website: https://www.zabbix.com/

According to https://support.zabbix.com/browse/ZBX-18161, the Zabbix Docker images I used do not produce a fully functional Zabbix install.

… you can use containers on production, but be advised that the zabbix-images are an example of what you should use and not a general solution for everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash

# source: https://www.devopsschool.com/blog/how-to-install-zabbix-server-and-dashboard-using-docker/
# With a lot of additions

set -e

function cleanup () {
  docker stop $Z_NAME_MYSQL $Z_SERVER_NAME $Z_WEB_NAME > /dev/null 2>&1 || true
  docker rm $Z_NAME_MYSQL $Z_SERVER_NAME $Z_WEB_NAME > /dev/null 2>&1 || true
}

# ------------------------------------------------------------------------------------------
# Configuration settings
Z_NAME_MYSQL=zabbix-mysql
Z_MYSQL_USERNAME=root
Z_MYSQL_PASSWORD=password
Z_SERVER_NAME=zabbix-server-mysql
Z_WEB_NAME=zabbix-web-apache-mysql
Z_PHP_TZ=America/New_York

Z_DB_PATH=${PWD}/Z_database_files # This is the local path on where to store the DB files
# ------------------------------------------------------------------------------------------


echo "Stopping any existing Zabbix pods that may be running..."
cleanup


echo "---------------------------------------------------------------------------"
echo "Creating MySQL pod..."
docker run -d --name $Z_NAME_MYSQL -e MYSQL_ROOT_USER="$Z_MYSQL_USERNAME" \
  -e MYSQL_ROOT_PASSWORD="$Z_MYSQL_PASSWORD" \
  -v $Z_DB_PATH:/var/lib/mysql \
  mysql

Z_MYSQL_IP=$(docker inspect $Z_NAME_MYSQL | grep '"IPAddress"' | head -n 1 | cut -d ':' -f 2 | cut -d '"' -f 2)

#echo "Internal MySQL IP address: $Z_MYSQL_IP"


echo "---------------------------------------------------------------------------"
echo "Creating Server pod..."
docker run -d --name $Z_SERVER_NAME -p 10051:10051 \
  -e DB_SERVER_HOST="$Z_MYSQL_IP" -e MYSQL_USER="$Z_MYSQL_USERNAME" \
  -e MYSQL_PASSWORD="$Z_MYSQL_PASSWORD" \
  zabbix/zabbix-server-mysql

Z_SERVER_IP=$(docker inspect $Z_SERVER_NAME | grep '"IPAddress"' | head -n 1 | cut -d ':' -f 2 | cut -d '"' -f 2)
#echo "Internal Zabbix server IP address: $Z_SERVER_IP"



echo "---------------------------------------------------------------------------"
echo "Creating Apache web server pod..."
docker run -d --name $Z_WEB_NAME -p 80:8080 -e DB_SERVER_HOST="$Z_MYSQL_IP" \
  -e MYSQL_USER="$Z_MYSQL_USERNAME" -e MYSQL_PASSWORD="$Z_MYSQL_PASSWORD" \
  -e ZBX_SERVER_HOST="$Z_SERVER_IP" -e PHP_TZ="$Z_PHP_TZ" \
  zabbix/zabbix-web-apache-mysql


echo "---------------------------------------------------------------------------"
HOST_IP=$(hostname -I | cut -d " " -f 1)

echo "If this is the first time you have run this script, you will need to wait 1-2 minutes so Zabbix can initialize the database."
echo "Access the site here: http://$HOST_IP/"
echo "Username: Admin"
echo "Password: zabbix"
This post is licensed under CC BY 4.0 by the author.