Book Image

Docker for Serverless Applications

By : Chanwit Kaewkasi
Book Image

Docker for Serverless Applications

By: Chanwit Kaewkasi

Overview of this book

Serverless applications have gained a lot of popularity among developers and are currently the buzzwords in the tech market. Docker and serverless are two terms that go hand-in-hand. This book will start by explaining serverless and Function-as-a-Service (FaaS) concepts, and why they are important. Then, it will introduce the concepts of containerization and how Docker fits into the Serverless ideology. It will explore the architectures and components of three major Docker-based FaaS platforms, how to deploy and how to use their CLI. Then, this book will discuss how to set up and operate a production-grade Docker cluster. We will cover all concepts of FaaS frameworks with practical use cases, followed by deploying and orchestrating these serverless systems using Docker. Finally, we will also explore advanced topics and prototypes for FaaS architectures in the last chapter. By the end of this book, you will be in a position to build and deploy your own FaaS platform using Docker.
Table of Contents (15 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Preface

The container technology is very mature today.  Docker, the software package that helps popularizing containers, is now being used by hundreds of thousands of developers as their day-to-day DevOps tool. I would say that Docker, the container engine, has now become a boring piece of software. For an infrastructure-level software package, boring means high quality and stability. I use it every day and I know you all use it as a part of your tool chain too. But we no longer feel excited when a new version of Docker is released. Just like what we feel to the releases of Linux kernels. With this feeling, the golden age of container has just recently passed, in my opinion. 

The rise of Docker was in 2013. Its Renaissance was during 2014 - 2016. Many orchestration engine races between Docker Swarm and Kubernetes were at their peaks in 2016. One of them, the Swarm2K project, was my once-in-a-lifetime event. Docker later had an announcement to also support Kubernetes in 2017. The race ended there.

A couple of days ago, in March 2018, just before publishing this book, its founder, Solomon Hykes, left Docker Inc. Docker, the company, has been slowly and strongly moving from the startup world towards the enterprise business. What does this mean to us? Enterprise means stability, but startup means adventure. Let's move onto the new adventure, the post container—the serverless era.

What we will be talking about in this book is serverless. It is the natural evolution next to containers and microservices, in the different ways. First, a Docker container becomes a deployment unit for a function, a primitive unit-of-work in the Function-as-a-Service or FaaS architecture. Second, the microservices architecture has been gradually evolving to the FaaS architecture. FaaS could actually be anywhere on-premises or in the cloud. When the whole FaaS stack is managed by cloud providers, it becomes completely serverless.

But there will be something in between. Something is called the hybrid serverless FaaS architecture. This kind of architecture is the main idea I would like the readers to find out and enjoy in this book. It is the point where we could balance between costs, managing the servers by ourselves, and the degree of control we should have for our servers.

This book covers all three major FaaS platforms for Docker in detail, OpenFaas, OpenWhisk and the Fn Project. All of these projects are in their early stages and actively become more and more mature. So it is a great opportunity for the readers and me to learn and ride this new wave together. Let's do it.

Who this book is for

If you are a developer, a Docker engineer, a DevOps engineer, or any stakeholder interested in learning the use of Docker on serverless environments, then this book is for you.

If you are an undergrad or graduate student, this book is also for you to strengthen your knowledge in the area of serverless and cloud computing.

What this book covers

Chapter 1, Serverless and Docker, introduces serverless and Docker. We will find the relationship between them in this chapter. We will also learn the common architecture crystallized from studying architectures of several FaaS platforms. By the end of this chapter, we will learn how to say hello world using all three FaaS platforms, OpenFaaS, The Fn Project, and OpenWhisk.

Chapter 2, Docker and Swarm Clusters, reviews the container technology, namespaces and cgroups. Then, we will follow this by introducing Docker, how to install it, how to use its basic commands, and understand its workflow for build, ship, and run. Continuing further, we will then move to review its built-in orchestration engine, Docker Swarm. We will learn how to set up a cluster and see how Docker Swarm works internally. We will then learn how to set up a Docker network, attach it to containers, and how to scale services in Docker Swarm.

Chapter 3, Serverless Frameworks, discusses serverless frameworks, including platforms such as AWS Lambda, Google Cloud Functions, Azure Functions, and IBM Cloud Functions. We will end this chapter with a FaaS platform-independent framework, the serverless framework.

Chapter 4, OpenFaaS on Docker, explains how to use OpenFaaS. We will explore its architecture and components. Then we will learn how to prepare, build, and deploy functions with its provided tools and templates, how to prepare its cluster on top of Swarm, how to use its user interface, and how OpenFaaS leverages Docker multi-stage build. And we will also discuss how to use Prometheus to monitor the FaaS platform.

Chapter 5, The Fn Project, explores another FaaS platform. Similar to Chapter 4OpenFaaS on Docker, we will start with its architecture and components, then go through a set of CLI commands to build, package, and deploy functions to Fn. Later in this chapter, we will learn how to monitor the platform using its built-in UI. Also, we will use a familiar tool to help analyze its logs.

Chapter 6, OpenWhisk on Docker, discusses OpenWhisk, the third and final FaaS platform for this book. We will walk through its concept and architecture.

Chapter 7, Operating FaaS Clusters, speaks about several techniques of preparing and operating production-grade FaaS clusters using Docker Swarm. We will discuss how to replace the whole layer of networking with another easy-to-use container networking plugin. We will also show how to implement the new routing mesh mechanism to avoid bugs from the current ingress implementation. Also, we will discuss some advanced topics such as distributed tracing and how to implement it. We will even cover the concept of cost reduction with spot instances and how to implement Swarm on this dynamic infrastructure.

Chapter 8Putting Them All Together, explains how to implement a heterogeneous FaaS system combining all three FaaS platforms running seamlessly together on a robust product-grade Swarm cluster. We will show a mobile-based bank transfer use case, also with a legacy wrapper, a mobile backend WebHook, and stream data processing with FaaS. A bonus here is we also add a blockchain to the use case to show their interoperation.

Chapter 9,The Future of Serverless, concludes this book with advanced concepts and research prototype implementations that go beyond the current serverless and FaaS technologies.

To get the most out of this book

The reader should know the basics of Linux and Docker commands. Although this is optional, it would be a big plus if the reader has a basic understanding of network protocols and has some familiarity to the cloud computing concepts.

Although it is possible to use a MacBook or PC with Windows OS to run examples in this book, it is highly recommended for the reader to use Ubuntu Linux 16.04 and above. The reader with Mac book or Windows could run examples via Linux on a virtual machine, or a cloud instance.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Docker-for-Serverless-Applications. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We will try the echoit function to hello world with OpenFaaS."

A block of code is set as follows:

FROM ubuntu
RUN apt-get update && apt-get install -y nginx
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Any command-line input or output is written as follows:

$ curl -sSL https://get.docker.com | sudo sh
$ docker swarm init --advertise-addr=eth0

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "The following screenshot shows the browser running OpenFaaS Portal."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.