|
本帖最后由 灰哥哥 于 2019-3-18 14:25 编辑
一、环境准备
二、镜像制作
2.1 基础镜像myalpine-openjdk8-curl制作
此镜像将其它镜像公有的部分抽取出来(如jdk,curl工具),减少制作edgexfoundry各服务的步骤。
2.1.1 Dockerfile文件
- FROM alpine
- #为了下载加速,替换为mirrors.ustc.edu.cn
- RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
- RUN apk --update add openjdk8-jre
- RUN apk --update add curl
- CMD /bin/sh
复制代码 2.1.2 镜像制作命令
- sudo docker build -t myalpine-openjdk8-curl .
复制代码
2.2 镜像myedgex/myedgex-volume:java制作
2.2.1 Dockerfile文件
- FROM ubuntu:latest
- MAINTAINER Cloud Tsai <Cloud.Tsai@Dell.com>
- RUN addgroup consul && \
- adduser --system --ingroup consul consul
- # standard mongo db data dir directories
- RUN mkdir /data
- RUN mkdir /data/db
- RUN echo "this directory is reserved for EdgeX database files" > /data/db/README
- # EdgeX shared directories
- ENV EDGEX_BASE=/edgex
- RUN mkdir $EDGEX_BASE
- RUN mkdir $EDGEX_BASE/logs
- RUN echo "this directory is reserved for EdgeX log files" > $EDGEX_BASE/logs/README
- #用来持久化规则引擎模块中的文件(规则模板与用户定义的规则),确定容器停止后这些文件不丢失
- ENV EDGEX_RULES=/edgex/edgex-support-rulesengine
- RUN mkdir $EDGEX_RULES
- RUN mkdir $EDGEX_RULES/rules
- RUN mkdir $EDGEX_RULES/templates
- # Consul config and data directories
- # The /consul/data dir is used by Consul to store state. The agent will be started
- # with /consul/config as the configuration directory so you can add additional
- # config files in that location.
- RUN mkdir /consul
- RUN mkdir /consul/config
- RUN mkdir /consul/data
- RUN echo "this directory is reserved for EdgeX Consul config files" > /consul/config/README
- RUN echo "this directory is reserved for EdgeX data files" > /consul/data/README
- #将consul 对 mongodb配置文件
- COPY static-services-config.json /consul/config
- ENTRYPOINT /usr/bin/tail -f /dev/null
复制代码
2.2.3 static-services-config.json文件
consul读取此文件,注册mongdb服务
- {
- "service": {
- "name": "edgex-mongo", #edgex-mongo在consul上显示的名字
- "tags": [
- "database",
- "nosql"
- ],
- "address": "edgex-mongo", #edgex-mongo服务地址
- "port": 27017, #edgex-mongo端口
- "check": {
- "name": "status",
- "tcp": "edgex-mongo:27017", #edgex-mongo心跳检测服务地址
- "interval": "20s", #第20秒中检测一次
- "timeout": "5s" #5秒超时
- }
- }
- }
复制代码
2.2.4 镜像制作命令
- sudo docker build -t myedgex/myedgex-volume:java .
复制代码
2.3 镜像myedgex/core-config-seed:java制作
2.3.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/core-config-seed
- ENV APP=core-config-seed
- ENV WAIT_FOR_A_WHILE=10
- ENV CONSUL_ARGS="-server -client=0.0.0.0 -bootstrap -ui"
- #将注册到consul的配置信息文件拷入到镜像,注意这个路径是/edgex/core-config-seed/config ,在后面的launch-consul-config.sh文件使用路径保持一致
- COPY ./config $APP_DIR/config
- COPY *.sh $APP_DIR/
- RUN chmod +x $APP_DIR/*.sh
- #将已下载consul应用程序拷入镜像
- COPY consul /usr/local/bin/
- RUN chmod +x /usr/local/bin/consul
- #set the working directory
- WORKDIR $APP_DIR
- #此步可以不同实现方法,你可以使用COPY命令将编译好的jar包拷入。下面我是通过nexus私服中获取,为了加快下载速度,我选择了172.17.0.1内网地址(容器访问主机的地址)
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #call the wrapper to launch consul and main app
- #执行如下脚本,拉起consul,并读入config配置信息
- CMD $APP_DIR/launch-consul-config.sh
复制代码 目录结构如下:
2.3.2 launch-consul-config.sh文件
- #!/bin/sh
- set -e
- if [ -z $ARCH ]
- then
- #因为$ARCH为空,执行此行,注意此行需要加上source
- rm -rf /consul/data/* && source docker-entrypoint.sh agent $CONSUL_ARGS | tee /edgex/logs/core-consul.log &
- else
- rm -rf /consul/data/* && source $APP_DIR/docker-entrypoint.sh agent $CONSUL_ARGS | tee /edgex/logs/core-consul.log &
- fi
- echo "Waiting for $WAIT_FOR_A_WHILE seconds until consul is configured"
- sleep $WAIT_FOR_A_WHILE
- #将config配置信息写入到consul中去。edgex/core-config-seed/config路径需要与Dockerfile定义的一致(使用了绝对路径)
- java -jar $APP.jar --configPath=/edgex/core-config-seed/config
- wait
复制代码
2.3.3 docker-entrypoint.sh文件
此脚本启动consul
- #!/bin/bash
- set -e
- # to Consul.
- CONSUL_BIND=
- if [ -n "$CONSUL_BIND_INTERFACE" ]; then
- CONSUL_BIND_ADDRESS=$(ip -o -4 addr list $CONSUL_BIND_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1)
- if [ -z "$CONSUL_BIND_ADDRESS" ]; then
- echo "Could not find IP for interface '$CONSUL_BIND_INTERFACE', exiting"
- exit 1
- fi
- CONSUL_BIND="-bind=$CONSUL_BIND_ADDRESS"
- echo "==> Found address '$CONSUL_BIND_ADDRESS' for interface '$CONSUL_BIND_INTERFACE', setting bind option..."
- fi
- # You can set CONSUL_CLIENT_INTERFACE to the name of the interface you'd like to
- # bind client intefaces (HTTP, DNS, and RPC) to and this will look up the IP and
- # pass the proper -client= option along to Consul.
- CONSUL_CLIENT=
- if [ -n "$CONSUL_CLIENT_INTERFACE" ]; then
- CONSUL_CLIENT_ADDRESS=$(ip -o -4 addr list $CONSUL_CLIENT_INTERFACE | head -n1 | awk '{print $4}' | cut -d/ -f1)
- if [ -z "$CONSUL_CLIENT_ADDRESS" ]; then
- echo "Could not find IP for interface '$CONSUL_CLIENT_INTERFACE', exiting"
- exit 1
- fi
- CONSUL_CLIENT="-client=$CONSUL_CLIENT_ADDRESS"
- echo "==> Found address '$CONSUL_CLIENT_ADDRESS' for interface '$CONSUL_CLIENT_INTERFACE', setting client option..."
- fi
- # CONSUL_DATA_DIR is exposed as a volume for possible persistent storage. The
- # CONSUL_CONFIG_DIR isn't exposed as a volume but you can compose additional
- # config files in there if you use this image as a base, or use CONSUL_LOCAL_CONFIG
- # below.
- CONSUL_DATA_DIR=/consul/data
- CONSUL_CONFIG_DIR=/consul/config
- # You can also set the CONSUL_LOCAL_CONFIG environemnt variable to pass some
- # Consul configuration JSON without having to bind any volumes.
- if [ -n "$CONSUL_LOCAL_CONFIG" ]; then
- echo "$CONSUL_LOCAL_CONFIG" > "$CONSUL_CONFIG_DIR/local.json"
- fi
- # If the user is trying to run Consul directly with some arguments, then
- # pass them to Consul.
- if [ "${1:0:1}" = '-' ]; then
- set -- consul "$@"
- fi
- # Look for Consul subcommands.
- if [ "$1" = 'agent' ]; then
- shift
- set -- consul agent \
- -data-dir="$CONSUL_DATA_DIR" \
- -config-dir="$CONSUL_CONFIG_DIR" \
- $CONSUL_BIND \
- $CONSUL_CLIENT \
- "$@"
- elif [ "$1" = 'version' ]; then
- # This needs a special case because there's no help output.
- set -- consul "$@"
- elif consul --help "$1" 2>&1 | grep -q "consul $1"; then
- set -- consul "$@"
- fi
- # If we are running Consul, make sure it executes as the proper user.
- if [ "$1" = 'consul' ]; then
- if [ ! -z ${CONSUL_ALLOW_PRIVILEGED_PORTS+x} ]; then
- setcap "cap_net_bind_service=+ep" /bin/consul
- fi
- # set -- consul "$@"
- fi
- exec "$@"
复制代码
2.3.4 镜像制作命令
- sudo docker build -t myedgex/core-config-seed:java .
复制代码
2.4 镜像myedgex/myedgex-mongo:java制作
2.4.1 Dockerfile文件
- FROM mongo:3.4.9
- MAINTAINER Jim White <james_white2@dell.com>
- #copy initialization script for later initialization
- COPY *.js /edgex/mongo/config/
- COPY launch-edgex-mongo.sh /edgex/mongo/config/
- RUN chmod +x /edgex/mongo/config/launch-edgex-mongo.sh
- #expose Mongodb's port
- EXPOSE 27017
- #启动mongodb并初始化数据
- CMD /edgex/mongo/config/launch-edgex-mongo.sh
复制代码 2.4.2 launch-edgex-mongo.sh文件
- set -e
- #以smallfiles方式启动mongodb 服务
- mongod --smallfiles &
- while true; do
- #客户端工具执初始化数据库(建用户、库、集合)
- mongo /edgex/mongo/config/init_mongo.js && break
- sleep 5
- done
- wait
复制代码
2.4.3 镜像制作命令
- sudo docker build -t myedgex/myedgex-mongo:java .
复制代码
2.5 镜像myedgex/support-logging:java制作
2.5.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-support-logging
- ENV APP=support-logging
- ENV APP_PORT=48061
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码 2.5.2 镜像制作命令
- sudo docker build -t myedgex/support-logging:java .
复制代码
2.6 镜像myedgex/support-notifications:java制作
2.6.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-support-notifications
- ENV APP=support-notifications
- ENV APP_PORT=48060
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.6.2 镜像制作命令
- sudo docker build -t myedgex/support-notifications:java .
复制代码
2.7 镜像myedgex/core-metadata:java制作
2.7.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-core-metadata
- ENV APP=core-metadata
- ENV APP_PORT=48081
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.7.2 镜像制作命令
- sudo docker build -t myedgex/core-metadata:java .
复制代码
2.8 镜像myedgex/core-metadata:java制作
2.8.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-core-metadata
- ENV APP=core-metadata
- ENV APP_PORT=48081
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN
- curl -o $APP.jar
- http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.8.2 镜像制作命令
- sudo docker build -t myedgex/core-metadata:java .
复制代码 2.9 镜像myedgex/core-data:java制作
2.9.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-core-data
- ENV APP=core-data
- ENV APP_PORT=48080
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.9.2 镜像制作命令
- sudo docker build -t myedgex/core-data:java .
复制代码
2.10 镜像myedgex/core-command:java制作
2.10.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-core-command
- ENV APP=core-command
- ENV APP_PORT=48082
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.10.2 镜像制作命令
- sudo docker build -t myedgex/core-command:java .
复制代码
2.11 镜像myedgex/export-client:java制作
2.11.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-export-client
- ENV APP=export-client
- ENV APP_PORT=48071
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码 2.11.2 镜像制作命令
- sudo docker build -t myedgex/export-client:java .
复制代码
2.12 镜像myedgex/export-distro:java制作
2.12.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/etrol-export-distro
- ENV APP=export-distro
- ENV APP_PORT=48070
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码 2.12.2 镜像制作命令
- sudo docker build -t myedgex/export-distro:java .
复制代码
2.13 镜像myedgex/support-rulesengine:java制作
2.13.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/edgex-support-rulesengine
- ENV APP=support-rulesengine
- ENV APP_PORT=48075
- ENV TEMPLATE_DIR=/edgex/edgex-support-rulesengine/templates
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #确保使用最新的规则引擎模板拷入
- RUN rm -rf $TEMPLATE_DIR/*.drl
- COPY *.drl $TEMPLATE_DIR/
- #RUN mkdir /edgex/edgex-support-rulesengine/rules
- #RUN chmod 777 /edgex/edgex-support-rulesengine/rules
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码 2.13.2 镜像制作命令
- sudo docker build -t myedgex/support-rulesengine:java .
复制代码
2.14 镜像myedgex/device-virtual:java制作
2.14.1 Dockerfile文件
- FROM myalpine-openjdk8-curl
- # environment variables
- ENV APP_DIR=/edgex/device-virtual
- ENV APP=device-virtual
- ENV APP_PORT=49990
- #copy JAR and property files to the image
- #COPY *.properties $APP_DIR/
- #将设备文件目录拷入到镜像,虚拟机启动时默认会加载此两目录下的设备文件
- COPY bacnet_sample_profiles $APP_DIR/bacnet_sample_profiles/
- COPY modbus_sample_profiles $APP_DIR/modbus_sample_profiles/
- #expose logging port
- EXPOSE $APP_PORT
- #set the working directory
- WORKDIR $APP_DIR
- RUN curl -o $APP.jar http://172.17.0.1:8081/nexus/content/repositories/releases/org/edgexfoundry/$APP/0.5.0-docker-RELEASE/$APP-0.5.0-docker-RELEASE.jar
- #kick off the micro service
- ENTRYPOINT java -jar $APP.jar
复制代码
2.14.2 镜像制作命令
- sudo docker build -t myedgex/device-virtual:java .
复制代码
此至,主要的镜像已制作完毕!
三、docker-compose运行edgexfoundry
3.1 docker-compose-java.yml文件
下面显示缩进有问题,可参考文件:
3.2 运行edgexfounry服务
3.2.1 方式一
依次执行如下命令:
- docker-compose -f docker-compose-registry-java.yml up -d volume
- docker-compose -f docker-compose-registry-java.yml up -d config-seed
- docker-compose -f docker-compose-registry-java.yml up -d mongo
- docker-compose -f docker-compose-registry-java.yml up -d logging
- docker-compose -f docker-compose-registry-java.yml up -d notifications
- docker-compose -f docker-compose-registry-java.yml up -d metadata
- docker-compose -f docker-compose-registry-java.yml up -d data
- docker-compose -f docker-compose-registry-java.yml up -d command
- docker-compose -f docker-compose-registry-java.yml up -d export-client
- docker-compose -f docker-compose-registry-java.yml up -d export-distro
- docker-compose -f docker-compose-registry-java.yml up -d rulesengine
- docker-compose -f docker-compose-registry-java.yml up -d device-virtual
复制代码 3.2.2 方式二
执行一个脚本,解压此文件,运行如下:
- chmod +x run-all.sh
- ./run-all.sh docker-compose-java.yml
复制代码 注:docker-compose-java.yml为上面3.1节提到的文件。
3.2.3 运行结果
在浏览器中输入 “http://localhost:8500/ui/dc1/services” , 可打开consul界面, 如下图:
3.2.4 日志查看方法
进入到docker-compose-java.yml文件同目录下,执行:
- docker-compose -f docker-compose-registry-java.yml logs device-virtual
复制代码 说明:device-virtual 是 docker-compose-registry-java.yml中定义的服务名
3.2.4 停止edgexfoundry服务
停止所有服务,并删除容器:
- docker-compose -f docker-compose-registry-java.yml down
复制代码
暂停与继续运行某个服务:
- docker-compose -f docker-compose-registry-java.yml stop device-virtual #暂停
- docker-compose -f docker-compose-registry-java.yml start device-virtual #继续运行
复制代码 3.2.4 其它
查看 edgexfoundry服务
- 命令:docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}\t{{.RunningFor}}"
- 结果:
- NAMES STATUS PORTS CREATED
- edgex-device-virtual Up 3 minutes 0.0.0.0:49990->49990/tcp 3 minutes ago
- support-rulesengine Up 4 minutes 0.0.0.0:48075->48075/tcp 4 minutes ago
- export-distro Up 4 minutes 0.0.0.0:48070->48070/tcp, 0.0.0.0:32769->5566/tcp 4 minutes ago
- export-client Up 4 minutes 0.0.0.0:48071->48071/tcp 4 minutes ago
- core-command Up 5 minutes 0.0.0.0:48082->48082/tcp 5 minutes ago
- core-data Up 5 minutes 0.0.0.0:5563->5563/tcp, 0.0.0.0:48080->48080/tcp 5 minutes ago
- core-metadata Up 5 minutes 0.0.0.0:48081->48081/tcp 5 minutes ago
- support-notifications Up 5 minutes 0.0.0.0:48060->48060/tcp 6 minutes ago
- support-logging Up 5 minutes 0.0.0.0:48061->48061/tcp 6 minutes ago
- edgex-mongo Up 6 minutes 0.0.0.0:27017->27017/tcp 6 minutes ago
- core-config-seed Up 6 minutes 0.0.0.0:8400->8400/tcp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8600->8600/tcp 6 minutes ago
- edgex-files Up 7 minutes 7 minutes ago
复制代码
进入容器查看
注明:下面命令中的“edgex-files” 是上面查看 edgexfoundry服务结果的第一列(Name列),如进入volume服务(容器名为:edgex-files)
- docker exec -it edgex-files /bin/sh
复制代码
拷贝容器里的文件到宿主主机
- docker cp edgex-files:/edgex/logs/core-consul.log ./
复制代码
从volume容器中拷贝core-consul.log到宿主主机整个过程如下面所示
- myEdgex@ubuntu:~$ docker exec -it edgex-files /bin/sh
- # ls
- bin boot consul data dev edgex etc home
- lib lib64 media mnt
- opt proc root run sbin srv sys tmp usr var
- # cd edgex
- # ls
- edgex-support-rulesengine logs
- # cd logs
- # ls
- README core-consul.log edgex-core-data.log edgex-export-client.log edgex-support-logging.log edgex-support-rulesengine.log
- core-config-seed.log edgex-core-command.log edgex-core-metadata.log edgex-export-distro.log edgex-support-notifications.log edgex-device-virtual.log
- # exit
- myEdgex@ubuntu:~$ docker cp edgex-files:/edgex/logs/core-consul.log ./
- myEdgex@ubuntu:~$ ls
- core-consul.log docker-compose-registry-java.yml Documents Downloads Music Pictures Public run-all.sh Templates Videos
复制代码 volumes在主宿中存放的路径是/var/lib/docker/volumes/dockercompose_*,因为我是在 docker-compose目录下执行的docker-compose,所以为“dockercompose_”前辍
(需要root用户权限查看)
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|