Table of Content

launch ubuntu dev vm

it is based on my dev ready vagrant box dreamcloud/ubuntu17.10.

create project folder

$ mdkir ~/vagrant/ub17k8sbox
$ cd ~/vagrant/ub17k8sbox

prepare the Vagrantfile,

Vagrant.configure("2") do |config|
    config.vm.box="dreamcloud/ubuntu17.10"
    
    config.vm.define "ub17k8s" do |ub17k8s|
        ub17k8s.vm.hostname = "ub17k8s"
        ub17k8s.vm.provider :virtualbox do |vb|
            vb.name="ub17k8s"
            vb.memory=1024
        end
    end
end

then boot up the vm,

$ vagrant up
$ vagrant ssh

reset ssh keypair

The reason I re-init ssh keypair for new vm ub17k8s here, because the keypair has been changed when I launch this vm from vagrant box dreamcloud/ubuntu17.10. It will be failed if we don't reset keypair, vagrant has no idea for this new keypair. It is common practice if we build new vagrant box based on existing box.

vagrant@ub17k8s:$ curl -Lo /home/vagrant/.ssh/authorized_keys \
https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
vagrant@ub17k8s:$ chmod 0600 /home/vagrant/.ssh/authorized_keys

docker CE

$ curl -fsSL get.docker.com -o get-docker.sh
$ sh get-docker.sh
vagrant@ub17k8s:~$ sh get-docker.sh
# Executing docker install script, commit: 1d31602
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sudo -E sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu zesty edge" > /etc/apt/sources.list.d/docker.list
+ [ ubuntu = debian ]
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sudo -E sh -c docker version
Client:
 Version:       17.12.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    c97c6d6
 Built: Wed Dec 27 20:10:45 2017
 OS/Arch:       linux/amd64

Server:
 Engine:
  Version:      17.12.0-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   c97c6d6
  Built:        Wed Dec 27 20:09:19 2017
  OS/Arch:      linux/amd64
  Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker ubuntu

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

add vagrant to docker group

vagrant@ub17k8s:~$ sudo usermod -aG docker vagrant

relogin, then can run docker as normal user,

vagrant@ub17k8s:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

k8s installation

latest release is still kubernetes-xenial, it works for ubuntu 17.10.

sudo apt-get update && sudo apt-get install -y apt-transport-https
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo su -c "echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list"
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni

verify by,

vagrant@ub17k8s:~$ which kubeadm
/usr/bin/kubeadm

vagrant@ub17k8s:~$ which kubectl
/usr/bin/kubectl

defragmentation the vm

sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY

clean up vm

sudo apt-get autoremove
sudo apt-get clean

and clean history,

vagrant@ub17k8s:$ cat /dev/null > ~/.bash_history && history -c

package vagrant box

from local shell, when give vm name ub17k8s we just created, vagrant will compress it as box format,

$ cd ~/vagrant/ub17k8sbox
$ vagrant package --output dreamcloud-ub17k8s-vagrant.box --base ub17k8s
==> ub17k8s: Attempting graceful shutdown of VM...
    ub17k8s: Guest communication could not be established! This is usually because
    ub17k8s: SSH is not running, the authentication information was changed,
    ub17k8s: or some other networking issue. Vagrant will force halt, if
    ub17k8s: capable.
==> ub17k8s: Forcing shutdown of VM...
==> ub17k8s: Clearing any previously set forwarded ports...
==> ub17k8s: Exporting VM...
==> ub17k8s: Compressing package to: C:/oldhorse/portableapps/msys64/home/oldhorse/vagrant/ub17k8s/dreamcloud-ub17k8s-vagrant.box

dreamcloud-ub17k8s-vagrant.box 839M.

test vagrant box

$ cd ~/vagrant/ub17k8sbox
$ vagrant box add dreamcloud/ub17k8s dreamcloud-ub17k8s-vagrant.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dreamcloud/ub17k8s' (v0) for provider:
    box: Unpacking necessary files from: file://C:/oldhorse/portableapps/msys64/home/oldhorse/vagrant/ub17k8s/dreamcloud-ub17k8s-vagrant.box
    box: Progress: 100% (Rate: 127M/s, Estimated time remaining: --:--:--)

$ vagrant box list
$ vagrant init dreamcloud/ub17k8s

create new project folder for test,

$ mkdir ~/vagrant/k8stest
$ cd ~/vagrant/k8stest

prepare test Vagrantfile,

Vagrant.configure("2") do |config|
    config.vm.box="dreamcloud/ub17k8s"

    config.vm.define "ub17k8s-master" do |master|
        master.vm.hostname = "ub17k8s-master"
        master.vm.network :private_network, ip: "10.110.0.15"
        master.vm.network "forwarded_port", guest: 8443, host: 8443, protocol: "tcp"
        master.vm.network "forwarded_port", guest: 30000, host: 30000, protocol: "tcp"
        master.vm.provider :virtualbox do |vb|
            vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
            vb.name="ub17k8s-master"
            vb.memory=1024
        end
    end

    config.vm.define "ub17k8s-node" do |node|
        node.vm.hostname = "ub17k8s-node1"
        node.vm.network :private_network, ip: "10.110.0.16"
        node.vm.provider :virtualbox do |vb|
            vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
            vb.name="ub17k8s-node"
            vb.memory=1024
        end
    end
end

Let’s boot k8s cluster up,

$ vagrant up
$ vagrant status
Current machine states:
ub17k8s-master running (virtualbox)
ub17k8s-node running (virtualbox)