By Hitesh Jethva, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
LXC also known as a Linux Containers is a free, open source, lightweight and operating system-level virtualization software that allows us to run a multiple isolated Linux systems on a single Linux host. LXC provides the cgroups functionality that allows the limitation of resources like, CPU, memory, network without the need for starting any virtual machines. You can start Linux container without using the separate kernel using LXC. LXC allows you to run multiple instances of an operating system or application on a single host, without inducing overhead on CPU and memory. Containers can be used to quickly set up a virtual environment for testing/QA purposes.
Features:
In this tutorial, we will explain how to set up LXC container on Alibaba Cloud Elastic Compute Service (ECS) with Ubuntu 16.04.
First, log in to your Alibaba Cloud ECS Console. Create a new ECS instance, choosing Ubuntu 16.04 as the operating system with at least 2GB RAM. Connect to your ECS instance and log in as the root user.
Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.
apt-get update -y
By default, LXC is available in the Ubuntu 16.04 default repository. You can install it by running the following command:
apt-get install lxc lxc-templates -y
After installing LXC, you can check the LXC using the following command:
lxc-checkconfig
If everything fine, you should see the following output:
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-4.2.0-27-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled
--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled
--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled
FUSE (for use with lxcfs): enabled
--- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
LXC comes with lots of ready-made templates for creating Linux containers. You can list out them by running the following command:
ls /usr/share/lxc/templates/
Output:
lxc-alpine lxc-archlinux lxc-centos lxc-debian lxc-fedora lxc-openmandriva lxc-oracle lxc-slackware lxc-sshd lxc-ubuntu-cloud
lxc-altlinux lxc-busybox lxc-cirros lxc-download lxc-gentoo lxc-opensuse lxc-plamo lxc-sparclinux lxc-ubuntu
Now, create your first Ubuntu container with the following command:
lxc-create -n new-container -t ubuntu
Once the container created succesfully, you should see the following output:
Download complete
Copy /var/cache/lxc/xenial/rootfs-amd64 to /var/lib/lxc/new-container/rootfs ...
Copying rootfs to /var/lib/lxc/new-container/rootfs ...
Generating locales (this might take a while)...
en_IN.UTF-8... done
Generation complete.
Creating SSH2 RSA key; this may take some time ...
2048 SHA256:kiRG9aMIT87ZRJ/+RJoKh9dO7ykS0MNQmNKlbgDveiM root@mail.distcourt.local (RSA)
Creating SSH2 DSA key; this may take some time ...
1024 SHA256:Y0Ajo4RuUP3ty+BJOmHrQPscF+9oxDSfgLTALBE3/Yg root@mail.distcourt.local (DSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:TsHzuCEgoRAMobpdUJCVCyRQo2YgKpGEZLZp4OF1iBw root@mail.distcourt.local (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:wehDNjEy/AxVknDWj7BFTtvGKUnVXKLcmTTbm63OyM0 root@mail.distcourt.local (ED25519)
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Current default time zone: 'Etc/UTC'
Local time is now: Mon Jul 30 07:40:47 UTC 2018.
Universal Time is now: Mon Jul 30 07:40:47 UTC 2018.
##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##
You should see that the Ubuntu container created with the default user ubuntu and password ubuntu.
You can now list the created container with the following command:
lxc-ls
Output:
new-container
Ubuntu container is now created. You can now start the container with the following command:
lxc-start -n new-container -d
Next, take a console of the container by running the following command:
lxc-console -n new-container
You will be asked to provide username and password to log in. Provide the username ubuntu and password ubuntu to log in. You should see the following output after login:
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
Ubuntu 16.04.5 LTS new-container pts/0
new-container login: ubuntu
Password:
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-45-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@new-container:~$
You can exit from the container any time by pressing "Ctrl+a" followed by "q"
You can get the details of the running container using the following command:
lxc-info -n new-container
Output:
Name: new-container
State: RUNNING
PID: 26342
IP: 10.0.3.66
CPU use: 0.20 seconds
BlkIO use: 8.00 KiB
Memory use: 13.44 MiB
KMem use: 0 bytes
Link: veth9DB525
TX bytes: 1.34 KiB
RX bytes: 2.09 KiB
Total bytes: 3.43 KiB
You can start and stop the container with the following command:
lxc-start -n new-container
lxc-stop -n new-container
You can Pause and Unpause the container with the following command:
lxc-freeze -n new-container
lxc-unfreeze -n new-container
You can also take a snapshot of the container. First, stop the running container with the following command:
lxc-stop -n new-container
Next, take a snapshot of the container with the following command:
lxc-snapshot -n new-container
Output:
lxc-snapshot: lxccontainer.c: do_lxcapi_snapshot: 3405 Snapshot of directory-backed container requested.
lxc-snapshot: lxccontainer.c: do_lxcapi_snapshot: 3406 Making a copy-clone. If you do want snapshots, then
lxc-snapshot: lxccontainer.c: do_lxcapi_snapshot: 3407 please create an aufs or overlayfs clone first, snapshot that
lxc-snapshot: lxccontainer.c: do_lxcapi_snapshot: 3408 and keep the original container pristine.
You can see the location of the snapshot using the following command:
lxc-snapshot -L -n new-container
Output:
snap0 (/var/lib/lxc/new-container/snaps) 2018:07:30 13:43:16
You can restore the snapshot using the following command:
lxc-snapshot -r snap0 -n new-container
If you want to delete the container, run the following command:
lxc-stop -n new-container
lxc-destroy -n new-container
LXC Web Panel is a GUI management tool to manage Linux containers. You can create, start, stop, clone, delete and restart Linux container from the web browser using LXC web panel.
You can install the LXC web panel to your system by running the following command:
wget https://lxc-webpanel.github.io/tools/install.sh -O - | bash
This command will download and install LXC web panel automatically to your system:
Installing requirement...
+ Installing Python pip
Extracting templates from packages: 100%
| + Flask Python...
Collecting flask==0.9
Downloading https://files.pythonhosted.org/packages/49/0a/fe5021b35436202d3d4225a766f3bdc7fb51521ad89e73c5162db36cdbc7/Flask-0.9.tar.gz (481kB)
100% |################################| 491kB 598kB/s
Collecting Werkzeug>=0.7 (from flask==0.9)
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
100% |################################| 327kB 517kB/s
Collecting Jinja2>=2.4 (from flask==0.9)
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
100% |################################| 133kB 544kB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask==0.9)
Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Building wheels for collected packages: flask, MarkupSafe
Running setup.py bdist_wheel for flask ... done
Stored in directory: /root/.cache/pip/wheels/69/1b/77/d1aa13cdcc09430d1ba010a48e2cb59fda00f4ef18620ca8dc
Running setup.py bdist_wheel for MarkupSafe ... done
Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built flask MarkupSafe
Installing collected packages: Werkzeug, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 flask-0.9
+ Installing Git
Cloning LXC Web Panel...
Cloning into '/srv/lwp'...
remote: Counting objects: 243, done.
remote: Total 243 (delta 0), reused 0 (delta 0), pack-reused 243
Receiving objects: 100% (243/243), 198.33 KiB | 141.00 KiB/s, done.
Resolving deltas: 100% (108/108), done.
Checking connectivity... done.
Installation complete!
Adding /etc/init.d/lwp...
Done
Starting server...done.
Connect you on http://your-ip-address:5000/
Once the installation is completed, open your web browser and type the URL http://your-server-ip:5000. You will be redirected to the LXC login page as shown below:
Now, enter the default username admin and password admin, then click on the Log In button. You should see the LXC web panel in the following page:
You can update the LXC web panel using the following command:
wget https://lxc-webpanel.github.io/tools/update.sh -O - | bash
Congratulations! You have successfully installed LXC and LXC web panel to your system. You can now easily create and manage Linux container from the central location.
Alibaba Cloud IoT Platform allows IoT companies stable communication between devices and the IoT Platform. Featuring nodes deployed globally, devices around the world can communicate with the IoT platform rapidly and with low latency. Alibaba Cloud IoT Platform also provides various security measures that guarantee individual device security as well as secure communication between devices and the IoT platform.
Apsara Stack enables enterprises to improve utilization of their existing on-premises data center as well as install innovative services in security, and meet requirements in data sovereignty and compliance. Apsara Stack supports hyperscale cluster systems containing more than 6,000 servers per region to meet the requirements of operating large business systems and applications visited by a high number of users. Users can efficiently operate and manage those resources, applications, and data on the Apsara Stack platform through a unified management portal, which supports multi-tenancy mode.
SUSE and Alibaba Cloud Partner to Meet Global Demand for Cloud-Based Business-Critical Applications
How to Setup MariaDB Master and Slave Replication on Ubuntu 16.04
2,599 posts | 762 followers
FollowAlibaba Clouder - August 23, 2018
Alibaba Clouder - August 22, 2019
Alibaba Clouder - July 9, 2018
Alibaba Clouder - June 13, 2018
Alibaba Clouder - May 14, 2018
Alibaba Clouder - October 18, 2018
2,599 posts | 762 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAn encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world
Learn MoreLearn More
More Posts by Alibaba Clouder