‘State’ in Kubernetes Stateful Applications

By May 29, 2019 August 22nd, 2019 Blog, Kubernetes, Stateful Applications

‘State’ in Kubernetes

Kubernetes has become the defacto application orchestration engine for applications running in containers. Docker pioneered the workflows to build, package and run applications in containers. The first wave of applications that were targeted by Docker and subsequently Kubernetes are stateless applications. What are stateless and stateful application and why is orchestration of stateful application a difficult task? Let us dig deep into some of the core concepts.

What is State

There are several definitions. Here is mine…

  • Connection/Request state: DB connection, sessions, HTTP sessions etc
  • Process state: In memory state of the process, or groups of processes using shared memory
  • Persistent state: The persistent state of the application

Connection/Request state

The HTTP protocol is stateless by definition. Every request should contain enough data for the server to fulfill the request. HTTP request should not depend on the previous or the next one. Having said that, there are exceptions in the use of the protocol, for example, sessions. Note that statelessness of HTTP means that the protocol itself does not maintain state on either side, the client or the server can save state.

Database connections, however, are a different story. Database sessions and transaction rely on the state. Consider a DB operation using JDBC or ODBC — a connection is opened with the database using user credentials, a session is created, multiple inserts or update statements are executed and then the session is committed or rolled back. All these actions cannot be performed in multiple steps, much less over multiple connections and clients.

Process state

Every process has on-disk state and in-memory state. There is of course network state, which the open TCP sessions etc. There are applications built completely on in-memory state — caching services like Memcached or Redis. There are applications that build an in-memory state and then flush the state to disk at periodic intervals like HDFS namenodes, SAP HANA, etc. VMWare vmotion is a solution that can migrate a process state from one host to another. TCP handoff is used to move the open session from one host to another.

Persistent state

The core function of any application is read, process, write data. A process can die, and come back up. When they do, they will need to reconcile with the last state of the applications, which is saved on persistent media. There are several products built around provisioning and protecting this persistent state. The storage industry, backup industry thrive on protecting this state.

Applications on Docker

Let us examine Nginx, the most referred to example for a stateless application. Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server). We will also use Docker to run Nginx server to host our static HTML content. Let us examine the docker command.

$ docker run \
–name my-custom-nginx-container \
# Configuration \
-v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro \
# Data (Content) \
-v /some/content:/usr/share/nginx/html:
ro \
-d nginx

Ravi Alluboyina is a Senior Software Architect at Robin.io

State in Kubernetes Stateful Applications



Author Ravikumar Alluboyina

More posts by Ravikumar Alluboyina