MySQL and Docker Strategies Patrick Galbraith Giuseppe Maxia
About the speakers Patrick Galbraith (patg@patg.net) HP Advanced Technology Group Has worked at Blue Gecko, MySQL AB, Classmates, Slashdot, Cobalt Group, US Navy, K-mart MySQL projects: memcached UDFs, DBD::mysql, federated storage engine New technologies and automaron Family Outdoors
About the speakers Giuseppe Maxia, a.k.a. "The Data Charmer" QA Architect at VMware 25+ years development and DB experience Long Rmer MySQL community member. Oracle ACE Director Blog: hzp://datacharmer.blogspot.com TwiZer: @datacharmer
What are containers? Server Host OS Hypervisor Guest OS Guest OS Guest OS Bins/ Libs Bins/ Libs Mysql Bins/ Libs Mysql App Server Host OS Bins/Libs Bins/Libs M y s q l M y s q l A p p A p p A p p M y s q l D o c k e r VM Containers
Containers vs. VMs MulRple isolated userspace instances Only libraries and components needed for applicaron Runs on the same kernel (using Cgroups). Much smaller, easier to package VERY fast to start! Container runs using (a) specific process(es) SSH not needed Security limited to app
What is Docker? Both a daemon and a command line tool ApplicaRon that manages containers (CLI, API) Automates the deployment of applicarons inside soaware containers WriZen in Go, Opensource dotcloud Uses union file system (AUFS) Can use CLI to search Docker repos for images Used to be awsome sauce around lxc, now using libcontainer No dependency hell
Two ways to Docker For tesrng Quick manual deployment (1 10 containers) Easy configuraron Rapid deploy-test-destroy-retry cycle Works everywhere For producron Quick tool-assisted deployment (100, 1000, N containers) Maintained by orchestrators Perishable by default Easy to replace
Virtual Machines
Containers
Docker concepts
Giuseppe s Agenda (20 min) Difference between VMs and Containers (Patrick) Two cases for containers (test and prod) Seven key points (full system Linux minimal passwords isolaron volumes configuraron ) Shared volume for backup Simple example More complex example InspecRng a container (Quick demo)
Installing Docker 1. GO TO hzps://docs.docker.com/engine/installaron/ 2. Follow the instrucrons for your O.S.
First off: get the MySQL image $ docker pull mysql/mysql-server Using default tag: latest latest: Pulling from mysql/mysql-server 5e7ce0e805ba: Pull complete 4b9736773855: Pull complete 4fb9c55b1f85: Pull complete 19452ca711c2: Pull complete 0c660f33db44: Pull complete b8e96405c8c7: Pull complete 9bdbb574fa66: Pull complete 24c2343e048d: Pull complete Digest: sha256:da24eddcb99cbeba979d7da8170991278ca59838401a7c7bf17c66 307acc641c Status: Downloaded newer image for mysql/mysql-server:latest
SEVEN key points
#1 - Containers are FULL systems Every container contains an executable layer It's a full O.S.
#2 Containers are always Linux Linux flavor in the container is independent from the host But they have a comparble kernel That s why containers are faster than VMs
#3 The O.S. in the container is minimal Don t expect all the tools you get from a Ubuntu DVD You will have only the bare essenrals
#4 MySQL installayon requires a password Security varies depending on the maintainer You may use files (recommended) You may generate a random password (don t)
#5 Containers are ISOLATED You can access containerized MySQL from other containers You need explicit access from the exterior.
#6 Data storage can be tricky By default, the data directory is inside the container Containers are volarle by default and by design You must use volumes to preserve your data
#7 ConfiguraYon is done by adding files With VM, you deploy and then configure. This takes Rme With container, you deploy and add configuraron files on-the-fly Containers are ready to work from the start.
Examples
Simple MySQL deployment docker run \ --name mybox \ -e MYSQL_ROOT_PASSWORD=secret \ -d \ mysql/mysql-server 06d3392543500455adcfe6cba36736739c46688de8028647d38109 c0ec3cf250 # WILL RUN WITH DEFAULT VALUES
Adding log-bin and server-id (1) ## OPTION 1 : on the command line docker run \ --name mybox \ -e MYSQL_ROOT_PASSWORD=secret \ -d \ mysql/mysql-server --log-bin --server-id=100
Adding log-bin and server-id (2) ## OPTION 2 : with a "volume" file $ cat minimal.cnf [mysqld] user=mysql log-bin=mysql-bin server-id=100 $ docker run \ --name mybox \ -e MYSQL_ROOT_PASSWORD=secret \ -d --hostname mybox \ -v $PWD/minimal.cnf:/etc/my.cnf mysql/mysql-server
InspecYng a container docker exec -ti mybox bash [root@mybox /]# mysql -p Enter password: [...] Server version: 5.7.11-log MySQL Community Server (GPL) [...] mysql> select @@server_id; +-------------+ @@server_id +-------------+ 100 +-------------+ 1 row in set (0.00 sec)
Shared backup with containers database server Shared container Physical host shared folder database server database server
Shared backup with containers $ docker create -v /dbdata \ --name dbdata mysql/mysql-server /bin/true $ docker run --name mysql1 \ -e MYSQL_ROOT_PASSWORD=secret \ --volumes-from dbdata -d mysql/mysql-server $ docker run --name mysql2 \ -e MYSQL_ROOT_PASSWORD=secret \ --volumes-from dbdata -d mysql/mysql-server
Demo
Shared backup with containers $ docker create -v /dbdata \ --name dbdata mysql/mysql-server /bin/true $ docker run --name mysql1 \ -e MYSQL_ROOT_PASSWORD=secret \ --volumes-from dbdata -d mysql/mysql-server $ docker run --name mysql2 \ -e MYSQL_ROOT_PASSWORD=secret \ --volumes-from dbdata -d mysql/mysql-server
Container AutomaYon Ansible (hzp://docs.ansible.com/ansible/docker_module.html) YAML Define any number of containers Various ansible modules for building, running, and fact-gathering and service discovery of containers as well as managing images dynamic inventory plugin. Docker Compose (hzps://docs.docker.com/compose/) Define and run mulrple containerized applicarons
Container AutomaYon (cont) Chef (hzps://docs.chef.io/containers.html) Docker cookbook -- install and manage Docker, manage containers and images Etcd and consul cookbooks service discovery Puppet (hzps://forge.puppet.com/puppetlabs/docker_plaporm) Packer (hzps://www.packer.io) crearng idenrcal machine images for mulrple plaporms from a single source configuraron
Ansible Example --- - hosts: "{{ target default('docker') }} sudo: false tasks: - name: run db1 container local_action: module: docker name: db1 pull: missing
Ansible Example (cont) image: registry-host:443/db-img hostname: db1 state: started restart_policy: always volumes: - /var/data/db1-data:/var/lib/mysql - /var/data/backups:/var/data/site/backups - /home:/home detach: True ansible-playbook i inv.txt db1.yaml
Container Clustering Kubernetes -- hzp://kubernetes.io/ CoreOS -- hzps://coreos.com/ Swarm -- hzps://docs.docker.com/swarm/overview/ Mesos -- hzp://mesos.apache.org/ RancherOS -- hzp://rancher.com/rancher-os/ Deis -- hzp://deis.io/ Kupack Redspread KPM
Kubernetes
Galera on Kubernetes For more info 4:30pm to 5:20pm Ballroom A pxc_node1 pod and service file pxc_node2 pod and service file pxc_node3 pod and service file kubectl Api server pxc_cluster Service file pxc_node1 pod pxc_node2 pod pxc_node3 pod pxc container pxc container pxc container pxc_node1 service pxc_node2 service pxc_cluster service pxc_node3 service
Demo
Summary Technology rapidly evolving Containers good for prototyping (manually) And for producron (with tools) Great for distriburng soaware (dev & prod use the same bits) NOT virtual machines (even if they play one on TV)
More on this topic hzp://www.datacharmer.blogspot.com/ hzp://patg.net