Skip to content

Latest commit

 

History

History
229 lines (141 loc) · 5.06 KB

07a-containerizing-for-deploy.md

File metadata and controls

229 lines (141 loc) · 5.06 KB

Containerizing a process for deployment

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.

Task 1: Create a simple node.js process

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.

  1. 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);
  1. Create Dockerfile and enter the following code
  FROM node:6.9.2
  EXPOSE 8080
  COPY server.js .
  CMD node server.js
  1. Build the Docker image
$ docker build -t mynode:v1.0 .
  1. 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.

  1. Stop the locally running container
$ docker stop mynode-sample

Task 2: Deploy from the ICP cluster image repository

  1. login to the cluster repo (username: admin / pwd: admin)
$ docker login mycluster.icp:8500
  1. 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
  1. Edit the image to change the scope from namespace to global so anyone can run it
$ kubectl edit image hello-node -n default

  1. 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
  1. Expose the pod to the internet by creating a service
$ kubectl expose deployment irvnet-hello-node --type=LoadBalancer
  1. find the new service and get the new port
$ kubectl get services

  1. scale the deployment
$ kubectl scale deployment irvnet-hello-my-node --replicas=5

  1. Update the application to have different output... for our 2nd version

  2. 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.


Task 3: Push an updated image to the ICP image repository

  1. 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

  1. 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

  1. curl the open port to see the updated message

  2. rollback to version 1

$ kubectl rollout undo deployment/irvnet-hello-my-node

  1. cleanup
$ kubectl delete deployment irvnet-hello-my-node


Task 4: Deploying the sample process as a helm chart

  1. create a directory to keep the manifests
$ mkdir manifests

  1. 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
  1. clean up the environment
$ kubectl delete -f manifests/deployment.yaml

$ kubectl delete -f manifests/service.yaml
  1. 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

  1. install the cart from the exploded directory and review it
$ helm install -n sample-chart sample-chart

  1. cleanup
$ helm delete sample-chart

Summary: The containerized process has been packaged as a chart and tested as an exploded directory


Task 5: Uploading the chart to the ICP catalog

  1. update values.yaml so the default values show correctly at deployment time

  2. package the chart

$ helm package sample-charts
  1. log into the cluster with bx (using admin/admin)
$ bx pr login -a https://{icp-cluster-ip}:8443  --skip-ssl-validation
  1. load the helm chart into the repo
$ bx pr load-helm-chart --archive sample-chart-0.1.0.1.tgz
  1. sync repository (or the details won't show)