Today, the single-server era is gone, and there is no peace of mind in deploying any service without adding a few more servers.

And in the local learning test, also often need to use the server cluster, such as building a MySQL cluster, a Redis cluster, a HDFS cluster, but for the test to buy a cloud server is too luxury, so the local cluster is an economic and affordable way.

This article describes a method for setting up a server cluster locally.

Software and hardware requirements:

  • One computer (unlimited system, higher configuration is better)
  • VirtualBox
  • Centos7

VirtualBox provides the installation versions for each system. After downloading the software, click the software package to install it.

Centos can choose its own version, or other Linux distributions also line, note to select Minimal version, as a server, do not need a desktop, such as the size of the server image can be small.

The objectives of setting up a centos cluster are as follows:

  • Build a cluster of three servers using virtual machines
  • All three VMS can communicate with the host
  • All three VMS can access the Internet
  • SSH connection between three machines can be directly used without entering a password

This article is based on Mac OS 10.15.5, VirtualBox6.1.8, and centos7, plus some basic networking knowledge is required to read this article

Installing the Server

To start, you need to add a network card to the Global configuration of VirtualBox, as shown below:

Here it is important to note the IPv4 address and network mask, IPv4 address will become the gateway of the server cluster, normally does not need to be modified (if you’re charting of computer networks, portable, literally change), set up gateway and subnet mask, subsequent configuration of IP virtual machine needs to be within the network segment.

You can then create a virtual machine with a random name and select a destination folder to store files related to the virtual machine.

If Centos 7 is installed on a Linux VM, select Red Hat(64-bit). If Centos 7 is installed, select Red Hat(64-bit).

After the configuration is complete, it is necessary to allocate memory to the VIRTUAL machine, according to the configuration of your own computer to choose, basically about 2G is ok:

Create a virtual hard disk. The default configuration is sufficient for a Centos operating system without a graphical interface.

At this point, the VIRTUAL machine is created. The next step is to provide the system startup image for the virtual machine. Select the VIRTUAL machine and click Settings:

Click Save again:

Select the downloaded image:

By default, only one NIC is enabled. To meet subsequent requirements, we need to enable another NIC:

Select host-only mode:

Click OK to save the configuration, and then you can start the virtual machine. The first boot will install the system, set the root password during the installation process, and the virtual machine installation is complete.

To form a server cluster, we need three virtual machines, and the other two are installed the same way as above.

The network configuration

In this article, I use NAT + host-only to configure the virtual machine to access both the external network and the host machine, but this is not the only way.

The newly installed VM cannot access the external network because the VM network interface card (NIC) is not enabled. Log in to the VM and enable the NIC so that the VM can access the external network through NAT. Then use host-only to enable the host to access the VM.

Log in to the system as root and go to the network configuration directory:

cd /etc/sysconfig/network-scripts/
Copy the code

If the previous configuration is correct, you can see the configuration of two network cards here:

Ifcfg-enp0s3 corresponds to the NIC configured by NAT, and ifcfg-enp0s8 corresponds to the NIC configured by host-only.

To configure the NAT network, run the vi command to open ifcfg-enpos3.

Vi is a built-in Linux editor

Change the value of ONBOOT to yes, which indicates that the NIC is enabled upon startup. After NAT is configured, the VM can access the Internet.

Configure the host-only network.

Set ONBOOT to yes. IPADDR specifies the fixed IP address of the VM. The IP address can be set at will, but NETMASK is the network mask of the vm.

Finally configure the gateway:

vi /etc/sysconfig/networking
Copy the code

Fill in the following:

NETWORKING=yes GATEWAY=192.168.56.2 # This value is the GATEWAY IP address of the VirtualBox global network adapterCopy the code

After all this is done, restart the network service:

service network restart
Copy the code

Then access the extranet:

The host accesses the VM:

The network of one vm has been configured. The other two VMS need to be configured with the same IP address.

In this example, the IP addresses of the three VMS are 192.168.56.3 192.168.56.4 192.168.56.5

Server interconnection

At this point, the three servers can connect to the Internet, the host can also access VMS, and VMS can also access each other through SSH after entering passwords.

But many times servers need to be able to transfer files directly, and it would be inconvenient to enter a password every time.

SSH is still used to log in between machines, but it can be modified by each server setting the other two as trusted machines so that you can log in directly without entering a password each time.

For example, run the ssh-keygen command to generate the public key and key of the VM whose IP address is 192.168.56.3.

Then use ssh-copy-id to copy the public key to a machine like a secret free login:

Then you can log in without password:

SSH [email protected] SSH to [email protected]Copy the code

Perform the same configuration on the other two machines, and then the three VMS can access each other confidentially.

At this point, the local server cluster is set up.

The text/Rayjun

Follow the wechat official account and chat about other things