如何在CoreOS etcd集群上运行Docker容器

时间:2020-03-05 15:29:21  来源:igfitidea点击:

COREOS是一个新的令人兴奋的操作系统,旨在考虑集群概念,主要是安全和高可用性。
实际上,核心意味着三件事,是操作系统后面的的名称,操作系统的名称和群集平台的名称。

CoreOS是轻量级的基于Linux的操作系统,它可以全面支持Docker。
COREOS允许我们轻松运行整个集群平台,到目前为止,CoreOS主要基于其他组件,etcd,docker和舰队。

Docker.

CoreOS使用Docker容器确保其底层服务和应用程序的孤立的运行时。
它还带来了Docker开箱即用,以便在CoreOS集群中部署和运行应用程序。

Etcd.

etcd是一个分布式键值存储(类型的键值数据库),专为高可用性而设计,主要用于存储在集群中运行应用程序的设置。
CoreOS附带etcdctl,它是与etcd交互的命令行工具。
粗略地说话是在群集中的每台计算机上运行的服务,以处理在集群上运行的软件之间的协调。

Fleet

Fleet是核心集群的一种服务经理。
使用船队,我们可以管理我们的群集,就像我们管理单个机器一样简单。
例如,我们需要启动用于应用程序的特定服务的成像,舰队通过将其提交到群集并决定我们服务的主机来处理我们的服务。
除此之外,通过舰队,我们可以为我们的服务定义一组策略等。
舰队也有Fleetctl一个命令行工具来与之交互。

在Coreos下设置集群

我应该提到CoreOS集群可以在不同的平台上运行,如DigitaloCean,Amazon,Google Compute Engine等,但是,在本文中,我将使用Vagrant和VirtualBox设置并运行本地群集。
因此,Vagrant和VirtualBox是当前文章的两个要求,肯定地,有没有关于如何在机器上安装它们的好文章。

安装了Vagrant和VirtualBox后,我们需要下载与CoreOS团队维护的存储库,因此请继续下载此repo https://github。
如下图像中所示的COM/COREOS/COREOS-VARANT否则我们可以克隆它,如果我们熟悉GIT,则可以克隆它。

用git clone:

➜  coreos-vagrant git:(master) git clone https://github.com/coreos/coreos-vagrant.git

一旦下载归档存档并使用首选文本编辑器打开文件夹。
让我们在整个存储库中散步,并进行一些改变以击杀我们的集群。

ettd发现令牌

为了快速启动核心群集etcd需要一个发现服务来将实例连接在一起,通过存储群集地址,元数据和群集的初始大小等。
ROUROS提供了一种免费服务来处理这种需求。
让我们为大小4的群集生成一个令牌:

➜  coreos-vagrant git:(master)  curl -w "\n" 'https://discovery.etcd.io/new?size=4'
https://discovery.etcd.io/c8ac80bef794eff84e4d52c5af310db0

云配置

可以通过Cloud-Config提供上述发现URL,这是一个最小的配置工具,该工具旨在获取连接到网络并加入群集的机器。

下载的存储库为Cloud-Config命名为用户数据提供蓝图文件。
示例。
因此,我们首先将此文件重命名为用户数据并将发现标记更改为我们的生成发现URL,这是我的情况:https://发现。
ettd。
IO/C8AC80BEF794EFF84E4D52C5AF310DB0

➜  coreos-vagrant git:(master) ✗ cat user-data
#cloud-config
coreos:
etcd2:
#generate a new token for each unique cluster from https://discovery.etcd.io/new
discovery: https://discovery.etcd.io/c8ac80bef794eff84e4d52c5af310db0
# multi-region and multi-cloud deployments need to use $public_ipv4
advertise-client-urls: http://$public_ipv4:2379
initial-advertise-peer-urls: http://$private_ipv4:2380
# listen on both the official ports and the legacy ports
# legacy ports can be omitted if your application doesn't depend on them
listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
listen-peer-urls: http://$private_ipv4:2380,http://$private_ipv4:7001
fleet:
public-ip: $public_ipv4
flannel:
interface: $public_ipv4
units:
- name: etcd2.service
command: start
- name: fleet.service
command: start
- name: flanneld.service
drop-ins:
- name: 50-network-config.conf
content: |
[Service]
ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
command: start
- name: docker-tcp.socket
command: start
enable: true
content: |
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=2375
Service=docker.service
BindIPv6Only=both
[Install]
WantedBy=sockets.target

