Example application – Container Orchestration with Kubernetes-2

The following is the initContainers section:

initContainers:

  • name: init-nginx

image: busybox:1.28

command: [‘sh’, ‘-c’, ‘cp -L /config/nginx.conf /etc/nginx/nginx.conf && sed -i “s/

REDIS_HOST/${REDIS_HOST}/g” /etc/nginx/nginx.conf’]

env:

  • name: REDIS_HOST valueFrom:

configMapKeyRef: name: redis-config key: host

  • name: REDIS_PORT valueFrom:

configMapKeyRef: name: redis-config

key: port

volumeMounts:

  • mountPath: /etc/nginx

name: nginx-volume

  • mountPath: /config

name: config

This sectiondefines a busybox container – init-nginx. The container needs to generate the nginx-ambassador proxy configuration to communicate with Redis; therefore, two environment variables are present. Both environment variables are sourced from the redis-config config map. Apart from that, we have also mounted the nginx.conf file from the nginx-config config map. The command section within the init container uses the environment variables to replace placeholders within the nginx.conf file, after which we get a TCP proxy to the Redis backend.

The volumes section defines nginx-volume as an emptyDir volume, and the config volume is mounted from the nginx.conf file present in the nginx-config config map:

volumes:

  • name: nginx-volume

emptyDir: {}

  • name: config configMap:

name: nginx-config items:

  • key: “nginx.conf”

path: “nginx.conf”

Now, let’s start applying the YAML files in steps.

Apply both of the config         maps using the following commands:

$ kubectl apply -f redis-config-map.yaml

$ kubectl apply -f nginx-config-map.yaml

Let’s apply the pod configuration using the following command:

$ kubectl apply -f flask-ambassador.yaml

Get the pod to see whether the configuration is correct by using the following command:

$ kubectl get pod/flask-ambassador

NAME

READY   STATUS

RESTARTS     AGE

flask-ambassador     2/2

Running   0

10s

As the pod is running successfully now, let’s port-forward 5000 to the localhost for some tests by using the following command:

$ kubectl port-forward flask-ambassador 5000:5000

Now, open a duplicate Terminal and try to curl on localhost:5000 using the following command:

$ curl localhost:5000

Hi there! This page was last visited on 2023-06-18, 16:52:28.

$ curl localhost:5000

Hi there! This page was last visited on 2023-06-18, 16:52:28.

$ curl localhost:5000

Hi there! This page was last visited on 2023-16-28, 16:52:32.

As we can see, every time we curl the application, we get the last visited time on our screen. The ambassador pattern is working.

This was a simple example of the ambassador pattern. There are advanced configurations you can do to add fine-grained control on how your application should interact with the outside world. You can use the ambassador pattern to secure traffic that moves from your containers. It also simplifies application development for your development team as they need not worry about these nuances. In contrast, the operations team can use these containers to manage your environment in a better way without stepping on each other’s toes.

Tip

As the ambassador pattern adds some overhead as you tunnel connections via a proxy, you should only use it if the management benefits outweigh the extra cost you incur because of the ambassador container.

Now, let’s look at another multi-container pod pattern – sidecars.

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 *