Wednesday, June 20, 2018

Docker swarm


In this blog we will be discussing the below tasks:

1) Add one more Windows servers with Docker’s on it.

2) Create a docker Swarm by promoting one node as manager and adding one node as worker/manager.

What is docker swarm? To answer it in an easy way - cluster of Docker Engines, which can be on a physical or virtual servers.

There are two type of nodes in docker swarm: Manager and Workers.

Manager node: This node handles cluster management tasks like, Scheduling service, maintaining cluster state etc.

Worker node: The sole purpose of this docker node is to execute the containers.

By default all manager nodes are worker’s also. We can promote or demote worker nodes as Managers. In production environment we try not to run containers on manager node, we set the availability for manager node as Drain. We will see later on as how to drain a node and how to run the container as service. Those are interesting topics and may require a separate blog of its own.

For step 1 we can follow the same steps as per our last post  or just to save some time we can do export and import of the VM. Lets walk through the steps on exporting and importing, this way we can save some time and also learn some new skills Smile

Adding New Windows server:

a) Shutdown the newly installed Windows server running docker.

b) Open the hyper-v manager and then right click on the server which you want to export or clone.

image

Once you click on the export, you will be asked a location where to save the files. Provide a valid path to save the file. Lets assume we save it at c:\backup

image

Click on Export button and the export process would start. The time to export will depend on the Disk speed and IOPS.

image

Lets check the path and find out for our self as to what gets exported

As you can see export creates folder same as the name of the server being exported and three folders within that

image

Virtual Hard Disks folder will have the virtual hard drive file

Virtual Machines will have the configuration files.

Snapshots will have snapshots based on the setting, in our case the snapshot will be empty as we disabled it while creating the VM

c) Next lets import the VM created as a new server. Lets name the server as VMWD20162 indicating it to be the second server. For that on the Actions pane, click on Import Virtual Machine

image

Click next on the pop up screen, click on browse button and select the folder where export was stored, varify the path from the above step.

image

Click on next and on the following screen verify the server and click next again. Select copy the virtual machine (create new unique ID) in the import type and click next.

image

The three methods in the above screen are explained below:

Register: If you have a virtual machine where you have already put all of the virtual machine files exactly where you want them, and you just need Hyper-V to start using the virtual machine where it is.
Restore: If your virtual machine files are stored on a file share / removable drive / etc… and you want Hyper-V to move the files to the appropriate location for you, and then register the virtual machine.
Copy: If you have a set of virtual machine files that you want to import multiple times (e.g. you are using them as a template for new virtual machines) this is what you want to choose. This will copy the files to an appropriate location, give the virtual machine a new unique ID, and then register the virtual machine.

Now on the next screen you can set the path for the configuration files for the server where they need to be stored and similarly on the choose storage folders session mention the path for vhd.

image

Finally in the summary verify all the paths are okay and then click on Finish.

image

Wait for the new VM to be created and to be listed on the Hyper-v Manager.

Wow !! now what you will have two VM’s with same name on listed on the hyper-v manager, you have to rename the newly installed server, right click on server and click on rename and add a 2 to end of the name.

image

Lets start both the servers up.

d) In our previous blog we had not configured the network connection. let us set it up for the swarm to work.

Lets create a virtual switch, click on the Virtual Switch Manager on the Actions Pane

image

Once Launched Select Internal and click on Create Switch button

image


It will Create a new Virtual Switch on the Left hand pane. in the right hand pane, type in the name and description for the switch, ensure internal network is selected and then click on OK button.

image


Now we need to launch the network connections. To do this we can open a command prompt and type ncpa.cpl

OR

Via the GUI by clicking on control panel –> network connection.

image

We will have a new network adapter listed with the name we used above

image

Double click on that, then on properties for that adapter, select IPv4, and set the IP as shown in the capture below. You can set any IP, I just took this as an example.

image

e) If there are any Virtual servers running shut them down to add the network adapters.

Next right click on the VM and open settings for that VM, select Network Adapter and click on Add

image

Then continue adding the DocSwitch and click ok, this will add the new network adapter on the VM and also link it to the switch which we created.

image

f) Turn on both the servers and lets set the IP(preferred) for the newly added network cards. You would like to set the IP 192.168.10.xx, Default gateway as 192.168.10.1 as shown in the picture below

image

I have used the 10.11 and 10.12 as the IP’s for 2 servers.

So now we have two windows server installed with dockers, ready to join the swarm.

In production environment you will not find standalone Docker engines, they will all be in a swarm, managed either by Docker UCP or other 3rd party tools. We can have Dockers (nodes) managed using Kubernetes, at later stage I will write a blog on this as well, In that we will prepare a Linux master running the kubernetes and we will add Windows Docker server as client. For now let me come back to creating a Docker Swarm.

For this Blog we were discussing two topics, installation of Additional server and then creating a swarm.

For the second part we will follow the below steps:

a) We need to add our first node to the Docker Swarm by declaring it as the master/manager. Later we can add more nodes as masters or workers.

To add our first node we need to run the below command:

docker swarm init –advertise-addr <ip address of the server>

in our case the command would be

docker swarm init –advertise-addr 192.169.10.11

image

Lets verify the status of the node by running the following command:

docker node ls

This command will list all the nodes in the swarm, and we can see our first server /node is added to the swarm as leader. Next we need to add the worker.

image

b) To add node to the swarm we need to have the respective token. (Token for a worker and a token for a manager)

To check the token you can run the below command:

docker swarm join-token worker

or

docker swarm join-token manager

image

Copy one of the outputs

image

or

image

Depending on what you want the other node to be - manager or worker.

You can promote or demote nodes at later stage as well.

c) Lets connect to our other server and join it to the swarm as a worker, so i would be using the first token for that .

image

Done!

d) Great, now lets run the docker node ls command to find if we can see all the nodes

image

  • What Error !!?

That is because only the Manager nodes can list all nodes. So lets connect to the master node or our first server and run the command again

image

We have 2 nodes, one as leader/Manager and other one as worker. Great we have our swarm ready with 2 servers/nodes in it.

Let us connect to the manager node and run the promote command just to check

docker node promote WinDocker2

and run the docker node ls again to verify the status

image

run the docker node ls command on the other node as well.

image

To demote we can run the demote command. Refer to Docker docs they have extensive help for all commands

https://docs.docker.com/engine/swarm/manage-nodes/#promote-or-demote-a-node

In the next blog we will discuss on how to setup a management portal, below is the screen shot of portainer, using it we can verify the cluster performance add registry etc.

image

So what are you waiting for? setup your lab and join us in learning journey. Keep learning and Enjoying I will see you in my next blog.

1 comment: