如何部署容器化的xxl-job服务器

xxl-job服务器的Docker镜像已经制作完成了,现在需要部署相应的容器化服务,本文将介绍如何部署容器化的xxl-job服务器。

一、环境描述

1. 宿主机

  • CPU:双核
  • 内存:4 GB
  • 硬盘:120 GB
  • IP:192.168.190.129
  • 操作系统:CentOS 7.4-1708 x86_64 Minimal

2. Docker

3. MySQL

4. Nginx

5. xxl-job

二、部署MySQL容器

1. 创建MySQL配置文件

在shell中运行以下命令,创建my.cnf文件:

cat > /root/Downloads/my.cnf << "EOF"
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
default-character-set=utf8
EOF

注意,my.cnf文件中的[mysql]节非常重要,它设置客户端使用utf-8编码,否则初始化数据库将会出错。

2. 创建必要的目录和文件

在shell中运行以下命令,在宿主机中创建MySQL的数据、日志和配置目录,然后修改访问权限:

mkdir -p /usr/local/mysql/{data,log,config}
touch /usr/local/mysql/log/mysqld.log
cp /root/Downloads/my.cnf /usr/local/mysql/config/
chown -R 27:27 /usr/local/mysql

注意,27:27是MySQL容器中的mysql用户的UID和GID,宿主机中没有这个用户。

3. 启动MySQL容器

在shell中运行以下命令,启动MySQL容器:

docker run --detach \
    --name mysql \
    --hostname mysql \
    --volume /usr/local/mysql/log/mysqld.log:/var/log/mysqld.log \
    --volume /usr/local/mysql/config/my.cnf:/etc/my.cnf \
    --volume /usr/local/mysql/data:/var/lib/mysql \
    --publish 3306:3306 \
    registry.cn-hangzhou.aliyuncs.com/ghoulich-centos/centos-6.9-mysql:5.7.21

上述命令有两点需要注意:

  • 将MySQL容器的3306端口映射至宿主机的3306端口;
  • 挂载MySQL容器的数据、配置和日志目录。

4. 修改初始密码

在shell中运行以下命令,修改MySQL的初始密码,并且开启远程访问权限:

init_passwd=$(sed -rn 's/^(.*)(root@localhost: )(.*)$/\3/p' /usr/local/mysql/log/mysqld.log)
docker exec -it mysql mysql --user=root --password=${init_passwd} --connect-expired-password --execute="ALTER USER 'root'@'localhost' IDENTIFIED BY '123.Org$%^';"
docker exec -it mysql mysql --user=root --password=123.Org$%^ --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123.Org$%^' WITH GRANT OPTION;"
docker exec -it mysql mysql --user=root --password=123.Org$%^ --execute="FLUSH PRIVILEGES;"

MySQL数据库的新密码被设置成123.Org$%^

5. 初始化xxl-job数据库

在shell中运行以下命令,克隆xxl-job的源码库,然后初始化数据库:

yum install -y git
cd /root/Downloads/
git clone https://github.com/xuxueli/xxl-job.git
docker exec -i mysql mysql --user=root --password="123.Org$%^" < xxl-job/doc/db/tables_xxl_job.sql

6. 检查数据库表

在shell中运行以下命令,查看xxl-job数据库的表:

docker exec -it mysql mysql --user=root --password=123.Org$%^ --database=xxl-job -e "show tables;"

若输出信息如下图所示,则表示MySQL数据库部署完成:

xxl-job的数据库结构

三、部署xxl-job容器

1. 准备Tomcat的配置文件

在shell中运行以下命令,下载Tomcat的压缩包,然后拷贝和修改Tomcat的配置文件:

yum install -y wget
mkdir -p /usr/local/xxl-job
cd /root/Downloads
wget http://mirrors.advancedhosters.com/apache/tomcat/tomcat-9/v9.0.7/bin/apache-tomcat-9.0.7.tar.gz
tar xvzf apache-tomcat-9.0.7.tar.gz
cp -r /root/Downloads/apache-tomcat-9.0.7/{conf,logs} /usr/local/xxl-job/
sed -i '150a\      <Context path="/" docBase="/usr/local/tomcat/webapps/xxl-job"></Context>' /usr/local/xxl-job/conf/server.xml

2. 获取MySQL容器的IP地址

在shell中运行以下命令,查找MySQL容器的IP地址,启动xxl-job容器时需要用到:

cat > get_container_ip.sh << "EOF"
#! /bin/bash
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1
EOF
mysql_ip=$(/bin/bash get_container_ip.sh mysql)

3. 启动xxl-job容器

在shell中运行以下命令,启动xxl-job容器:

docker run --detach \
           --name xxl-job \
           --hostname xxl-job \
           --volume /usr/local/xxl-job/conf:/usr/local/tomcat/conf \
           --volume /usr/local/xxl-job/logs:/usr/local/tomcat/logs \
           --expose 8080 \
           registry.cn-hangzhou.aliyuncs.com/ghoulich-centos/centos-6.9-xxl-job:1.9.1 \
           --mysql-host $mysql_ip \
           --mysql-port 3306 \
           --mysql-user root \
           --mysql-password 123.Org$%^ \
           --mail-username xxxx@163.com \
           --mail-password password

上述命令有三点需要注意:

  • xxl-job容器公开8080端口,但不映射至宿主机;
  • 挂载xxl-job的配置和日志目录;
  • 采用entrypoint的方式启动XXL-Job容器。

四、部署Nginx容器

1. 创建MIME类型文件

