By Alex, Alibaba Cloud Community Blog author.
In a nutshell, a microservice architecture is an application constituting a collection of services. The microservice architecture aims to have applications that are:
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.
Sample Microservices Architecture: microservices.io
Some of the advantages of microservices include:
Some of the disadvantages of microservices include:
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.
To begin with, this tutorial requires the following:
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.
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.
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.
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.
Alibaba Clouder - November 7, 2019
Xi Ning Wang - August 17, 2018
Alibaba Clouder - October 1, 2020
Xi Ning Wang - August 30, 2018
Alibaba Cloud Native Community - May 17, 2022
Xi Ning Wang - August 21, 2018
ApsaraDB Dedicated Cluster provided by Alibaba Cloud is a dedicated service for managing databases on the cloud.
Learn MoreMSE provides a fully managed registration and configuration center, and gateway and microservices governance capabilities.
Learn MoreProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreA secure image hosting platform providing containerized image lifecycle management
Learn MoreMore Posts by Alex