Cloud applications and containers
@xav-b|November 10, 2015 (9y ago)8 views
We can find a certain comfort while developing an application on our local computer. We debug logs in real time. We know the exact location of everything, for we probably started it by ourselves.
Make it work, make it right, make it fast - Kent Beck Optimization is the root of all devil - Donald Knuth
So hey, we hack around until interesting results pop up (ok that's a bit exagerated). The point is, when hitting the production server our code will sail a much different sea. And a much more hostile one. How to connect to third party resources ? How to get a clear picture of what is really happening under the hood ?
In this article we will try to answer those questions with existing tools. We won't discuss continuous integration or complex orchestration. Instead, we will focus on what it takes to wrap a typical program to make it run as a public service.
A sample application
Before diving into the real problem, we need some code to throw on remote servers. Our sample application below exposes a random key/value store over http.
And we define the following package.json
and Dockerfile
.
A Dockerfile
? Yeah, here is a first step toward cloud computation under
control. Packing our code and its dependencies into a container will allow us to
ship and launch the application with a few reproducible commands.
Enough for the preparation, time to actually run the code.
Service Discovery
The server code needs a connection to redis. We can't hardcode it because host and port are likely to change under different deployments. Fortunately The Twelve-Factor App provides us with an elegant solution.
The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code;
Indeed, this strategy integrates smoothly with an infrastructure composed of containers.
In another terminal, we can check everything is working as expected.
We didn't precise any network informations but even so, containers can communicate. This method is widely used and projects like etcd or consul let us automate the whole process.
Monitoring
Performances can be a critical consideration for end-user experience or infrastructure costs. We should be able to identify bottlenecks or abnormal activities and once again, we will take advantage of containers and open-source projects. Without modifying the running server, let's launch three new components to build a generic monitoring infrastructure.
- Influxdb is a fast time series database where we will store containers metrics. Since we properly defined the application into two single-purpose containers, it will give us an interesting overview of what's going on.
- cadvisor Analyzes resource usage and performance characteristics of running containers. The command flags will instruct it how to use the database above to store metrics.
- Grafana is a feature rich metrics dashboard and graph editor for Graphite, InfluxDB and OpenTSB. From its web interface, we will query the database and graph the metrics cadvisor collected and stored.
Now we can head to localhost:8000
and build a custom dashboard to monitor the
server. I won't repeat the comprehensive documentation but here is a query
example:
# note: cadvisor stores metrics in series named 'stats'
select difference(cpu_cumulative_usage) where container_name='cadvisor' group by time 60s
Grafana's autocompletion feature shows us what we can track : cpu, memory and network usage among other metrics. We all love screenshots and dashboards so here is a final reward for our hard work.
img Metrics Dashboard lost
Conclusion
Development best practices and a good understanding of powerful tools gave us a rigorous workflow to launch applications with confidence. To sum up:
- Containers bundle code and requirements for flexible deployment and execution isolation.
- Environment stores third party services informations, giving developers a predictable and robust solution to read them.
- InfluxDB + Cadvisor + Grafana feature a complete monitoring solution independently of the project implementation.
We fullfilled our expections but there's room for improvements. As mentionned service discovery could be automated, but we also omitted how to manage logs. There are many discussions around this complex subject and we can expect shortly new improvements in our toolbox.