LXD 2.0 係列(十):LXD 和 Juju
介紹
Juju 是 Canonical 的服務建模和部署工具。 它支持非常廣泛的雲服務提供商,使您能夠輕鬆地在任何雲上部署任何您想要的服務。
此外,Juju 2.0 還支持 LXD,既適用於本地部署,也適合開發,並且可以在雲實例或物理機上共同協作。
本篇文章將關注本地使用,通過一個沒有任何Juju經驗的LXD用戶來體驗。
要求
本篇文章假設你已經安裝了 LXD 2.0 並且配置完畢(看前麵的文章),並且是在 Ubuntu 16.04 LTS 上運行的。
設置 Juju
第一件事是在 Ubuntu 16.04 上安裝 Juju 2.0。這個很簡單:
stgraber@dakara:~$ sudo apt install jujuReading package lists... DoneBuilding dependency treeReading state information... DoneThe following additional packages will be installed:juju-2.0Suggested packages:juju-coreThe following NEW packages will be installed:juju juju-2.00 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.Need to get 39.7 MB of archives.After this operation, 269 MB of additional disk space will be used.Do you want to continue? [Y/n]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]Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 juju all 2.0~beta7-0ubuntu1.16.04.1 [9,556 B]Fetched 39.7 MB in 0s (53.4 MB/s)Selecting previously unselected package juju-2.0.(Reading database ... 255132 files and directories currently installed.)Preparing to unpack .../juju-2.0_2.0~beta7-0ubuntu1.16.04.1_amd64.deb ...Unpacking juju-2.0 (2.0~beta7-0ubuntu1.16.04.1) ...Selecting previously unselected package juju.Preparing to unpack .../juju_2.0~beta7-0ubuntu1.16.04.1_all.deb ...Unpacking juju (2.0~beta7-0ubuntu1.16.04.1) ...Processing triggers for man-db (2.7.5-1) ...Setting up juju-2.0 (2.0~beta7-0ubuntu1.16.04.1) ...Setting up juju (2.0~beta7-0ubuntu1.16.04.1) ...
安裝完成後,我們可以使用 LXD 啟動一個新的“控製器”。這意味著 Juju 不會修改你主機上的任何東西,它會在 LXD 容器中安裝它的管理服務。
現在我們創建一個“test”控製器:
stgraber@dakara:~$ juju bootstrap localhost testCreating Juju controller "local.test" on localhost/localhostBootstrapping model "admin"Starting new instance for initial controllerLaunching instance- juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0Installing Juju agent on bootstrap instancePreparing for Juju GUI 2.1.2 release installationWaiting for addressAttempting to connect to 10.178.150.72:22Logging to /var/log/cloud-init-output.log on remote hostRunning apt-get updateRunning apt-get upgradeInstalling package: curlInstalling package: cpu-checkerInstalling package: bridge-utilsInstalling package: cloud-utilsInstalling package: cloud-image-utilsInstalling package: tmuxFetching 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]>Bootstrapping Juju machine agentStarting Juju machine agent (jujud-machine-0)Bootstrap agent installedWaiting for API to become available: upgrade in progress (upgrade in progress)Waiting for API to become available: upgrade in progress (upgrade in progress)Waiting for API to become available: upgrade in progress (upgrade in progress)Bootstrap complete, local.test now available.
這會花費一點時間,這時你可以看到一個正在運行的一個新的 LXD 容器:
stgraber@dakara:~$ lxc list juju-+-----------------------------------------------------+---------+----------------------+------+------------+-----------+| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |+-----------------------------------------------------+---------+----------------------+------+------------+-----------+| juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+----------------------+------+------------+-----------+
在 Juju 這邊,你可以確認它是有響應的,並且還沒有服務運行:
stgraber@dakara:~$ juju status[Services]NAME STATUS EXPOSED CHARM[Units]ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGE[Machines]ID STATE DNS INS-ID SERIES AZ
你也可以在瀏覽器中訪問 Juju 的 GUI 界麵:
stgraber@dakara:~$ juju guiOpening the Juju GUI in your browser.If it does not open, open this URL:https://10.178.150.72:17070/gui/97fa390d-96ad-44df-8b59-e15fdcfc636b/

