Derrick 是一个可以帮助开发者快速完成应用容器化的工具。Derrick 主要关注的是希望容器化的开发者如何在本地进行开发的问题。开发者可以通过使用 Derrick 加速本地应用容器化迁移的过程。
依赖:
Derrick 安装:
# 安装 derrick$ pip install python-derrick# 报 fatal error: Python.h: No such file or directory 错误,请安装下面包(CentOS 例子)$ yum install -y python-devel
# 下载 derrick-nodejs-demo node 项目$ git clone https://github.com/ringtail/derrick-nodejs-demo# 选择需要部署的平台,支持 Kubernetes 和 Swarm# 因机器上没有安装 Kubernetes,这里选择 Swarm$ derrick config? Which Orchestration Engine would you like to choose? Swarm# 运行 Derrick$ derrick init? Please input image name with tag (such as "registry.com/user/repo:tag"): yangpeng2468/test:v3Derrick detect your platform is NodeJs and compile successfully.# 查看生成的配置$ git status# 位于分支 master# 未跟踪的文件:# (使用 "git add <file>..." 以包含要提交的内容)## .dockerignore# Dockerfile# Jenkinsfile# derrick_conf# docker-compose.yml# kubernetes-deployment.yaml提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
# 本地验证容器化结果$ derrick upSwarm is the default Orchestration Engine.Creating network "derrick-nodejs-demo_default" with the default driverBuilding webStep 1/14 : FROM node:8-alpine AS base---> 26881633664eStep 2/14 : WORKDIR /app---> Running in 2d1bf9d12f4fRemoving intermediate container 2d1bf9d12f4f---> b123be5925b6Step 3/14 : COPY package.json .---> ba2c32fa8283Step 4/14 : RUN npm set progress=false && npm config set depth 0---> Running in 3f20024fd462Removing intermediate container 3f20024fd462---> f100fe6b5639Step 5/14 : RUN npm install --only=production --registry=https://registry.npm.taobao.org---> Running in 44660d8e2f98npm WARN deprecated json3@3.3.2: Please use the native JSON object instead of JSON 3> ejs@2.7.4 postinstall /app/node_modules/ejs> node ./postinstall.jsThank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)npm notice created a lockfile as package-lock.json. You should commit this file.added 59 packages from 43 contributors in 2.481sRemoving intermediate container 44660d8e2f98---> 950daf993e48Step 6/14 : RUN cp -R node_modules prod_node_modules---> Running in c306e49790f3Removing intermediate container c306e49790f3---> 822c75e9a625Step 7/14 : RUN npm install --registry=https://registry.npm.taobao.org---> Running in d0c4ee623cd0added 34 packages from 291 contributors in 4.053sRemoving intermediate container d0c4ee623cd0---> 2fdc9f38bb35Step 8/14 : FROM base As test---> 2fdc9f38bb35Step 9/14 : COPY . /app---> aa81f12f20c1Step 10/14 : RUN npm test---> Running in 5202ce0a6f9d> derrick-nodejs-generator@0.0.0 test /app> node_modules/mocha/bin/mochaArray#indexOf()✓ should return -1 when the value is not present1 passing (6ms)Removing intermediate container 5202ce0a6f9d---> 9ea003d029a4Step 11/14 : FROM base AS release---> 2fdc9f38bb35Step 12/14 : COPY --from=base /app/prod_node_modules /app/node_modules---> 74e76976eb8fStep 13/14 : COPY . /app---> 9fd22d80079aStep 14/14 : CMD ["npm","start"]---> Running in 18a736e9b63fRemoving intermediate container 18a736e9b63f---> 1e9cda2d6697Successfully built 1e9cda2d6697Successfully tagged yangpeng2468/test:v3Creating derrick-nodejs-demo_web_1 ... doneYour application has been up to running! You can run `docker ps` to get exposed ports.
# 查看运行容器$ docker-compose psName Command State Ports--------------------------------------------------------------------------------------------derrick-nodejs-demo_web_1 docker-entrypoint.sh npm start Up 0.0.0.0:32769->3000/tcp
浏览器访问 172.17.0.6:32769,展示如下页面
# 查看 k8s deployment 配置$ cat kubernetes-deployment.yaml
---apiVersion: v1kind: Servicemetadata:annotations:derrick.service.type: nodeportderrick.version: 0.0.24labels:derrick.service: derrick-nodejs-demoname: derrick-nodejs-demospec:ports:- name: "3000"port: 3000targetPort: 3000selector:derrick.service: derrick-nodejs-demotype: NodePort---apiVersion: extensions/v1beta1kind: Deploymentmetadata:annotations:derrick.version: 0.0.24labels:derrick.service: derrick-nodejs-demoname: derrick-nodejs-demospec:replicas: 1template:metadata:labels:derrick.service: derrick-nodejs-demospec:containers:- image: yangpeng2468/test:v3name: webports:- containerPort: 3000
# 查看 jenkinsfile配置$ cat Jenkinsfile
pipeline {environment {REGISTRY_ENDPOINT = "https://yangpeng2468/v2/"IMAGE_WITH_TAG = "yangpeng2468/test:v3"REGISTRY_CERTS = "registry"}agent {node {label 'nodejs'}}stages {stage('Build') {steps {sh 'npm install --registry=https://registry.npm.taobao.org'}}stage('Test') {steps {sh 'npm test '}}stage('Code Quality') {steps {script {try{checkstyle canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''}catch(e){echo e}}}}stage('Image Build&Publish') {steps {echo 'Build Images'script {docker.withRegistry("${REGISTRY_ENDPOINT}", "${REGISTRY_CERTS}") {sh 'docker build -t ${IMAGE_WITH_TAG} .'sh 'docker push ${IMAGE_WITH_TAG}'}}}}}triggers {pollSCM('* * * * *')}}
欢迎大家关注交流,定期分享自动化运维、DevOps、Kubernetes、Service Mesh和Cloud Native