很久不看docker的东西了,之前了解的一些基本命令都忘得差不多了,适逢工作需要,再来复习巩固下。今天想完成的是:借助docker不部署下自己的服务。

环境准备

都说“巧妇难为无米之炊”,所以还是需要先准备下的。

OS:Ubuntu 16.04, 2G内存
docker:1.13.2
coding language: golang (gin web framework)

编码

将服务跑起来,是我们要完成的第一个步骤,而且是最重要的一个步骤。所以这一步需要仔细调试,为了方便,我就写的简单点。

  1. app.go
package mainimport ("os""log""github.com/gin-gonic/gin""net/http""time"
)func GetHostName() string {hostname, err := os.Hostname()if err != nil {log.Fatal(err)}return hostname
}func GetCurrentTime() string {timer := time.Now()return timer.String()
}func startGinApp() {app := gin.Default()app.GET("/ping", func(context *gin.Context) {context.JSON(http.StatusOK, gin.H{"message": "当前为您服务的主机为:" + GetHostName(),})})app.GET("/", func(context *gin.Context) {context.JSON(http.StatusOK, gin.H{"message": "当前时间为:" + GetCurrentTime(),})})app.Run(":8080")
}func main() {startGinApp()
}
  1. 把服务跑起来
go run app.go
  1. curl一下看看服务是否正确跑起来了
➜  gin curl http://localhost:8080
{"message":"当前时间为:2018-10-14 11:31:23.016121853 +0800 CST m=+0.254631109"}%
➜  gin curl http://localhost:8080/ping
{"message":"当前为您服务的主机为:BiaodeMacBook-Pro.local"}%
➜  gin

制作Makefile

经过刚才的测试,代码可以正确跑起来了。但是要做到“一次编码,到处运行”,还是需要在构建阶段下点心思的,为了更好的维护,借助Makefile来规范构建过程,是比较合适的方法。

  1. Makefile
# 这个是注释
# 开头可以声明一大堆变量名
BUILD_NAME ?= httpserver
COMPILER ?= go
BUILD ?= build# 上方留一个空格,区分变量区和构建区
all: build test deploy cleanbuild:CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(COMPILER) $(BUILD) -o $(BUILD_NAME)test:echo "test over."deploy:echo "deploy over."clean:echo "clean over.".PHONY: build test deploy clean
  1. 构建服务
