In this post I’ll be installing a basic zabbix server in a docker container. Why? Because it’s dead easy, super fast and plain awesome. It gives me the opportunity to simply roll out a zabbix instance for testing purposes, with only a few commands.
I’m going to assume you know about Vagrant and Docker in this post, if you don’t I’d recommend to check out their homepages and this page in particular. It’s been a base for this blogpost.
1. Get a mini virtual machine running to host docker containers
So the goal is to put zabbix in a docker container, and to do this we need a supported OS which has docker. And since that is pretty much the only thing we need from that OS I’m going to use CoreOS. CoreOS has docker build in, and we’re going to create a virtual machine in VirtualBox with CoreOS installed on it.
Now normally this would be a lot of work, creating a virtual machine, configuring it, downloading ISO files, attach those to the VM, boot and go through the installation wizard. Not with Vagrant.
First, use git to clone the coreos repo:
git clone https://github.com/coreos/coreos-vagrant/ cd coreos-vagrant
Now we need to edit the Vagrantfile for two reasons:
- When we’re running zabbix we’ll want to connect to the web interface on port 80
- It’s handy to have a shared folder with your host OS and coreos VM (thrust me, I’m an engineer!)
Open up the Vagrantfile and add this line to the network configuration:
config.vm.network :forwarded_port, guest: 80, host: 8080
And uncomment the config.vm.synced_folder parameter:
config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp']
All right, let’s start the hard work of creating the coreos VM and configuring it…
Once this command finishes, you’ll have a fully functional new VM with CoreOS installed on it, and more importantly: docker!
Please do note that CoreOS has ChaosMonkey build in and might randomly reboot (which is a good thing if you’re hardening your applications and servers).
2. Building the zabbix image
Log into your new VM:
Now we can create our Zabbix image using docker. First pull the docker-zabbix repo using git:
git clone https://github.com/dkanbier/docker-zabbix.git cd docker-zabbix
Let’s build the zabbix image:
docker build -t custom/zabbix .
This will download all the needed software for zabbix, configure the database and make sure everything starts when you start a zabbix container. Once this command finishes you can start a zabbix container:
docker run -d -p 10051:10051 -p 10052:10052 -p 80:80 -p 22 custom/zabbix
The zabbix container is now up and running and reachable from your workstation on http://localhost:8080/zabbix/.
3. Linking docker containers
Start the Zabbix container in the background.
docker run -d -p 80:80 --name zabbix custom/zabbix
Start the zabbix-agent container:
docker run -t -i --link zabbix:zabbix --name agent centos bash
Now in the new agent container run env to see how the containers are linked:
bash-4.1# env HOSTNAME=1d2ea59177a8 TERM=xterm ZABBIX_PORT_80_TCP=tcp://10.1.0.2:80 ZABBIX_PORT_10051_TCP_PORT=10051 ZABBIX_PORT_80_TCP_PORT=80 ZABBIX_PORT_10051_TCP=tcp://10.1.0.2:10051 ZABBIX_PORT_10052_TCP_ADDR=10.1.0.2 ZABBIX_PORT_10051_TCP_PROTO=tcp ZABBIX_PORT_80_TCP_ADDR=10.1.0.2 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ZABBIX_PORT=tcp://10.1.0.2:22 PWD=/ ZABBIX_PORT_22_TCP_PORT=22 ZABBIX_PORT_10052_TCP_PROTO=tcp ZABBIX_PORT_22_TCP_ADDR=10.1.0.2 ZABBIX_PORT_22_TCP_PROTO=tcp SHLVL=1 HOME=/ ZABBIX_PORT_10051_TCP_ADDR=10.1.0.2 ZABBIX_NAME=/agent/zabbix ZABBIX_PORT_80_TCP_PROTO=tcp ZABBIX_PORT_10052_TCP_PORT=10052 ZABBIX_PORT_22_TCP=tcp://10.1.0.2:22 ZABBIX_PORT_10052_TCP=tcp://10.1.0.2:10052
As you can see the exposed ports of the zabbix server container are now linked to the agent container, and for your convenience these environment variables are automatically generated.
They even updated your hosts file for you so you can easily resolve the name of container you linked to, in our case “zabbix”:
bash-4.1# ping zabbix PING zabbix (10.1.0.2) 56(84) bytes of data. 64 bytes from zabbix (10.1.0.2): icmp_seq=1 ttl=64 time=0.071 ms<
So now we can install the zabbix agent in our agent container and use "zabbix" for the Server and ServerActive parameters in the zabbix_agentd.conf file.