Notes on deployment, migration and monitoring of offline CentOS 8 environment

Hi, I summarized and recorded the deployment notes of docker under the offline centos8 two days ago. Today is the last day of 2021. Today, I was sitting on the train home from this business trip. There was nothing to do on the train. It's not beautiful to summarize and record other processes of the installation of the offline centos8 environment.

Haha, the writing style is limited, and the technology is even more limited. It's just an operation summary. If there's anything wrong, you're welcome to give advice and forgive me.

1. Background description

Before we start, we'd better briefly explain the environment background. Otherwise, we're confused and confused. This environment deployment is based on the offline centos8 operating system and takes docker as the container to deploy the front-end website + business API Application. The docker deployment of offline CentOS 8 has been summarized in the previous article. This time, we mainly record the relevant application deployment under CentOS, including basic image migration, business API deployment and service monitoring.

2. Basic image migration

The basic images involved in this environment deployment include nginx and netcore related environment, the migration idea of basic image: find a deployed online server environment, and then copy the image to the offline environment. Take the migration of nginx as an example, and others can follow suit.

2.1 copy nginx image

Log in to the service environment where nginx is deployed, and copy the nginx image by executing the following command

  

docker save -o /home/installpack/nginx.zip nginx

  

Parameter Description: docker save -o [storage path of generated image] [image name]

The generated image file nginx Zip is copied to the offline server. I usually put it in the / home/ installpack directory.

2.2 server image restore

If you don't talk much, you can just execute the following commands:

# Enter the folder where the image is located

cd /home/installpack

# Restore mirror

docker load < nginx.zip

# View mirror

docker images

  

You can see nginx in the image list below. It's done

 

The specific configuration of nginx is not written. You can configure it according to your actual needs

3. Business API deployment

In fact, API deployment is simple. Directly copy the release file to the specified directory of the server, generate an image + run an image.

I usually write a service SH file and put it in / home / SH / services SH, write all services involved in the server as batch commands to facilitate rapid installation and deployment. The specific file contents are as follows:

#!/bin/bash

# The command is divided into two parts: the first part is to redeploy and install all; Part II: specify service deployment and installation

restartContainers="$1" 

if [ ! $restartContainers ]; then

    #Restart all services

    #Deploy and install user services

    echo "User service starting ..."

    docker rm -f usermic && docker rmi user

    docker build -t user /home/project/user && docker run -d -m 1024M --memory-swap -1 --restart=always --name usermic -p 8081:8081 -v /home/app/user/Log:/app/Log -v /home/app/user/wwwroot:/app/wwwroot user

    echo "The log service is starting ..."

    docker rm -f logmic && docker rmi log

    docker build -t log /home/project/log && docker run -d -m 1024M --memory-swap -1 --restart=always --name logmic -p 8086:8086 -v /home/app/log/Log:/app/Log -v /home/app/log/wwwroot:/app/wwwroot log
   
    # Deploy other services according to the plan

 else

       #Start the specified service and link between different services

        #Replace IFS variables

        OLD_IFS="$IFS"

        IFS=","

        restartContainersArray=($restartContainers)

        IFS="$OLD_IFS"

        for var in ${restartContainersArray[@]}

        do

           case $var in

            "usermic")

                        echo "User service starting ..."

                        docker rm -f usermic && docker rmi user

                        docker build -t user /home/project/user && docker run -d -m 1024M --memory-swap -1 --restart=always –name usermic -p 8081:8081 -v /home/app/user/Log:/app/Log -v /home/app/user/wwwroot:/app/wwwroot user

                        ;;             

                "logmic")

                        echo "The log service is starting ..."

                        docker rm -f logmic && docker rmi log

                        docker build -t log /home/project/log && docker run -d -m 1024M --memory-swap -1 --restart=always --name logmic -p 8086:8086  -v /home/app/log/Log:/app/Log -v /home/app/log/prologs:/app/prologs -v /home/app/log/wwwroot:/app/wwwroot log

                        ;;

                # Deploy other services according to the plan

                esac

        done

fi

 # Check whether the service container is running normally

docker ps -a

  

Description of service startup command: in fact, you are familiar with the run command, but there are three points to note:

  • --restart=always don't miss it. It will help you start the service automatically when you restart docker
  • -m 1024M is the best configuration. It sets the maximum memory consumption of the container. When the memory consumption reaches the set value, the service will restart automatically. The specific value is configured according to its own server environment.
  • -v / Host Directory: / the container directory file can not be mounted less. It is better to mount and map the configuration file, log file, data file and static resource file to the physical host to avoid the container destroying the data that cannot be returned. The specific files to be mounted are defined according to the specific service.

File execution example description:

# Initialize and deploy all services

sh /home/sh/services.sh

# Deploy and install a service, such as user service:

sh /home/sh/services.sh usermic

# Deploy and install multiple services, such as user service and log service:

sh /home/sh/services.sh usermic, logmic

  

