<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: bmwhopper</title>
    <description>The latest articles on DEV Community by bmwhopper (@bmwhopper).</description>
    <link>https://dev.to/bmwhopper</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F141361%2F47169a26-b6f1-4737-9171-ef4d0efae638.png</url>
      <title>DEV Community: bmwhopper</title>
      <link>https://dev.to/bmwhopper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bmwhopper"/>
    <language>en</language>
    <item>
      <title>How to Deploy a Express Node.js app on Kubernetes and an Intro to Containerisation</title>
      <dc:creator>bmwhopper</dc:creator>
      <pubDate>Fri, 01 Mar 2019 14:28:35 +0000</pubDate>
      <link>https://dev.to/bmwhopper/how-to-deploy-a-express-nodejs-app-on-kubernetes-and-an-intro-to-containerisation-2fin</link>
      <guid>https://dev.to/bmwhopper/how-to-deploy-a-express-nodejs-app-on-kubernetes-and-an-intro-to-containerisation-2fin</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wNxG4c9a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2ARgCNLyra9rfaJdWUxh38xw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wNxG4c9a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2ARgCNLyra9rfaJdWUxh38xw.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Containerisation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While container technology has existed for years, Docker really took it&lt;br&gt;
mainstream. A lot of companies and developers now use containers to ship their&lt;br&gt;
apps. Docker provides an easy to use interface to work with containers.&lt;/p&gt;

&lt;p&gt;However, for any non-trivial application, you will not be deploying “one&lt;br&gt;
container”, but rather a group of containers on multiple hosts. In this article,&lt;br&gt;
we’ll take a look at &lt;a href="https://kubernetes.io/"&gt;Kubernetes&lt;/a&gt;, an open-source system&lt;br&gt;
for automating deployment, scaling, and management of containerised&lt;br&gt;
applications.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;What Problem Does Kubernetes Solve?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With Docker, you have simple commands like &lt;code&gt;docker run&lt;/code&gt; or &lt;code&gt;docker stop&lt;/code&gt; to start/stop a container respectively. Unlike these simple commands that let you do operations on a single container, there is no &lt;code&gt;docker deploy&lt;/code&gt; command to push new images to a group of hosts.&lt;/p&gt;

&lt;p&gt;Many tools have appeared in recent times to solve this problem of “container orchestration”; popular ones being &lt;a href="http://mesos.apache.org/"&gt;Mesos&lt;/a&gt;, &lt;a href="https://docs.docker.com/engine/swarm/"&gt;Docker Swarm&lt;/a&gt; (now part of the Docker engine), &lt;a href="https://www.nomadproject.io/"&gt;Nomad&lt;/a&gt;, and Kubernetes. All of them come with their pros and cons but, recently we have seen, Kubernetes take a considerable lead in usage and features.&lt;/p&gt;

&lt;p&gt;Kubernetes (also referred to as ‘k8s’) provides powerful abstractions that completely decouple application operations such as deployments and scaling from underlying infrastructure operations. So, with Kubernetes, you do not work with individual hosts or virtual machines on which to run you code, but rather Kubernetes sees the underlying infrastructure as a &lt;em&gt;sea of compute&lt;/em&gt; on which to put containers.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Kubernetes Concepts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Kubernetes has a client/server architecture. Kubernetes server runs on your &lt;em&gt;cluster&lt;/em&gt; (a group of hosts) on which you will deploy your application. And you typically interact with the cluster using a client, such as the &lt;code&gt;kubectl&lt;/code&gt; CLI.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Pods&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A pod is the basic unit that Kubernetes deals with, a group of containers. If there are two or more containers that always need to work together, and should be on the same machine, make them a &lt;code&gt;pod&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Node&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A node is a physical or virtual machine, running Kubernetes, onto which pods can be scheduled.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Label&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A label is a key/value pair that is used to identify a resource. You could label all your &lt;code&gt;pods&lt;/code&gt; serving production traffic with “role=production”, for example.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Selector&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Selections let you search/filter resources by labels. Following on from the previous example, to get all production pods your selector would be “role=production”.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Service&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A service defines a set of pods (typically selected by a “selector”) and a means by which to access them, such as single stable IP address and corresponding DNS name.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Deploy a Express Node.js App on OKE using Kubernetes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now, that we are aware of basic Kubernetes concepts, let’s see it in action by deploying a Node.js application on First of all, if you don’t have access to OCI, please go to &lt;a href="https://myservices.us.oraclecloud.com/mycloud/signup?sourceType=:so:bl:or::RC_WWSA180813P00054:Q4_MM_BM&amp;amp;SC=:so:bl:or::RC_WWSA180813P00054:Q4_MM_BM&amp;amp;pcode=WWSA180813P00054"&gt;Try it | OCI&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;1. Install Kubernetes Client&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;kubectl&lt;/code&gt; is the command line interface for running commands against Kubernetes clusters. &lt;code&gt;kubectl&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;To verify the installation run &lt;code&gt;kubectl version&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;2. Create a Docker Image of your application&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here is the application that we’ll be working with: &lt;a href="https://github.com/bmwhopper/express.js-hello-world"&gt;express.js-hello-world&lt;/a&gt;. You can see in the &lt;a href="https://github.com/bmwhopper/express.js-hello-world/blob/master/Dockerfile"&gt;Dockerfile&lt;/a&gt; that we are using an existing Node.js image from &lt;a href="https://hub.docker.com/"&gt;dockerhub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, we’ll build our application image by running:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t hello-world-image .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Run the app locally by running:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name hello-world -p 3000:3000 hello-world-image
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you visit &lt;code&gt;localhost:3000&lt;/code&gt; you should get the response.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;3. Create a cluster&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now we’ll create a cluster with three nodes (virtual machines), on which we’ll deploy our application. You can do this easily using the container &lt;a href="https://console.eu-frankfurt-1.oraclecloud.com/containers/clusters"&gt;container engine page&lt;/a&gt; in your free OCI account.&lt;/p&gt;

