Cloud Infrastructure Community's Space

AKS #4: Deploy a NodeJS application to AKS

Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js is the runtime and npm is the Package Manager for Node.js modules. In this article, we’re going to explore methods to deploy a simple NodeJS application to a running Azure Kubernetes Cluster(AKS)

In this article, we’re going to explore different methods to Deploy a Node.JS application to an existing Azure Kubernetes Cluster.

Prerequisites

Preparing Kubernetes resources

Create a Deployment

A Kubernetes deployment is a resource object in Kubernetes that provides declarative updates to applications. A deployment allows you to describe an application’s life cycle, such as which images to use for the app, the number of pods there should be, and the way in which they should be updated.

Create a new file called deployment.yaml and paste following code block:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-deployment
  labels:
    app: nodejs-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nodejs-deployment
  template:
    metadata:
      labels:
        app: nodejs-deployment
    spec:
      containers:
      - name: nodejs-deployment
        image: hidetran/simple-express:latest
        ports:
        - containerPort: 8000

Create a Service

A Service in Kubernetes is a REST object, similar to a Pod. Like all of the REST objects, you can POST a Service definition to the API server to create a new instance. The name of a Service object must be a valid RFC 1035 label name.

Create a new file called service.yaml and paste following code block:

apiVersion: v1
kind: Service
metadata:
  name: nodejs-service
spec:
  ports:
    - port: 8000
      targetPort: 8000
      protocol: TCP
  selector:
    app: nodejs-deployment

Method 1: Apply Deployment & Service by kubeCTL

The command set kubectl apply is used at a terminal’s command-line window to create or modify Kubernetes resources defined in a manifest file. This is called a declarative usage. The state of the resource is declared in the manifest file, then kubectl apply is used to implement that state.

Before running the kubectl apply command, make sure you have a valid Kubernetes context and it connected to your kubectl. Bellow steps describes how to grab Azure AKS context and apply to kubectl.

# login into Azure Cloud using AZ ClI
az login -t <your Azure Active Directory ID>
az account set -s <your Azure subscription where AKS cluster was deployed on>

# Get your Azure AKS credentials and save it
az aks get-credentials -n <your AKS name> --resource-group <your AKS resource group>
# now set kubectl context to that AKS cluster
kubectl config use-context <your AKS name>
#
#
# now run kubectl apply commands to deploy K8S resource
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
#
#
#
# after deployment and service was successfully deployed, you get get them by
kubectl get deployment
# Output
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
nodejs-deployment   3/3     3            3           2m41s
#
# or
kubectl get service
# Output
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes       ClusterIP   10.0.0.1      <none>        443/TCP    18m
nodejs-service   ClusterIP   10.0.117.29   <none>        8000/TCP   2m31s
#
#
# you can also check k8s pods which was created during deployment by
kubectl get pod --namespace default
#
# Output
NAME                                 READY   STATUS    RESTARTS   AGE
nodejs-deployment-5779894959-c2k46   1/1     Running   0          105s
nodejs-deployment-5779894959-cp5dc   1/1     Running   0          105s
nodejs-deployment-5779894959-t74d4   1/1     Running   0          105s

Method 2: Create ArgoCD application for NodeJS app

After ArgoCD was installed on your AKS cluster, you can access ArgoCD dashboard to start deploying your application. We need to retrieve your ArgoCD admin credentials by using kubectl, run following command to get default admin password:

# get ArgoCD default admin password

hostname=$(az network public-ip show -n $PIP_NAME -g $AKS_RG --query ipAddress -o tsv)
password=$(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d)

Let’s open your favorite web browser and type the hostname returned from above command, use admin as username and above password as secured password to login. After logged in, ArgoCD will display

argocd admin dashboard

Click to CREATE APPLICATION button and fill to the creating application form as following suggestion:

Set application name and default ArgoCD project
Set source repository where your configuration files located
Set the destination kubernetes cluster where configuration files will be deployed on

After submit, you can see your application listed in the dashboard and it starts deploying resources based on the configuration files.

Application added to the ArgoCD dashboard

Click on the application and you’ll see your deployment, service working perfectly (green color)

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close