LXD 2.0 系列(十):LXD 和 Juju

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:

介绍

Juju 是 Canonical 的服务建模和部署工具。 它支持非常广泛的云服务提供商,使您能够轻松地在任何云上部署任何您想要的服务。

此外,Juju 2.0 还支持 LXD,既适用于本地部署,也适合开发,并且可以在云实例或物理机上共同协作。

本篇文章将关注本地使用,通过一个没有任何Juju经验的LXD用户来体验。

要求

本篇文章假设你已经安装了 LXD 2.0 并且配置完毕(看前面的文章),并且是在 Ubuntu 16.04 LTS 上运行的。

设置 Juju

第一件事是在 Ubuntu 16.04 上安装 Juju 2.0。这个很简单:

 
  1. stgraber@dakara:~$ sudo apt install juju
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following additional packages will be installed:
  6. juju-2.0
  7. Suggested packages:
  8. juju-core
  9. The following NEW packages will be installed:
  10. juju juju-2.0
  11. 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
  12. Need to get 39.7 MB of archives.
  13. After this operation, 269 MB of additional disk space will be used.
  14. Do you want to continue? [Y/n]
  15. Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 juju-2.0 amd64 2.0~beta7-0ubuntu1.16.04.1 [39.6 MB]
  16. Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 juju all 2.0~beta7-0ubuntu1.16.04.1 [9,556 B]
  17. Fetched 39.7 MB in 0s (53.4 MB/s)
  18. Selecting previously unselected package juju-2.0.
  19. (Reading database ... 255132 files and directories currently installed.)
  20. Preparing to unpack .../juju-2.0_2.0~beta7-0ubuntu1.16.04.1_amd64.deb ...
  21. Unpacking juju-2.0 (2.0~beta7-0ubuntu1.16.04.1) ...
  22. Selecting previously unselected package juju.
  23. Preparing to unpack .../juju_2.0~beta7-0ubuntu1.16.04.1_all.deb ...
  24. Unpacking juju (2.0~beta7-0ubuntu1.16.04.1) ...
  25. Processing triggers for man-db (2.7.5-1) ...
  26. Setting up juju-2.0 (2.0~beta7-0ubuntu1.16.04.1) ...
  27. Setting up juju (2.0~beta7-0ubuntu1.16.04.1) ...

安装完成后,我们可以使用 LXD 启动一个新的“控制器”。这意味着 Juju 不会修改你主机上的任何东西,它会在 LXD 容器中安装它的管理服务。

