Switching from virtual machines to LXC containers: reasons, advantages, and ready-to-use instructions

    What we will talk about:

    - Why did we decide to switch to LXC containers?
    - How to create a container and run a Bitrix-based host on it?

    For whom it will be useful:

    For anyone who wants to try a new solution, while using less resources.

    LXC Benefits Over Virtual Machines


    We switched to containers when we encountered a lack of server resources, the difficulty of differentiating access rights for different projects, and software conflicts in one virtual machine. Here are some key benefits of LXC over virtual machines:

    1. Less loss of CPU performance, disk operations and RAM. Almost everything that is available in the container works at server speed.
    2. No need to allocate RAM for the OS kernel, video memory, disk buffers, etc.
    3. Quick start. In fact, only those applications are launched that are necessary for the container to work.
    4. It supports launching individual applications in the container, rather than the complete system. It’s convenient that accesses can be granted simply by adding a user (rather than chroot), since we have only one project in the container (there were several on the virtual machines).
    5. The ability to manage the resources of each container.
    6. Move containers between servers quickly and easily.

    We use rsync. Just create a container with the same name on the new server and copy:

    rsync -alvz старыйСервер:/var/lib/lxd/container/test/ /var/lib/lxd/container/test/

    We need to elaborate on the fifth paragraph and talk about resource management. We must provide the container with enough resources to work, and at the same time be sure that the container will not consume unnecessary resources, thereby interfering with the work of the rest of the system.

    You can view the statistics of resource consumption by the command: lxc info test

    Remote: unix://
    Архитектура: x86_64
    Создано: 2018/12/04 14:27 UTC
    Status: Running
    Type: persistent
    Профили: default
    Pid: 28317
    IPs:
    eth0:	inet	172.27.2.204	vethF91F2U
    Resources:
    Процессы: 56
    CPU usage:
    Использование ЦП (в секундах): 20583
    Memory usage:
    Memory (current): 1.03GB
    Memory (peak): 3.11GB
    Network usage:
    eth0:
    Получено байтов: 17.45GB
    Отправлено байтов: 9.93GB
    

    According to these statistics, you can track how many resources a container consumes and, if necessary, limit their consumption using special commands:

    • lxc config set test limits.memory 2048M # set the memory limit.
    • lxc config set test limits.cpu 1,2,3,8 # bind the container to the CPU cores.
    • lxc config set test cpu.allowance 10% # limit CPU consumption.
    • lxc config set test root size 50GB # limit the amount of disk space used by the container (works only with ZFS or btrfs).
    • lxc config get test limits.memory - view the set limit value

    As a result, containers allow you to pick up dozens of containers on a very standard desktop, while maintaining sufficient performance.

    We configure the server and transfer the standard host with Bitrix from the virtual machine to the container


    1. Server setup

    1.1 Install the latest version of Ubuntu 18.04 server on the server. It already has LXD. LXD is an add-on to LXC, a hypervisor that simplifies interaction with the containerization system.
    1.2 apt install bridge-utils # install bridge-utils
    1.3 lxd init # initialize lxd
    1.4 lxc profile edit default # edit profile file:

    devices:
    eth0:
    name: eth0
    nictype: bridged
    parent: br0
    type: nic
    root:
    path: /
    pool: default
    type: disk
    name: default
    

    1. 5 lxc launch ubuntu: 18.04 test # Create a container with the name test. He will download the image, create and launch it. This completes the configuration and creation of the container, then proceed to configure the container and transfer the host to it.

    2. Configuring the container for the host and transferring the site

    2.1 lxc exec test / bin / bash # We go into the created container.
    2.2 add-apt-repository ppa: ondrej / php # Add a repository.
    2.3 apt update # Update the repository.
    2.4 apt install
    php7.1 {fpm, bcmath, bz2, cli, common, curl, dev, enchant, fpm, gd, gmp, imap, intl, json,
    ldap, mbstring, mcrypt, mysql, odbc, opcache, phpdbg, pspell , readline, recode, soap,
    tidy, xml, xmlrpc, xsl, zip} #
    Install php and all the necessary modules for bitrix.
    2.5 apt install nginx # Install nginx.
    2.6 vim /etc/netplan/50-cloud-init.yaml # Make ip static:

    network:
    version: 2
    ethernets:
    eth0:
    addresses: [172.27.2.108/16]
    gateway4: 172.27.0.1
    nameservers:
    addresses: [172.27.1.1]
    dhcp4: false

    2.7 Copy php and nginx configs from our virtual machine, check that everything starts and works.
    2.8 We transfer the whole site from our virtual machine. For example:

    rsync -alvz oldVirtualka: / var / www / / var / www /

    3. Creating a separate container for the database

    3.1 Repeat steps 1 .5, 2.1 and 2.6 , naming the container for example test-db.
    3.2 apt install mysql-server # install the database
    3.3 apt install pv # Install PV to see the progress when we upload the database.

    4. Configuring a new database server

    To begin with, we will collect information from the old server:

    4.1 We need user and pass from the desired database.
    4.2mysql -u test -p'test '-e “show create database testDB;” # Find out how the database was created (what we need is highlighted):

    mysql: [Warning] Using a password on the command line interface can be insecure.
    +----------+--------------------------------------------------------------------------+
    | Database | Create Database |
    +----------+--------------------------------------------------------------------------+
    | demoshop | CREATE DATABASE `demoshop` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ |
    +----------+--------------------------------------------------------------------------+

    4.3 mysqldump -ER --single-transaction --quick testDB | gzip> testDB.sql.gz # Run this command on the server where our database is located. She will dump and compress it into the archive.

    We will go into our new database and perform the following actions:

    4.4 rsync -alvz old DB: / root / testDB.sql.gz / root / # we will transfer the archive with the dump to our container.
    4.5 mysql # Let's go to our database
    4.5.1 CREATE USER 'user' @ '%' IDENTIFIED BY 'pass'; # create the same user and password.
    4.5.2 From paragraph 4.2, we enter the command: CREATE DATABASE `DBName` / *! 40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci * / ;
    4.5.3GRANT ALL PRIVILEGES ON DB name. * TO 'user' @ '%' IDENTIFIED by 'pass'; # We will give rights to our database.
    4.6 We exit the database and run the container console: pv testDB.sql.gz | zcat | mysql testDB # Fill the dump into our database

    5. Check

    5.1 Change the database connection parameters, specify the new IP of our database.
    5.2 After that, the site should open over IP or DNS, it depends on your configuration.

    Conclusion


    Thus, LXC containers help us reduce the consumption of test server resources and fit more hosts on one server, while maintaining the ability to conveniently manage the consumed resources of each host. Now you have ready-made instructions showing how easy it is to implement.

    Also popular now: