[Juju] Use juju to deploy minecraft server in LXD | Wenhan Code life

[Juju] Use juju to deploy minecraft server in LXD

Juju is a deploy tool which supports a very wide range of cloud providers,
like AWS, Azure, Google Cloud Platform, MAAS and LXD. This artcle will focus on
how to build an OpenStack test environment using Juju and LXD.

installing LXD

It is very easy to install LXD, just run below command

1
$ sudo apt-install lxd

If you can’t find lxd package, run below command to add PPA(Personal Package
Archive) to find LXD package. Then re-run the install command.

1
2
3
$ sudo apt-add-repository ppa:ubuntu-lxc/stable
$ sudo apt update
$ sudo apt dist-upgrade

Configuring LXD

Run below command to config lxd setting step by step

1
2
3
4
5
6
7
8
$ sudo lxd init
Do you want to configure a new storage pool (yes/no) [default=yes]?
Name of the storage backend to use (dir or zfs) [default=dir]:
Would you like LXD to be available over the network (yes/no) [default=no]?
Do you want to configure the LXD bridge (yes/no) [default=yes]?
Warning: Stopping lxd.service, but it can still be activated by:
lxd.socket
LXD has been successfully configured.

Installing juju

Run below command to install juju

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ sudo apt install juju
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
distro-info juju-2.0
Suggested packages:
shunit2 juju-core
The following NEW packages will be installed:
distro-info juju juju-2.0
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 32.0 MB of archives.
After this operation, 168 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 distro-info amd64 0.14build1 [20.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 juju-2.0 amd64 2.3.7-0ubuntu0.16.04.1 [32.0 MB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 juju all 2.3.7-0ubuntu0.16.04.1 [10.2 kB]
Fetched 32.0 MB in 4s (6,778 kB/s)
Preconfiguring packages ...
Selecting previously unselected package distro-info.
(Reading database ... 60478 files and directories currently installed.)
Preparing to unpack .../distro-info_0.14build1_amd64.deb ...
Unpacking distro-info (0.14build1) ...
Selecting previously unselected package juju-2.0.
Preparing to unpack .../juju-2.0_2.3.7-0ubuntu0.16.04.1_amd64.deb ...
Unpacking juju-2.0 (2.3.7-0ubuntu0.16.04.1) ...
Selecting previously unselected package juju.
Preparing to unpack .../juju_2.3.7-0ubuntu0.16.04.1_all.deb ...
Unpacking juju (2.3.7-0ubuntu0.16.04.1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up distro-info (0.14build1) ...
Setting up juju-2.0 (2.3.7-0ubuntu0.16.04.1) ...
Setting up juju (2.3.7-0ubuntu0.16.04.1) ...

Once the install is done, we can bootstrap a new controller using LXD. This
means that juju will create a new LXD container for management service.

We can create a controller called juju-controller with below command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ juju bootstrap localhost juju-controller
Since Juju 2 is being run for the first time, downloading latest cloud information.
Fetching latest public cloud list...
Your list of public clouds is up to date, see `juju clouds`.
Creating Juju controller "juju-controller" on localhost/localhost
Looking for packaged Juju agent version 2.3.7 for amd64
To configure your system to better support LXD containers, please see: https://github.com/lxc/lxd/blob/master/doc/production-setup.md
Launching controller instance(s) on localhost/localhost...
- juju-9ccefc-0 (arch=amd64) s)
Installing Juju agent on bootstrap instance
Fetching Juju GUI 2.12.3
Waiting for address
Attempting to connect to 10.229.139.129:22
Connected to 10.229.139.129
Running machine configuration script...
Bootstrap agent now started
Contacting Juju controller at 10.229.139.129 to verify accessibility...
Bootstrap complete, "juju-controller" controller now available
Controller machines are in the "controller" model
Initial model "default" added

After this, we can see a new LXD container is running.

1
2
3
4
5
6
$ lxc list
+---------------+---------+-----------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+---------------+---------+-----------------------+------+------------+-----------+
| juju-9ccefc-0 | RUNNING | 10.229.139.129 (eth0) | | PERSISTENT | 0 |
+---------------+---------+-----------------------+------+------------+-----------+

And run juju status to check there is nothing running yet.

1
2
3
4
5
6
7
8
9
$ juju status
Model Controller Cloud/Region Version SLA
default juju-controller localhost/localhost 2.3.7 unsupported

App Version Status Scale Charm Store Rev OS Notes

Unit Workload Agent Machine Public address Ports Message

Machine State DNS Inst id Series AZ Message

Deploy minecraft server

OK, now we are ready for deploy minecraft server!
Run below command to tell juju you want to deploy it. and it should return
immediately. However it doesn’t mean the service is ready. Run juju status to
check the status.

1
2
3
4
5
6
7
8
9
10
11
12
$ juju status
Model Controller Cloud/Region Version SLA
default juju-controller localhost/localhost 2.3.7 unsupported

App Version Status Scale Charm Store Rev OS Notes
minecraft waiting 0/1 minecraft jujucharms 3 ubuntu

Unit Workload Agent Machine Public address Ports Message
minecraft/0 waiting allocating 0 10.229.139.124 waiting for machine

Machine State DNS Inst id Series AZ Message
0 pending 10.229.139.124 juju-72dd35-0 trusty Running

From above we can see juju is working on creating the server. And we also can
check a new container is created for minecraft server, by lxc list command.

1
2
3
4
5
6
7
8
$ lxc list
+---------------+---------+-----------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+---------------+---------+-----------------------+------+------------+-----------+
| juju-72dd35-0 | RUNNING | 10.229.139.124 (eth0) | | PERSISTENT | 0 |
+---------------+---------+-----------------------+------+------------+-----------+
| juju-9ccefc-0 | RUNNING | 10.229.139.129 (eth0) | | PERSISTENT | 0 |
+---------------+---------+-----------------------+------+------------+-----------+

After a while, the deploying is done and the service is active.

1
2
3
4
5
6
7
8
9
10
11
12
$ juju status
Model Controller Cloud/Region Version SLA
default juju-controller localhost/localhost 2.3.7 unsupported

App Version Status Scale Charm Store Rev OS Notes
minecraft active 1 minecraft jujucharms 3 ubuntu

Unit Workload Agent Machine Public address Ports Message
minecraft/0* active idle 0 10.229.139.124 25565/tcp Ready

Machine State DNS Inst id Series AZ Message
0 started 10.229.139.124 juju-72dd35-0 trusty Running

Now you can start your minecraft client, and point to 10.229.139.124 on port
25565 to play with your all new minecraft server!

If you want to get rid of it, just run below command.
Every service or server related to minecraft server will be gone.

1
$ juju destroy-service minecraft

You can also destory juju controller and all service/server created by it.
The easiest way to destory everything is as below.

1
2
3
4
5
6
7
8
9
10
11
$ juju destroy-controller juju-controller --destroy-all-models
WARNING! This command will destroy the "juju-controller" controller.
This includes all machines, applications, data and other resources.

Continue? (y/N):y
Destroying controller
Waiting for hosted model resources to be reclaimed
Waiting on 1 model, 1 machine, 1 application
Waiting on 1 model, 1 machine, 1 application
Waiting on 1 model, 1 machine
All hosted models reclaimed, cleaning up controller machines

We can confirm that all containers are gone.

1
2
3
4
$ lxc list
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+