×
Community Blog How to Deploy Kubernetes Microservices on Alibaba Cloud

How to Deploy Kubernetes Microservices on Alibaba Cloud

This article discusses how you can deploy Kubernetes microservices on Alibaba Cloud and shows how you can use it to deploy a Node.

By Alex, Alibaba Cloud Community Blog author.

An Overview of Microservices

In a nutshell, a microservice architecture is an application constituting a collection of services. The microservice architecture aims to have applications that are:

  • Easy to test and maintain
  • Loosely coupled
  • Independent
  • Tightly focused on the business
  • Highly scalable

The microservice architecture minimizes the complexity while working on large and complex applications by breaking down the application to smaller services that are wired together. It allows for the speedy evolution of core technologies.

1
Sample Microservices Architecture: microservices.io

Pros and Cons of Microservices

Some of the advantages of microservices include:

  • It allows for the communication of services deployed in different machines which in turn improves resilience and reliability.
  • It is very easy to scale specific services to keep up with traffic demands.
  • It allows to work on specific services and deploy them without impacting the entire application setup.
  • The architecture does not have a single point of failure.
  • The approach is cheaper over time.

Some of the disadvantages of microservices include:

  • The microservice application set up is a complicated process requiring separate build and deployment for each service hence necessitating automated deployment.
  • It requires to monitor and maintain all services separately, making it tedious for every large application.
  • Wiring together a large number of services is a tedious process.
  • Common problems with distributed computing such as security and concurrency.
  • Increased overhead, more documentation, and difficulty to cancel tasks running across the services.

Microservices Architecture vs Monolithic Architecture

  • Monolithic applications are difficult to work with as compared to microservices, especially in long term maintenance and bringing new services. Also, troubleshooting is a challenge.
  • Unlike microservices, the entire application must stop even while making small updates. The same happens with load balancing, where specific modules aren't scaled individually to handle traffic demands.
  • Another important aspect is the design complexity. Monolithic applications become increasingly complex with time, and building new features requires coordination between many people and thus, increases the time to test and deploy.
  • Monolithic applications must implement in a uniform technology and are prone to single points of failure. As the app becomes larger, it costs more to develop and maintain, and eventually, an immense code base may lead to failure.

Previous articles in the Kubernetes tutorial series examine the Kubernetes building blocks and configurations. This tutorial describes the concepts to create, deploy, and manage Kubernetes microservices applications and demonstrates how to create a Dockerfile container image and push it to Docker Hub for deploying it to the cluster.

Requirements

To begin with, this tutorial requires the following:

  • A Kubernetes cluster
  • Docker Hub account
  • Git running on a local machine

Docker

Use Docker to build applications that are independent of the operating system. On the command terminal, run the command below to test the installation.

sudo docker pull hello-world

Next, run the following command.

sudo docker run hello-world

The following snippet shows the output for the preceding command.

Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1.    The Docker client contacted the Docker daemon.
2.    The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64)
3.    The Docker daemon created a new container from that image that runs the executable that produces the output you are currently reading.
4.    The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

Now that the Docker works, let's create a new deployment.

Creating a New Deployment

In this step, create a new deployment using a YAML file. Run the following command to make a new directory.

mkdir test

Next, Change to the directory.

cd test

Create a new deployment.yaml file as shown below.

sudo nano deployment.yaml

Paste the contents below in the file.

kind: Service
apiVersion: v1
metadata:
  name: mysqlservice
spec:
  selector:
    app: mysql
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80
      # Port accessible outside cluster
      nodePort: 30002
  type: LoadBalancer
 
 
 
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql/mysql-server
          ports:
            - containerPort: 80

Save and exit the editor. Run the command below to verify the created file.

ls

The code lines mentioned above defines the deployment and service. The deployment has five replicas of a MySQL instance that are pulled from Docker hub.

Run the command below to create a deployment.

sudo kubectl create -f deployment.yaml

The following snippet shows the response to the preceding command.

Service 'mysqlservice' created
Deployment 'mysql' created

Now, run the command below to check whether pods are running side by side.

sudo kubectl get pods

The following snippet shows the response to the preceding command.

mysql-n535t   1/1       Running             0          5s
mysql-wr7nf   1/1       Running             0          5s
mysql-ws59m   1/1       Running             0          4s
mysql-wd66m   1/1       Running             0          4s
mysql-wr77m   1/1       Running             0          4s

Get the deployments by executing the command below.

sudo kubectl get deployments

The following snippet shows the response to the preceding command.

mysql       1        1              1        1            20s

