容器化技术的出现大大简化了应用开发人员在构建底层基础设施的工作。SequoiaDB 巨杉数据库于3.2.1版本正式推出了 Docker 容器化部署方案,本文将会基于 SequoiaDB 巨杉数据库与 Nodejs 的 Docker 镜像搭建一个简易的 Web 服务器。
我们将会搭建一个三分区三副本的高可用 SequoiaDB 巨杉数据库。同时,我们将会创建一个SequoiaDB 巨杉数据库的 MySQL 实例,用以提供 Nodejs 作为数据源。
对于已经安装了 Docker 环境的读者可以跳过本章节。
对于之前没有使用过Docker 的读者可以通过本章节安装本地 Docker 环境。
Docker 可以运行在绝大部分主流操作系统上,包括常用的 Windows、Mac 以及 Linux的多个版本均可支持。
对于 Mac 用户可以安装 DockerDesktop for Mac,下载地址在:
https://hub.docker.com/editions/community/docker-ce-desktop-mac
对于 Windows 用户可以安装 DockerDeskop for Windows,下载地址在:
https://docs.docker.com/docker-for-windows/install/
对于 Linux 用户则可以直接使用 yum或 apt-get 进行安装:
https://docs.docker.com/install/linux/docker-ce/centos/ https://docs.docker.com/install/linux/docker-ce/ubuntu
将 SequoiaDB 引擎与SequoiaSQL-MySQL 数据库实例下载至本地
docker pull sequoiadb/sequoiadb:v3.2.1 docker pull sequoiadb/sequoiasql-mysql:v3.2.1
启动数据库引擎容器
docker run -it -d --name coord_catalog sequoiadb/sequoiadb:v3.2.1 docker run -it -d --name sdb_data1 sequoiadb/sequoiadb:v3.2.1 docker run -it -d --name sdb_data2 sequoiadb/sequoiadb:v3.2.1 docker run -it -d --name sdb_data3 sequoiadb/sequoiadb:v3.2.1
验证各个容器的 IP 地址
docker inspect --format '{{ .NetworkSettings.IPAddress}}' coord_catalog docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data1 docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data2 docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data3
预期输出结果为:
172.17.0.2 172.17.0.3 172.17.0.4 172.17.0.5
部署 SequoiaDB 巨杉数据库引擎集群
docker exec coord_catalog "/init.sh" \ --coord='172.17.0.2:11810' \ --catalog='172.17.0.2:11800' \ --data='group1=172.17.0.3:11820,172.17.0.4:11820,172.17.0.5:11820;group2=172.17.0.4:11830,172.17.0.5:11830,172.17.0.3:11830;group3=172.17.0.5:11840,172.17.0.3:11840,172.17.0.4:11840'
预期输出结果为:
Begin generating SequoiaDB conf file Finish generating SequoiaDB conf file Restarting sdbcm process, it will take 10 seconds Deploy... Execute command: /opt/sequoiadb/tools/deploy/../../bin/sdb -f /opt/sequoiadb/tools/deploy/quickDeploy.js -e '' ************ Deploy SequoiaDB ************************ Create catalog: 172.17.0.2:11800 Create coord: 172.17.0.2:11810 Create data: 172.17.0.3:11820 Create data: 172.17.0.4:11820 Create data: 172.17.0.5:11820 Create data: 172.17.0.4:11830 Create data: 172.17.0.5:11830 Create data: 172.17.0.3:11830 Create data: 172.17.0.5:11840 Create data: 172.17.0.3:11840 Create data: 172.17.0.4:11840
启动 MySQL 实例容器
docker run -it -d -p 3306:3306 --name mysql sequoiadb/sequoiasql-mysql:v3.2.1
验证 IP 地址
docker inspect --format '{{ .NetworkSettings.IPAddress}}' mysql
预期输出结果为:
172.17.0.6
创建 MySQL 实例
docker exec mysql "/init.sh" --port=3306 --coord='172.17.0.2:11810
其中 coord 参数填写协调节点所在的 IP 地址与监听端口。
预期输出结果为:
Creating SequoiaSQL instance: MySQLInstance Modify configuration file and restart the instance: MySQLInstance Restarting instance: MySQLInstance Opening remote access to user root Restarting instance: MySQLInstanceInstance MySQLInstance is created on port 3306, default user is root
得到 MySQL 实例的容器 ID
docker ps --filter name=mysql --format {{.ID}}
预期结果为 MySQL 实例容器所在的 Container ID:
cc17df22a908
登录 MySQL 实例命令行
docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root
其中 -it 参数为 MySQL 实例的容器ID,预期结果为:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.25 Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective owners. No entry for terminal type "xterm"; using dumb terminal settings. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
创建数据库与表,并创建一条测试数据
create database sample; use sample; create table t1 ( c1 varchar(100)); insert into t1 values ("SequoiaDB");select * from t1;
预期输出结果为:
+-----------+ | c1 | +-----------+ | SequoiaDB | +-----------+ 1 row in set (0.02 sec)
创建 app.js文件
var express = require('express'); //引入express模块 var mysql = require('mysql'); //引入mysql模块 var app = express(); //创建express的实例 var connection = mysql.createConnection({ //创建mysql实例 host:'172.17.0.6', port:'3306', user:'root', password:'', database:'sample'}); var sql = 'SELECT * FROM t1'; connection.connect(); app.get('/',function (req,res) { connection.query(sql, function (err,result) { if(err){ console.log('[SELECT ERROR]:',err.message); } res.send('Hello: ' + result[0].c1 ) ; }); }); app.listen(3000,function () { ////监听3000端口 console.log('Server running at 3000 port'); });
代码中的 host 使用 MySQL 实例的IP 地址
创建 package.json文件
{ "name": "nodetest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1", "mysql": "^2.17.1" } }
创建 Dockerfile文件
FROM node:latest RUN mkdir -p /usr/src/ COPY package.json /usr/src/ COPY app.js /usr/src/ WORKDIR /usr/src/ RUN npm install #定义程序默认端口 EXPOSE 3000 #运行程序命令 CMD ["node","app.js"]
当前目录中应仅包含三个文件
$ ls -la total 24 drwxr-xr-x 5 sequoiadb staff 160 7 16 15:22 . drwxr-xr-x 94 sequoiadb staff 3008 7 16 10:50 .. -rw-r--r-- 1 sequoiadb staff 206 7 16 12:24 Dockerfile -rw-r--r-- 1 sequoiadb staff 766 7 16 12:27 app.js -rw-r--r-- 1 sequoiadb staff 278 7 16 12:03 package.json
创建 Nodejs 服务镜像
docker build -t nodetest .
运行 Nodejs 服务容器
docker run -d -p 3000:3000 nodetest
打开浏览器,连接本地的 3000 端口
更改数据库中的记录,可以看到浏览器显示的内容随之发生调整
$ docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.25 Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. No entry for terminal type "xterm"; using dumb terminal settings. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use sample; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update t1 set c1="Node" where c1="SequoiaDB"; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0
刷新浏览器后显示:
本文展示了如何通过 SequoiaDB 巨杉数据库的 Docker 容器,结合 Nodejs 快速搭建一个 Web 应用。灵活将分布式数据库结合容器使用,能够使用户快速构建开发测试环境,大幅度降低开发过程中的基础设施维护成本。