<?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: AkankshaPriyadrshini</title>
    <description>The latest articles on DEV Community by AkankshaPriyadrshini (@itsakankxa).</description>
    <link>https://dev.to/itsakankxa</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%2F373881%2Ffacf74fa-0570-4681-bbc2-4098e0ee39c9.jpg</url>
      <title>DEV Community: AkankshaPriyadrshini</title>
      <link>https://dev.to/itsakankxa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsakankxa"/>
    <language>en</language>
    <item>
      <title>Node JS: How to access MySQL remotely using SSH.</title>
      <dc:creator>AkankshaPriyadrshini</dc:creator>
      <pubDate>Tue, 16 Jun 2020 05:56:06 +0000</pubDate>
      <link>https://dev.to/itsakankxa/node-js-how-to-access-mysql-remotely-using-ssh-heo</link>
      <guid>https://dev.to/itsakankxa/node-js-how-to-access-mysql-remotely-using-ssh-heo</guid>
      <description>&lt;p&gt;Recently I was working on a Data Migration Project. For one of the cases, I had to connect to a MySql database through an SSH Tunnel.&lt;/p&gt;

&lt;p&gt;Here, I will show you how to get it done quickly.&lt;/p&gt;

&lt;p&gt;We will be using &lt;a href="https://www.npmjs.com/package/ssh2"&gt;ssh2&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/mysql2"&gt;mysql2&lt;/a&gt; npm packages. So, make sure to install these.&lt;/p&gt;