Run the following command in case there is a requirement to delete a pod using the pod IDs.

kubectl delete pod mysql-n535t 

Now, run the command below to get the pods again.

sudo kubectl get pods

The following snippet shows the response to the preceding command.

mysql-n535t   0/1       Terminating         0          55s
mysql-wr7nf   1/1       Running             0          55s
mysql-ws59m   1/1       Running             0          54s
mysql-wd66m   1/1       Running             0          54s
mysql-wr77m   1/1       Running             0          54s
mysql-wt99m   1/1       Running             0          4s

Each time a pod is down, Kubernetes spins up a new one to consistently maintain the declared state.

Deploy a Node.JS App

Let's run a Node.js application container with Docker on Kubernetes as an example to examine how to do a microservices architecture. First, install npm as shown below:

sudo apt install npm

Create a directory for the app using the following command.

cd ~
mkdir nodeapp
cd kube

Now run the following command.

npm init

Just press Enter until the process is complete. Now, check the package.json file by running the following command.

sudo nano package.json

The following snippet shows the output for the preceding command.

{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Here's the content of the file. Make necessary changes as shown below.

{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC"
}

Now create an index.js file by running the following command.

touch index.js

Open the file using the command below.

sudo nano index.js

Paste the following content in the file.

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Save the file and exit the editor.

Next, create a Docker file to containerize the application.

touch Dockerfile

v

Paste the following content in the file.

FROM node:carbon
 
# Create app directory
WORKDIR /usr/src/app
 
COPY package.json .
COPY package-lock.json .
 
RUN npm install
 
COPY . .
 
EXPOSE 8080
 
CMD [ "npm", "start" ]

Run the command below to check the installed node version.

node -v

The output must show either the following version or higher, to get the package-lock.

v10.15.1

Run this command to remove Node.js.

sudo apt remove nodejs

Install Node as shown below.

nvm install 10.15.1

Verify successful installation by running the following command.

node -v 

Check npm version using the command below.

npm -v

It should be version 6.4.1 or higher.

Finally, run the command below to generate the package-lock.json.

npm i

After getting the package-lock file run the ls command to countercheck.

Also, create a .dockerignore file as shown below.

sudo touch .dockerignore 

Paste the following in the file.

node_modules

Now run the following command to build the container.

sudo docker build -t flasky/nodeapp .

After the build is complete, push it to docker using the following command.

sudo docker push flasky/nodeapp

Modify the YAML file to use the container nodeapp stored in my Docker hub.

Change to the directory containing the YAML file.

cd ~
cd test
sudo nano deployment.yaml

Open the file and make the changes shown below.

---
kind: Service
apiVersion: v1
metadata:
  name: NodeService
spec:
  selector:
  app: nodeapp
  ports:
 - protocol: "TCP"
 # Port accessible inside cluster
  port: 8080
 # Port to forward to inside the pod
  targetPort: 8080
 # Port accessible outside cluster
  nodePort: 30003
  type: LoadBalancer
 
 
 
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: NodeApp
spec:
  replicas: 5
  template:
  metadata:
  labels:
  app: nodeapp
  spec:
   containers:
  - name: nodeapp
   image: flasky/nodeapp
   ports:
  - containerPort: 8080

Save the file and exit the editor.

Run the following command.

sudo kubectl apply -f deployment.yaml

Finally, Node.js container is successfully created and deployed on Kubernetes. Get pods by running the command below.

sudo kubectl get pods

The following snippet shows the output for the preceding command.

nodeapp-ioe35t       1/1       Running             0          17s
nodeapp-ope7nf       1/1       Running             0          17s
nodeapp-jke59m       1/1       Running             0          17s
nodeapp-jkk66m       1/1       Running             0          17s
nodeapp-hjy77m       1/1       Running             0          17s

The above output shows 5 pods that are up and running since the application of changes to Kubernetes. Run the command below to see the running service.

curl -i localhost:30003

Remember to replace it with the port accessible outside the cluster.

Conclusion

This tutorial successfully illustrates the microservices concept for Kubernetes. It explains how to use Docker and Kubernetes to create a highly available service along with containers running in pods. Alibaba Cloud services are reliable and helpful for deploying highly available applications running on Kubernetes. In addition, it also stresses on how to boot up an already provisioned Kubernetes service.

Do you have an Alibaba Cloud account? Sign up for an account and try over 40 products for free worth up to $1200. Get Started with Alibaba Cloud to learn more.

0 0 0
Share on

Alex

53 posts | 8 followers

You may also like

Comments

Alex

53 posts | 8 followers

Related Products