➜  gin ls
Makefile app.go
➜  gin make all
go build -o httpserver
echo "test over."
test over.
echo "deploy over."
deploy over.
echo "clean over."
clean over.
➜  gin ls
Makefile   app.go     httpserver
➜  gin ./httpserver &
[1] 6450
➜  gin [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.- using env:    export GIN_MODE=release- using code:   gin.SetMode(gin.ReleaseMode)[GIN-debug] GET    /ping                     --> main.startGinApp.func1 (3 handlers)
[GIN-debug] GET    /                         --> main.startGinApp.func2 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080➜  gin curl http://localhost:8080/ping
[GIN] 2018/10/14 - 11:50:23 | 200 |     214.161µs |             ::1 | GET      /ping
{"message":"当前为您服务的主机为:BiaodeMacBook-Pro.local"}%
➜  gin

好了基本没什么问题,然后就可以通过scp命令将文件拷贝到linux服务器上了。

制作Dockerfile

由于golang构建出来的是二进制可执行程序,所以制作Dockerfile很简单。

  1. Dockerfile
FROM ubuntu:latestMAINTAINER guopu marksinoberg@gmail.comWORKDIR /appEXPOSE 8080ADD . /appCMD ["./httpserver"]
  1. 构建自己的镜像
root@Server218 /h/d/g/d/httpserver# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ginserver           latest              6277a0180f4f        19 hours ago        101 MB
flaskindocker       latest              170c6c0a41db        22 hours ago        131 MB
python              2.7-slim            14dad3ead5f4        4 days ago          120 MB
docker/compose      1.23.0-rc2          dc59a0b5e981        5 days ago          45.6 MB
ubuntu              latest              cd6d8154f1e1        5 weeks ago         84.1 MB
root@Server218 /h/d/g/d/httpserver# docker build -t httpserver .
Sending build context to Docker daemon 17.07 MB
Step 1/6 : FROM ubuntu:latest---> cd6d8154f1e1
Step 2/6 : MAINTAINER guopu marksinoberg@gmail.com---> Running in 7106748df3ad---> 0ae808029537
Removing intermediate container 7106748df3ad
Step 3/6 : WORKDIR /app---> 7278bf9659e7
Removing intermediate container f7fdc76b19a8
Step 4/6 : EXPOSE 8080---> Running in bedfabcb4b16---> edf4c123f72f
Removing intermediate container bedfabcb4b16
Step 5/6 : ADD . /app---> 36390e554a2f
Removing intermediate container b14cce9da53e
Step 6/6 : CMD ./httpserver---> Running in 6682c8364717---> b490fef8a9ca
Removing intermediate container 6682c8364717
Successfully built b490fef8a9ca
root@Server218 /h/d/g/d/httpserver# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpserver          latest              b490fef8a9ca        4 seconds ago       101 MB
ginserver           latest              6277a0180f4f        19 hours ago        101 MB
flaskindocker       latest              170c6c0a41db        22 hours ago        131 MB
python              2.7-slim            14dad3ead5f4        4 days ago          120 MB
docker/compose      1.23.0-rc2          dc59a0b5e981        5 days ago          45.6 MB
ubuntu              latest              cd6d8154f1e1        5 weeks ago         84.1 MB
root@Server218 /h/d/g/d/httpserver#
  1. 让服务在docker中跑起来
root@Server218 /h/d/g/d/httpserver# docker run -d  -p 8000:8080 httpserver
ebb1926206cdaf16eae9f4e13d5a7e70dd695da91463b438f77e45c6f65d3323
root@Server218 /h/d/g/d/httpserver# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
ebb1926206cd        httpserver          "./httpserver"      4 seconds ago       Up 3 seconds        0.0.0.0:8000->8080/tcp   nostalgic_wright
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:ebb1926206cd"}~                                                                                                                                 root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/
{"message":"当前时间为:2018-10-14 04:14:29.116207751 +0000 UTC"}~                                                                                                                root@Server218 /h/d/g/d/httpserver#

至此,看起来服务已经成功在服务器环境下的docker中运行了。

弹性服务

一个容器跑一个服务,有些服务访问峰差很大的场景,就需要做下弹性适配,于是需要用一下docker swarm服务。这个在linux环境下需要进行安装。具体可以参考官网链接: https://github.com/docker/compose/releases
其运行以来一个YAML配置文件,具体细节不多说,上手吧。

  1. docker-compose.yml
version: "3"
services:web:image: httpserver:latestdeploy:replicas: 5resources:limits:cpus: "0.1"memory: 50Mrestart_policy:condition: on-faliureports:- "8000:8080"networks:- webnet
networks:webnet:
  1. 初始化swarm
root@Server218 /h/d/g/d/httpserver# docker swarm init
Swarm initialized: current node (atiuy6c8k1qcig5w3br1bwf0n) is now a manager.To add a worker to this swarm, run the following command:docker swarm join \--token SWMTKN-1-5e0sj0glbwl5w5rqytyqokeg91n7piwdyw9ik598x0poiz5s20-do445zhrdr5io93lplov42c2y \172.31.237.68:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.root@Server218 /h/d/g/d/httpserver#
  1. 发布服务到swarm中
root@Server218 /h/d/g/d/httpserver# docker stack deploy -c docker-compose.yml httpserver
Creating service httpserver_web
root@Server218 /h/d/g/d/httpserver#
root@Server218 /h/d/g/d/httpserver#
root@Server218 /h/d/g/d/httpserver#
root@Server218 /h/d/g/d/httpserver# docker stack ps httpserver
ID            NAME              IMAGE              NODE       DESIRED STATE  CURRENT STATE           ERROR  PORTS
kvpvwptlj44c  httpserver_web.1  httpserver:latest  Server218  Running        Running 14 seconds ago
6549g79dz5iz  httpserver_web.2  httpserver:latest  Server218  Running        Running 14 seconds ago
xkz42mnetmws  httpserver_web.3  httpserver:latest  Server218  Running        Running 14 seconds ago
rpziwzmpogn2  httpserver_web.4  httpserver:latest  Server218  Running        Running 14 seconds ago
y2kfe8bp09ld  httpserver_web.5  httpserver:latest  Server218  Running        Running 6 seconds ago
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:a5419e3d3f9c"}~
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:f636819bd9c4"}~
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:97a6e3c9a064"}~
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:7f1b28e14970"}~
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:0ce251661188"}~
root@Server218 /h/d/g/d/httpserver# curl http://localhost:8000/ping
{"message":"当前为您服务的主机为:a5419e3d3f9c"}~
root@Server218 /h/d/g/d/httpserver#
  1. 实例弹性变化
    具体的操作只需要修改docker-compose.yml中的replicas的数量即可。然后重新使用:
docker stack deploy -c docker-compose.yml httpserver

发布就可以了,可以看出swarm管理下的实例会进行自动的负载均衡。

  1. 关停服务
root@Server218 /h/d/g/d/httpserver# docker stack ls
NAME        SERVICES
httpserver  1
root@Server218 /h/d/g/d/httpserver# docker stack rm httpserver
Removing service httpserver_web
Removing network httpserver_webnet
root@Server218 /h/d/g/d/httpserver# docker stack ls
NAME  SERVICES
root@Server218 /h/d/g/d/httpserver#
  1. 关掉swarm
root@Server218 /h/d/g/d/httpserver# docker swarm leave --force
Node left the swarm.
root@Server218 /h/d/g/d/httpserver#

总结

最后,回头看看这个目录。

root@Server218 /h/d/g/d/httpserver# ls -al
total 16688
drwxr-xr-x 2 root root     4096 Oct 14 12:20 ./
drwxr-xr-x 5 root root     4096 Oct 14 12:00 ../
-rw-r--r-- 1 root root      121 Oct 14 12:11 Dockerfile
-rw-r--r-- 1 root root      362 Oct 14 12:01 Makefile
-rw-r--r-- 1 root root      682 Oct 14 12:01 app.go
-rw-r--r-- 1 root root      385 Oct 14 12:27 docker-compose.yml
-rwxr-xr-x 1 root root 17063672 Oct 14 12:03 httpserver*
root@Server218 /h/d/g/d/httpserver#

基本上,在docker中弹性部署自己的服务就结束了,基本上也能满足需要。

用docker弹性部署自己的服务相关推荐

  1. Docker实战-部署GPE微服务的监控体系(二)

    前言 上篇文章:我们介绍了GPE体系中,grafana的部署和安装(<Docker实战-部署GPE微服务的监控体系>),今天这个文章,我们继续介绍GPE体系中,Prometheus和Exp ...

  2. Docker实战-部署GPE微服务的监控体系

    Docker实战-部署GPE微服务的监控体系 前言 微服务体系架构里,有很多的解决方案都是使用GPE作为微服务体系的监控体系, 如下图所示: 我们这里经常提到的GPE,包括Grafana,Promet ...

  3. Docker的部署-包括网关服务(Ocelot)+认证服务(IdentityServer4)+应用服务

    本文主要介绍通过Docker来部署通过.Net Core开发的微服务架构,部署的微服务主要包括统一网关(使用Ocelot开发).统一认证(IdentityServer4).应用服务(asp.net c ...

  4. docker 部署java_使用Docker堆栈部署的微服务-WildFly,Java EE和Couchbase

    docker 部署java 关于微服务的资料很多,只是用谷歌搜索就可以了 ! 几年前,我在比利时的Devoxx上发表了有关将单片重构为微服务的演讲,它获得了很好的评价: 该博客将展示Docker如何简 ...

  5. 使用Docker堆栈部署的微服务-WildFly,Java EE和Couchbase

    关于微服务的资料很多,只是用谷歌搜索就可以了 ! 几年前,我在比利时的Devoxx上发表了有关将单片重构为微服务的演讲,它得到了很好的评价: 该博客将展示Docker如何简化微服务的创建和关闭. 该博 ...

  6. docker中部署piggymetrics微服务项目

    1.环境 vmware 10. centos7.0,Docker(version 18.09.7, build 2d0083d),docker-compose(version 1.24.1, buil ...

  7. macOS、Linux CentOS 、Docker安装部署canal-server(canal-deployer)服务

    1.环境准备 canal-server(canal-deployer)依赖jdk,需要先安装部署好jdk1.8 mysql开启binlog 2.安装部署 2.1.Docker方式安装部署 这里以1.1 ...

  8. 基于docker部署的微服务架构(九): 分布式服务追踪 Spring Cloud Sleuth

    为什么80%的码农都做不了架构师?>>>    前言 微服务架构中完成一项功能经常会在多个服务之间远程调用(RPC),形成调用链.每个服务节点可能在不同的机器上甚至是不同的集群上,需 ...

  9. docker一键部署jenkins服务(一)

    一.简介 1)docker是当前流行的容器技术,通过docker技术可以实现快速的部署常见的服务. 2)jenkins是当前主流的持续集成的工具和框架(CI),通过jenkins可以实现项目的智能化构 ...

最新文章

  1. 剖析 Laravel 计划任务--事件属性
  2. #region(C# 参考)
  3. jvm:虚方法与非虚方法
  4. 第2周项目2程序的多文件组织
  5. Spring学习笔记15--注解Bean
  6. 2020-11-12(JNI开发常见错误)
  7. Deeplearnng.AI第四部分第一周、卷积神经网络
  8. 《阿里巴巴数据中台实践》深入理解
  9. 数据写入规则IBufferWriterT
  10. 玩转oracle 11g(32):plsql版本低需到配置文件中添加配置
  11. rman 脚本备份全过程
  12. SQL Server 2005大小写敏感设置
  13. 特斯拉Cybertruck与新款Model S同时在加州工厂曝光
  14. Windows XP解决显示桌面图标消失的问题
  15. PYTHON_错误处理
  16. 句法结构可视化工具(成分句法)
  17. 查看链接文件的最终目标的多种方法
  18. Linux学习笔记——1、Basic knowledge
  19. 论文阅读笔记:MuTual: A Dataset for Multi-Turn Dialogue Reasoning
  20. yolov7利用onnx进行推理同时调用usb摄像头

热门文章

  1. PyCharm打包py文件为exe文件
  2. 无需NAS,让你躺在床上用ipad也能够直接观看电脑上的视频(安卓手机也可的简易方法)
  3. 如果 结局没有你 我也会很好
  4. vue + ArcGIS 地图应用系列三:添加常规的地图组件
  5. 文学方面的思考3 孙犁《嘱咐》欣赏
  6. GoAccess 一款好用的web日志分析工具
  7. 2021各地区数字中国发展成效评价
  8. html经典坦克大战,HTML+CSS+JQ试做经典坦克大战(二)
  9. 一场关于物理学本质的争论:实验是检验科学的唯一标准吗?
  10. 2018中国数据科学家工资究竟是多少?(最新数据)