After I’ve started my CoreOS cluster, I demonstrate how to run a sample service on it. In this case a Tomcat 8 Docker container controlled by fleet.

I created a Dockerfile, which creates a custom container based on the official Tomcat 8 image. Additionally it adds the official sample application provided on apache.org.

Dockerfile
1
2
3
4
5
6
7
8
9
FROM tomcat:8-jre8
MAINTAINER visit

ENV TOMCAT_VERSION 8.0
RUN curl -SL \
-o /usr/local/tomcat/webapps/sample.war \
"https://tomcat.apache.org/tomcat-${TOMCAT_VERSION}-doc/appdev/sample/sample.war"

CMD ["catalina.sh", "run"]

To make this Dockerfile available to all workers in my cluster, I uploaded it to etcd. The path is completely free to choose.

1
$ etcdctl set /units/tomcat8/Dockerfile < ./Dockerfile

Now I could create a service file for fleet. The instance notation (@) and specifiers (%p, %i) allow to start as many instances as I like, all from one single service file. For details on that review man 5 systemd.service and man 5 systemd.units. I also specified role=worker in the X-Fleet section, to let fleet automatically choose a suitable instance in my cluster.

tomcat8@.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Tomcat 8 servlet container
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=/usr/bin/mkdir -p /tmp/%p.%i
ExecStartPre=/usr/bin/bash -c "etcdctl get /units/%p/Dockerfile > /tmp/%p.%i/Dockerfile"
ExecStart=/usr/bin/bash -c "docker run -rm --name %p-%i -p 8080:8080 $(docker build /tmp/%p.%i | awk '/^Successfully built/ {print $3}')"
ExecStop=/usr/bin/docker stop %p-%i
Restart=always
TimeoutStartSec=0

[X-Fleet]
Conflicts=%p@*.service
MachineMetadata=role=worker

Last not least, I registered the service in fleet and started some instances.

1
2
3
$ fleetctl submit tomcat8@.service
$ fleetctl start tomcat8@1
$ fleetctl start tomcat8@2

Cheers,
visit