Docker Swarm Create a server cluster tutorial

Source: Internet
Author: User
Tags web services ssh centos port number docker swarm

Docker Swarm is a tool for creating server clusters. Tie a bunch of servers together and make them look like a server that runs applications on these servers through a unified interface.

Servers in a cluster know each other, and they know which servers are running what services, and if the access service does not exist on the server, it transfers access to the correct server in the cluster. A service can be supported by multiple containers, which run on different servers in the cluster, and requests can be distributed evenly to these containers. When a server is dead, the services running on it are moved to another server to run.

Docker Swarm requires a few lines of command to create a powerful cluster for your application, providing a highly available service for your application. Ninghao's "Docker: Cluster" describes how to create a clustered server using Docker Swarm.

Simulate cluster environment

If you already have several servers, you can create a server cluster after installing Docker, if you want to test the Docker Swarm function locally, you can simulate a cluster environment, that is, to create several Linux systems locally, install Docker, and configure the cluster. Here's how to create a virtual machine using vagrant.

Install Virtualbox: virtual machine Software
Install vagrant: A tool for managing virtual machines that can manage Virtualbox virtual machines
Install Vagrant Hostmanager Plugin
Windows can no longer use Virtualbox or other virtual software when Hyper-V is enabled.
Vagrant Hostmanager is a vagrant plug-in, set the hostname of the virtual machine, this plugin can help us to set up the/etc/hosts file in the virtual machine, add the correct host record inside, so if more than one virtual machine is created, These virtual machines can use the hostname of the virtual machine to access each other, this plug-in is not necessary, but it is useful, it can also set the host file on the local hosts, so you can also directly use the host name of the virtual machine to access it.

# Create a project directory
CD ~/desktop
mkdir Docker-swarm
CD Docker-swarm

# Initialize project, use CENTOS/7 this box
Vagrant Init CENTOS/7

# Open the project directory with the editor
Atom./
Edit the Vagrantfile in the root of the project and replace it with the following code, which can create three virtual machines, node1,node2,node3.

Vagrant.configure (2) Do |config|

Config.vm.box = "CENTOS/7"

# Please install the Vagrant-hostmanager plugin
# Https://github.com/devopsgroup-io/vagrant-hostmanager
Config.hostmanager.enabled = True
Config.hostmanager.manage_host = True
Config.hostmanager.manage_guest = True

Config.vm.define "Node1" do |node1|
Node1.vm.network "Private_network", IP: "192.168.33.11"
Node1.vm.hostname= "Node1"
End

Config.vm.define "Node2" do |node2|
Node2.vm.network "Private_network", IP: "192.168.33.12"
Node2.vm.hostname= "Node2"
End

Config.vm.define "Node3" do |node3|
Node3.vm.network "Private_network", IP: "192.168.33.13"
Node3.vm.hostname= "Node3"
End

End
Start a virtual machine

Vagrant up
Install Docker

After the virtual machine is started, log on to the virtual machines and install Docker on the above:

Curl-fssl https://test.docker.com/| Sh
sudo systemctl enable Docker
sudo systemctl start Docker
Create a cluster

The server in the cluster has two identities, one is manager (administrator), the other is worker (worker), on the administrator server can create the service, extend the service, update the service, the administrator server itself can also run the service. First initialize a clustered server, for example, I want to initialize the server on Node1, log on to this server first.

# Log on to the NODE1 server
Vagrant SSH Node1

# Initialize Swarm cluster
Docker swarm Init--listen-addr node1:2377
This node1 by default will be the administrator of this cluster, now there is only one node in the cluster server, you can view:

Docker node ls
When the cluster is initialized with--LISTEN-ADDR, specify the address of the Cluster Administrator and the port number, the default address should be 0.0.0.0, here I use a host name to express, Node1. Listing this option is to show that you are free to set up the server's listening address and port number, and if you use the hostname, you need to make sure that other servers in the cluster can access the server using this host name.

Join cluster

When you have a cluster, you want to have other servers join in:

# Login to Node2
Vagrant SSH Node2

# Let Node2 join the cluster
Docker swarm join--listen-addr node2:2377 node1:2377

# Login to Node3
Vagrant SSH Node3

# Let NODE3 join the cluster
Docker swarm join--listen-addr node3:2377 node1:2377
Now there are three servers in the cluster, and a list of servers can be viewed in the cluster Manager:

Docker node ls
When the server joins the cluster, it also uses the--LISTEN-ADDR option, which sets the address and port that the server listens to, and the other servers use this address and port to communicate with it. The following specifies the cluster to which this server is joined, that is to tell the administrator server in the cluster.

Creating a cluster network

Servers in a cluster use a overlay type of network, and we can create a network that can then be designated to use when running a service in a cluster. On the Cluster Manager (NODE1), execute:

Docker Network Create--driver overlay skynet
This creates a network of overlay types whose name is Skynet.

Create a service

On the Manger node of the cluster, you can create a service that runs the application and perform the following:

Docker Service Create--name Web--network skynet--publish 3000:3000--replicas 1 Ninghao/node
The above command will create a Web-based service using the Ninghao/node image of the Ninghao network, which uses the Skynet name network and publishes a port of 3000. --replicas specified the service to run in a container. When you are done, you can open your browser and visit:

http://node1:3000
http://node2:3000
http://node3:3000
Although the Web service has only one container to run, it will be on a server in the cluster, most likely node1 this server, no matter which server it runs on, access to all the servers in the cluster, can open the service provided pages, you should see a hello on the page, There is also the ID number of the container running the application.

Extended Services

Now I'm going to run the application with multiple containers that run on different servers in the cluster. Expand the number of containers that run the service, and execute it on the manager node of the cluster:

Docker Service Scale Web=6
The above command runs the Web service in 6 containers, and the 6 containers are distributed across different servers to view the tasks that run the service, and perform the following:

Docker Service Tasks Web
You will see which servers are running the Web service. In a different browser to visit the application of the page, you will find that the application of the container to run the ID number will change, that is, users of the application of the request will be distributed evenly across the different servers.

Update Service

After the service is created, it can be updated, such as the service port number, the data volume, the network, the mirror, these things can be updated. I want to update the mirrors used for Web services and perform them on the cluster Manager:

Docker Service Update Web--image Ninghao/node:hola
It will make the Web service based on the new mirror Ninghao/node:hola to create, after the update is completed, reopen the browser, access to the application of the page, the content will be displayed on the page changes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.