Docker集群整理(docker+etcd+swarm)

1、准备环境

1-1、准备host主机

  • node195: 192.168.0.195
  • node196: 192.168.0.196
  • node197: 192.168.0.197

注意:要设置host主机hostname为不一样,因为etcd要根据hostname来识别不同的主机,如果hostname不小心设置成一样,集群中同一overlay网络不同host节点容器之间的网络通信将会失败(无法ping通)

1-2、节点信息

  • etcd 服务器: node195
  • swarm manage: node195
  • cluster hosts(集群中的主机): node196, node197
  • registry host(局域网镜像仓库): node197

1-3、软件安装

  • 在所有集群上安装docker,CentOS系统安装docker步骤可以参考官网

注意:由于后续配置集群搭建的需要,这里需要对docker的启动配置做一下调整,首先创建并编辑docker启动配置文件,具体参考下面命令:

# 创建并进入配置文件
vi /etc/default/docker
# 填写以下内容
DOCKER_OPTS="--registry-mirror=https://x5jytudq.mirror.aliyuncs.com -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --insecure-registry 192.168.0.197:5000 --cluster-advertise 192.168.0.196:2375 --cluster-store etcd://192
.168.0.195:2379"
# 打开docker的启动文件
vi /lib/systemd/system/docker.service


# 内容如下
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target


# 修改docker.service配置文件
#在ExecStart=/usr/bin/dockerd 前添加内容
EnvironmentFile=-/etc/default/docker
# 修改ExecStart=/usr/bin/dockerd
ExecStart=/usr/bin/dockerd $DOCKER_OPTS
# 重启docker服务
systemctl daemon-reload
systemctl restart docker

其中 --registry-mirror=https://x5jytudq.mirror.aliyuncs.com 表示配置阿里云私有镜像仓库;-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock表示开启docker的远程访问;--insecure-registry 192.168.0.197:5000表示非安全(http请求,安全型是https,类似阿里云私有镜像仓库)的本地镜像仓库;--cluster-advertise 192.168.0.196:2375表示集群中host节点的服务发现IP配置;--cluster-store etcd://192.168.0.195:2379"表示服务发现的etcd数据存储服务;

  • 在node195安装docker compose,具体安装步骤可参考官网
  • 在node196 上执行docker pull quay.io/coreos/etcd(官网可能下载的比较慢,可以开加速器尝试加速下载)
  • 在所有host上执行docker pull swarm拉取最新的swarm镜像
  • 关闭局域网所有host防火墙,不然后续跨主机容器间通信可能会受到影响

2、安装etcd k-v数据库

在node195上执行以下代码,具体可以参考官网

# 设置HostIP
export HostIP=192.168.0.195
# 执行etcd安装启动命令
docker run -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
 --restart=always \
 --name etcd quay.io/coreos/etcd \
 /usr/local/bin/etcd \
 -name etcd0 \
 -advertise-client-urls http://${HostIP}:2379,http://${HostIP}:4001 \
 -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
 -initial-advertise-peer-urls http://${HostIP}:2380 \
 -listen-peer-urls http://0.0.0.0:2380 \
 -initial-cluster-token etcd-cluster-1 \
 -initial-cluster etcd0=http://${HostIP}:2380 \
 -initial-cluster-state new

3、将host节点加入集群

执行以下代码,让node196和node197加入集群:

# 在node196上执行,将node196加入集群
docker run -d --name node196 --restart=always swarm join --addr=192.168.0.196:2375 etcd://192.168.0.195:2379/swarm
#在node197上执行,将node197加入集群
docker run -d --name node197 --restart=always swarm join --addr=192.168.0.197:2375 etcd://192.168.0.195:2379/swarm

4、启动swarm manage管理节点,并测试集群是否搭建成功

在node195上执行

# 启动swarm管理节点
docker run -d -p 3376:3376 -t \
--restart=always \
--name manage swarm manage \
-H 0.0.0.0:3376 \
etcd://192.168.0.195:2379/swarm
#设置DOCKER_HOST变量
export DOCKER_HOST=192.168.0.195:3376
#查看节点中的集群信息
docker info


#内容如下
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: swarm/1.2.6
Role: primary
Strategy: spread
Filters: health, port, containerslots, dependency, affinity, constraint, whitelist
Nodes: 2
 node196: 192.168.0.196:2375
  └ ID: 2XXV:XLC4:6DZR:RIDS:GJI7:QIVK:ITKM:ZD2O:FX7R:J3CT:O6ST:ZU4B
  └ Status: Healthy
  └ Containers: 0 (0 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 1
  └ Reserved Memory: 0 B / 4.914 GiB
  └ Labels: kernelversion=3.10.0-327.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=overlay
  └ UpdatedAt: 2017-03-04T02:19:23Z
  └ ServerVersion: 1.13.1
 node197: 192.168.0.197:2375
  └ ID: LMIN:53UN:MPR5:NEAQ:EQNZ:T3ZD:265Y:6CHI:JQPV:QWWV:RKK3:YTJW
  └ Status: Healthy
  └ Containers: 0 (0 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 2
  └ Reserved Memory: 0 B / 5.086 GiB
  └ Labels: kernelversion=3.10.0-514.6.2.el7.x86_64, operatingsystem=CentOS Linux 7 (Core), storagedriver=overlay
  └ UpdatedAt: 2017-03-04T02:19:28Z
  └ ServerVersion: 17.03.0-ce
Plugins: 
 Volume: 
 Network: 
Swarm: 
 NodeID: 
 Is Manager: false
 Node Address: 
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: linux
Architecture: amd64
CPUs: 4
Total Memory: 14.91 GiB
Name: 24fb2d69de9d
Docker Root Dir: 
Debug Mode (client): false
Debug Mode (server): false
WARNING: No kernel memory limit support
Experimental: false
Live Restore Enabled: false

5、自定义overlay网络,并测试不同节点之间的网络通信

在node195 swarm manage节点上执行,关于overlay可参考官网

#创建自定义网络my-net
docker network create --driver overlay --subnet=10.0.9.0/24 my-net
#安装并启动一个nginx web容器,并约束该容器安装到node196上
docker run -itd --name=web --network=docker_default --env="constraint:node==node196" nginx
#安装并启动一个busybox容器,并约束将该容器安装到node197上,执行命令访问web服务
docker run -it --rm --network=my-net --env="constraint:node==node196" busybox wget -O- http://web

#展示内容
Unable to find image 'busybox:latest' locally
 latest: Pulling from library/busybox
 ab2b8a86ca6c: Pull complete
 2c5ac3f849df: Pull complete
 Digest: sha256:5551dbdfc48d66734d0f01cafee0952cb6e8eeecd1e2492240bf2fd9640c2279
 Status: Downloaded newer image for busybox:latest
 Connecting to web (10.0.0.2:80)
 <!DOCTYPE html>
 <html>
 <head>
 <title>Welcome to nginx!</title>
 <style>
 body {
         width: 35em;
         margin: 0 auto;
         font-family: Tahoma, Verdana, Arial, sans-serif;
 }
 </style>
 </head>
 <body>
 <h1>Welcome to nginx!</h1>
 <p>If you see this page, the nginx web server is successfully installed and
 working. Further configuration is required.</p>

 <p>For online documentation and support please refer to
 <a href="http://nginx.org/">nginx.org</a>.<br/>
 Commercial support is available at
 <a href="http://nginx.com/">nginx.com</a>.</p>

 <p><em>Thank you for using nginx.</em></p>
 </body>
 </html>
 -                    100% |*******************************|   612   0:00:00 ETA
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容