Flocker is a free and open source software for managing container data volume in a dockerized applications. In native docker technology, if we migrate a container from one server to another new server, the data volume is left behind whereas only container is moved. But with the advancement of technology and heavy development on Docker technology, a new platform was born named as Flocker. It not only moves containers but it helps to migrate both the container and data volume together. This makes flocker data volume which is also known as dataset, pretty portable and can be used with any container in the cluster. This key feature of Flocker makes it very popular among the ops team to run containerized stateful services like databases in production. This tutorial is all about how we can migrate a container from one server to another server along with data volume.
Here are some steps on how we can migrate a container with data volume from one server to another using Flocker.
First of all, before we get started, we'll need to fulfill some essential things. We'll need to have 3 nodes to do this job. First we'll have a Client Node in which we'll store the configuration files and run flocker-cli on. A client node can be our own laptop, desktop or any other computer or even a server. Next, we'll need 2 other nodes in which we'll run the docker containers using flocker and move a running container along with its data volume without any interruption. Following are the systems that we are going to setup with their ip address which are running Ubuntu 15.04 as their operating system and are in the same flocker cluster.
|Client Node 0||Container Node 1||Container Node 2|
After we are done with setting up the flocker cluster, we'll now go for running a docker container using flocker.
1. Creating Application File
We'll now create a docker compose file or application file which will define the containers we want to run with their respective configurations such as docker image, name, ports, data volume and more. We'll create the YAML file using a text editor and under the structure of docker compose. To do so, we'll run the following command and start a text editor and create docker-compose.yml file in Node 0.
# nano docker-compose.yml
After opening the text editor, we'll now append the file as shown below.
web: image: clusterhq/flask links: - "redis:redis" ports: - "80:80" redis: image: redis:latest ports: - "6379:6379" volumes: ["/data"]
The above configuration defines that on running the above configuration, it will create 2 containers one named web and another redis. The web one will run a container from an image clusterhq/flash and will expose on port 80 whereas the redis will run a container from the latest release of image redis and will expose on port 6379 with data volumer under /data directory.
2. Creating Deployment File
Next, we'll create another file named flocker-deploy1.yml in which we'll define the most important part of our tutorial, we'll define where those containers will be deployed. Here, we'll define to deploy both of the containers Python Web App (FLASK) and Redis Database Server under same host ie Node 1. To do so, we'll run the following command to open the text editor.
# nano flocker-deploy1.yml
After opening, we'll append the YAML file as shown below.
"version": 1 "nodes": "188.8.131.52": ["web", "redis"] "184.108.40.206": 
Then, we'll simply save the file and exit.
In above YAML file, we have defined to run the both of the containers ie web and redis to run under the same host ie node 1 without running anything under node 2.
3. Deploying Containers
After we have created those files, we'll now deploy the containers using those YAML files. To do so, we'll simply need to run the following command under sudo or root privilege.
# flocker-deploy control-service flocker-deploy1.yml docker-compose.yml
The cluster configuration has been updated. It may take a short while for changes to take effect, in parti
cular if Docker images need to be pulled.
We'll be prompted that it may take some time to get the containers deployed as defined by the above configuration. As we have defined in above configuration, both the FLASK and Redis must be running under the same host ie Node 1. So, we'll get into Node 1 to check if its running both of the containers or not.
4. Inspecting Docker Containers
To check if the Node 1 is running both the containers or not, we'll see the list of running docker containers in Node 1. We can do that by SSH tunneling into Node 1 which has ip address as220.127.116.11 and running docker command to see the list of running containers. To do so, we'll need to run the following command.
# ssh email@example.com docker ps
5. Testing the Application
After we get those containers running, we'll surely wanna test the application running in Node 1. To do so, we'll gonna open those ip addresses using a web browser. When we browse http://18.104.22.168/ , we'll see that the visit count is displayed whereas when we browse http://22.214.171.124/ , we'll see that the visit count persists because flocker routes the traffic from either node defined in the Deployment file to the one that has the application. It makes flocker possible to move our containers and its volumes around the cluster without having to update any DNS or application settings.
6. Recreating Deployment File
Now, we'll finally rewrite the deployment file in order to move the container with its data volume. We'll gonna create a new file or edit the previous file, append and save as flocker-deploy2.yml.
# nano flocker-deploy2.yml
Then, we'll append the file as shown below.
"version": 1 "nodes": "126.96.36.199": ["web"] "188.8.131.52": ["redis"]
This will define the web container to run under node 1 and redis container to run under node 2.
7. Moving Container with Data Volume
Finally, we'll now deploy the newly created deploy YAML file which will migrate the running redis container from Node 1 to Node 2 including its data volume. This will keep the web container in the same node as before ie Node 1 without affecting the application.
# flocker-deploy control-service flocker-deploy2.yml docker-compose.yml
The cluster configuration has been updated. It may take a short while for changes to take effect, in particular if Docker images need to be pulled.
8. Inspecting the Migration
To check if the redis container is really migrated or not, we can see that by listing the running containers in those nodes. First, we'll see in Node 2 if the redis server is migrated or not via SSH tunneling by running the following command.
# ssh firstname.lastname@example.org docker ps
As we can see, there is only Redis server container running in this node whereas the web container is not running in this node.
Now, to cross check, we'll gonna see what containers are running in the Node 1.
# ssh email@example.com docker ps
And finally, we see that there is no Redis server running in this node whereas there is only web container running.
9. Checking the Application
We'll now check the application whether its running fine as expected or not. To do so, we'll open a web browser and point it to both of the nodes ie http://184.108.40.206/ and http://220.127.116.11/ . Here, we see that the count still persists while pointing on Node 1, even though the container with the volume has moved between hosts. And we also see that the visit count still persists on Node 2 even though the application is no longer running on that host. This verifies that the redis container has been successfully migrated with its data volume.
This tutorial is about how we easily we can migrate a container with its data volume in a flocker cluster from one host to another within the same cluster. Flocker can be used with popular container managers or orchestration tools like Docker Engine, Docker Swarm, Docker Compose and in different platforms like Amazon AWS, RackSpace, OpenStack, Vagrant. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you !