1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant ansible で Minikube 環境を構築する

Posted at

Kubernetes を試す環境をローカルに欲しいため、Vagrant ansible で Minikube 環境を構築します。
ホストOSは Windows10 です。
Vagrant ansible で VirtualBox 上に Ubuntu18.04 を構築し、そこに Docker, Minikube をインストールします。

ファイル・ディレクトリ構成

用意するのは3つのファイルになります。

├─ ansible
│  ├─ mykube.yml
│  └─ hosts
└─ Vagrantfile
1. Vagrantfile

参考: Shared Ansible Options

Vagrantfile
# Vagrantfile
# coding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # mykube vm
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = 'mykube'
  config.vm.network :private_network,ip: "192.168.62.10"
  config.vm.provider "virtualbox" do |vbox|
    vbox.gui = false
    vbox.name = "Ubuntu18.04-mykube"
    vbox.cpus = 2
    vbox.memory = 4096
    vbox.customize [
      "modifyvm", :id,
      "--vram", "256", # video memory for full screen
      "--clipboard", "bidirectional", # sharing clipboard
      "--draganddrop", "bidirectional", # enable drag & drop
      "--cpus", "2", # 2 cpu
      "--ioapic", "on" # enable I/O APIC
    ]
  end

  # ansible
  config.vm.synced_folder "./ansible", "/ansible"
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook       = "/ansible/mykube.yml"
    ansible.version        = "latest"
    ansible.verbose        = false  # not show detail
    ansible.limit          = "mykube"
    ansible.inventory_path = "/ansible/hosts"
  end
end
2. mykube.yml
mykube.yml
- hosts: mykube
  connection: local
  become: yes
  gather_facts: true

  tasks:

  ## Install Docker CE
  - name: Add Docker GPG key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg

  - name: Add Docker APT repository
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable

  - name: Install packages
    apt:
      name: ['apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common']
      state: present
      update_cache: yes

  - name: Install Docker-CE
    apt:
      name: docker-ce=18.06.2~ce~3-0~ubuntu
      state: present
      update_cache: yes

  - name: vagrant user in docker group
    user:
      name: vagrant
      groups: docker

  ## Set sysctl
  - name: Set sysctl
    sysctl:
      name: net.bridge.bridge-nf-call-iptables
      value: 1
      sysctl_set: yes
      sysctl_file: /etc/sysctl.conf
      state: present
      reload: yes

  ## Install kubectl command
  - name: Install kubectl
    get_url:
      url: https://storage.googleapis.com/kubernetes-release/release/v1.13.4/bin/linux/amd64/kubectl
      dest: /usr/local/bin/
      mode: 0755
      owner: root
      group: root

  ## Install minikube
  - name: Install minikube
    get_url:
      url: https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
      dest: /usr/local/bin/minikube
      mode: 0755
      owner: root
      group: root

  ## Start Minikube
  - name: Start Minikube
    command: /usr/local/bin/minikube start --vm-driver none
    args:
      chdir: /root
    register: mini

  ## Stop Minikube
  - name: Stop Minikube
    command: /usr/local/bin/minikube stop

  ## Move .minikube and .kube
  - shell: mv /root/.kube /home/vagrant/.kube
  - shell: chown -R vagrant:vagrant /home/vagrant/.kube

  - shell: mv /root/.minikube /home/vagrant/.minikube
  - shell: chown -R vagrant:vagrant /home/vagrant/.minikube

  - name: Replace path
    replace:
      dest: /home/vagrant/.kube/config
      regexp: '(root|home)'
      replace: vagrant
3. hosts
hosts
mykube ansible_connection=local

構築(vagrant up)

Vagrantfile があるディレクトリで vagrant up します。
ネットワーク通信が遅い環境ということもありますが、完了までに20分以上要しました。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/ubuntu-18.04'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/ubuntu-18.04' version '201906.18.0' is up to date...
(中略)
PLAY RECAP *********************************************************************
mykube                     : ok=18   changed=17   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Box にログインして、minikube を起動してみます。

ログイン
$ vagrant ssh
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-51-generic x86_64)
(中略)
Minikube 起動

各ポッドのステータスが Running になっていることを確認。

$ sudo minikube start
* minikube v1.6.2 on Ubuntu 18.04 (vbox/amd64)
* Selecting 'none' driver from existing profile (alternates: [])
(中略)

$ kubectl get pod -n kube-system
NAME                               READY   STATUS    RESTARTS   AGE
coredns-6955765f44-6rx6l           1/1     Running   0          16s
coredns-6955765f44-cghcv           1/1     Running   0          36m
etcd-minikube                      1/1     Running   0          36m
kube-addon-manager-minikube        1/1     Running   0          36m
kube-apiserver-minikube            1/1     Running   0          36m
kube-controller-manager-minikube   1/1     Running   0          36m
kube-proxy-4qwx9                   1/1     Running   0          36m
kube-scheduler-minikube            1/1     Running   0          36m
storage-provisioner                1/1     Running   0          36m
Minikube 停止
$ sudo minikube stop
* Stopping "minikube" in none ...
* Stopping "minikube" in none ...
* "minikube" stopped.

Minikube 環境が構築できたところまで確認ができました。
ローカルマシンなので、基本的な動作確認などが出来れば良いと考えています。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?