Own cloud hosting in 5 minutes. Part 0: Virtualization



    Hi Habr! I have already published three parts from a series of articles ( one , two , three ), and here part 0, like snow on my head. How so? The thing is that virtualization is optional when building our hosting. This article is self-sufficient, it is not related to other parts of the cycle. You generally can not read them if you just want to split your dedicated server into several virtual machines.

    All that I will tell can be done by an ordinary programmer within 5 minutes, simply by running a set of scripts for Ansible, which I prepared specially for you and posted on GitHub .

    Content



    Training


    Download a set of scripts or clone a repository:

    » git clone https://github.com/vkozlovski/ansible-virtualization
    » git checkout v1.x
    » cd ansible-virtualization
    

    On this preparatory work can be considered completed.

    Oh yes, I almost forgot, you will need at least one dedicated server with key authorization.

    Configuration


    We will perform the configuration using the Hetzner example. All configuration files that we will edit are in the host_vars directory :

    • dc16-host1-vm1.yml - virtual machine configuration # 1
    • dc16-host1-vm2.yml - virtual machine configuration # 2
    • ..
    • dc16-host1.yml - host configuration

    In our example, we create 6 virtual machines, exactly so many single IP addresses per server can be issued by Hetzner. Let's see what we have in the configuration files:

    dc16-host1.yml


    Host Configuration:

    # Host 1
    ansible_ssh_host: 5.9.45.106  # IPv4 адрес хостовой машины
    ansible_ssh_user: root  # Пользователь хостовой машины
    # net
    vm_bridge: virbr0
    ipv4: true
    ipv4_address: 5.9.45.106/27  # IPv4 адрес и маска хостовой машины
    ipv4_gateway: 5.9.45.97  # IPv4 шлюз хостовой машины
    ipv4_dns: 213.133.100.100 213.133.98.98 213.133.99.99  # Hetzner IPv4 DNS
    ipv6: true
    ipv6_address: 2a01:4f8:163:326a::2  # IPv6 адрес хостовой машины
    ipv6_mask: 64
    ipv6_gateway: fe80::1  # IPv6 шлюз хостовой машины
    ipv6_dns: 2a01:4f8:0:a0a1::add:1010 2a01:4f8:0:a102::add:9999 2a01:4f8:0:a111::add:9898  # Hetzner IPv6 DNS
    # apt
    apt_host: ftp.de.debian.org
    

    Hetzner sends IPv4 and IPv6 addresses in a letter when ordering a server. You can look at other values ​​of variables in a personal account. I took IPv4 and IPv6 addresses of DNS servers in the Hetzner wiki.

    dc16-host1-vm1.yml


    Virtual machine configuration # 1:

    # Debian 1
    # kvm-host
    ansible_ssh_host: 5.9.45.106  # IP адрес хостовой (не гостевой) машины
    ansible_ssh_user: root  # Пользователь хостовой (не гостевой) машины
    # vnc (port: 5900)
    vnc_password: "kBz4Yp3UyVEPMr"  # Пароль для подключения к VNC серверу
    # vm
    vm_num: 1  # uniq 0-15
    vm_name: debian1  # Уникальное название виртуальной машины
    vm_hdd_size: 10G  # Диск 10 гигабайт
    vm_memory: 2048  # Память в мегабайтах
    vm_swap_size: 2048  # Размер файла подкачки в мегабайтах
    vm_cpu: 2  # Количество ядер
    vm_bridge: virbr0
    vm_root_password: "3yMAqs3yTcuKvZ"  # Пароль для root пользователя виртуальной машины
    # net
    vm_ipv4: true
    vm_ipv4_address: 5.9.244.210  # IPv4 адрес гостевой машины
    vm_ipv4_mask: 29
    vm_ipv4_gateway: 5.9.244.209  # IPv4 шлюз гостевой машины
    vm_ipv4_dns: 213.133.98.98 213.133.99.99 213.133.100.100  # Hetzner IPv4 DNS
    vm_ipv6: true
    vm_ipv6_address: 2a01:4f8:163:326a::d1  # IPv6 адрес гостевой машины
    vm_ipv6_mask: 64
    vm_ipv6_gateway: fe80::1  # IPv6 шлюз гостевой машины
    vm_ipv6_dns: 2a01:4f8:0:a0a1::add:1010 2a01:4f8:0:a102::add:9999 2a01:4f8:0:a111::add:9898  # Hetzner IPv6 DNS
    vm_mac: 00:52:54:56:88:88
    

    The values ​​of the variables vm_ipv4_address , vm_ipv4_mask and vm_ipv4_gateway Hetzner sends when ordering an additional IP address. IPv4 and IPv6 addresses of DNS servers are the same as those of the host machine. You can order an additional IPv4 address in your account. Hetzner asks to indicate the purpose for which you need an additional address, I write one word there - "Virtualization".

    Regarding IPv6 : Each server receives a / 64 subnet. Accordingly, you can take any addresses from it. For example, for 2a01: 4f8: 163: 326a :: / 64 :

    • 2a01: 4f8: 163: 326a :: d1
    • 2a01: 4f8: 163: 326a :: d2
    • ...
    • 2a01: 4f8: 163: 326a :: d6

    To specify the value of the vm_mac variable , you need to get a separate MAC address for the specified IP . This can be done in your account.

    That's all, you can start the launch.

    Launch


    The launch is done by two teams. The first installs the necessary packages and configures the host machine:

    $ ansible-playbook -i prod kvm.yml
    

    The second command creates, configures, and starts the virtual machines:

    $ ansible-playbook -i prod guests.yml
    

    After starting and executing these two commands, your virtual machines should be running and accessible from the outside. Your public key has been copied to all virtual machines, so authorization will occur by key.

    Total


    We have been using this set of scripts for quite some time and everything works out as it should. This greatly simplified life and allowed us to quickly add new computing resources to our modest cloud. If you have any questions, welcome to comment.

    That's all. Thank you all for your attention. Stable to you clouds and good luck!

    Follow me on Twitter , I talk about work in a startup, my mistakes and the right decisions, about python and everything related to web development.

    PS I'm looking for developers in the company, the details are in my profile .

    Also popular now: