星期六, 4月 07, 2018

Docker-machine 與 digital ocean 小記

Docker-machine 與 digital ocean 小記


OS:  openSUSE Leap 42.3


因為讀書會的關係, 進入 Chapter 5 docker-machine
所以接下來進行 docker-machine 實驗


就目前的了解, 就是可以使用 docker-machine 來進行本機或是遠端的docker 安裝.
Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands.
You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.




( 謎之音: 但是這個部份好像你都是用 ansible 解決掉了 ... )


參考官方網站


先來進行 docker-machine 的安裝
Docker-machine 官方 github, 目前是 0.14 版


下載 docker-machine
# curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >  /tmp/docker-machine


  • uname 指令, 上面兩個指令結果會跑出 Linux 以及 x86_64
    • -s, --kernel-name        print the kernel name
    • -m, --machine            print the machine hardware name


使用 install 指令將 docker-machine 複製到 /usr/local/bin 下
# install    /tmp/docker-machine  /usr/local/bin/docker-machine


這樣就安裝完成了


還沒開始之前先觀察一下


# docker-machine   ls
NAME   ACTIVE  DRIVER STATE   URL SWARM DOCKER   ERRORS


接下來就是 docker-machine 與 digital ocean 的實驗


開始之前, 必須先要有 digital ocean 的 API, 可以參考


接下來就可以測試 docker-machine 指令了


# docker-machine  create  --driver digitalocean  --digitalocean-access-token 599122321a3b75aea39f07c3XXXXXXXXX52911013806421589763bdd667e511 docker-machine-test


Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(docker-machine-test) Creating SSH key...
(docker-machine-test) Creating Digital Ocean droplet...
(docker-machine-test) Waiting for IP address to be assigned to the Droplet...
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 ubuntu(systemd)...
Installing Docker...
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 docker-machine-test


  • 重點在 --driver 選擇要建立的 host 種類
  • 會在 digital ocean 建立一個 droplet 名稱為 docker-machine-test
  • 看他的行為是透過 SSH 與憑證來控管


可以在 Digital Ocean 的 dashboard 上面觀察




再次觀察


# docker-machine   ls
NAME                  ACTIVE DRIVER  STATE URL              SWARM DOCKER ERRORS
docker-machine-test   - digitalocean  Running tcp://XXX.YYY.ZZ.WWW:2376           v18.03.0-ce   


觀察 Host IP
# docker-machine  ip docker-machine-test


查看相關資訊
# docker-machine   inspect  docker-machine-test


  • 這邊可能要注意的是 docker-machine 會把相關的資訊存在 ~/.docker/machine/machines/主機名稱/config.json


接下來使用 docker-machine  env 來觀察相關環境變數


# docker-machine  env docker-machine-test
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST=" tcp://XXX.YYY.ZZ.WWW:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/docker-machine-test"
export DOCKER_MACHINE_NAME="docker-machine-test"
# Run this command to configure your shell:
# eval $(docker-machine env docker-machine-test)


按照上面建議的 eval $(docker-machine env docker-machine-test) 去執行相關變數


# eval   $(docker-machine  env docker-machine-test)


觀察相關資訊
# echo   $DOCKER_TLS_VERIFY
1


# echo $DOCKER_HOST
tcp://XXX.YYY.ZZ.WWW:2376


# echo $DOCKER_MACHINE_NAME
docker-machine-test


這個時候如果下 docker  images 就會發現已經是在遠端 droplet 了 ( 因為是空的 …. )


# docker  images
REPOSITORY          TAG IMAGE ID            CREATED SIZE


進行相關測試


# docker  run   -d  -p  80:80  --name   demo  yeasy/simple-web


開啟 Droplet 的網頁觀察




成功 :)


接下來練習  docker-machine  stop 停止 droplet
# docker-machine  stop  docker-machine-test


Stopping "docker-machine-test"...
Machine "docker-machine-test" was stopped.


在 Digital Ocean Dashboard 觀察


# docker-machine  rm  docker-machine-test
About to remove docker-machine-test
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed docker-machine-test


  • 會把 ~/.docker/machine/machines 下面的主機相關設定移除
  • 也會移除 Digital Ocean 上面的 Droplet


好了, 可以繼續看第五章了
~ enjoy  it


Reference


沒有留言: