Docker Machine

 

Je continue mon apprentissage de Docker quand j'ai un peu de temps libre. Donc aujourd'hui on va jouer avec Docker Machine.

Docker Machine est un outils permettant de déployer des hôtes docker sur différentes plateformes. Cela permet aussi de crer des hôtes pour les clusters swarm dont on parlera plus tard.

Installer Docker Machine

Je pars toujours du principe que je travaille sur des machines GNU/Linux, il existe des possibilités pour tourner docker-machine sous MacOS et Windows.

On en profite pour installer la dernière version :

# curl -L https://github.com/docker/machine/releases/download/v0.7.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine
# chmod +x /usr/local/bin/docker-machine
# docker-machine -v
docker-machine version 0.7.0, build a650a40

 

Drivers

Le Driver correspond à la plateforme sur laquelle vous allez pouvoir déployer vos machines. La liste est assez intéressante et diversifiée :
https://docs.docker.com/machine/drivers/

Nous allons voir comment déployer des machines VirtualBox, Generic et Openstack.

Création d'une machine VirtualBox

Je suppose bien sûr que VirtualBox est installé sur votre machine, sur ma Fedora, docker-machine m'impose une version 5 minimum de VirtualBox.

$ docker-machine create --driver virtualbox mac01-vb
Running pre-create checks...
(mac01-vb) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(mac01-vb) Latest release for github.com/boot2docker/boot2docker is v1.11.0
(mac01-vb) Downloading /home/guidtz/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.11.0/boot2docker.iso...
(mac01-vb) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(mac01-vb) Copying /home/guidtz/.docker/machine/cache/boot2docker.iso to /home/guidtz/.docker/machine/machines/mac01-vb/boot2docker.iso...
(mac01-vb) Creating VirtualBox VM...
(mac01-vb) Creating SSH key...
(mac01-vb) Starting the VM...
(mac01-vb) Check network to re-create if needed...
(mac01-vb) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env mac01-vb

 

On a donc une première machine docker sur le virtualbox local

$ docker-machine ls
NAME       ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
mac01-vb   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.0   

 

Création d'une machine Openstack

Pour cela j'utilises un compte sur le cloud public d'OVH : https://www.ovh.com/fr/cloud/

Mais cela peut fonctionner avec toute infrastructure Openstack en place, il suffit juste de récupérer le tenant et les paramètres d'authentification.

On commence par charger le fichier d'authentification sur l'API Openstack

# . ./openrc_test.sh

 

On peut alors déployer notre machine :

#  docker-machine create -d openstack --openstack-flavor-name="vps-ssd-1" --openstack-image-name="Debian 8"  --openstack-net-name="Ext-Net" --openstack-ssh-user="admin" mac02-ost
Running pre-create checks...
Creating machine...
(mac02-ost) Creating machine...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with debian...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env mac02-ost

 

Et une seconde machine.

$ docker-machine ls
NAME        ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
mac01-vb    -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.0   
mac02-ost   -        openstack    Running   tcp://167.114.242.26:2376           v1.11.0   

 

Utilisation du drivers Generic

Ce drivers va nous permettre d'utiliser une machine n'étant pas installée sur une infrastructure cloud, un serveur physique par exemple ou la dans mon cas de tests une vm sur un serveur Proxmox.

$ docker-machine create --driver generic --generic-ip-address=IP --generic-ssh-key=~/.ssh/cle_ssh --generic-ssh-user=root mac03-gen
Running pre-create checks...
Creating machine...
(mac03-gen) Importing SSH key...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with debian...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Error creating machine: Error checking the host: Error checking and/or regenerating the certs: There was an error validating certificates for host "94.23.188.254:2376": dial tcp 94.23.188.254:2376: i/o timeout
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
Be advised that this will trigger a Docker daemon restart which will stop running containers.

 

Si il y a un firewall sur la machine, ne pas oublier d'ouvrir le port 2376

Et une machine de plus

$ docker-machine ls
NAME        ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
mac01-vb    -        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.0   
mac02-ost   -        openstack    Running   tcp://167.114.242.26:2376           v1.11.0   
mac03-gen   -        generic      Running   tcp://IP:2376            v1.11.0   

 

Déployer des conteneurs sur les machines

Vous avez remarqué cette petite phrase après le déploiement des machines :

To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env mac02-ost

Cela nous dis comment docker machine soit se connecter sur l'environnement de la machine :

$ eval $(docker-machine env mac02-ost)

 

Pour vérifier sur quelle machine on agit :

$ docker-machine active
mac02-ost

 

On démarre un petit conteneur :

$ docker run --name web01 -d --hostname web01 nginx
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ba478320cd25        nginx               "nginx -g 'daemon off"   11 seconds ago      Up 10 seconds       80/tcp, 443/tcp     web01

 

On change de machine :

$ eval $(docker-machine env mac03-gen)

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Bien sûr on a plus notre conteneur vu qu'on a changé de machine.

 

Quelques commandes

$ docker-machine stop machine

$ docker-machine restart machine 

$ docker-machine start machine

$ docker-machine rm machine (vous imaginez bien les conséquences)

 

On peut se connecter dessus :

$ docker-machine ssh mac01-vb
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.0, build HEAD : 32ee7e9 - Wed Apr 13 20:06:49 UTC 2016
Docker version 1.11.0, build 4dc5990

docker@mac01-vb:~$ cat /etc/issue 
Core Linux

Voilà un bel outils pour déployer nos machines pour héberger nos conteneurs.

Prochain épisode ... docker swarm.