mime.types文件能够定义Nginx能够处理的媒体文件类型。在shell中运行以下命令,创建mime.types文件:

cat > /root/Downloads/mime.types << "EOF"
types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/javascript                js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;

    text/mathml                           mml;
    text/plain                            txt;
    text/vnd.sun.j2me.app-descriptor      jad;
    text/vnd.wap.wml                      wml;
    text/x-component                      htc;

    image/png                             png;
    image/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg svgz;
    image/webp                            webp;

    application/font-woff                 woff;
    application/java-archive              jar war ear;
    application/json                      json;
    application/mac-binhex40              hqx;
    application/msword                    doc;
    application/pdf                       pdf;
    application/postscript                ps eps ai;
    application/rtf                       rtf;
    application/vnd.apple.mpegurl         m3u8;
    application/vnd.ms-excel              xls;
    application/vnd.ms-fontobject         eot;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    application/x-7z-compressed           7z;
    application/x-cocoa                   cco;
    application/x-java-archive-diff       jardiff;
    application/x-java-jnlp-file          jnlp;
    application/x-makeself                run;
    application/x-perl                    pl pm;
    application/x-pilot                   prc pdb;
    application/x-rar-compressed          rar;
    application/x-redhat-package-manager  rpm;
    application/x-sea                     sea;
    application/x-shockwave-flash         swf;
    application/x-stuffit                 sit;
    application/x-tcl                     tcl tk;
    application/x-x509-ca-cert            der pem crt;
    application/x-xpinstall               xpi;
    application/xhtml+xml                 xhtml;
    application/xspf+xml                  xspf;
    application/zip                       zip;

    application/octet-stream              bin exe dll;
    application/octet-stream              deb;
    application/octet-stream              dmg;
    application/octet-stream              iso img;
    application/octet-stream              msi msp msm;

    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

    audio/midi                            mid midi kar;
    audio/mpeg                            mp3;
    audio/ogg                             ogg;
    audio/x-m4a                           m4a;
    audio/x-realaudio                     ra;

    video/3gpp                            3gpp 3gp;
    video/mp2t                            ts;
    video/mp4                             mp4;
    video/mpeg                            mpeg mpg;
    video/quicktime                       mov;
    video/webm                            webm;
    video/x-flv                           flv;
    video/x-m4v                           m4v;
    video/x-mng                           mng;
    video/x-ms-asf                        asx asf;
    video/x-ms-wmv                        wmv;
    video/x-msvideo                       avi;
}
EOF

2. 创建Nginx配置文件

Nginx容器的配置文件存放在宿主机中,便于调整Nginx服务的配置项。在shell中运行以下命令,创建nginx.conf文件:

cat > /root/Downloads/nginx.conf << "EOF"
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
EOF

3. 创建必要的目录和文件

在宿主机中创建Nginx的数据、配置和日志目录,然后修改访问权限,最后拷贝配置文件:

mkdir -p /usr/local/nginx/{html,conf,logs}
mkdir -p /usr/local/nginx/conf/conf.d
chown -R 499:499 /usr/local/nginx/logs
cp /root/Downloads/{nginx.conf,mime.types} /usr/local/nginx/conf/

4. 启动Nginx容器

在shell中运行以下命令,启动Nginx容器:

docker run --detach \
    --name nginx \
    --hostname nginx \
    --volume /usr/local/nginx/html:/var/lib/nginx \
    --volume /usr/local/nginx/conf:/etc/nginx \
    --volume /usr/local/nginx/logs:/var/log/nginx \
    --publish 80:80 \
    registry.cn-hangzhou.aliyuncs.com/ghoulich-centos/centos-6.9-nginx:1.12.2

上述命令有两点需要注意:

  • 将Nginx容器的80端口映射至宿主机的80端口。
  • 将Nginx容器的数据、配置和日志目录映射至宿主机的相应目录;

5. 获取宿主机IP地址

在shell中运行以下命令,获取宿主机的IP地址,配置xxl-job的站点时需要用到:

cat > get_host_ip.sh << "EOF"
#! /bin/bash
ifconfig $1 | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"
EOF
host_ip=$(/bin/bash get_host_ip.sh ens33)

6. 获取xxl-job容器的IP地址

在shell中运行以下命令,获取xxl-job容器的IP地址,配置xxl-job的站点时需要用到:

xxl_ip=$(/bin/bash get_container_ip.sh xxl-job)

7. 配置xxl-job的站点

在shell中运行以下命令,创建xxl-job的站点配置文件:

cat > /usr/local/nginx/conf/conf.d/xxl-job.conf << EOF
upstream xxl-job {
    server $xxl_ip:8080;
}

server {

    listen   80;
    server_name $host_ip;
    access_log /var/log/nginx/xxl-job_access.log;
    error_log /var/log/nginx/xxl-job_error.log;

    location / {
        proxy_redirect off;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_pass http://xxl-job;
    }
}
EOF

若访问压力较大,则可以多部署几个xxl-job容器,通过Nginx的负载均衡分散访问压力!

8. 重启Nginx容器

在shell中运行以下命令,重启Nginx容器,否则站点配置不能生效:

docker restart nginx

五、验证测试

在浏览器中访问以下网址:

http://192.168.190.129/toLogin

此时,会打开登录页面,如下图所示:

xxl-job的登录页面

默认的用户名是admin,密码是123456,登录后的首页,如下图所示:

xxl-job的管理控制台首页

至此,容器化的xxl-job服务器已经部署成功了!