&lt;p&gt;Let’s get started by writing our database config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// dbConfig.js
// define connection config for the database
const dbServer = {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE
}
// define connection config for the ssh tunnel
const tunnelConfig = {
    host: process.env.DB_SSH_HOST,
    port: 22,
    username: process.env.DB_SSH_USER,
    password: process.env.DB_SSH_PASSWORD
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you have to connect to SSH using a permission file, use &lt;strong&gt;privateKey&lt;/strong&gt; key in place of &lt;strong&gt;password&lt;/strong&gt; in the &lt;strong&gt;tunnelConfig&lt;/strong&gt; object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const tunnelConfig = {
    host: process.env.DB_SSH_HOST,
    port: 22,
    username: process.env.DB_SSH_USER,
    privateKey:
    require('fs').readFileSync('&amp;lt;path to your permission file&amp;gt;')
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we will specify the forward configuration for SSH.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// dbConfig.js
...
const forwardConfig = {
    srcHost: '127.0.0.1', // any valid address
    srcPort: 3306, // any valid port
    dstHost: dbServer.host, // destination database
    dstPort: dbServer.port // destination port
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are good to set up an SSH connection now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// dbConfig.js
const mysql = require('mysql2');
const { Client } = require('ssh2');
// create an instance of SSH Client
const sshClient = new Client();
...
const SSHConnection = new Promise((resolve, reject) =&amp;gt; {
    ssh.on('ready', () =&amp;gt; {
        ssh.forwardOut(
        forwardConfig.srcHost,
        forwardConfig.srcPort,
        forwardConfig.dstHost,
        forwardConfig.dstPort,
        (err, stream) =&amp;gt; {
             if (err) reject(err);

            // create a new DB server object including stream
            const updatedDbServer = {
                 ...dbServer,
                 stream
            };
            // connect to mysql
            const connection =  mysql.createConnection(updatedDbServer);
            // check for successful connection
           //  resolve or reject the Promise accordingly          
           connection.connect((error) =&amp;gt; {
            if (error) {
                reject(error);
            }
            resolve(connection);
            });
       });
    }).connect(tunnelConfig.sshConfig);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Final Database Config File:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mysql = require('mysql2');
const { Client } = require('ssh2');
const sshClient = new Client();
const dbServer = {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE
}
const tunnelConfig = {
    host: process.env.DB_SSH_HOST,
    port: 22,
    username: process.env.DB_SSH_USER,
    password: process.env.DB_SSH_PASSWORD
}
const forwardConfig = {
    srcHost: '127.0.0.1',
    srcPort: 3306,
    dstHost: dbServer.host,
    dstPort: dbServer.port
};
const SSHConnection = new Promise((resolve, reject) =&amp;gt; {
    ssh.on('ready', () =&amp;gt; {
        ssh.forwardOut(
        forwardConfig.srcHost,
        forwardConfig.srcPort,
        forwardConfig.dstHost,
        forwardConfig.dstPort,
        (err, stream) =&amp;gt; {
             if (err) reject(err);
             const updatedDbServer = {
                 ...dbServer,
                 stream
            };
            const connection =  mysql.createConnection(updatedDbServer);
           connection.connect((error) =&amp;gt; {
            if (error) {
                reject(error);
            }
            resolve(connection);
            });
        });
    }).connect(tunnelConfig.sshConfig);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, &lt;strong&gt;SSHConnection&lt;/strong&gt; Promise can be used wherever required.&lt;/p&gt;

&lt;p&gt;Originally published on my &lt;a href="https://akanksha.io/post/node-js-how-to-access-mysql-remotely-using-ssh"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>ssh</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Spin up your first Kubernetes Cluster in under 30 mins.</title>
      <dc:creator>AkankshaPriyadrshini</dc:creator>
      <pubDate>Fri, 22 May 2020 08:19:36 +0000</pubDate>
      <link>https://dev.to/itsakankxa/spin-up-your-first-kubernetes-cluster-in-under-30-mins-2f2c</link>
      <guid>https://dev.to/itsakankxa/spin-up-your-first-kubernetes-cluster-in-under-30-mins-2f2c</guid>
      <description>&lt;p&gt;&lt;a href="https://kubernetes.io/"&gt;Kubernetes is an open-source system for automating deployment, scaling, and managing containerized applications.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It provides us with a platform to orchestrate different microservices or systems while deploying. It serves a load of features to manage our resources efficiently. To containerise an application, we need a base image which can be used while creating an instance of the container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Hub&lt;/strong&gt; is the centralised container image registry where we can host our images. These images can then be used for development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope of the Article:
&lt;/h3&gt;

&lt;p&gt;Here, we will look at the following things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Package application in a Docker container image.&lt;/li&gt;
&lt;li&gt;Host the image on Docker Hub.&lt;/li&gt;
&lt;li&gt;Set up a Kubernetes Cluster locally using &lt;strong&gt;MiniKube&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Deploy the image on a locally running Kubernetes cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the purpose of this article, let's quickly create a sample application in Node.js&lt;br&gt;
Create a directory for the application where all the files will reside.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir hello-world&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we will create &lt;code&gt;package.json&lt;/code&gt; file to describe the app and its dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// package.json
{
    "name": "docker_sample_app",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "akanksha priyadarshini",
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
   "dependencies": {
        "express": "^4.16.1"
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;npm install&lt;/code&gt; to install all the dependencies for our app.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;server.js&lt;/code&gt; file that will define our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// server.js
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) =&amp;gt; {
    res.send('Hello world');
});
app.listen(PORT, HOST);
console.log(`App is running on http://${HOST}:${PORT}`);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, let's create a file called &lt;strong&gt;Dockerfile&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://docs.docker.com/engine/reference/builder/"&gt;A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;touch Dockerfile&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;In this file, first we will define from what we want to build the image from. Here, we will use node version 10 available from the Docker Hub.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FROM node:10&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Next, we will specify the directory where our application code will reside inside the image. If the directory doesn't exist it will be created.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;WORKDIR /home/node/app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Copy package.json and package-lock.json and install the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY package*.json ./
RUN npm install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To bundle the app's source code inside the docker image, we can use COPY command.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COPY . .&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Since our app binds to port 8080. We will expose this port to be mapped by docker.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPOSE 8080&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Lastly, specify the command to run the application.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CMD [ "node", "server.js" ]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Final Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Dockerfile
FROM node:10
WORKDIR /home/node/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's create .dockerignore file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .dockerignore
node_modules
npm-debug.log
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we move further, head on to &lt;a href="https://hub.docker.com/"&gt;Docker Hub&lt;/a&gt;, create an account if you don't already have one.&lt;/p&gt;

&lt;p&gt;Let's create a repository named &lt;code&gt;hello-world&lt;/code&gt; here to keep our application's images to be used and shared.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vcGnvsTL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2xwqubvzxcrcuqgyunor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vcGnvsTL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2xwqubvzxcrcuqgyunor.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Build the docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sample app directory where Dockerfile is present
cd hello-world
docker build -t &amp;lt;your username&amp;gt;/hello-world .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-t&lt;/code&gt; flag is used to tag the image so that it's easier to find.&lt;br&gt;
Run the command &lt;code&gt;docker images&lt;/code&gt; to see the list of images. It should contain the image we built above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QTpevAy0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v6b50ahgpotuj9qg9z32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QTpevAy0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v6b50ahgpotuj9qg9z32.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the image locally and verify that everything is working fine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8000:8080 -d &amp;lt;your username&amp;gt;/hello-world
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-p&lt;/code&gt; flag redirects a public port to a private port inside the container.&lt;br&gt;
&lt;code&gt;docker ps&lt;/code&gt; will give the list of running containers images.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fPP9j-IQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/29q4ybihbrsdb2k32tvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fPP9j-IQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/29q4ybihbrsdb2k32tvf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example docker mapped the &lt;code&gt;8080&lt;/code&gt; port inside of the container to the port &lt;code&gt;8000&lt;/code&gt; on our machine.&lt;/p&gt;

&lt;p&gt;Test:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl -i localhost:8000&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L07FP2JW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3gp6kl0vfmfwtm2sgfzp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L07FP2JW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3gp6kl0vfmfwtm2sgfzp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully built and tested our docker image.&lt;/p&gt;

&lt;p&gt;Let's push the locally created image to the repo we created on &lt;strong&gt;Docker Hub&lt;/strong&gt; using the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// docker tag &amp;lt;local-image&amp;gt;:&amp;lt;tagname&amp;gt; &amp;lt;new-repo&amp;gt;:&amp;lt;tagname&amp;gt;
docker tag &amp;lt;your-username&amp;gt;/hello-world:latest &amp;lt;your-username&amp;gt;/hello-world:latest
// docker push &amp;lt;new-repo&amp;gt;:&amp;lt;tagname&amp;gt;
docker push &amp;lt;your-username&amp;gt;/hello-world:latest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can check our repo on &lt;strong&gt;Docker Hub&lt;/strong&gt; to verify the container image upload. In the tags section, we can see the recently pushed tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AlhaAufA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b3fbjnf58v6opkr139c1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AlhaAufA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b3fbjnf58v6opkr139c1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use these images for development and sharing.&lt;/p&gt;

&lt;p&gt;Now, we will look at how to set up a Kubernetes cluster locally and deploy our application's docker image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-intro/#kubernetes-clusters"&gt;Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will use Minikube to run Kubernetes cluster locally. You can check out how to install Minikube &lt;a href="https://kubernetes.io/docs/tasks/tools/install-minikube/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Start minikube and create a cluster.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;minikube start&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can check out more info about starting the cluster &lt;a href="https://kubernetes.io/docs/setup/learning-environment/minikube/#starting-a-cluster"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To interact with the cluster, we need a command-line tool called &lt;strong&gt;kubectl&lt;/strong&gt; . Install and set up &lt;strong&gt;kubectl&lt;/strong&gt; from &lt;a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kubernetes.io/docs/user-guide/kubectl/"&gt;The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a Kubernetes Deployment and use our application's docker image with it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create deployment hello-kubernetes --image=&amp;lt;your-username&amp;gt;/hello-world
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deployment.apps/hello-kubernetes created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To access the hello-kubernetes Deployment, we will expose it as a Service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl expose deployment hello-kubernetes --type=NodePort --port=8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service/hello-kubernetes exposed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The pod &lt;code&gt;hello-kubernetes&lt;/code&gt; is now launched. It takes a few seconds for the pod to get to the running state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/"&gt;A Pod (as in a pod of whales or pea pod) is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run the following command to get the list of pods.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl get pods&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5ujwVnHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/14ki6s4eebu7k1dolsgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5ujwVnHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/14ki6s4eebu7k1dolsgx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Get the url of the exposed service.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;minikube service hello-kubernetes --url&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, you can curl to the url and verify the deployment.&lt;br&gt;
And we are done!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/aQYR1p8saOQla/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/aQYR1p8saOQla/giphy.gif" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But you don't have to stop here. Continue to learn and test out different features supported by Kubernetes 😊&lt;/p&gt;

&lt;p&gt;Originally Published on my &lt;a href="https://akanksha.io/post/spin-up-your-first-kubernetes-cluster-in-under-30-mins"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Handling Nested Db Transactions using CLS</title>
      <dc:creator>AkankshaPriyadrshini</dc:creator>
      <pubDate>Thu, 21 May 2020 06:13:58 +0000</pubDate>
      <link>https://dev.to/itsakankxa/handling-nested-db-transactions-using-cls-9p3</link>
      <guid>https://dev.to/itsakankxa/handling-nested-db-transactions-using-cls-9p3</guid>
      <description>&lt;p&gt;Before we start, let me briefly tell you about one of the issues we faced &lt;a href="https://www.linkedin.com/company/casaone/"&gt;@Casaone&lt;/a&gt;. Once an order gets placed, we do a couple of associated actions like inventory allocation. Inventory Allocation performs a set of operations in a nested transactions. At times we encountered situations where the inventory allocation failed but other warehouse transactions (inside the main transaction) was successful. This led to a lot of confusion while debugging what went wrong.&lt;/p&gt;

&lt;p&gt;What we really wanted is for the entire transaction to roll back if anything goes wrong while allocating the inventory. A change should reflect everywhere or nowhere at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Database Transactions
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Func1() {
    Transaction1(() =&amp;gt; {
        Func2()
        // other set of actions.
    });
};



function Func2() {
    Transaction2(() =&amp;gt; {
       // set of actions
    });
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s assume we have some function Func1 and it has some code to perform a set of actions inside a Db Transaction (Transaction1). One of the actions inside it calls another function named Func2 which has another set of actions to be performed inside a transaction (Transaction2).&lt;/p&gt;

&lt;p&gt;Now, imagine a case where the transaction inside Func2 gets committed but for some reason transaction inside Func1 rolls back. This could lead to different kinds of problem like data inconsistency.&lt;/p&gt;

&lt;p&gt;One of the ways we could solve this is by passing the Transaction Object from parent to the child. We can then use the same transaction object instead of creating a new one. But, imagine having to do this at all the places. It’s quite cumbersome. This is where CLS comes at our rescue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuation-Local Storage or CLS
&lt;/h2&gt;

&lt;p&gt;In multithreaded languages like JAVA, global data can be associated or attached with each thread using Thread Local Storage. This however is not helpful in a single-threaded and asynchronous behaviour of Node JS. This is where CLS or Continuation-Local Storage comes into picture.&lt;/p&gt;

&lt;p&gt;CLS enables us to attach the data to the current asynchronous execution context. Values in CLS are grouped into namespaces. These values are available to us until all the functions, called synchronously or asynchronously from the original function have finished executing.&lt;/p&gt;

&lt;p&gt;We can automatically pass transaction object to all queries using CLS. Let’s look at how to do this. I will use &lt;strong&gt;Sequelize&lt;/strong&gt; as an ORM for the purpose of this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Jeff-Lewis/cls-hooked"&gt;cls-hooked&lt;/a&gt; is the module we will be using. Internally it uses &lt;a href="https://nodejs.org/api/async_hooks.html"&gt;async_hooks&lt;/a&gt; which keeps an eye on the context changes. Accordingly, it loads and unloads the data attached with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;async_hooks&lt;/strong&gt; module provides an API to track asynchronous resources. It helps in registering callbacks which is used to track the entire life of an asynchronous resource.&lt;/p&gt;

&lt;p&gt;Let’s import the required packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cls = require('cls-hooked');
const Sequelize = require('sequelize');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we will create a namespace for keeping values in CLS using createNamespace method and ask Sequelize to use it. The level of grouping or the namespace depends on the use case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const namespace = cls.createNamespace('your-namespace');
Sequelize.useCLS(namespace);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, whenever a db transaction will start, the details of that transaction will be stored against a key &lt;strong&gt;transaction&lt;/strong&gt; in the particular namespace.&lt;/p&gt;

&lt;p&gt;The idea is to execute a set of actions (task) under the same transaction object (if exists) else create a new one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sequelize = new Sequelize();
const db = {};
db.sequelize = sequelize();
// check if transaction object exists in the namespace else create a new one
db.transaction = (task) =&amp;gt; {
    return namespace.get('transaction')
        ? task()
        : sequelize.transaction(task);
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can use our db.transaction wherever required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = require('../db/models')
function Func1() {
    db.transaction(() =&amp;gt; {
        Func2()
        // other set of actions.
    });
};
function Func2() {
    db.transaction(() =&amp;gt; {
       // set of actions
    });
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Notes:
&lt;/h4&gt;

&lt;p&gt;async_hooks used with CLS might have some impact on performance. You should evaluate whether to go with it or not according to the use case. You can check out the demo by Benedikt Meurer on performance comparison with and without async_hooks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/api/async_hooks.html"&gt;https://nodejs.org/api/async_hooks.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Jeff-Lewis/cls-hooked"&gt;https://github.com/Jeff-Lewis/cls-hooked&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/bmeurer/async-hooks-performance-impact"&gt;https://github.com/bmeurer/async-hooks-performance-impact&lt;/a&gt;
                        ...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Originally published on my &lt;a href="https://akanksha.io/post/handling-nested-db-transactions-using-cls"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>database</category>
    </item>
  </channel>
</rss>
