Skip to content

How to deploy guestbook-app on Kubernetes

This document describe step-by-step how to deploy a guestbook-app on STACKIT Kubernetes Engine.

In this Tutorial, we will use Redis to write and retrieve guestbook entries. To make the Service highly available, we will create a single pod deployment to write the data to the database and create a multiple pod deployment to retrieve the data.

Create the deployment manifest redis-leader-deployment.yaml.

redis-leader-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: leader
tier: backend
spec:
containers:
- name: leader
image: "docker.io/redis:6.0.5"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

Apply the deployment file against your cluster.

Terminal window
kubectl apply -f redis-leader-deployment.yaml

Check your Pods to be sure that the Redis Pod is running.

Terminal window
kubectl get pods

The result should look similar like this:

Terminal window
redis-leader-fb76b4755-fmfq2 1/1 Running 0 79s

The guestbook frontend will have to communicate to the Redis leader through a Service ressource. A Service in Kubernetes is an abstract way to expose an application running on a set of Pods as a network service.

Create the Redis Leader Service manifest.

redis-leader-service.yaml

apiVersion: v1
kind: Service
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: leader
tier: backend

Apply the service file against your cluster.

Terminal window
kubectl apply -f redis-leader-service.yaml

Check the Service to be sure that the Redis Service is running.

Terminal window
kubectl get service

The result should look similar to this:

Terminal window
redis-leader ClusterIP 100.66.106.69 <none> 6379/TCP 5s

To retrieve the data from the Redis Database, we will add Pods to carry the demand.

Create the redis-follower-deployment.yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: follower
tier: backend
spec:
containers:
- name: follower
image: registry.ske.eu01.stackit.cloud/test/guestbook-redis-follower:v1
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

Apply the Deployment file against your cluster.

Terminal window
kubectl apply -f redis-follower-deployment.yaml

Check your Pods to be sure that the Redis Pod is running.

Terminal window
kubectl get pods

The result should look similar like this:

Terminal window
redis-follower-dddfbdcc9-2sc2h 1/1 Running 0 9s
redis-follower-dddfbdcc9-wkrbg 1/1 Running 0 9s
redis-leader-fb76b4755-fmfq2 1/1 Running 0 79s

The frontend application needs to get data from the Redis Follower. To access the Pods, we will create another Service.

redis-follower-service.yaml

apiVersion: v1
kind: Service
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
ports:
- port: 6379
selector:
app: redis
role: follower
tier: backend

Apply the service file against your cluster.

Terminal window
kubectl apply -f redis-follower-service.yaml

Check the Service to be sure that the Redis Service is running.

Terminal window
kubectl get service

The result should look similar to this:

Terminal window
redis-follower ClusterIP 100.69.108.108 <none> 6379/TCP 32s
redis-leader ClusterIP 100.66.106.69 <none> 6379/TCP 5s

The frontend application will receive guestbook entries and store it into the Redis database. It is configured to talk to either the Redis leader or follower, depending on if the application receives a new guestbook entry or if it should serve existing ones.

Create the frontend-deployment.yaml.

frontend-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: registry.ske.eu01.stackit.cloud/test/guestbook-frontend:v1
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80

Apply the deployment file against your cluster.

Terminal window
kubectl apply -f frontend-deployment.yaml

Check the Pods to be sure that the Frontend Deployment works.

Terminal window
kubectl get pods

The result should look similar to this:

Terminal window
frontend-85595f5bf9-5p4xd 0/1 ContainerCreating 0 25s
frontend-85595f5bf9-n2ftd 0/1 ContainerCreating 0 25s
frontend-85595f5bf9-vnd7d 0/1 ContainerCreating 0 25s

To access the application from outside the Cluster, we need a service to configure the application externally visible.

Create the frontend-service.yaml.

frontend-service.yaml

apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: guestbook
tier: frontend

Apply the Service file against your cluster.

Terminal window
kubectl apply -f frontend-service.yaml

Check the Service to be sure it works.

Terminal window
kubectl get service

The result should look similar to this:

Terminal window
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 100.70.46.37 <pending> 80:31620/TCP 11s
kubernetes ClusterIP 100.64.0.1 <none> 443/TCP 87d
redis-follower ClusterIP 100.69.108.108 <none> 6379/TCP 18h
redis-leader ClusterIP 100.66.106.69 <none> 6379/TCP 18h

Notice that the frontend External-IP is in the state pending. Wait a couple of seconds and check your service again. Now you should get something similar like this:

Terminal window
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 100.70.46.37 193.148.175.214 80:31620/TCP 66s

Use the External-IP and paste it into the webbrowser of your choice. You should see something similar to this: