📜 ⬆️ ⬇️

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

What we will tell 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 fewer resources.

Advantages of LXC over Virtual Machines


We switched to containers when faced with a shortage of server resources, the complexity of differentiating access rights for different projects and software conflicts in one virtual machine. Here are some key LXC advantages over virtual machines:

  1. Less CPU, disk, and RAM loss. Almost everything that is available in the container works at the speed of the server.
  2. No need to allocate memory for the OS kernel, video memory, disk buffers, etc.
  3. Quick start. In fact, only those applications that are necessary for the operation of the container are launched.
  4. Supports launch in the container of individual applications, rather than a complete system. It is convenient that access can be issued by simply adding a user (and not chroot), since we have only one project in the container (there were several on the virtual machines).
  5. Ability to manage the resources of each container.
  6. Quickly and easily move containers between servers.

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/ 

It is necessary to elaborate on the fifth paragraph and tell about the management of resources. We must provide the container with enough resources to operate, and at the same time be sure that the container will not consume extra resources, thereby interfering with the work of the rest of the system.

You can view resource consumption statistics with 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 with the help of special commands:


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

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


1. Server setup

1.1 Install the latest version of Ubuntu 18.04 server on the server. It already has LXD. LXD is a superstructure over 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 the 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 called test. He will download the image, create and run it. This completes the configuration and creation of the container, then proceed to configure the container and transfer the host to it.

2. Configure the container for the host and transfer the site

2.1 lxc exec test / bin / bash # Go to 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 the bitrix.
2.5 apt install nginx # Install nginx.
2.6 vim /etc/netplan/50-cloud-init.yaml # Making 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 We copy the configs php and nginx from our virtual machine, check that everything starts and works.
2.8 Transferring the entire site from our virtual machine. For example:

rsync -alvz oldVirtual: / var / www / / var / www /

3. Creating a separate database container

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 progress when filling the database.

4. Setting up a new database server

To begin, collect information from the old server:

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

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

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

Go to our new database and perform the following actions:

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

5. Verification

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

Conclusion


Thus, LXC containers help us reduce the resource consumption of test servers and fit more hosts on a single server, while maintaining the ability to conveniently manage the resources consumed by each host. Now you have a ready instruction showing how easy it is to implement.

Source: https://habr.com/ru/post/458658/


All Articles