In this section, you will learn how to capture a simple node.js process in a docker container to run on the ICP platform. In addition you'll see how to wrap a docker image in a helm chart for deployment from the catalog.
Development processes such as these are typically sped up with enhancements such as automation DevOps processes, however this is a helpful to get a fundamental look at how some of the underlying parts of the process work.
In this section we'll create a simple node.js process and wrap it in a docker container. After testing that the container works locally as expected, we'll push the container to the IBM Cloud Private registry and run it in our environment.
- create server.js using your preferred editor and add the following code:
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);
- Create Dockerfile and enter the following code
FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js
- Build the Docker image
$ docker build -t mynode:v1.0 .
- Run the image and test it locally
$ docker run --rm -d -p 8080:8080 --name mynode-sample hello-node:v1
For a quick test of the container running locally on your machine, In your browser, access http://localhost:8080.
- Stop the locally running container
$ docker stop mynode-sample
- login to the cluster repo (username: admin / pwd: admin)
$ docker login mycluster.icp:8500
- Tag the image for pushing to the cluster's image repo
$ docker tag hello-node:v1 mycluster.icp:8500/default/hello-node:v1
$ docker push mycluster.icp:8500/default/hello-node:v1
- Edit the image to change the scope from namespace to global so anyone can run it
$ kubectl edit image hello-node -n default
- Run the image from the command line and see the available deployment in the dashboard
$ kubectl run irvnet-hello-my-node --image=mycluster.icp:8500/default/hello-node:v1 --port=8080
- Expose the pod to the internet by creating a service
$ kubectl expose deployment irvnet-hello-node --type=LoadBalancer
- find the new service and get the new port
$ kubectl get services
- scale the deployment
$ kubectl scale deployment irvnet-hello-my-node --replicas=5
-
Update the application to have different output... for our 2nd version
-
Build v2 of the application
$ docker build -t hello-node:v2 .
Summary: The image with the node process has been tested on the ICP cluster showing several capabilities that deployments offer.
- tag the image and push to the cluster's image repo
$ docker tag hello-node:v2 mycluster.icp:8500/default/hello-node:v2
$ docker push mycluster.icp:8500/default/hello-node:v2
- deploy the updated version to the cluster
$ kubectl set image deployment/irvnet-hello-my-node irvnet-hello-my-node=mycluster.icp:8500/default/hello-node:v2
-
curl the open port to see the updated message
-
rollback to version 1
$ kubectl rollout undo deployment/irvnet-hello-my-node
- cleanup
$ kubectl delete deployment irvnet-hello-my-node
- create a directory to keep the manifests
$ mkdir manifests
- re-run the deployment and redirect the output into manifest files for the deployment and service
$ kubectl run irvnet-hello-my-node --image=mycluster.icp:8500/default/hello-node:v1 --port=8080 -o yaml > manifests/deployment.yaml
$ kubectl expose deployment irvnet-hello-my-node --type=NodePort -o yaml > manifests/service.yaml
- clean up the environment
$ kubectl delete -f manifests/deployment.yaml
$ kubectl delete -f manifests/service.yaml
- init helm project and add in our new definitions
$ helm create sample-chart
$ cp manifests/deployment.yaml sample-chart/templates/deployment.yaml
$ cp manifests/service.yaml sample-chart/templates/service.yaml
$ rm sample-chart/templates/ingress.yaml
- install the cart from the exploded directory and review it
$ helm install -n sample-chart sample-chart
- cleanup
$ helm delete sample-chart
Summary: The containerized process has been packaged as a chart and tested as an exploded directory
-
update values.yaml so the default values show correctly at deployment time
-
package the chart
$ helm package sample-charts
- log into the cluster with bx (using admin/admin)
$ bx pr login -a https://{icp-cluster-ip}:8443 --skip-ssl-validation
- load the helm chart into the repo
$ bx pr load-helm-chart --archive sample-chart-0.1.0.1.tgz
- sync repository (or the details won't show)