Docker-machine是什么?
ocker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。
Docker 与 Docker Machine 的区别
Docker 是一个 Client-Server 架构的应用,Docker是大家对Docker Engine简称。
Docker包括以下三个部分:
1、Docker daemon
2、一套与 Docker daemon 交互的 REST API
3、一个命令行客户端
下图很清晰的展示了它们之间的关系:
Docker Machine 是安装和管理 Docker 的工具。它有命令行工具:docker-machine。
docker-machine安装
<code>[root@master?~]#?curl?-L?https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname?-s`-`uname?-m`?>/tmp/docker-machine<br/>??%?Total????%?Received?%?Xferd??Average?Speed???Time????Time?????Time??Current<br/>?????????????????????????????????Dload??Upload???Total???Spent????Left??Speed<br/>100???617????0???617????0?????0????390??????0?--:--:--??0:00:01?--:--:--???390<br/>100?26.7M??100?26.7M????0?????0??1618k??????0??0:00:16??0:00:16?--:--:--?3622k<br/>[root@master?~]#?chmod?+x?/tmp/docker-machine?<br/>[root@master?~]#?cp?/tmp/docker-machine?/usr/local/bin/docker-machine<br/>[root@master?~]#?ll?/usr/local/bin/docker-machine?<br/>-rwxr-xr-x?1?root?root?28034848?6月??12?15:24?/usr/local/bin/docker-machine<br/>[root@master?~]#?docker-machine?-v<br/>docker-machine?version?0.14.0,?build?89b8332<br/></code>
docker-machine命令
<code>[root@master?~]#?docker-machine?--help<br/>Usage:?docker-machine?[OPTIONS]?COMMAND?[arg...]<br/><br/>Create?and?manage?machines?running?Docker.<br/><br/>Version:?0.14.0,?build?89b8332<br/><br/>Author:<br/>??Docker?Machine?Contributors?-?<https://github.com/docker/machine><br/><br/>Options:<br/>??--debug,?-D????????????????????Enable?debug?mode<br/>??--storage-path,?-s?"/root/.docker/machine"????Configures?storage?path?[$MACHINE_STORAGE_PATH]<br/>??--tls-ca-cert?????????????????CA?to?verify?remotes?against?[$MACHINE_TLS_CA_CERT]<br/>??--tls-ca-key?????????????????????Private?key?to?generate?certificates?[$MACHINE_TLS_CA_KEY]<br/>??--tls-client-cert?????????????????Client?cert?to?use?for?TLS?[$MACHINE_TLS_CLIENT_CERT]<br/>??--tls-client-key?????????????????Private?key?used?in?client?TLS?auth?[$MACHINE_TLS_CLIENT_KEY]<br/>??--github-api-token?????????????????Token?to?use?for?requests?to?the?Github?API?[$MACHINE_GITHUB_API_TOKEN]<br/>??--native-ssh????????????????????Use?the?native?(Go-based)?SSH?implementation.?[$MACHINE_NATIVE_SSH]<br/>??--bugsnag-api-token?????????????????BugSnag?API?token?for?crash?reporting?[$MACHINE_BUGSNAG_API_TOKEN]<br/>??--help,?-h????????????????????show?help<br/>??--version,?-v????????????????????print?the?version<br/><br/>Commands:<br/>??active????????Print?which?machine?is?active<br/>??config????????Print?the?connection?config?for?machine<br/>??create????????Create?a?machine<br/>??env????????????Display?the?commands?to?set?up?the?environment?for?the?Docker?client<br/>??inspect????????Inspect?information?about?a?machine<br/>??ip????????????Get?the?IP?address?of?a?machine<br/>??kill????????????Kill?a?machine<br/>??ls????????????List?machines<br/>??provision????????Re-provision?existing?machines<br/>??regenerate-certs????Regenerate?TLS?Certificates?for?a?machine<br/>??restart????????Restart?a?machine<br/>??rm????????????Remove?a?machine<br/>??ssh????????????Log?into?or?run?a?command?on?a?machine?with?SSH.<br/>??scp????????????Copy?files?between?machines<br/>??mount????????????Mount?or?unmount?a?directory?from?a?machine?with?SSHFS.<br/>??start????????????Start?a?machine<br/>??status????????Get?the?status?of?a?machine<br/>??stop????????????Stop?a?machine<br/>??upgrade????????Upgrade?a?machine?to?the?latest?version?of?Docker<br/>??url????????????Get?the?URL?of?a?machine<br/>??version????????Show?the?Docker?Machine?version?or?a?machine?docker?version<br/>??help????????????Shows?a?list?of?commands?or?help?for?one?command<br/><br/>docker-machine?active<br/>#显示当前的活动主机<br/><br/>docker-machine?config<br/>#显示连接主机的配置<br/><br/>docker-machine?create<br/>#创建一个主机<br/><br/>docker-machine?env<br/>#设置当前的环境与哪个主机通信<br/><br/>docker-machine?inspect<br/>#查看主机的详细信息<br/><br/>docker-machine?ip<br/>#查看主机的IP?<br/><br/>docker-machine?kill<br/>#强制关闭一个主机<br/><br/>docker-machine?ls?<br/>#查看所有的主机信息<br/><br/>docker-machine?provision<br/>#重新配置现在主机<br/><br/>docker-machine?regenerate-certs<br/>#为主机重新生成证书<br/><br/>docker-machine?restart<br/>#重启主机<br/><br/>docker-machine?rm<br/>#删除主机<br/><br/>docker-machine?ssh<br/>#以SSH的方式连接到主机上<br/><br/>docker-machine?scp<br/>#远程复制<br/><br/>docker-machine?status<br/>#查看主机的状态<br/><br/>docker-machine?stop<br/>#停止一个正在运行的主机<br/><br/>docker-machine?upgrade<br/>#升级主机的docker服务到最新版本<br/><br/>docker-machine?version<br/>#查看docker-machine版本<br/></code>
常用实例操作
<code>[root@master?~]#?docker-machine?create?-d?virtualbox?testhost<br/>[root@master?~]#?docker-machine?create?--driver?virtualbox?testhost<br/>#两个命令的作用相同,创建一个名为testhost的主机,驱动方式是virtualbox<br/><br/>[root@master?~]#?docker-machine?create?-d?virtualbox?testhost<br/>Creating?CA:?/root/.docker/machine/certs/ca.pem<br/>Creating?client?certificate:?/root/.docker/machine/certs/cert.pem<br/>Running?pre-create?checks...<br/>Error?with?pre-create?check:?"VBoxManage?not?found.?Make?sure?VirtualBox?is?installed?and?VBoxManage?is?in?the?path"<br/>#报错提示没有发现VBoxManage。因此,需要手工安装,具体安装操作如下。<br/></code>
安装Virtualbox驱动
<code>1、配置Yum源<br/>[root@master?~]#?vim??/etc/yum.repos.d/virtualbox.repo?<br/>[virtualbox]<br/>name=Oracle?Linux?/?RHEL?/?CentOS-$releasever?/?$basearch?-?VirtualBox<br/>baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch<br/>enabled=1<br/>gpgcheck=0<br/>repo_gpgcheck=0<br/>gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc<br/><br/>2、查看可安装的版本<br/>[root@master?~]#?yum?search?VirtualBox???#查找具体安装版本<br/>已加载插件:fastestmirror<br/>Loading?mirror?speeds?from?cached?hostfile<br/>?*?base:?mirrors.163.com<br/>?*?epel:?mirrors.ustc.edu.cn<br/>?*?extras:?centos.ustc.edu.cn<br/>?*?updates:?centos.ustc.edu.cn<br/>==============?N/S?matched:?VirtualBox?===================================<br/>VirtualBox-4.3.x86_64?:?Oracle?VM?VirtualBox<br/>VirtualBox-5.0.x86_64?:?Oracle?VM?VirtualBox<br/>VirtualBox-5.1.x86_64?:?Oracle?VM?VirtualBox<br/>VirtualBox-5.2.x86_64?:?Oracle?VM?VirtualBox<br/>[root@master?~]#?yum?install?-y?VirtualBox-5.2??#安装<br/><br/>3、加载virtualbox<br/>[root@master?~]#?/sbin/vboxconfig???#重新加载virtualbox服务<br/>vboxdrv.sh:?Stopping?VirtualBox?services.<br/>vboxdrv.sh:?Building?VirtualBox?kernel?modules.<br/>This?system?is?currently?not?set?up?to?build?kernel?modules.<br/>Please?install?the?Linux?kernel?"header"?files?matching?the?current?kernel<br/>for?adding?new?hardware?support?to?the?system.<br/>The?distribution?packages?containing?the?headers?are?probably:<br/>????kernel-devel?kernel-devel-3.10.0-693.el7.x86_64<br/>This?system?is?currently?not?set?up?to?build?kernel?modules.<br/>Please?install?the?Linux?kernel?"header"?files?matching?the?current?kernel<br/>for?adding?new?hardware?support?to?the?system.<br/>The?distribution?packages?containing?the?headers?are?probably:<br/>????kernel-devel?kernel-devel-3.10.0-693.el7.x86_64<br/><br/>There?were?problems?setting?up?VirtualBox.??To?re-start?the?set-up?process,?run<br/>??/sbin/vboxconfig<br/>as?root.<br/>#如果内核版本不一致,会出现上面的报错,需要安装相同的内核版本<br/><br/>4、安装对应的内核版本<br/>[root@master?~]#?rpm?-ivh?kernel-devel-3.10.0-693.el7.x86_64.rpm?<br/>准备中...??????????????????????????#################################?[100%]<br/>正在升级/安装...<br/>???1:kernel-devel-3.10.0-693.el7???#################################?[100%]<br/>[root@slave1?~]#?yum?install?gcc?make?perl?-y<br/>[root@slave1?~]#?rpm?-qa?kernel*<br/>kernel-tools-3.10.0-693.el7.x86_64<br/>kernel-devel-3.10.0-693.el7.x86_64<br/>kernel-tools-libs-3.10.0-693.el7.x86_64<br/>kernel-3.10.0-693.el7.x86_64<br/>kernel-headers-3.10.0-862.3.2.el7.x86_64<br/>[root@slave1?~]#?/sbin/vboxconfig?<br/>vboxdrv.sh:?Stopping?VirtualBox?services.<br/>vboxdrv.sh:?Building?VirtualBox?kernel?modules.<br/>vboxdrv.sh:?Starting?VirtualBox?services.<br/></code>
重新安装
<code>[root@centos7?~]#?docker-machine?create?--driver?virtualbox?testhost<br/>Running?pre-create?checks...<br/>Error?with?pre-create?check:?"This?computer?doesn't?have?VT-X/AMD-v?enabled.?Enabling?it?in?the?BIOS?is?mandatory"<br/>#这个报错不用解释了吧,没有开启虚拟化功能,直接打开<br/>[root@centos7?~]#?docker-machine?create?--driver?virtualbox?default<br/>Running?pre-create?checks...<br/>(default)?No?default?Boot2Docker?ISO?found?locally,?downloading?the?latest?release...<br/>(default)?Latest?release?for?github.com/boot2docker/boot2docker?is?v18.05.0-ce<br/>(default)?Downloading?/root/.docker/machine/cache/boot2docker.iso?from?https://github.com/boot2docker/boot2docker/releases/download/v18.05.0-ce/boot2docker.iso...<br/>(default)?0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%<br/>Creating?machine...<br/>(default)?Copying?/root/.docker/machine/cache/boot2docker.iso?to?/root/.docker/machine/machines/default/boot2docker.iso...<br/>(default)?Creating?VirtualBox?VM...<br/>(default)?Creating?SSH?key...<br/>(default)?Starting?the?VM...<br/>(default)?Check?network?to?re-create?if?needed...<br/>(default)?Found?a?new?host-only?adapter:?"vboxnet0"<br/>(default)?Waiting?for?an?IP...<br/>Waiting?for?machine?to?be?running,?this?may?take?a?few?minutes...<br/>Detecting?operating?system?of?created?instance...<br/>Waiting?for?SSH?to?be?available...<br/>Detecting?the?provisioner...<br/>Provisioning?with?boot2docker...<br/>Copying?certs?to?the?local?machine?directory...<br/>Copying?certs?to?the?remote?machine...<br/>Setting?Docker?configuration?on?the?remote?daemon...<br/>Checking?connection?to?Docker...<br/>Docker?is?up?and?running!<br/>To?see?how?to?connect?your?Docker?Client?to?the?Docker?Engine?running?on?this?virtual?machine,?run:?docker-machine?env?default<br/>#可以先安装个默认的虚拟机<br/>[root@centos7?~]#?docker-machine?ls<br/>NAME??????ACTIVE???DRIVER???????STATE?????URL?????????????????????????SWARM???DOCKER????????ERRORS<br/>default???-????????virtualbox???Running???tcp://192.168.99.100:2376???????????v18.05.0-ce???<br/>[root@centos7?~]#?docker-machine?status<br/>Running<br/></code>
接下来配置环境变量,方便后面直接操作主机
<code>[root@centos7?~]#?docker-machine?env?default<br/>export?DOCKER_TLS_VERIFY="1"<br/>export?DOCKER_HOST="tcp://192.168.99.100:2376"<br/>export?DOCKER_CERT_PATH="/root/.docker/machine/machines/default"<br/>export?DOCKER_MACHINE_NAME="default"<br/>#?Run?this?command?to?configure?your?shell:?<br/>#?eval?$(docker-machine?env?default)<br/>[root@centos7?~]#??eval?$(docker-machine?env?default)<br/>#连接虚拟主机<br/>[root@centos7?~]#?docker-machine?ssh?default<br/>????????????????????????##?????????.<br/>??????????????????##?##?##????????==<br/>???????????????##?##?##?##?##????===<br/>???????????/"""""""""""""""""___/?===<br/>??????~~~?{~~?~~~~?~~~?~~~~?~~~?~?/??===-?~~~<br/>???????????______?o???????????__/<br/>??????????????????????????__/<br/>??????????????___________/<br/>?_?????????????????_???____?????_????????????_<br/>|?|__???___???___?|?|_|___??__|?|?___???___|?|?_____?_?__<br/>|?'_??/?_??/?_?|?__|?__)?/?_`?|/?_??/?__|?|/?/?_??'__|<br/>|?|_)?|?(_)?|?(_)?|?|_?/?__/?(_|?|?(_)?|?(__|???<??__/?|<br/>|_.__/?___/?___/?__|_______,_|___/?___|_|____|_|<br/>Boot2Docker?version?18.05.0-ce,?build?HEAD?:?b5d6989?-?Thu?May?10?16:35:28?UTC?2018<br/>Docker?version?18.05.0-ce,?build?f150324<br/>docker@default:~$?docker?--version<br/>Docker?version?18.05.0-ce,?build?f150324<br/>#正常进入虚拟机<br/></code>实例
两台服务器
本地主机:192.168.22.177
远程主机:192.168.22.175
可能大家还是不太明白,这个docker-machine到底有什么用?docker容器的好处都知道了。而docker-machine可以在本地布署相应环境的同时完成远程docker主机相同环境的布署,减少重复的操作。
1、配置免密登录
<code>[root@test001?~]#?ssh-keygen?-t?rsa<br/>Generating?public/private?rsa?key?pair.<br/>Enter?file?in?which?to?save?the?key?(/root/.ssh/id_rsa):?<br/>Created?directory?'/root/.ssh'.<br/>Enter?passphrase?(empty?for?no?passphrase):?<br/>Enter?same?passphrase?again:?<br/>Your?identification?has?been?saved?in?/root/.ssh/id_rsa.<br/>Your?public?key?has?been?saved?in?/root/.ssh/id_rsa.pub.<br/>The?key?fingerprint?is:<br/>SHA256:7Ue0bc08pEs+PonJqy7/hyxUhNO4uEegVizX4EJC8J8?root@centos7.3<br/>The?key's?randomart?image?is:<br/>+---[RSA?2048]----+<br/>|?.oo?...o?+??????|<br/>|??.?o..=?=?o?????|<br/>|???.?.=.o?+?.??.?|<br/>|????.oo.?+?o?oo+?|<br/>|????.E??S?o?ooo.+|<br/>|???????.?+?.o..?.|<br/>|????????o?+?=+.??|<br/>|???????.?.?B.+.??|<br/>|????????+++oo..??|<br/>+----[SHA256]-----+<br/>[root@centos7?~]#?ssh-copy-id?192.168.22.175<br/>/usr/bin/ssh-copy-id:?INFO:?Source?of?key(s)?to?be?installed:?"/root/.ssh/id_rsa.pub"<br/>The?authenticity?of?host?'192.168.22.175?(192.168.22.175)'?can't?be?established.<br/>ECDSA?key?fingerprint?is?SHA256:p6+FPeTxTUx37cwJWJP8cUE9NhcUHSvAppVPyj4aj8c.<br/>ECDSA?key?fingerprint?is?MD5:89:6d:f7:46:11:45:2e:fd:21:87:42:bd:62:06:fe:fd.<br/>Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes<br/>/usr/bin/ssh-copy-id:?INFO:?attempting?to?log?in?with?the?new?key(s),?to?filter?out?any?that?are?already?installed<br/>/usr/bin/ssh-copy-id:?INFO:?1?key(s)?remain?to?be?installed?--?if?you?are?prompted?now?it?is?to?install?the?new?keys<br/>root@192.168.22.175's?password:?<br/><br/>Number?of?key(s)?added:?1<br/><br/>Now?try?logging?into?the?machine,?with:???"ssh?'192.168.22.175'"<br/>and?check?to?make?sure?that?only?the?key(s)?you?wanted?were?added.<br/><br/>[root@test001?~]#?ssh?192.168.22.175<br/>Last?login:?Tue?Apr?24?06:51:06?2018?from?192.168.22.170<br/>[root@test002?~]#?ip?add?|grep?192.168.22<br/>????inet?192.168.22.175/24?brd?192.168.22.255?scope?global?ens32<br/></code>
<code><br/></code>
2、创建远程主机
远程主机需要安装有docker环境
<code>[root@test001?~]#?docker-machine?create?-d?generic?--generic-ip-address=192.168.22.175?--generic-ssh-user=root?--engine-registry-mirror?http://ef017c13.m.daocloud.io???dockerhost<br/>Running?pre-create?checks...<br/>Creating?machine...<br/>(dockerhost)?No?SSH?key?specified.?Assuming?an?existing?key?at?the?default?location.<br/>Waiting?for?machine?to?be?running,?this?may?take?a?few?minutes...<br/>Detecting?operating?system?of?created?instance...<br/>Waiting?for?SSH?to?be?available...<br/>Detecting?the?provisioner...<br/>Provisioning?with?centos...<br/>Copying?certs?to?the?local?machine?directory...<br/>Copying?certs?to?the?remote?machine...<br/>Setting?Docker?configuration?on?the?remote?daemon...<br/>Checking?connection?to?Docker...<br/>Docker?is?up?and?running!<br/>To?see?how?to?connect?your?Docker?Client?to?the?Docker?Engine?running?on?this?virtual?machine,?run:?docker-machine?env?dockerhost<br/>[root@test001?~]#?docker-machine?ls<br/>NAME?????????ACTIVE???DRIVER???????STATE?????URL?????????????????????????SWARM???DOCKER????ERRORS<br/>dockerhost???-????????generic??????Running???tcp://192.168.22.175:2376???????????v1.13.1???<br/></code>
3、配置环境变量
<code>[root@centos7?~]#?docker-machine?env?dockerhost<br/>export?DOCKER_TLS_VERIFY="1"<br/>export?DOCKER_HOST="tcp://192.168.22.175:2376"<br/>export?DOCKER_CERT_PATH="/root/.docker/machine/machines/dockerhost"<br/>export?DOCKER_MACHINE_NAME="dockerhost"<br/>#?Run?this?command?to?configure?your?shell:?<br/>#?eval?$(docker-machine?env?dockerhost)<br/>[root@test001?~]#?eval?$(docker-machine?env?dockerhost)<br/>[root@test001?~]#?docker-machine?ssh?dockerhost<br/>Last?login:?Thu?Jun?14?02:32:42?2018?from?192.168.22.177<br/>[root@dockerhost?~]#?docker?--version<br/>Docker?version?1.13.1,?build?94f4240/1.13.1<br/></code>
4、运行一个容器
<code>[root@test001?~]#?docker?run?-d?nginx:1.13<br/>Unable?to?find?image?'nginx:1.13'?locally<br/>Trying?to?pull?repository?docker.io/library/nginx?...?<br/>sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35:?Pulling?from?docker.io/library/nginx<br/>f2aa67a397c4:?Pull?complete?<br/>3c091c23e29d:?Pull?complete?<br/>4a99993b8636:?Pull?complete?<br/>Digest:?sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35<br/>Status:?Downloaded?newer?image?for?docker.io/nginx:1.13<br/>72efb659ec38d263519c894bf0b5eb3d5ca35af1e3d0e9522abbcc19d8739403<br/>[root@test001?~]#?docker?image?ls<br/>REPOSITORY??????????TAG?????IMAGE?ID???????CREATED???????SIZE<br/>docker.io/nginx?????1.13????ae513a47849c???6?weeks?ago???109?MB<br/>[root@test001?~]#?docker-machine?ssh?dockerhost<br/>Last?login:?Thu?Jun?14?02:58:51?2018?from?192.168.22.177<br/>[root@dockerhost?~]#?docker?image?ls<br/>REPOSITORY??????????TAG??????IMAGE?ID??????CREATED???????SIZE<br/>docker.io/nginx?????1.13?????ae513a47849c??6?weeks?ago???109?MB<br/>#由此可以发现,利用docker-machine可以减少重复操作,便于环境的创建<br/></code>
关于Docker容器技术的基础知识点,可以阅读下面的文章 :
推荐阅读
·end·
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!