vagrantfile.

我们再次需要通过分配值5更改此文件中的更改$num_instances。

启动群集

要启动群集只需打开终端并指向下载的存储库并运行:

➜  coreos-vagrant git:(master) ✗ vagrant up
Bringing machine 'core-01' up with 'virtualbox' provider...
Bringing machine 'core-02' up with 'virtualbox'
...

现在我们可以像下面的那样ssh创造的机器:

➜  coreos-vagrant git:(master) ✗ vagrant ssh core-02
Last login: Fri Dec 23 17:22:51 UTC 2015 from 10.0.2.2 on ssh
CoreOS stable (1185.5.0)
core@core-03 ~ $

现在我们可以检查我们集群的实例数:

core@core-02 ~ $etcdctl member list
cce1ee23391bec4: name=397f492f266c4acab61e83ecc2adb388 peerURLs=http://172.17.8.103:2380 clientURLs=http://172.17.8.103:2379 isLeader=false
3e5d3e6bfe52303b: name=fb1c3d855e764520bb675ecf1fcbad22 peerURLs=http://172.17.8.102:2380 clientURLs=http://172.17.8.102:2379 isLeader=false
47f62622394dbef2: name=43bc777f8b654d19abddc2978a74cd0c peerURLs=http://172.17.8.101:2380 clientURLs=http://172.17.8.101:2379 isLeader=true
a8b3dab66f0a5fc5: name=57fcfc8813bd4bf49947e660574b525f peerURLs=http://172.17.8.104:2380 clientURLs=http://172.17.8.104:2379 isLeader=false
core@core-02 ~ $

我们还可以检查集群中的计算机:

core@core-02 ~ $fleetctl list-machines --full=true
MACHINE                  IP     METADATA
397f492f266c4acab61e83ecc2adb388   172.17.8.103   
43bc777f8b654d19abddc2978a74cd0c   172.17.8.101   
57fcfc8813bd4bf49947e660574b525f   172.17.8.104   
c773afd96d37462288c97c1b8695fad2   172.17.8.105   
fb1c3d855e764520bb675ecf1fcbad22   172.17.8.102   

示例NodeJS应用程序

现在我要创建一个示例nodejs应用程序,以演示我们如何运行群集中的Docker容器。

//文件应用程序。
JS.

➜  sampleNodeJsApp git:(master) ✗ cat app.js
var express = require('express');
var app = express();
var PORT = 3000;
app.get('/ping', function (req, res) {
res.write('Hi there I\'m up and running!!');
res.end();
})
app.listen(PORT, function () {
console.log('Server listening on port: ', PORT);
})

//文件 - 包。
杰森

➜  sampleNodeJsApp git:(master) ✗ cat package.json
{
"name": "sampleNodeJsApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0"
}
}

//文件dockerfile.

➜  sampleNodeJsApp git:(master) ✗ cat Dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/package.json
RUN npm i
COPY . /usr/src/app/
EXPOSE 8080
CMD [ "node", "app.js" ]

构建Docker图片:

➜  sampleNodeJsApp git:(master) ✗ docker build -t theitroad/sample-nodejs-app:v0.0.1 .

将Docker图像推到Docker Hub

➜  sampleNodeJsApp git:(master) ✗ docker push theitroad/sample-nodejs-app:v0.0.1

单元文件

