Example application – Container Orchestration with Kubernetes-1

We will use the example application we used in Chapter 3, Containerization with Docker, in the Deploying a sample application with Docker Compose section. The source code has been replicatedinto the following directory:

$ cd ~/modern-devops/ch5/multi-container-pod/ambassador

You can visualize the app.py file of the Flask application, the requirements.txt file, and the Dockerfile to understand what the application does.

Now, let’s build the container using the following command:

$ docker build -t <your_dockerhub_user>/flask-redis .

Let’s push it to our container registry using the following command:

$ docker push <your_dockerhub_user>/flask-redis

As you may have noticed, the app.py code defines the cache as localhost:6379. We will run an ambassador container on localhost:6379. The proxy will tunnel the connection to the redis pod running elsewhere.

First, let’s create the redis pod using the following command:

$ kubectl run redis –image=redis

Now, let’s expose the redis pod to the cluster resources via a Service resource. This will allow any pod within the cluster to communicate with the redis pod using the redis hostname. We will discuss Kubernetes Service resources in the next chapter in detail:

$ kubectl expose pod redis –port 6379

Cool! Now that the pod and the Service resource are up and running, let’s work on the ambassador pattern.

We need to define two config maps first. The first describes theredis host and port details, while the second defines the template nginx.conf file to work as a reverse proxy.

The redis-config-map.yaml file looks like this:

apiVersion: v1

kind: ConfigMap

metadata:

name: redis-config

data:

host: “redis”

port: “6379”

The preceding YAML file defines a config properties. You can have multiple config

map called redis-config that contains host and port maps, one for each environment.

The nginx-config-map.yaml file looks as follows:

apiVersion: v1

kind: ConfigMap

metadata:

name: nginx-config

data:

nginx.conf: |

stream {

server {

listen         6379;

proxy_pass stream_redis_backend;

}

upstream stream_redis_backend {

server REDIS_HOST:REDIS_PORT;

}

This config mapinjects the nginx.conf template as a config map value. This template defines the configuration of our ambassador pod to listen on localhost:6379 and tunnel the connection to

REDIS_HOST:REDIS_PORT. As the REDIS_HOST and REDIS_PORT values are placeholders, we must fill these up with the correct values that we obtained from the redis-config config map. To do that, we can mount this file to a volume and then manipulate it. We can use initContainer to initialize the proxy with the correct configuration.

Now, let’s look at the pod configuration manifest, flask-ambassador.yaml. There are multiple parts of this YAML file. Let’s look at the containers section first:

spec:

containers:

  • name: flask-app

image: <your_dockerhub_user>/flask-redis

  • name: nginx-ambassador

image: nginx volumeMounts:

  • mountPath: /etc/nginx

name: nginx-volume

This section contains a container calledflask-app that uses the <your_dockerhub_user>/ flask-redis image that we built in the previous section. The second container is the nginx-ambassador container that will act as the proxy to redis. Therefore, we have mounted the / etc/nginx directory on a volume. This volume is also mounted on the init container to generate the required configuration before nginx boots up.

Related Posts

Static provisioning – Managing Advanced Kubernetes Resources-2

As the Service resource is created, we can create a StatefulSet resource that uses the created PersistentVolume and Service resources. The StatefulSet resource manifest, nginx-manual-statefulset.yaml, looks like…

Static provisioning – Managing Advanced Kubernetes Resources-1

Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the…

StatefulSet resources – Managing Advanced Kubernetes Resources

StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and…

Managing stateful applications – Managing Advanced Kubernetes Resources

Imagine you’re a librarian in a magical library. You have a bunch of enchanted books that store valuable knowledge. Each book has a unique story and is…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-2

Now, let’s autoscale this deployment. The Deployment resource needs at least 1 pod replica and can have a maximum of 5 pod replicas while maintaining an average…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-1

Imagine you’re the manager of a snack bar at a park. On a sunny day, lots of people come to enjoy the park, and they all want…

Leave a Reply

Your email address will not be published. Required fields are marked *