Fly, Penguin!

I blog so I don't forget.

Migrate Rancher database from container to external

1 minute read #rancher #ops #solved #infrastructure

I wanted to switch from an in-container database setup to an external database setup. And I didn’t know what happens when you just lose all database contents, and I thought with Docker and some tweaking that should also not be necessary. So I just migrated the databases. Here’s what I did for those interested:

  • stop rancher
  • use a container (sameersbn/mysql) to mount the rancher database content and do a mysqldump
  • import the dump into the external database (AWS RDS instance)
  • start rancher up with different parameters (use external database, as described in the official docs)

And now the actual command lines:

# create socket directory
$ cd RANCHER_MYSQL_MOUNT
$ mkdir sockets

# start sameersbn/mysql to have a mysql container for dumping everything
$ docker run -d --name temp-mysql -v $(pwd)/sockets:/var/run/mysqld -v $(pwd):/var/lib/mysql sameersbn/mysql

# dump the database
$ mysqldump -S ./sockets/mysqld.sock --add-drop-database --add-drop-table --add-drop-trigger --routines --triggers cattle > cattle.sql

# restore the database in AWS / whatever
$ mysql -u USERNAME -p -h DB_HOST DB_NAME < cattle.sq

(Don’t forget to stop the sammersbn container once you’re done). I have configured puppet to start rancher. The final configuration in puppet looks like this:

::docker::run { 'rancher-master':
  image   => 'rancher/server',
  ports   => "${rancher_port}:8080",
  volumes => [],
  env     =>  [
    "CATTLE_DB_CATTLE_MYSQL_HOST=${db_host}",
    "CATTLE_DB_CATTLE_MYSQL_NAME=${db_name}",
    "CATTLE_DB_CATTLE_MYSQL_PORT=${db_port}",
    "CATTLE_DB_CATTLE_MYSQL_USERNAME=${db_user}",
    "CATTLE_DB_CATTLE_MYSQL_PASSWORD=${db_pass}",
  ],
}

Restart, and it seems to be working just fine. To check go to http://RANCHER_URL/admin/ha (yes, we still use HTTP internally, it will change), and you should see this:

setup screen

Nice.