Configuring Ingress Controller on IBM Cloud Private

Learn how to configure access logging for your Node.js express applications

Todd Kaplinger
IBM Cloud
7 min readFeb 13, 2018

--

Overview

Skill Level: Beginner
Some basic understanding of Kubernetes and deploying resources
In this recipe, I demonstrate how to enable access logging in IBM Cloud Private using the built in Ingress. This recipe will teach you how to deploy an Ingress routing requests to a Node.js application based upon Express.

Ingredients

To get started, you should have an elementary understanding of Kubernetes and have installed IBM Cloud Private, Docker, and Node.js into your development environment. In this tutorial, I will demonstrate some basic concepts around creating a simple Node.js Express application and pairing the resource with an Ingress Controller. I will also show how to deploy these YAML files as Kubernetes Resources.

IBM Cloud Private Installation Guide: https://www.ibm.com/support/knowledgecenter/en/SSBS6K_2.1.0/installing/install_containers_CE.html
Docker Install: https://docs.docker.com/install/
Node.js Install:https://nodejs.org/en/download/

Step-by-step

To demonstrate the power of Ingress controllers, I want to create a basic Express application written using Node.js that is easy to write and explain for this article. In the application, I have defined two Express routes that return a message back to the user with a user provided payload appended to the end. These two routes will respond based upon the match of either `foo` or `helloworld`.

server.js

As this application has a dependency on Express, I will add this dependency to my package.json of my application. This will be used later on when packaging my Docker image and deploying to IBM Cloud Private.

package.json

Pushing the application to Docker Hub

Now that we have created the script and done some local testing, we are now ready to package this and push the image to Docker Hub (or whatever registry you choose to leverage). To publish the image, we need to define the Dockerfile which we will use to build and publish the artifact.

The Dockerfile I am using is a standard Dockerfile that packages the depedencies and the main entry point into the application named server.js

Dockerfile

To package the container and publish the image, I wrote a simple script that refers to this Dockerfile.

deployAppDocker.sh

Once you set the appropriate execute permissions on this script, run the deployAppDocker.sh script from the same directory as your package.json, server.js and Dockerfile. The built image will be published as todkap/node-ingress:v2 and willl used later on in the article when we deploy to IBM Cloud Private. Once this script completes, we are ready to deploy this container to IBM Cloud Private but first we need to learn how to create a Kubernetes resource.

Deploying application to IBM Cloud Private

We are now at the point where we can deploy the resource to IBM Cloud Private. Since we want to deploy this as a Kubernetes resource, we need to first define the Deployment yaml file that will not only create the deployment artifact but also will create the service.

deployment.yaml

Now that the we have defined the Service and Deployment types for this deployment, we can now run the command `kubectl apply -f deployment.yaml` that will deploy this application to IBM Cloud Private. Once the pods have listed as running (you can verify using the command `kubectl get pods` and look for the prefix node-app). Once the status is running, we are ready for the next step of configuring the Ingress.

Defining ingress rules

We are now ready to define our Ingress. Ingress is a great way to provide a single point of entry for your Kubernetes resources. Ingresses such as Nginx provide features such as SSL termination, URL rewriting and a host of other features that are found in many proxies in the market today. In this section, we will define a set of rules that will route our two Express routes from the Ingress to our application using our ingress.yaml.

ingress.yaml

Now that the we have defined the ingress resource, we can now run the command `kubectl apply -f ingress.yaml` which will configure the ingress to route requests to our application.

To verify this is working, locate the proxy node of your deployment and find the ip address for this deployment. In the example I provided, I set PROXY to the proxy node of my IBM Cloud Private deployment. Once you have that value, you can curl the various REST APIs as follows…

Enabling access logging

The final step to tie all of the pieces together is to enable access logging. By default, IBM Cloud Private has disabled access logging but provides a simple way to enable it by editing one of the IBM Cloud Private ConfigMap resources.

To enable access logging, edit the ConfigMap resource nginx-load-balancer-conf. To edit this resource, go to the command line and use the following command.

In this resource, change the disable-access-log parameter to false.

Once this is changed, save the file for the change to take effect.

Now that this change has been made, let’s repeat the same CURL commands we did previously.

And now we can view the logs for the pod. In IBM Cloud Private, the pod name for the Ingress starts with nginx-ingress-lb. You can list the pods `kubectl get pods –namespace=kube-system` to find the list of pods.

Once I located my pod, I ran the following command `kubectl logs nginx-ingress-lb-amd64–2frlr –namespace=kube-system` and at the bottom of the logs, I see access logs interspersed with the various pod events.

We have now successfully demonstrated how to deploy our application to leverage the IBM Cloud Private Ingress and also to enable access logging.

Conclusion

Successful deployments of large scale applications require transparency and insight of application and their environments. By enabling access logging, we are able to quickly track the status of our deployments in relation to their HTTP endpoint health status and can easily integrate access logging with our overall logging and monitoring strategy. In this recipe, we showed how one can take a Node.js application, configure the Ingress to route to this application and ultimate start to track the traffic this application is handling in terms of HTTP requests. This simple but powerful scenario can easily be scaled up to many enterprise deployments we are seeing on IBM Cloud Private.

Bonus content

Shortly after this article was published, there was a set of users asking about the role of the annotation rewrite-target in many of the Nginx Ingress examples. Here is a quick example of the purpose and how it would work.

ingress.yaml

In the example above, I have defined the rewrite target of `/` and the path for the route to be `/banking`. This is very useful if you want to have specific URL patterns to route to existing applications without modifying the original application deployment and REST APIs.

I would then still add the path to my request as follows where the remainder of the path elements remain the same as what I demonstrated above.

--

--

Todd Kaplinger
IBM Cloud

Vice President SW Engineering — Chief Architect, Retail Solutions@NCR Voyix. The opinions expressed here are my own. Follow me on Twitter @todkap