CoreOS部署使用被叫单元文件,因此为了能够部署示例NodeJS应用程序已经创建,我们需要创建两个文件;第一个文件我将调用它sample-nodejs-app-deployment @。
服务此文件将拉动我们的构建和推送Docker镜像(见上文)并启动Docker容器;第二个文件我将调用它sample-nodejs-app-service @。
服务此文件公开我们的应用部署。

请注意,需要在我们的群集计算机之一内创建这些文件:

// file - [email protected]

core@core-02 ~ $ cat sample-nodejs-app-deployment\@.service

 

[Unit]

 

Description=Sample NodeJs app inside a docker container under CoreOS cluster

 

After=etcd2.service

 

After=docker.service

 

Requires=sample-nodejs-app-service@%i.service

 

[Service]

 

TimeoutStartSec=0

 

KillMode=none

 

EnvironmentFile=/etc/environment

 

ExecStartPre=-/usr/bin/docker kill sample-nodejs-app-deployment%i

 

ExecStartPre=-/usr/bin/docker rm sample-nodejs-app-deployment%i

 

ExecStartPre=/usr/bin/docker pull linoxide/sample-nodejs-app:v0.0.1

 

ExecStart=/usr/bin/docker run --name sample-nodejs-app-deployment%i -p %i:3000 \

 

    -P -e COREOS_PRIVATE_IPV4=${COREOS_PRIVATE_IPV4}        \

 

    linoxide/sample-nodejs-app:v0.0.1

 

ExecStop=/usr/bin/docker stop sample-nodejs-app-deployment%i

 

[X-Fleet]

 

Conflicts=sample-nodejs-app-deployment@*.service

 

// file - [email protected]

core@core-02 ~ $ cat sample-nodejs-app-service\@.service

 

[Unit]

 

Description=Announce sample-nodejs-app-deployment@%i service

 

BindsTo=sample-nodejs-app-deployment@%i.service

 

[Service]

 

EnvironmentFile=/etc/environment

 

ExecStart=/bin/sh -c "while true; do etcdctl set /announce/services/sample-nodejs-app-deployment%i ${COREOS_PUBLIC_IPV4}:%i --ttl 60; sleep 45; done"

 

ExecStop=/usr/bin/etcdctl rm /announce/services/sample-nodejs-app-deployment%i

 

[X-Fleet]

 

MachineOf=sample-nodejs-app-deployment@%i.service

提交 [email protected][email protected]

core@core-02 ~ $fleetctl submit sample-nodejs-app-deployment\@.service sample-nodejs-app-service\@.service
Unit [email protected] inactive
Unit [email protected] inactive

列出所有服务:

core@core-02 ~ $fleetctl list-unit-files
UNIT                 HASH   DSTATE     STATE      TARGET
[email protected]  8b9612b    inactive inactive 
[email protected] d929f3f   inactive inactive 

现在两个服务都在群集中可用。
但是,它们可能会在非活动状态下注意到。
让我们加载这两个服务。
我们将以如下图所示的服务名称在Fleetctl中传递端口3000,此端口将用于运行此端口上的端口的Docker容器。

core@core-02 ~ $fleetctl load [email protected]
WARNING: Unit [email protected] in registry differs from local template unit file [email protected]
Unit [email protected] inactive
Unit [email protected] loaded on 397f492f.../172.17.8.103
core@core-02 ~ $fleetctl load [email protected]
Unit [email protected] inactive
Unit [email protected] loaded on 397f492f.../172.17.8.103

一旦加载服务,我们就可以使用Fleetctl开始启动它们:

core@core-02 ~ $fleetctl start [email protected]
WARNING: Unit [email protected] in registry differs from local template unit file [email protected]
Unit [email protected] launched on 397f492f.../172.17.8.103
core@core-02 ~ $fleetctl start [email protected]
Unit [email protected] launched on 397f492f.../172.17.8.103

注意IP地址172.17. 8. 103托管NodeJS应用程序的103.
所以,让我们,打开你的浏览器并尝试点击'/ping'端点:172.17. 8. 103:3000/ping