&lt;p&gt;The first thing you need to do for creating an OKE cluster is too give your Kubernetes access to manage resources in your tenancy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can do this by adding the following **&lt;a href="https://docs.cloud.oracle.com/iaas/Content/ContEng/Concept/contengpolicyconfig.htm"&gt;policy to your compartment&lt;/a&gt;&lt;/strong&gt;:**&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Allow service OKE to manage all-resources in tenancy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You will then be able to access the OKE container console and get started creating your cluster, as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iVoLNiKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2AfPkuiYF_xrP1sMsGvfce5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iVoLNiKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2AfPkuiYF_xrP1sMsGvfce5g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have &lt;strong&gt;2&lt;/strong&gt; options when creating your cluster, “&lt;strong&gt;Quick&lt;/strong&gt;” or “&lt;strong&gt;Custom&lt;/strong&gt;” create:&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Quick Create:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Allows you too quickly create a cluster with default settings, also creates a dedicated network.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Custom Create:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Create a cluster with custom settings, assumes an existing network.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;For this you can choose whichever is more applicable for your needs, for my cluster I chose “Quick Create”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this tutorial we will create a cluster with 3 nodes, the master and 2 worker nodes. We are using the VM.Standard 2.1 machine type because for this app we do not need larger compute power.&lt;/p&gt;

&lt;p&gt;Once your Cluster is up and running we can connect it to&lt;code&gt;kubectl&lt;/code&gt;, so that we have access to the cluster from our Kubernetes command line. You can do this by accessing the “kubeconfig: This can be downloaded from the “Getting Started” menu as seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2JtQ5b_d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2AnoZWbFoVep65IjDco4FZNA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2JtQ5b_d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/1%2AnoZWbFoVep65IjDco4FZNA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;4. Upload Docker Image to Oracle Container Image Registry&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;So, now we have a docker image and a cluster. We want to deploy that image to our cluster and start the containers, which will serve the requests.&lt;/p&gt;

&lt;p&gt;The Oracle container image registry is a cloud registry where you can push your images and these images automatically become available to your container engine cluster. To push an image, you have to build it with a proper name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To build the container image of this application and tag it for uploading, run the following command:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker tag bmwhopper/helloworld:latest &amp;lt;region-code&amp;gt;.ocir.io/&amp;lt;tenancy-name&amp;gt;/&amp;lt;repo-name&amp;gt;/&amp;lt;image-name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;v1&lt;/code&gt; is the &lt;em&gt;tag&lt;/em&gt; of the image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next step is to upload the image we just built to OCIR:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker push &amp;lt;region-code&amp;gt;.ocir.io/&amp;lt;tenancy-name&amp;gt;/&amp;lt;repo-name&amp;gt;/&amp;lt;image-name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For more detailed steps on image tagging and building see the detailed guide on &lt;a href="https://www.oracle.com/webfolder/technetwork/tutorial%20/obe/oci/registry/index.html"&gt;how to do this here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;5. First Deployment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now we have a cluster and an image in the cloud. Let’s deploy that image on our cluster with Kubernetes. We’ll do that by creating a &lt;code&gt;deployment&lt;/code&gt; spec file. Deployments are a kubernetes resource and all kubernetes resource can be defined by a spec file. This spec file lays-out the desired state of that resource and then Kubernetes figures out how to go from the current state to the desired state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So let’s create one for our first deployment:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c8mbKyEL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AOFE7T4E0L7Rr__PW5WcDSw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c8mbKyEL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AOFE7T4E0L7Rr__PW5WcDSw.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment.yaml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;apiVersion: apps/v1beta1&lt;br&gt;
kind: Deployment&lt;br&gt;
metadata:&lt;br&gt;
  name: hello-world-deployment&lt;br&gt;
