Deploy More Complex Microservice Apps Using Platform9 Managed Kubernetes Free Tier
In this example, we are going to see the deployment of a Redis master, Redis slaves, and a sample guestbook application that uses Redis as a store.
Join the DZone community and get the full member experience.Join For Free
In this tutorial, we are going to expand our examples by deploying a more complex microservice on Platform9 Managed Kubernetes Free Tier (PMKFT). The idea is to make you more comfortable with the platform and to show you how you can leverage it for more advanced scenarios.
In this example, we are going to see the deployment of:
- A Redis master
- Multiple Redis slaves
- A sample guestbook application that uses Redis as a store
We assume that you have already set up a Platform9 cluster with at least one node, and the cluster is ready. If you haven't sign up now for free: https://platform9.com/signup/
Let's start with the Redis parts.
Deploying and Exposing a Redis Cluster
Redis is a key-value in-memory store that is used mainly as a cache service. To set up Clustering for Data Replication, we need a Redis instance that acts as Master, together with additional instances as slaves. Then the guestbook application can use this instance to store data. The Redis master will propagate the writes to the slave nodes.
We can initiate a Redis Master deployment in a few different ways: either using the kubectl tool, the Platform9 UI or the Kubernetes UI. For convenience, we use the kubectl tool as it's the most commonly understood in tutorials.
First, we need to create a Redis Cluster Deployment. Looking at their documentation here, to set up a cluster, we need some configuration properties. We can leverage Kubernetes configmaps to store and reference them in the deployment spec.
We need to save a script and a Redis.conf file that is going to be used to configure the master and slave nodes.
Create the following config Redis-cluster.config.yml
With these values:
We define a script that will insert an IP value to the node.conf file. This is to fix an issue with Redis as referenced here. We use this script every time we deploy a new Redis image.
Then we have the redis.conf, which applies the minimal cluster configuration.
Apply this spec into the cluster:
Then verify that it exists in the list of configmaps:
Next, we need to define a spec for the Redis cluster instances. We can use a Deployment or a StatefulSet to define 3 instances:
Here is the spec: Redis-cluster.statefulset.yml
In the above step we defined a few things:
- An IP environment variable that we need in the update-IP.sh script that we defined in the configmap earlier. This is the pod-specific IP address using the Downward API.
- Some shared volumes including the configmap that we defined earlier.
- Two container ports - 6379 and 16379 - for the gossip protocol.
With this spec we can deploy the Redis cluster instances:
Once we verify that we have the deployment-ready, we need to perform the last step, which is bootstrapping the cluster. Consulting the documentation here for creating the cluster, we need to ssh into one of the instances and run the Redis-CLI cluster create command. For example, taken from the docs:
To do that in our case, we need to get the local pod IPs of the instances and feed them to that command.
We can query the IP using this command:
So if we save them in a variable or a file, we can pipe them at the end of the Redis-CLI command:
Then we can run the following command:
If everything is OK, you will see the following prompt. Enter 'yes' to accept and continue:
Then we can verify the cluster state by running the cluster-info command:
Before we continue deploying the guestbook app, we need to offer a unified service frontend for the Redis Cluster so that it's easily discoverable in the cluster.
Here is the service spec: Redis-cluster.service.yml
We expose the cluster as Redis-master here, as the guestbook app will be looking for a hosting service to connect to with that name.
Once we apply this service spec, we can move on to deploying and exposing the Guestbook Application:
Deploying and Exposing a GuestBook Application
The guestbook application is a simple PHP script that shows a form to submit a message. Initially, it will attempt to connect to either the
redis-master host or the
It needs the GET_HOSTS_FROM environment variable set pointing to the file with the following variables:
- REDIS_MASTER_SERVICE_HOST: of the master
- REDIS_SLAVE_SERVICE_HOST: of the slave
First, let's define the deployment spec bellow:
The code of the GB-frontend image is located here.
Next is the associated service spec:
Note: NodePort will assign a random port over the public IP of the Node. In either case, we get a public host:port pair where we can inspect the application. Here is a screenshot of the app after we deployed it:
Once we have finished experimenting with the application, we can clean up the resources and all the servers by issuing
kubectl delete statements. A convenient way is to delete labels.
Deploying multi-container systems in Kubernetes with Platform9 is no different than usual. The big difference is that you gain a quality of server and a maintenance-free Kubernetes experience with first-class support for troubleshooting issues. That's not to mention that you can host the cluster on your bare-metal server or AWS, eliminating any vendor lock-ins. For more information visit Platform9 Managed Kubernetes page.TFORM
Published at DZone with permission of Kamesh Pemmeraju, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.