Description of business deployment method: when deploying, many bosses directly mount the entire API service to the physical machine. Except that the container service created for the first time needs the above command, other upgrades only need to upload the update package to the specified mounted physical machine and restart the corresponding container. This is a good method.

4. Service monitoring

In fact, there are many visual third-party tools for service monitoring. I still use the most original way to monitor, that is, monitoring containers and service responses through centos crontab's scheduled tasks.
The specific monitoring idea is divided into two aspects of service monitoring: basic service monitoring and business service monitoring
Basic service monitoring: monitor whether the container status is normal. If not, restart the service directly, such as nginx
Business service monitoring: business services are not directly implemented by monitoring the container status. Because they have been encountered, the container operates normally, but the service cannot provide external services. Such monitoring is also futile. Therefore, the business service directly calls the health check interface of the service. If it cannot be adjusted, it can no longer provide services normally and directly execute services SH to initialize the corresponding service.

4. 1. Preparation of monitoring command file

I usually write a / home / SH / serviceguard on each service to monitor the command file SH, write all the service monitoring corresponding to the service in this file. The specific contents of the file are as follows:
#!/bin/sh
# First define the method to implement the check
# Check if a mirror exists
function checkContainerStats
{
           exist=`docker inspect --format '{{.State.Running}}' $1`
           if [ "${exist}" != "true" ]
           then
                 return 0 
           else
                  return 1 
           fi
}
 
# Check whether a service is normally provided (judge whether the service is normally provided by the health check address)
function checkContainerHealthStats
{
   webUrl="$1"
   serviceCode=$(curl -I -m 10 -X GET -o  -s -w %{http_code} $webUrl)     
           if [ $serviceCode -eq 200 ]
           then
                  return 1
           else
                  return 0
           fi
}
 
# Check the operation status of the container according to its name. If it does not exist, restart it
function checkContainer
{
    #Container name
           containerName="$1"
           echo ""
           echo "Start checking ${containerName}Operation status of"
 
           # Check if the process exists
           # Judge whether the service exists. If it does not exist, restart it directly
           if checkContainerStats ${containerName}
           then
                 echo "The service is disconnected and the service is restarted"
                 docker start ${containerName}
                 echo "Restart complete"
           else
                 echo "In normal service..." 
           fi
}
 
# By checking the address, check whether the service is provided normally, and make automatic repair
function checkContainerHealth
{
           #Container name
           containerName="$1"
           echo ""
           echo "Start checking ${containerName}Operation status of"
 
           # Judge whether the service exists. If it does not exist, directly restart it first. After restarting, check it again. If it still does not exist, directly delete the container and re create the container
           if checkContainerHealthStats $2; then
                 echo "The service is disconnected and the service is restarted" 
                 docker start ${containerName}
                 # Check whether the restart is successful through the process. If it fails, directly delete, image, container and restart to create
                 if checkContainerStats ${containerName}; then
                      echo "Restart failed, new deployment directly" 
                      sh /home/sh/services_22.sh ${containerName}
                      echo "New deployment complete" 
                   else
                      echo "Restart complete"
                fi
                else
                      echo "In normal service..." 
               fi
          }
 
 
  #The inspection is divided into two parts: service inspection + basic container inspection
 
  now=`date +"%Y-%m-%d %H:%M:%S"`
  echo
  echo "${now} Start checking docker Does the corresponding process exist for each container in the"
  #Check basic services
  checkContainer nginx
 
  #Check the operation of each business service
  # Check user service
  checkContainerHealth usermic http://localhost:8081/api/Consul/heathle
  # Check log service
  checkContainerHealth logmic http://localhost:8086/api/Consul/heathle
  #Other services

  

4. 2 start monitoring scheduled task

If you don't talk much, you can just execute the following commands:

Enter centos task setting interface

              

crontab -e

 #All tasks are checked every minute

 * * * * * sh /home/sh/ serviceGuard.sh >> /var/log/cron_log_$(date +\%Y-\%m-\%d).log 2>&1

/ Restart after exiting save centos

# Reload crond restart crond set crond to boot
/sbin/service crond reload && /sbin/service crond restart && chkconfig crond on

  

5. Several points for attention

  • The container cannot communicate with external services: if the database is connected, the simplest and rude way is to directly close the firewall of the host. The specific commands are as follows:
# Turn off firewall

systemctl stop firewalld.service

# Set the boot to prohibit starting the firewall. This sentence cannot be ignored, or restart the server and start it again

systemctl disable firewalld.service

  

  • There is a problem connecting the container to orcel database. You need to configure the time zone, otherwise the orcle connection will report a time zone error. The time zone setting can be directly added to the Dockerfile file with the following configuration:

#The time zone is set to access the orcel database

 

ENV TZ=Asia/Shanghai

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

  

6. Conclusion

Well, the time is just right, and the station is about to arrive. Pack up and get ready to get off. The writing is not good. I hope it will help.

END
In order to communicate more, you are welcome to pay attention to my official account.

Keywords: CentOS Docker

Added by alwaysme on Sun, 02 Jan 2022 14:46:51 +0200