spec:&lt;br&gt;
  replicas: 2&lt;br&gt;
  template:&lt;br&gt;
    metadata:&lt;br&gt;
      labels: # labels to select/identify the deployment&lt;br&gt;
        app: hello-world  &lt;br&gt;
    spec:     # pod spec                  &lt;br&gt;
      containers: &lt;br&gt;
      - name: hello-world &lt;br&gt;
        image: hello-world-image:v1 # image we pushed&lt;br&gt;
        ports:&lt;br&gt;
        - containerPort: 3000&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This spec file says: start two pods where each pod is defined by the given pod spec. Each pod should have one container containing the &lt;code&gt;hello-world-image:v1&lt;/code&gt; we pushed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, run:&lt;/strong&gt;&lt;br&gt;
    $ kubectl create -f deployment.yml --save-config&lt;/p&gt;

&lt;p&gt;You can see your deployment status by running &lt;code&gt;kubectl get deployments&lt;/code&gt;. To view the pod created by the deployment, run this command: &lt;code&gt;kubectl get pods&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You should see the running pods:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get pods
NAME                                     READY     STATUS    RESTARTS   AGE
hello-world-deployment-629197995-ndmrf   1/1       Running   0          27s
hello-world-deployment-629197995-tlx41   1/1       Running   0          27s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that we have two pods running because we set the replicas to 2 in the&lt;br&gt;
&lt;strong&gt;deployment.yml&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To make sure that the server started, check logs by running:&lt;/strong&gt;&lt;br&gt;
    $ kubectl logs {pod-name}  # kubectl logs hello-world-deployment-629197995-ndmrf&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6. Expose the Service to Internet&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we have the app running on our cluster we want to expose the service to Internet, you have to put your VMs behind a load balancer. To do that we create a Kubernetes &lt;code&gt;Service&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To do this run the following command:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl expose deployment hello-world-deployment --type="LoadBalancer"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Behind the scenes, it creates a &lt;code&gt;service&lt;/code&gt; object (a service is a Kubernetes resource, like a Deployment).&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;kubectl get services&lt;/code&gt; to see the public IP of your service. The console output should look like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME                     CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE
hello-world-deployment   10.244.0.16       *.*.*.*          3000:30877/TCP   27m
kubernetes               10.244.240.1      &amp;lt;none&amp;gt;           443/TCP          1d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Visit &lt;code&gt;http://&amp;lt;EXTERNAL-IP&amp;gt;:&amp;lt;PORT&amp;gt;&lt;/code&gt; to access the service. You can also buy a custom domain name and make it point to this IP.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;7. Scaling Your Service&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s say your service starts getting more traffic and you need to spin up more instances of your application. To scale up in such a case, just edit your &lt;em&gt;deployment.yml&lt;/em&gt; file and change the number of &lt;code&gt;replicas&lt;/code&gt; to, say, 3 and then run &lt;code&gt;kubectl apply -f deployment.yml&lt;/code&gt; and you will have three pods running in no time.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Wrapping Up&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We’ve covered a lot getting started material in this tutorial but as far as Kubernetes is concerned, this is only the tip of the iceberg. There is a lot more you can do, like scaling your services to more pods with one command, or mounting &lt;code&gt;secret&lt;/code&gt; on pods for things like credentials etc. However, this should be enough to get you started. For more information please feel free to reach out on &lt;a href="https://www.linkedin.com/in/brianbmathews/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/DevOps4Days?lang=en"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/tag/docker?source=post"&gt;Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/tag/javascript?source=post"&gt;JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/tag/kubernetes?source=post"&gt;Kubernetes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/tag/devops?source=post"&gt;DevOps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/tag/nodejs?source=post"&gt;Nodejs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://medium.com/@brianbmathews"&gt;Brian Mathews&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Technical Consultant and Evangelist with a focus on Serverless and DevOps. Why not give Oracle Cloud a try with $300 free credits! &lt;a href="https://bit.ly/2KQWy6k"&gt;https://bit.ly/2KQWy6k&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