Juju web UI
不過我更傾向使用命令行,因此我會在接下來使用。
部署一個 minecraft 服務
讓我們先來一個簡單的,部署在一個容器中使用一個 Juju 單元的服務。
stgraber@dakara:~$ juju deploy cs:trusty/minecraftAdded charm "cs:trusty/minecraft-3" to the model.Deploying charm "cs:trusty/minecraft-3" with the charm series "trusty".
命令返回會很快,然而這不意味著服務已經啟動並運行了。你應該使用 juju status 來查看:
stgraber@dakara:~$ juju status[Services]NAME STATUS EXPOSED CHARMminecraft maintenance false cs:trusty/minecraft-3[Units]ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGEminecraft/1 maintenance executing 2.0-beta7 1 10.178.150.74 (install) Installing java[Machines]ID STATE DNS INS-ID SERIES AZ1 started 10.178.150.74 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 trusty
我們可以看到它正在忙於在剛剛創建的 LXD 容器中安裝 java。
stgraber@dakara:~$ lxc list juju-+-----------------------------------------------------+---------+----------------------+------+------------+-----------+| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |+-----------------------------------------------------+---------+----------------------+------+------------+-----------+| juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+----------------------+------+------------+-----------+| juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 | RUNNING | 10.178.150.74 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+----------------------+------+------------+-----------+
過一會之後,如我們所見服務就部署完畢了:
stgraber@dakara:~$ juju status[Services]NAME STATUS EXPOSED CHARMminecraft active false cs:trusty/minecraft-3[Units]ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGEminecraft/1 active idle 2.0-beta7 1 25565/tcp 10.178.150.74 Ready[Machines]ID STATE DNS INS-ID SERIES AZ1 started 10.178.150.74 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-1 trusty
這時你就可以啟動你的 Minecraft 客戶端了,將其指向 10.178.150.74,端口是 25565。現在可以在新的 minecraft 服務器上玩了!
當你不再需要它,隻需運行:
stgraber@dakara:~$ juju destroy-service minecraft
隻要等待幾秒就好了。
部署一個更複雜的 web 應用
Juju 的主要工作是建模複雜的服務,並以可擴展的方式部署它們。
為了更好地展示,讓我們部署一個 Juju “組合”。 這個組合是由網站,API,數據庫,靜態 Web 服務器和反向代理組成的基本 Web 服務。
所以這將擴展到 4 個互聯的 LXD 容器。
stgraber@dakara:~$ juju deploy cs:~charmers/bundle/web-infrastructure-in-a-boxadded charm cs:~hp-discover/trusty/node-app-1service api deployed (charm cs:~hp-discover/trusty/node-app-1 with the series "trusty" defined by the bundle)annotations set for service apiadded charm cs:trusty/mongodb-3service mongodb deployed (charm cs:trusty/mongodb-3 with the series "trusty" defined by the bundle)annotations set for service mongodbadded charm cs:~hp-discover/trusty/nginx-4service nginx deployed (charm cs:~hp-discover/trusty/nginx-4 with the series "trusty" defined by the bundle)annotations set for service nginxadded charm cs:~hp-discover/trusty/nginx-proxy-3service nginx-proxy deployed (charm cs:~hp-discover/trusty/nginx-proxy-3 with the series "trusty" defined by the bundle)annotations set for service nginx-proxyadded charm cs:~hp-discover/trusty/website-3service website deployed (charm cs:~hp-discover/trusty/website-3 with the series "trusty" defined by the bundle)annotations set for service websiterelated mongodb:database and api:mongodbrelated website:nginx-engine and nginx:web-enginerelated api:website and nginx-proxy:websiterelated nginx-proxy:website and website:websiteadded api/0 unit to new machineadded mongodb/0 unit to new machineadded nginx/0 unit to new machineadded nginx-proxy/0 unit to new machinedeployment of bundle "cs:~charmers/bundle/web-infrastructure-in-a-box-10" completed
幾秒後,你會看到 LXD 容器在運行了:
stgraber@dakara:~$ lxc list juju-+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| juju-745d1be3-e93d-41a2-80d4-fbe8714230dd-machine-0 | RUNNING | 10.178.150.72 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-2 | RUNNING | 10.178.150.98 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-3 | RUNNING | 10.178.150.29 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-4 | RUNNING | 10.178.150.202 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+| juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-5 | RUNNING | 10.178.150.214 (eth0) | | PERSISTENT | 0 |+-----------------------------------------------------+---------+-----------------------+------+------------+-----------+
幾分鍾後,所有的服務應該部署完畢並運行了:
stgraber@dakara:~$ juju status[Services]NAME STATUS EXPOSED CHARMapi unknown false cs:~hp-discover/trusty/node-app-1mongodb unknown false cs:trusty/mongodb-3nginx unknown false cs:~hp-discover/trusty/nginx-4nginx-proxy unknown false cs:~hp-discover/trusty/nginx-proxy-3website false cs:~hp-discover/trusty/website-3[Relations]SERVICE1 SERVICE2 RELATION TYPEapi mongodb database regularapi nginx-proxy website regularmongodb mongodb replica-set peernginx website nginx-engine subordinatenginx-proxy website website regular[Units]ID WORKLOAD-STATUS JUJU-STATUS VERSION MACHINE PORTS PUBLIC-ADDRESS MESSAGEapi/0 unknown idle 2.0-beta7 2 8000/tcp 10.178.150.98mongodb/0 unknown idle 2.0-beta7 3 27017/tcp,27019/tcp,27021/tcp,28017/tcp 10.178.150.29nginx-proxy/0 unknown idle 2.0-beta7 5 80/tcp 10.178.150.214nginx/0 unknown idle 2.0-beta7 4 10.178.150.202website/0 unknown idle 2.0-beta7 10.178.150.202[Machines]ID STATE DNS INS-ID SERIES AZ2 started 10.178.150.98 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-2 trusty3 started 10.178.150.29 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-3 trusty4 started 10.178.150.202 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-4 trusty5 started 10.178.150.214 juju-97fa390d-96ad-44df-8b59-e15fdcfc636b-machine-5 trusty
這時你就可以在 80 端口訪問 https://10.178.150.214,並且會看到一個 Juju 學院頁麵。
Juju Academy web service
清理所有東西
如果你不需要 Juju 創建的容器並且不在乎下次需要再次啟動,最簡單的方法是:
stgraber@dakara:~$ juju destroy-controller test --destroy-all-modelsWARNING! This command will destroy the "local.test" controller.This includes all machines, services, data and other resources.Continue [y/N]? yDestroying controllerWaiting for hosted model resources to be reclaimedWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machines, 5 servicesWaiting on 1 model, 4 machinesWaiting on 1 model, 4 machinesWaiting on 1 model, 4 machinesWaiting on 1 model, 4 machinesWaiting on 1 model, 4 machinesWaiting on 1 model, 4 machinesWaiting on 1 model, 2 machinesWaiting on 1 modelWaiting on 1 modelAll hosted models reclaimed, cleaning up controller machines
我們用下麵的方式確認:
stgraber@dakara:~$ lxc list juju-+------+-------+------+------+------+-----------+| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |+------+-------+------+------+------+-----------+
總結
Juju 2.0 內置的 LXD 支持使得可以用一種非常幹淨的方式來測試各種服務。
在 Juju charm store 中有很多預製的“組合”可以用來部署,甚至可以用多個“charm”來組合你想要的架構。
Juju 與 LXD 是一個完美的解決方案,從一個小的 Web 服務到大規模的基礎設施都可以簡單開發,這些都在你自己的機器上,並且不會在你的係統上造成混亂!
原文發布時間為:2017-03-07
本文來自雲棲社區合作夥伴“Linux中國”
最後更新:2017-05-24 16:32:14
