Macのローカル環境(VirtualBox+Vagrant)でRancherを動かしてみる

作業用のMacのローカルのVirtualBox上に動作試験用のRancherを立ち上げてみたときのメモ。

#環境
MacOS Sierra 10.12.8
VirtualBox 5.1.28 r117968
Vagrant 1.9.1
Ubuntu 14.04.5 LTS

#VMの作成と起動
当初以下の記事を参考にしてRancherOSのVMでやろうと思ったが、複数台のRancherOSのVMのネットワーク設定とかがわからなかったためゲストOSはUbuntuに軌道修正。

[VagrantでRancherOSをVirtualBox/AWS上に立ち上げる](https://qiita.com/yanoshi/items/816e1c8bc97fd8d426b7)

Vagrantファイルをおく作業ディレクトリを作成します。

```text:作業ディレクトリの作成
$ mkdir ubuntu 
$ cd ubuntu 
```

Vagrantファイルも上記記事のものを参考にしつつ、Server用とHost用のVMを二つ作成するようにする。

```yaml:Vagrantファイル
agrant.configure(2) do |config|

  config.vm.box = "ubuntu/trusty64"
#  config.vm.guest = :linux

  config.ssh.forward_agent = true

  config.vm.define "server" do |server|
    server.vm.hostname = "rancher-server"
    server.vm.network "forwarded_port", guest: 8080, host: 8080
    server.vm.network "private_network", ip: "192.168.33.10", virtualbox__intnet: "test_net"

    server.vm.provider :virtualbox do |vb|
      vb.customize [
        "modifyvm", :id,
        "--hwvirtex", "on",
        "--nestedpaging", "on",
        "--largepages", "on",
        "--memory", "2048",
        "--cpus", "2",
        "--ioapic", "on",
        "--pae", "on",
        "--paravirtprovider", "kvm"
      ]
    end
  end

  config.vm.define "host" do |host|
    host.vm.hostname = "rancher-host"
    host.vm.network "forwarded_port", guest: 3000, host: 1234
    host.vm.network "forwarded_port", guest: 8088, host: 18088
    host.vm.network "private_network", ip: "192.168.33.11", virtualbox__intnet: "test_net"

    host.vm.provider :virtualbox do |vb|
      vb.customize [
        "modifyvm", :id,
        "--hwvirtex", "on",
        "--nestedpaging", "on",
        "--largepages", "on",
        "--memory", "2048",
        "--cpus", "2",
        "--ioapic", "on",
        "--pae", "on",
        "--paravirtprovider", "kvm"
      ]
    end
  end
end 
```
VM間をネットワーク接続するためにIPをつけて virtualbox__intnetの設定
Server側のVMはRancherのGUIにアクセスするために8080ポートをフォワーディング
Host側はアプリに応じてアクセスしたいポートをフォワーディング

### 起動してみる
```
$ vagrant up
$ vagrant status server
Current machine states:

server                    running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
$ vagrant status host
Current machine states:

host                      running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
```
### ログインできたらOK
```
$ vagrant ssh server
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-133-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Oct 13 07:22:18 UTC 2017

  System load:  0.16               Users logged in:        0
  Usage of /:   13.0% of 39.34GB   IP address for eth0:    10.0.2.15
  Memory usage: 47%                IP address for eth1:    192.168.33.10
  Swap usage:   0%                 IP address for docker0: 172.17.0.1
  Processes:    104

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

4 packages can be updated.
3 updates are security updates.


Last login: Fri Oct 13 07:22:20 2017 from 10.0.2.2
vagrant@rancher-server:~$
```
```
$ vagrant ssh host
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-133-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Oct 13 08:19:20 UTC 2017

  System load:  0.31              Users logged in:        0
  Usage of /:   8.1% of 39.34GB   IP address for eth0:    10.0.2.15
  Memory usage: 8%                IP address for eth1:    192.168.33.11
  Swap usage:   0%                IP address for docker0: 172.17.0.1
  Processes:    112

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

4 packages can be updated.
3 updates are security updates.

New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Fri Oct 13 07:56:58 2017 from 10.0.2.2
vagrant@rancher-host:~$
```
#Dockerのインストール
VMにログインしてDockerをインストールする。

```text:Dockerのインストール
$ vagrant ssh server
vagrant@rancher-server:~$ sudo apt-get update
........（省略）
Fetched 2,170 kB in 39s (54.5 kB/s)
Reading package lists... Done

# その時の最新Versionを入れるなら以下の通り。
vagrant@rancher-server:~$ curl -fsSL get.docker.com -o get-docker.sh
vagrant@rancher-server:~$ sudo sh get-docker.sh
 
# Versionを指定して入れ直してみる
vagrant@rancher-server:~$ apt-cache madison docker-ce
 docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.07.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.05.0~ce-0~ubuntu-trusty | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
 docker-ce | 17.04.0~ce-0~ubuntu-trusty | https://download.docker.com/linux/ubuntu/ trusty/edge amd64 Packages
vagrant@rancher-server:~$ sudo apt-get install docker-ce=17.06.2~ce-0~ubuntu
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be DOWNGRADED:
  docker-ce
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 10 not upgraded.
Need to get 20.5 MB of archives.
After this operation, 3,277 kB disk space will be freed.
Do you want to continue? [Y/n] y
Fetched 20.5 MB in 6s (3,029 kB/s)
dpkg: warning: downgrading docker-ce from 17.09.0~ce-0~ubuntu to 17.06.2~ce-0~ubuntu
(Reading database ... 64125 files and directories currently installed.)
Preparing to unpack .../docker-ce_17.06.2~ce-0~ubuntu_amd64.deb ...
docker stop/waiting
Unpacking docker-ce (17.06.2~ce-0~ubuntu) over (17.09.0~ce-0~ubuntu) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up docker-ce (17.06.2~ce-0~ubuntu) ...
Installing new version of config file /etc/bash_completion.d/docker ...
docker start/running, process 3287
vagrant@rancher-server:~$ sudo docker version
Client:
 Version:      17.06.2-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   cec0b72
 Built:        Tue Sep  5 20:00:33 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.06.2-ce
 API version:  1.30 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   cec0b72
 Built:        Tue Sep  5 19:59:26 2017
 OS/Arch:      linux/amd64
 Experimental: false

```
完了！

#Rancherのインストールと起動
VMにログインしてRancherをインストールする。簡単。

```text:Rancherのインストール
$ vagrant ssh server
vagrant@rancher-server:~$ sudo docker run -d --restart=unless-stopped -p 8080:8080 rancher/server
```

#ホストの追加
###GUIにアクセスしてIP設定
http://localhost:8080/admin/settings
![rancher-setting.jpg](https://qiita-image-store.s3.amazonaws.com/0/68086/44a5be46-bdb3-fe34-bb5e-de2f400bade6.jpeg)

VM間でアクセスできるIPを設定する。
Rancher ServerはVM作成時に設定した以下のIP。(192.168.33.10)
`
 server.vm.network "private_network", ip: "192.168.33.10", virtualbox__intnet: "test_net"
`
###Host Add
メニューから

* INFRASTRUCTURE -> Hosts
* AddHost 
* Customを選択

![addhost.jpg](https://qiita-image-store.s3.amazonaws.com/0/68086/cf6a98ad-7147-3356-b9a6-7687645ed16d.jpeg)

###ホスト側でAgentのインストール

```text:Agentのインストール
$ vagrant ssh host
#上の図の5.Copy, paste, and run the command below to register the host with Rancher:　のコマンドをコピーして実行
vagrant@rancher-host:~$ sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.6 http://192.168.33.10:8080/v1/scripts/644EAF3EB7D999CA807B:1483142400000:G2pVyuGufDHaM5mMv6VKJ7E4ZA
Unable to find image 'rancher/agent:v1.2.6' locally
v1.2.6: Pulling from rancher/agent
b3e1c725a85f: Pull complete
6a710864a9fc: Pull complete
d0ac3b234321: Pull complete
87f567b5cf58: Pull complete
063e24b217c4: Pull complete
d0a3f58caef0: Pull complete
16914729cfd3: Pull complete
2ce3828c0b9d: Pull complete
7df47a98fc4b: Pull complete
Digest: sha256:a68afd351c7417e6d66a77e97144113ceb7a9c3cdd46fb6e1fd5f5a5a33111cd
Status: Downloaded newer image for rancher/agent:v1.2.6

INFO: Running Agent Registration Process, CATTLE_URL=http://192.168.33.10:8080/v1
INFO: Attempting to connect to: http://192.168.33.10:8080/v1
INFO: http://192.168.33.10:8080/v1 is accessible
INFO: Inspecting host capabilities
INFO: Boot2Docker: false
INFO: Host writable: true
INFO: Token: xxxxxxxx
INFO: Running registration
INFO: Printing Environment
INFO: ENV: CATTLE_ACCESS_KEY=A2A1E9B6F30DA55157E8
INFO: ENV: CATTLE_HOME=/var/lib/cattle
INFO: ENV: CATTLE_REGISTRATION_ACCESS_KEY=registrationToken
INFO: ENV: CATTLE_REGISTRATION_SECRET_KEY=xxxxxxx
INFO: ENV: CATTLE_SECRET_KEY=xxxxxxx
INFO: ENV: CATTLE_URL=http://192.168.33.10:8080/v1
INFO: ENV: DETECTED_CATTLE_AGENT_IP=192.168.33.11
INFO: ENV: RANCHER_AGENT_IMAGE=rancher/agent:v1.2.6
INFO: Launched Rancher Agent: 622dbcf97f440538d8613123990ba05fe75903ca3be076172306e7568e26ed66
```

ホストの追加ができた！

![hostadded.jpg](https://qiita-image-store.s3.amazonaws.com/0/68086/456548b7-79d3-4f04-28a7-ec7c485e134d.jpeg)

#所感
今更ながらローカル環境で簡単に動いて確認できると色々楽チンになれますね。

#参考文献
[VagrantでRancherOSをVirtualBox/AWS上に立ち上げる](https://qiita.com/yanoshi/items/816e1c8bc97fd8d426b7)
[Get Docker CE for Ubuntu](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/)