/networkcontainertesting

This is a simple way to test connectivity in and out of a Docker container

Primary LanguageHTMLApache License 2.0Apache-2.0

networkcontainertesting

This is a simple way to test connectivity in and out of a Docker container

Background

I had a problem: I needed to have a simple app to deploy as a container that I could use to test and see if my containers, either standalone or within a Kubernetes pod, had outside connectivity to the Internet. To do that, I wrote a simple node.js application that uses express, some HTML and jQuery. The node.js app either responds directly to the user or it routes the request to the HTML. The HTML use simple javascript to call the API and process the returned Json and then display it using jQuery.

Bonus: a simpler set up

Some of you may not want to use node.js - you just want to test using Docker and Kubernetes. To facilitate that, I have added two Files

  • D24indexfile
  • index.HTML

if you get rid of the Dockerfile that comes with this repo and rename D2indexfile to Dockerfile, you can skip Step 1 and go to Step 2. You will have a simpler set up: it's just a web server with one web page and no integration.

If you go with this route, you do not need npm or node.

Assumptions

You have the following on your machine
  1. git
  2. Docker
  3. npm
  4. node
  5. ibmcloud cli
  • You also have an account on ibm.com/cloud

How to use

  1. Get a command prompt and go to a directory you can download this repository (e.g. /temp)
  2. Clone the repo into this directory with this command:
  1. Enter: cd networkcontainertesting
  2. Enter: npm install

You are now ready to start testing.

Step 1: test it as a node.js app running locally on your machine:

  1. Enter: node app.js
  2. In your browser, enter: http://localhost:8080
  3. In your browser, enter: http://localhost:8080/useless
  4. In your browser, enter: http://localhost:8080/random
  5. In your browser, enter: http://localhost:8080/fox
  6. From the window you entered node app.js, enter: ctrl+``` or ctrl+c

Step 2: test it in a Docker container

  1. Build your docker image. Enter: docker build -t networkcontainertesting .
  2. Check your image is running. Enter: docker images
  3. Create a container with your image. Enter: docker run --name networkcontainertesting -p 80:8080 -d networkcontainertesting
  4. In your browser, enter: http://localhost
  5. In your browser, enter: http://localhost/useless
  6. In your browser, enter: http://localhost/random
  7. In your browser, enter: http://localhost/fox
  8. If you have any problems, see if it is running. Enter: docker ps
  9. Now stop the container. Get the container id by running: docker ps
  10. You can stop it by entering: docker stop ID where ID is either the container id or the name of the container (i.e.networkcontainertesting)

Step 3: To test it in an IBM Kubernetes cluster

Assuming you have a cluster running already, and you have the ibmcloud command on your machine, do the following:

  1. Login. Enter: ibmcloud login (I like to login with the --sso option)
  2. Assuming your cluster is running in US South, enter:
  • ibmcloud target -r us-south (Washington and Toronto are in us-east) (You may not need this)
  1. Check your cluster is available. Enter: ibmcloud cs clusters
  • Mine is called cloudnativedev.
  1. Enter: ibmcloud ks cluster config --cluster cloudnativedev
  2. Login to the container registry. Enter: ibmcloud cr login
  3. Get a list of your namespaces. Enter: ibmcloud cr namespace-list (mine is blm849namespace)
  4. Build your image using the information that came back. Enter: docker build --no-cache -t ca.icr.io/blm849namespace/hello-world:1 . Try and use a unique tag. I am using "1" here but if you use the same tag over and over again, you may have issues with your deployment not being updated.
  5. Push it to the container registry. Enter: docker push ca.icr.io/blm849namespace/hello-world:1
  6. Deploy the app to a single pod with the name hello-world-deployment. Enter: kubectl run hello-world-deployment --image=ca.icr.io/blm849namespace/hello-world:1
  7. Make the app available to the world. Enter: kubectl expose deployment/hello-world-deployment --type=NodePort --port=8080 --name=hello-world-service --target-port=8080
  8. Determine what the nodeport is. Enter: kubectl describe service hello-world-service
  9. Determine the public facing IP of node. Enter: ibmcloud ks workers --cluster cloudnativedev
  10. Combine the public IP and nodeport to call your app. Enter in your browser:
  • http://public_IP:nodeport

  • http://public_IP:nodeport/random

  1. If you want to discontinue your application from running, enter:
  • kubectl delete deployment/hello-world-deployment
  • kubectl delete service/hello-world-service
  • ibmcloud cr image-rm blm849namespace/hello-world:1

Files

This repo consists of the following files and directories:

  • app.js: the main node.js code
  • Dockerfile: the file used to build a container to run app.js in
  • package.json: a list of the dependencies associated with app.js
  • views: this directory consists of HTML and simple javascript files. The HTML calls the javascript of the same name, the javascript calls an external API, gets the results, and rewrites the HTML with the results of the API call. There is a hello.html and .js file and a random.html and js file.
  • Dockerignore: when working with Docker, rename this file to .Dockerignore
  • README.md: this file
  • other files: LICENCE is the licence file for the repo, and .gitignore prevents some files from not being upload.
  • D24indexfile: an alternative Dockerfile
  • index.HTML: a file to go with the alternative Dockerfile

References

The instructions in this README were partially derived from:

  1. Docker: https://www.digitalocean.com/community/tutorials/how-to-build-a-node-js-application-with-docker
  2. IBM. https://cloud.ibm.com/docs/containers?topic=containers-cs_cluster_tutorial#cs_cluster_tutorial to create a cluster and https://cloud.ibm.com/docs/containers?topic=containers-cs_apps_tutorial#cs_apps_tutorial to deploy an app into your cluster.