The author | week sauce

This article was written in January 2020. First in zhouzhoujiang personal blog, reproduced please indicate the source.

This is a practical note for Node deployment, documenting the process from SSH connection, environment installation, image creation, and application startup. It is mainly for some practical steps in the process of individual learning, and the conceptual and principle content can be viewed separately in conjunction with the document.

Knowledge reserves

SSH

SSH is a security protocol based on the application layer. SSH is a reliable protocol designed to provide security for remote login sessions and other network services. The SSH protocol can effectively prevent information leakage during remote management. There are two SSH remote connection authentication modes. The SSH remote connection authentication mode is based on password authentication, which is not secure. You need to create a pair of keys, store the public key on the server to be accessed, and keep the private key. When you need to connect to the SSH server, the client software sends a request to the server to use the key of the client for security authentication. After receiving the request, the server looks for the public key in the user’s root directory and compares it with the public key that was sent. If the two keys match, the server encrypts the “challenge” with the public key and sends it to the client software. Once the client receives the challenge, it can decrypt it using the local private key and send it to the server. This method is quite secure.

Dockerfile Creates a push image

Dockerfile is a file composed of a group of instructions, each command corresponds to a command in Linux, Docker program will read the instructions in Dockerfile to generate the specified image. For details about the Dockerfile command, see Dockerfile introduction

  • Image building

docker build “-t“ nginx:test . The basic format is the Docker build [options] path. This command will read the Dockerfile in the specified path (including subdirectories) and send all contents in the path to the Docker server, who will create the image.

  • Listing mirrors

docker images

  • Setting a Mirror Label

Docker tag [' ID '] / 'test:[version number]

  • Upload the image

Docker push docker push docker push docker push [docker account username] /[image name] : [image version number]

  • Access to the mirror

Docker pull [image name] ' ':[image version number]

The environment to support

An Aliyun Linux CentOS server

Log in to the remote server over SSH

  • Install the SSH Server on the server

sudo yum install openssh-server

  • Start the service

service sshd start

  • The Client uses Bitvise SSH Client to generate keys

  • Exporting public Keys

  • Save the public key to the authorized_keys file on the server /root/.ssh/authorized_keys

  • Client Login

  • Login successful

Environment to prepare

mysql

  • Download warehouse file

wget`` http:``//``repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm yum`` localinstall mysql80-community-release-el7-``1``.noarch.rpm

  • Mysql installation

yum install mysql-community-server -y

  • Example Query information about installed mysql

RPM - qi mysql - community - for server x86_64 ` ` 0 ` ` : ` ` 8.0 ` `. ` ` 18 ` ` - 1. El7

  • Start the mysql service

systemctl start mysqld systemctl enable mysqld systemctl status mysqld

  • Find the initial password

cat`` /var/log/mysqld.log | ``grep`` password

  • Enter the mysql

mysql -uroot -p

  • Change the password

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password ';

docker

  • Docker installation

yum install -y docker

  • View docker installation information

yum list installed |grep docker

  • Start the service

systemctl daemon-reload service docker start

Image building

The project we need to deploy is a simple Web API CURD application built on nest CLI, with mysql as the database and TypeOrM as the ORM framework. The purpose of this article is to deploy the project rather than the content of the project itself, so there is only one model and one interface in this example. When the deployment is complete, the model is synchronized successfully and the interface is accessed correctly, the deployment is successful.

  • The project structure

  • Create a dockerfile file in the project root directory
FROM node:lts AS deps
RUN set -ex && \
    npm set progress=false && \
    npm config set registry https://registry.npm.taobao.org
WORKDIR /var/app

#Compile the project
FROM deps AS compiler
COPY package*.json tsconfig*.json ./
COPY src ./src
RUN set -ex && \
    npm i --ignore-scripts && \
    npm run build

#-- Installation production dependency --
FROM deps AS installer
COPY package*.json ./
RUN npm i --production

# -- runtime --
FROM node:lts-slim
LABEL maintainer="zzj <[email protected]>"
WORKDIR /var/app
COPY --from=installer /var/app/node_modules ./node_modules
COPY --from=installer /var/app/package*.json ./
COPY --from=compiler /var/app/dist ./dist
COPY ormconfig.json ./ormconfig.json

EXPOSE 3000
CMD ["node", "dist/main"]
Copy the code

That does a couple of things

  1. Install the build dependencies and perform the build
  2. Install production environment dependencies
  3. Copy the installed dependencies and the compiled dist directory into the final image
  4. Declares the service port provided by the runtime container
  5. Specifies the default program to run when the container is started (Node dist/main)
  • Build the mirror

docker build -t backend-test .

  • Viewing a Local Mirror

docker images

The image already exists in our local hard disk, and we can start the container to run our project program by using the Docker run command locally. In fact, it is usually uploaded to a container image repository to manage images.

Upload the image

  • Use Ali Cloud container mirror service to create a mirror warehouse

  • Go back to the local Docker environment and perform the operation:
  1. Log in to Aliyun Docker Registry

docker login --username=*** registry.cn-hangzhou.aliyuncs.com

  1. Tag the tag

Docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/namespace /backend-test:[backend version number]

  1. Push the mirror

'/'/' backend-test:[backend version number]

  • Image uploaded successfully

Start the container

  • Now start the container based on the image that has been built and uploaded to deploy the project on the server.

Docker run - d - name backend - test - p, 3001:3000 registry.cn-hangzhou.aliyuncs.com/xxx (namespace)/backend - test: hundreds

To do this, pull the image, start the container, and run the CMD command Node dist/main

  • Check container status after successful startup:docker ps

  • Try to see if the site is accessible

The interface returns results normally, and the Node project has been successfully deployed on the Linux server.