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.

Don’t be Scared of Dockers

Let me introduce myself, I am Harish and have been a Microsoft user for very long. I love the technology, but that didn’t stop me from exploring other open source platforms. If you look at the market trends the new buzz words are cloud, micro services and every person is talking about Agile. Dockers is one of the core components for micro services platform, and can help on Agile methodology as well. This blog is about my journey learning and experimenting on Dockers on Microsoft platform. The issues I faced and how I was able to successfully install Dockers and some GUI management tools for the same. I will try and explain my thought process and mistakes I made during the decision process.

For someone who is not so well versed on Dockers or containerization it can be scary at first. My suggestion would be take baby steps, slow and steady.

For people who have a Linux background it would be easy to pick up the containerization technology, Microsoft users need not feel bad. Microsoft has done a fine job on windows 2016 and they are working with containerization platform companies to help us conquer the new beast ‘Containers’.

I am not going to explain what is Docker here, there are lot of sites which talk about it and what are the benefits of Dockers and containerization. Most of the processes I follow here would refer back to the Docker documentation and Microsoft documentation. The documents are excellent, I would suggest everyone to go through the documents atleast once, and there is a lot of knowledge available in the following links.

https://docs.docker.com/docker-for-windows/

https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-docker/configure-docker-daemon

Let’s take one step at a time and create our testbed.

Step 1: Install Hyper-V on your windows laptop

To start the installation we need to enable Hyper-v on our Windows 10 laptop.

You can follow the detailed instructions from

https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v

I enabled Hyper-V using the settings from control panel. Nothing specific on why I did not use the PowerShell. I just feel comfortable using the GUI.

clip_image001

Step 2: Installation of Windows 2016

As I call it Docker comes in two flavors for Windows OS, Docker for Windows (for PC’s) and Docker on Windows 2016 (for servers or Production). There are lot of architectural differences which we will be discussing at a later point of time. Just as a highlight Docker Engine on Docker for Windows is based on Linux kernel and Docker on Windows 2016 is based on Windows kernel and HAL. There are few commands like docker-machine which will not work on Dockers on Windows 2016.

We can go on and on discussing about Dockers but let’s come back to our project scope where I want to test Dockers on Windows 2016 with an GUI monitoring tool. For that we would need Windows 2016. We will use the trail edition of Windows for this testing. You can download a absolutely free trial edition of windows 2016 using the below link

https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2016

Ensure you select the ISO option to download the file. The download would be approximately 8GB in size.

clip_image003

a) To start the installation open the Hyper-v manager from Control panel -> Administrative Tools -> Hyper-v Manager

clip_image005

b) Click on New under the Actions Pane and select Virtual Machine

clip_image006

c) Enter the name for Virtual server, I have named it VMWD2016 just to indicate it is a Virtual Machine, Windows, running Dockers and edition of windows 2016, you may name it as per your convenience. Set the location where you want to store the VM, I have left it as default

clip_image008

d) In Generation I left it as Generation 1, and for memory we set it as 4096 (4GB)

e) For now leave the network to be not connected. We will create a switch and join this VM to new switch later once we go and create Docker Swarm.

f) Create a Virtual Hard Disk for 100GB to be safe.

clip_image010

g) In the installation options select “ Install an operating system from a bootable CD/DVD-ROM and then select Image browse to the path where you downloaded the Windows iso file

clip_image012

h) Review the summary and click finish to start the VM to start configuring.

i) Newly created VM will be listed in Virtual Machines pane.

clip_image014

j) Next right click on the newly created VM and select settings and in the newly opened setting click on Checkpoints,

As this is a trail setup you can disable the checkpoints (uncheck). Disabling checkpoint is optional, I feel it is annoying to leave it enabled on test machines.

clip_image016

k) Next click on the newly created Virtual machine and click start

clip_image017

l) You will notice the VM will start consuming the processor, memory etc. Next right click again and click connect.

clip_image018

m) Once you are connected select the language and then click next

n) Click install on the screen

o) On windows setup select the “Windows Server 2016 Standard Evaluation (Desktop Experience)”. As this setup will install the desktop and the server core only. Windows has lot of other options now a days, the nano, core and desktop experience. For the newbies or old generation windows users the desktop will be of help as it makes it really easy to navigate in windows. You can use the core edition also for setting Dockers, just for ease I am selecting to install the desktop.

image

p) Click next and follow the setup. Just perform a simple default installation.

Step 3: Installation of Dockers:

Installation on Dockers can be very straight forward if you can follow the directions given on the Docker Docs:  https://docs.docker.com/install/windows/docker-ee/#use-a-script-to-install-docker-ee

I would suggest,

a) First go to the below link to find the stable version and download it direct on to the system which is connected to internet first

https://download.docker.com/win/static/stable/

b) Logon to the newly created Virtual server and  Launch Add Roles and Features Windows from Server manger.

Go to Features and Enable the Containers Feature

image

c) Copy the downloaded docker file over to the Virtual server which we created in our above step, Rename the file as Docker.zip.

image_thumb

d) Open a Windows PowerShell on the newly installed virtual server with Administrative permissions. Change the path to the location where the docker.zip is located and run the below command:

  Expand-Archive Docker.zip –DestinationPath $Env:ProgramFiles

image_thumb[2]

image_thumb[3]

Lets disect the above command, Expand-Archive means to expand a zip file which will take 2 paths the source and destination path. In destination path we are asking PowerShell to go to environment variables and take the path for program files.

So once the above command is run, it will create a Folder called Docker in Program Files and extract the files there.

Lets see if it worked, the folder will have 2 files, docker and dockerd.

image_thumb[1]

e) Add a new windows Environment path using the below command

   $Env:Path += “$Env:ProgramFiles\Docker”

image_thumb[4]

Lets open the the Environmental variables and check if the necessary setting is already added. For that lets type sysdm.cpl in command prompt, it will open up system properties for us. To learn how to launch control panel items from command prompt you can refer to the below link,

https://support.microsoft.com/en-us/help/192806/how-to-run-control-panel-tools-by-typing-a-command

image_thumb[5]

image_thumb[6]

Click on Environment Variables

image_thumb[7]

Select the System variables path, then click on edit

image_thumb[8]

Verify the C:\ProgramFiles\Docker is listed.

f) Next we need to register docker as a service, type the below command

dockerd –register -service

image_thumb[9]

g) Restart the server

h) Open the services and check if docker listed. Start the service

image_thumb[10]

Congrates !! Docker is installed on windows server 2016.

Let’s verify if docker is working

Open powershell and lets run the below command

docker ps

image_thumb[11]

Docker ps will list all the docker containers running. Usually as it is newly installed you will not see any containers running unlike in the screen shot(I was testing the portainer container, details of which will be explained in another blog. You can also install the docker using the below commands

Install-Module DockerMsftProvider -Force
Install-Package Docker -ProviderName DockerMsftProvider –Force

Rather going through the manual process of downloading the file and registering the same,

I wanted to keep the installation/ testbed scenario as close to Production experience as possible. And also wanted to learn the nitty gritty of installation process so it can be helpful in future for troubleshooting. Also in many a case our production environments may not be open to internet. If you want you can just enable container as discussed in step b of docker installation and then run the above two commands if your server is connected to the internet,

To conclude this blog, we enabled hyper-v, installed the trail edition Windows 2016 and installed Docker. Next blog we will see how to set a docker cluster and continue from there.