Excercise - 5
In this section, you will re-create the guestbook application, but this time with more components in a multi-tier architecture. This application uses the v2
version of the go application we used previously in the workshop as our web front end, and adds on 1) a Redis master for storage, 2) a replicated set of Redis slaves, and 3) a Python Flask application that calls the Watson Tone Analyzer service deployed in IBM Cloud. For all of these components, there are Kubernetes replication controllers, pods, and services. One of the main concerns with building a multi-tier application on Kubernetes, such as this one, is resolving dependencies between all of these seperately deployed components.
In a multiple tier application, there are two primary ways that service dependencies can be resolved. The v2/guestbook/main.go
code provides examples of each. For Redis, the master endpoint is discovered through environment variables. These environment variables are set when the Redis services are started, so the service resources need to be created before the guestbook replication controller starts the guestbook pods. For the analyzer service, an http request is made to a hostname, which allows for resource discovery at the time when the request is made. Consequently, we'll follow a specific order when creating the application components. First up, the Redis components will be created, then the guestbook application, and finally the analyzer microservice.
Setup
Continue by working in the web terminal. Change to the v2
folder where the deployment files reside:
Create the Redis master pod
Use the redis-master-deployment.yaml
file to create a replication controller and Redis master pod. The pod runs a Redis key-value server in a container. Using a replication controller is the preferred way to launch long-running pods, even for 1 replica, so that the pod inherits benefits from the self-healing mechanism in Kubernetes (i.e. keeps the pods alive).
Use the redis-master-deployment.yaml file to create the Redis master deployment in your Kubernetes cluster:
To verify that the redis-master controller is up, list the deployment and replicaset you created in the cluster with the
kubectl get
command (if you don't specify a--namespace
, the current project/namespace will be used):this will show the current deployments in the namespace
this will show the current deployments in the namespace
Result: The deployment creates the replicaset, which then creates the single Redis master pod.
Verify that the redis-master pod is running, by listing the pods you created in cluster:
Result: You'll see a single Redis master pod (may take up to thirty seconds).
Create the Redis master service
A Kubernetes service is a named load balancer that proxies traffic to one or more pods. The services in a Kubernetes cluster are discoverable inside other pods via environment variables or DNS.
Services find the pods to load balance based on pod labels. The pod that you created in previous step has the label app=redis
and role=master
. The selector field of the service determines which pods will receive the traffic sent to the service.
To verify that the redis-master service is up, list the services you created in the cluster:
Result: All new pods will see the
redis-master
service running on the host ($REDIS_MASTER_SERVICE_HOST
environment variable) at port6379
, or running onredis-master:6379
. After the service is created, the service proxy on each node is configured to set up a proxy on the specified port (in our example, that's port6379
).
Create the Redis slave pods
The Redis master we created earlier is a single pod (REPLICAS = 1), while the Redis read slaves we are creating here are 'replicated' pods with 2 instances that will be started. In Kubernetes, a replication controller is responsible for managing the multiple instances of a replicated pod.
To verify that the redis-slave controller is running:
Result: The deployment creates the replicaset, which then creates configures the Redis slave pods through the redis-master service (name:port pair, in our example that's
redis-master:6379
).Verify that the Redis master and slaves pods are running:
Result: You see the single Redis master and two Redis slave pods.
Create the Redis slave service
Just like the master, we want to have a service to proxy connections to the read slaves. In this case, in addition to discovery, the Redis slave service provides transparent load balancing to clients.
To verify that the redis-slave service is up, list the services you created:
Result: The service is created and accessible at
redis-slave:6379
by pods running in the project
Create the guestbook pods
This is a simple Go net/http
(negroni based) server that is configured to talk to either the slave or master services depending on whether the request is a read or a write. The pods we are creating expose a simple JSON interface and serves a jQuery-Ajax based UI. Like the Redis read slaves, these pods are also managed by a replication controller.
Tip: If you want to modify the guestbook code it can be found in the
guestbook
directory, along with its Makefile. If you have pushed your custom image be sure to update theimage
property accordingly in the guestbook-deployment.yaml.
Verify that the guestbook deployment is running:
Verify that the guestbook pods are running (it might take up to thirty seconds to create the pods):
Result: You see a single Redis master, two Redis slaves, and three guestbook pods.
Create and expose the guestbook service
Just like the others, we create a service to group the guestbook pods. Since guestbook uses a web application protocol we will expose it for access outside the cluster using a service of type `NodePort.
Use the guestbook-service.yaml file to create the guestbook service:
Verify that the guestbook service is up by listing the services in the cluster:
Result: The service is created, and exposed as a NodePort and in this example is listening on
30796
.
Create the analyzer pod
This is a simple Python Flask application that creates a POST endpoint /tone
and takes the input text and sends it to the Watson Tone Analyzer service. In the analyzer-deployment.yaml the spec for the pod defines environment variables for the service credentials by reading the secret binding-tone
created by the IBM Cloud operator.
Use the analyzer-deployment.yaml file to create the analyzer replication controller:
Tip: If you want to modify the analyzer code it can be found in the
analyzer
directory, along with its Makefile. If you have pushed your custom image be sure to update theimage
property accordingly in the analyzer-deployment.yaml.Verify that the guestbook deployment is running:
Create the analyzer service
Create a service so that the guestbook application can call the analyzer pod
Use the analyzer-service.yaml file to create the analyzer service:
To verify that the analyzer service is up, list the services created in the cluster:
Result: The service is created
View the guestbook
You can now play with the guestbook that you just created by opening it in a browser, use the IP and NodePort for your deployment. Find the IP address for your cluster using this command (use the Public IP):
In this example the IP is: 184.172.252.167
Get the nodeport (it will be different from the first exercise):
For this value of IP address and NodePort, you would use a url like http://184.172.252.167:30796
to access the guestbook.
Result: The guestbook displays in your browser:
Cleanup
After you're done playing with the guestbook, you can cleanup by deleting the guestbook service and removing the associated resources that were created, including routes, forwarding rules, target pools, and Kubernetes replication controllers and services.
Delete all the resources sourced by the files in the v2
directory:
Last updated