现在我们创建一个“test”控制器:

 
  1. stgraber@dakara:~$ juju bootstrap localhost test
  2. Creating Juju controller "local.test" on localhost/localhost
  3. Bootstrapping model "admin"
  4. Starting new instance for initial controller
  5. Launching instance
  6. - juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0
  7. Installing Juju agent on bootstrap instance
  8. Preparing for Juju GUI 2.1.2 release installation
  9. Waiting for address
  10. Attempting to connect to 10.178.150.72:22
  11. Logging to /var/log/cloud-init-output.log on remote host
  12. Running apt-get update
  13. Running apt-get upgrade
  14. Installing package: curl
  15. Installing package: cpu-checker
  16. Installing package: bridge-utils
  17. Installing package: cloud-utils
  18. Installing package: cloud-image-utils
  19. Installing package: tmux
  20. Fetching tools: curl -sSfw 'tools from %{url_effective} downloaded: HTTP %{http_code}; time %{time_total}s; size %{size_download} bytes; speed %{speed_download} bytes/s ' --retry 10 -o $bin/tools.tar.gz <[https://streams.canonical.com/juju/tools/agent/2.0-beta7/juju-2.0-beta7-xenial-amd64.tgz]>
  21. Bootstrapping Juju machine agent
  22. Starting Juju machine agent (jujud-machine-0)
  23. Bootstrap agent installed
  24. Waiting for API to become available: upgrade in progress (upgrade in progress)
  25. Waiting for API to become available: upgrade in progress (upgrade in progress)
  26. Waiting for API to become available: upgrade in progress (upgrade in progress)
  27. Bootstrap complete, local.test now available.

这会花费一点时间,这时你可以看到一个正在运行的一个新的 LXD 容器:

 
  1. stgraber@dakara:~$ lxc list juju-
  2. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+
  5. | juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |
  6. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+

在 Juju 这边,你可以确认它是有响应的,并且还没有服务运行:

 
  1. stgraber@dakara:~$ juju status
  2. [Services]
  3. NAME STATUS EXPOSED CHARM
  4. [Units]
  5. ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGE
  6. [Machines]
  7. ID STATE DNS INS-ID SERIES AZ

你也可以在浏览器中访问 Juju 的 GUI 界面:

 
  1. stgraber@dakara:~$ juju gui
  2. Opening the Juju GUI in your browser.
  3. If it does not open, open this URL:
  4. https://10.178.150.72:17070/gui/97fa390d-96ad-44df-8b59-e15fdcfc636b/

Juju web UI

Juju web UI

不过我更倾向使用命令行,因此我会在接下来使用。

部署一个 minecraft 服务

让我们先来一个简单的,部署在一个容器中使用一个 Juju 单元的服务。

 
  1. stgraber@dakara:~$ juju deploy cs:trusty/minecraft
  2. Added charm "cs:trusty/minecraft-3" to the model.
  3. Deploying charm "cs:trusty/minecraft-3" with the charm series "trusty".

命令返回会很快,然而这不意味着服务已经启动并运行了。你应该使用 juju status 来查看:

 
  1. stgraber@dakara:~$ juju status
  2. [Services]
  3. NAME STATUS EXPOSED CHARM
  4. minecraft maintenance false cs:trusty/minecraft-3
  5. [Units]
  6. ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGE
  7. minecraft/1 maintenance executing 2.0-beta7 1 10.178.150.74 (install) Installing java
  8. [Machines]
  9. ID STATE DNS INS-ID SERIES AZ
  10. 1 started 10.178.150.74 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 trusty

我们可以看到它正在忙于在刚刚创建的 LXD 容器中安装 java。

 
  1. stgraber@dakara:~$ lxc list juju-
  2. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+
  5. | juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |
  6. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+
  7. | juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 | RUNNING | 10.178.150.74 (eth0) | | PERSISTENT | 0 |
  8. +-----------------------------------------------------+---------+----------------------+------+------------+-----------+

过一会之后,如我们所见服务就部署完毕了:

 
  1. stgraber@dakara:~$ juju status
  2. [Services]
  3. NAME STATUS EXPOSED CHARM
  4. minecraft active false cs:trusty/minecraft-3
  5. [Units]
  6. ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGE
  7. minecraft/1 active idle 2.0-beta7 1 25565/tcp 10.178.150.74 Ready
  8. [Machines]
  9. ID STATE DNS INS-ID SERIES AZ
  10. 1 started 10.178.150.74 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 trusty

这时你就可以启动你的 Minecraft 客户端了,将其指向 10.178.150.74,端口是 25565。现在可以在新的 minecraft 服务器上玩了!

当你不再需要它,只需运行:

 
  1. stgraber@dakara:~$ juju destroy-service minecraft

只要等待几秒就好了。

部署一个更复杂的 web 应用

Juju 的主要工作是建模复杂的服务,并以可扩展的方式部署它们。

为了更好地展示,让我们部署一个 Juju “组合”。 这个组合是由网站,API,数据库,静态 Web 服务器和反向代理组成的基本 Web 服务。

所以这将扩展到 4 个互联的 LXD 容器。

 
  1. stgraber@dakara:~$ juju deploy cs:~charmers/bundle/web-infrastructure-in-a-box
  2. added charm cs:~hp-discover/trusty/node-app-1
  3. service api deployed (charm cs:~hp-discover/trusty/node-app-1 with the series "trusty" defined by the bundle)
  4. annotations set for service api
  5. added charm cs:trusty/mongodb-3
  6. service mongodb deployed (charm cs:trusty/mongodb-3 with the series "trusty" defined by the bundle)
  7. annotations set for service mongodb
  8. added charm cs:~hp-discover/trusty/nginx-4
  9. service nginx deployed (charm cs:~hp-discover/trusty/nginx-4 with the series "trusty" defined by the bundle)
  10. annotations set for service nginx
  11. added charm cs:~hp-discover/trusty/nginx-proxy-3
  12. service nginx-proxy deployed (charm cs:~hp-discover/trusty/nginx-proxy-3 with the series "trusty" defined by the bundle)
  13. annotations set for service nginx-proxy
  14. added charm cs:~hp-discover/trusty/website-3
  15. service website deployed (charm cs:~hp-discover/trusty/website-3 with the series "trusty" defined by the bundle)
  16. annotations set for service website
  17. related mongodb:database and api:mongodb
  18. related website:nginx-engine and nginx:web-engine
  19. related api:website and nginx-proxy:website
  20. related nginx-proxy:website and website:website
  21. added api/0 unit to new machine
  22. added mongodb/0 unit to new machine
  23. added nginx/0 unit to new machine
  24. added nginx-proxy/0 unit to new machine
  25. deployment of bundle "cs:~charmers/bundle/web-infrastructure-in-a-box-10" completed

几秒后,你会看到 LXD 容器在运行了:

 
  1. stgraber@dakara:~$ lxc list juju-
  2. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  5. | juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |
  6. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  7. | juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-2 | RUNNING | 10.178.150.98 (eth0) | | PERSISTENT | 0 |
  8. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  9. | juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-3 | RUNNING | 10.178.150.29 (eth0) | | PERSISTENT | 0 |
  10. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  11. | juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-4 | RUNNING | 10.178.150.202 (eth0) | | PERSISTENT | 0 |
  12. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
  13. | juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-5 | RUNNING | 10.178.150.214 (eth0) | | PERSISTENT | 0 |
  14. +-----------------------------------------------------+---------+-----------------------+------+------------+-----------+

几分钟后,所有的服务应该部署完毕并运行了:

 
  1. stgraber@dakara:~$ juju status
  2. [Services]
  3. NAME STATUS EXPOSED CHARM
  4. api unknown false cs:~hp-discover/trusty/node-app-1
  5. mongodb unknown false cs:trusty/mongodb-3
  6. nginx unknown false cs:~hp-discover/trusty/nginx-4
  7. nginx-proxy unknown false cs:~hp-discover/trusty/nginx-proxy-3
  8. website false cs:~hp-discover/trusty/website-3
  9. [Relations]
  10. SERVICE1 SERVICE2 RELATION TYPE
  11. api mongodb database regular
  12. api nginx-proxy website regular
  13. mongodb mongodb replica-set peer
  14. nginx website nginx-engine subordinate
  15. nginx-proxy website website regular
  16. [Units]
  17. ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGE
  18. api/0 unknown idle 2.0-beta7 2 8000/tcp 10.178.150.98
  19. mongodb/0 unknown idle 2.0-beta7 3 27017/tcp,27019/tcp,27021/tcp,28017/tcp 10.178.150.29
  20. nginx-proxy/0 unknown idle 2.0-beta7 5 80/tcp 10.178.150.214
  21. nginx/0 unknown idle 2.0-beta7 4 10.178.150.202
  22. website/0 unknown idle 2.0-beta7 10.178.150.202
  23. [Machines]
  24. ID STATE DNS INS-ID SERIES AZ
  25. 2 started 10.178.150.98 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-2 trusty
  26. 3 started 10.178.150.29 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-3 trusty
  27. 4 started 10.178.150.202 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-4 trusty
  28. 5 started 10.178.150.214 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-5 trusty

这时你就可以在 80 端口访问 http://10.178.150.214,并且会看到一个 Juju 学院页面。

Juju Academy web service

Juju Academy web service

清理所有东西

如果你不需要 Juju 创建的容器并且不在乎下次需要再次启动,最简单的方法是:

 
  1. stgraber@dakara:~$ juju destroy-controller test --destroy-all-models
  2. WARNING! This command will destroy the "local.test" controller.
  3. This includes all machines, services, data and other resources.
  4. Continue [y/N]? y
  5. Destroying controller
  6. Waiting for hosted model resources to be reclaimed
  7. Waiting on 1 model, 4 machines, 5 services
  8. Waiting on 1 model, 4 machines, 5 services
  9. Waiting on 1 model, 4 machines, 5 services
  10. Waiting on 1 model, 4 machines, 5 services
  11. Waiting on 1 model, 4 machines, 5 services
  12. Waiting on 1 model, 4 machines, 5 services
  13. Waiting on 1 model, 4 machines
  14. Waiting on 1 model, 4 machines
  15. Waiting on 1 model, 4 machines
  16. Waiting on 1 model, 4 machines
  17. Waiting on 1 model, 4 machines
  18. Waiting on 1 model, 4 machines
  19. Waiting on 1 model, 2 machines
  20. Waiting on 1 model
  21. Waiting on 1 model
  22. All hosted models reclaimed, cleaning up controller machines

我们用下面的方式确认:

 
  1. stgraber@dakara:~$ lxc list juju-
  2. +------+-------+------+------+------+-----------+
  3. | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
  4. +------+-------+------+------+------+-----------+

总结

Juju 2.0 内置的 LXD 支持使得可以用一种非常干净的方式来测试各种服务。

在 Juju charm store 中有很多预制的“组合”可以用来部署,甚至可以用多个“charm”来组合你想要的架构。

Juju 与 LXD 是一个完美的解决方案,从一个小的 Web 服务到大规模的基础设施都可以简单开发,这些都在你自己的机器上,并且不会在你的系统上造成混乱!

原文发布时间为:2017-03-07

本文来自云栖社区合作伙伴“Linux中国”

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
9月前
|
NoSQL 关系型数据库 MySQL
dokcer的介绍
dokcer的介绍
28 0
|
存储 Ubuntu Linux
Dokcer 安装
Dokcer 安装
115 0
|
Devops Unix Linux
在tinycorelinux上安装lxc,lxd (1)
本文关键字,在tinycorelinux上安装lxc,lxd,gcc4.4 self-reference struct typedef
248 0
在tinycorelinux上安装lxc,lxd (1)
|
存储 缓存 Ubuntu
LXD 2.0系列之二:LXD安装和配置
本文讲的是LXD 2.0系列之二:LXD安装和配置,【编者的话】第三方调查报告显示LXD有潜质成为一款受欢迎的第三方容器管理工具。本文是LXD核心维护者、加拿大程序员Stéphane Graber有关LXD连载博文的第二篇。
2472 0
|
Linux API UED
LXD 2.0系列之一:LXD简介
本文讲的是LXD 2.0系列之一:LXD简介,【编者的话】第三方调查报告显示LXD有潜质成为一款受欢迎的第三方容器管理工具。本文是LXD核心维护者、加拿大程序员Stéphane Graber有关LXD连载博文的第一篇。
1833 0
|
网络协议 应用服务中间件 API