Using Environment Variables

MySQL Example

Flocker supports passing environment variables to a container via its Application Configuration. This example will use a configured environment variable to set the root user password for a MySQL service running inside a container.

Create the Virtual Machines

You can reuse the Virtual Machines defined in the Vagrant configuration for the MongoDB tutorial. If you have since shutdown or destroyed those VMs, boot them up again:

[email protected]:~/flocker-tutorial$ vagrant up
Bringing machine 'node1' up with 'virtualbox' provider...
==> node1: Importing base box 'clusterhq/flocker-dev'...

Download the Docker Image

The Docker image used by this example is quite large, so you should pre-fetch it to your nodes.

[email protected]:~/flocker-tutorial$ ssh -t [email protected] docker pull mysql:5.6.17
...
[email protected]:~/flocker-tutorial$ ssh -t [email protected] docker pull mysql:5.6.17
...
[email protected]:~/flocker-tutorial$

Note

The mysql:5.6.17 Docker image is used in this example for compatibility with ZFS. Newer versions of the MySQL Docker image enable asynchronous I/O, which is not yet supported by ZFS on Linux.

Launch MySQL

Download and save the following configuration files to the flocker-tutorial directory:

mysql-application.yml

"version": 1
"applications":
  "mysql-volume-example":
    "image": "mysql:5.6.17"
    "environment":
      "MYSQL_ROOT_PASSWORD": "clusterhq"
    "ports":
    - "internal": 3306
      "external": 3306
    "volume":
      "mountpoint": "/var/lib/mysql"

mysql-deployment.yml

"version": 1
"nodes":
  "172.16.255.250": ["mysql-volume-example"]
  "172.16.255.251": []

Now run flocker-deploy to deploy the MySQL application to the target Virtual Machine.

[email protected]:~/flocker-tutorial$ flocker-deploy mysql-deployment.yml mysql-application.yml
[email protected]:~/flocker-tutorial$

Connect to MySQL & Insert Sample Data

You can now use the mysql client on the host machine to connect to the MySQL server running inside the container. Connect using the client to the IP address of the Virtual Machine. In this case the example has exposed the default MySQL port 3306 so it is not required to specify a connection port on the command line:

[email protected]:~/flocker-tutorial$ mysql -h172.16.255.250 -uroot -pclusterhq

Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql> CREATE DATABASE example;
Query OK, 1 row affected (0.00 sec)

mysql> USE example;
Database changed
mysql> CREATE TABLE `testtable` (`id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(45) NULL,PRIMARY KEY (`id`)) ENGINE = MyISAM;
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO `testtable` VALUES('','flocker test');
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> quit
Bye

[email protected]:~/flocker-tutorial$

Create a New Deployment Configuration and Move the Application

Download and save the following configuration file to your flocker-tutorial directory:

mysql-deployment-moved.yml

"version": 1
"nodes":
  "172.16.255.250": []
  "172.16.255.251": ["mysql-volume-example"]

Then run flocker-deploy to move the MySQL application along with its data to the new destination host:

[email protected]:~/flocker-tutorial$ flocker-deploy mysql-deployment-moved.yml mysql-application.yml
[email protected]:~/flocker-tutorial$

Verify Data Has Moved

Confirm the application has moved to the target Virtual Machine:

[email protected]:~/flocker-tutorial$ ssh [email protected] docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS                    NAMES
51b5b09a46bb        mysql:5.6.17   /bin/sh -c /init    7 seconds ago       Up 6 seconds        0.0.0.0:3306->3306/tcp   mysql-volume-example
[email protected]:~/flocker-tutorial$

And is no longer running on the original host:

[email protected]:~/flocker-tutorial$ ssh [email protected] docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS                    NAMES
[email protected]:~/flocker-tutorial$

You can now connect to MySQL on its host and confirm the sample data has also moved:

[email protected]:~/flocker-tutorial$ mysql -h172.16.255.251 -uroot -pclusterhq

Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| example            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.02 sec)

mysql> USE example;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT * FROM `testtable`;
+----+--------------+
| id | name         |
+----+--------------+
|  1 | flocker test |
+----+--------------+
1 row in set (0.01 sec)

mysql>

This concludes the MySQL example.