網頁

2017年2月25日 星期六

Docker Machine 0.9.0 --- Docker Machine install on the local client and manage Docker Engine on the remote host

Docker Machine is a tool that lets we install Docker Engine on virtual hosts and manager the hosts with docker-machine command.

By docker-machine command, we can start, inspect, stop and restart a managed host, upgrade the Docker client and daemon, and configure a Docker client to talk to the host next to run docker commands directly on this host.

In the moment, we will demo how to practice that docker machine manage the remote host.

Prerequisites
docker host never install a docker engine on the remote machine (# 1)
docker client   install docker client and docker machine on the local machine(as laptop)
where
OS in the remote need to be CentOS 7, Ubuntu 16.04 or above version
The net-tools package (# 2) need to be installed on the remote machine

[Remote Machine]
To create a docker manager account with new password (# 3)
$ sudo adduser {dockermanager}

To allow sudo for docker manager
In the remote machine, we have to edit the /etc/sudoers file and add the following content

                          {dockermanager}  ALL=(ALL) NOPASSWD:ALL
where
        {dockermanager} is the username on the remote machine
So do that Machine communicates with the hosts over password-less SSH

To check whether ssh daemon is running and is listening on port 22
sudo service ssh status
sudo netstat -anp | grep sshd
If it don't show the listening on tcp 0.0.0.0:22, we need to install openssh server
sudo apt-get update
sudo apt-get install openssh-server



[Local Machine]
To generate a public/private keys with empty passphrase (# 4)
$ ssh-keygen -t rsa
where
id_rsa.pub (RSA public key for authentication) file is the public key
id_rsa (RSA authentication identity of the user) file is the private key
In bash shell, the SSH key will be saved on ~/.ssh on linux platform

To copy a public key from local machine to a remote machine
$ ssh-copy-id -i ~/.ssh/id_rsa.pub {dockermanager}@{docker host}
where
{dockermanager} is the username on the remote machine
{docker host} is the ip address or host name of the remote machine

To connect the remote machine by using the SSH key
$ ssh {dockermanager}@{docker host}

To install Docker Client Tool
Until now, there is no docker client-only debs/rpms binary so that we need to do something for getting the individual binary. That is download the docker latest release next to extract it and execute docker command directly.


To install Docker Machine (# 5)
$ curl -L https://github.com/docker/machine/releases/download/v0.9.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine

$ chmod +x /tmp/docker-machine

$ sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

To check docker machine version
$ docker-machine version

To create the remote docker-vm using an existing VM/Host with SSH
$ docker-machine create \
 --driver generic \
 --generic-ip-address={remote docker host ip} \
 --generic-ssh-key ~/.ssh/id_rsa \
 --generic-ssh-user {dockermanager} \
 --generic-ssh-port 22 \
 --generic-engine-port 2376 \
{remote-docker-host}

To check which docker engine is active now
$ docker-machine ls

To print out the detail about the docker client to use the remote docker engine
$ docker-machine env {remote-docker-host}

To connect your shell to the new machine
$ eval $(docker-machine env {remote-docker-host})

To create a docker container on the remote docker host
$ docker run -d -p 8080:80 --name httpserver nginx
so do that any docker command you type at this command prompt will be executed on that remote host

Reference:
(# 1) How to prove that docker engine never is installed on this computer? To switch to the command path next to run $./docker ps command
(# 2) Installing net-tools package on Ubuntu 16.04 (Xenial Xerus) by running the following command on terminal
         $ sudo apt-get update
         $ sudo apt-get install net-tools
(# 3) We can change the account's password by running the following command on terminal
         $ sudo passwd {dockermanager}
(# 4)  If have already set a password for SSH passphrase, we have to change it by the following command
          To change directory to $HOME/.ssh         $ cd ~/.ssh/
          To change RSA passphrase                       $ ssh-keygen -f id_rsa -p
(# 5)  We can download the release version of docker machine from docker/machine release page.
(# 6)  Install Docker Machine https://docs.docker.com/machine/install-machine/
          Machine Driver Generic https://docs.docker.com/machine/drivers/generic/
Since 2010 Design by Davidwa
©Copyright Davidwa Inc. All rights reserved.