kubeadmを使ったKubernetesの構築方法(Single node構成)

# 目的
- kubeadmを使ったKubernetesのinstall方法まとめ
- network設定はflannelを使用
- masterのみのsingle node構成

# 参考
Ref: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
Ref: https://docs.docker.com/install/linux/docker-ce/ubuntu/
Ref: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

## VagrantでVMを用意
```bash:bash
### step1: Vagrantfileを生成
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study$ vagrant init bento/ubuntu-18.04

### step2: VMのcpu, memory, nwをconfig
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study (master *)$ git diff
diff --git a/Vagrantfile b/Vagrantfile
index 5476260..e8913d0 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -32,7 +32,7 @@ Vagrant.configure("2") do |config|

   # Create a private network, which allows host-only access to the machine
   # using a specific IP.
-  # config.vm.network "private_network", ip: "192.168.33.10"
+  config.vm.network "private_network", ip: "172.24.0.1"

   # Create a public network, which generally matched to bridged network.
   # Bridged networks make the machine appear as another physical device on
@@ -49,13 +49,14 @@ Vagrant.configure("2") do |config|
   # backing providers for Vagrant. These expose provider-specific options.
   # Example for VirtualBox:
   #
-  # config.vm.provider "virtualbox" do |vb|
+  config.vm.provider "virtualbox" do |vb|
   #   # Display the VirtualBox GUI when booting the machine
   #   vb.gui = true
   #
   #   # Customize the amount of memory on the VM:
-  #   vb.memory = "1024"
-  # end
+    vb.cpus = 4
+    vb.memory = "4096"
+  end
   #
   # View the documentation for the provider you are using for more
   # information on available options.
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study (master *)$

### step3: vmを起動
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study (master *)$ vagrant up

### step4: vmにsshでlogin
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study (master *)$ vagrant ssh

```

## kubeadmをinstall
- Ref: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

### Disable SWAP
これを忘れるとkubeletが起動しない。

```bash:bash
vagrant@vagrant:~$ sudo su -
root@vagrant:~# sudo swapoff -a
root@vagrant:~# sudo sed -i '/ swap / s/^/#/' /etc/fstab
```

### Ensure iptables tooling does not use the nftables backend
```bash:bash
vagrant@vagrant:~$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
vagrant@vagrant:~$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
vagrant@vagrant:~$ sudo update-alternatives --set arptables /usr/sbin/arptables-legacy
vagrant@vagrant:~$ sudo update-alternatives --set ebtables /usr/sbin/ebtables-legacy
```
### Installing runtime
runtimeとしてDockerをInstallする。
Ref: https://docs.docker.com/install/linux/docker-ce/ubuntu/

#### Installing Docker

##### Uninstall old versions
```bash:bash
vagrant@vagrant:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
```
##### Install using the repository
```bash:bash
# 1.Update the apt package index:
vagrant@vagrant:~$ sudo apt-get update

# 2.Install packages to allow apt to use a repository over HTTPS:
vagrant@vagrant:~$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-commo

# 3.Add Docker’s official GPG key:
vagrant@vagrant:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 4.Use the following command to set up the stable repository.
vagrant@vagrant:~$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# INSTALL DOCKER ENGINE - COMMUNITY
# 1.Update the apt package index.
vagrant@vagrant:~$ sudo apt-get update

# 2.Install the latest version of Docker Engine - Community and containerd, or go to the next step to install a specific version:
vagrant@vagrant:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io

# 3. Verify that Docker Engine - Community is installed correctly by running the hello-world image.
vagrant@vagrant:~$ sudo docker run hello-world
```
#### Manage Docker as a non-root user
Ref: https://docs.docker.com/install/linux/linux-postinstall/

```bash:bash
# To create the docker group and add your user:
vagrant@vagrant:~$ sudo groupadd docker
vagrant@vagrant:~$ sudo usermod -aG docker $USER
vagrant@vagrant:~$ logout

# re-login
iguchi@igc-mbp:~/my-vagrant/openstack-on-k8s-study (master *)$ vagrant ssh

# verify docker command without sudo
vagrant@vagrant:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
vagrant@vagrant:~$
```

### Installing kubeadm, kubelet and kubectl
```bash:bash
vagrant@vagrant:~$ sudo apt-get update && sudo apt-get install -y apt-transport-https curl
vagrant@vagrant:~$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
vagrant@vagrant:~$ cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
vagrant@vagrant:~$ sudo apt-get update
vagrant@vagrant:~$ sudo apt-get install -y kubelet kubeadm kubectl
vagrant@vagrant:~$ sudo apt-mark hold kubelet kubeadm kubectl
```

## Installing Kubernetes
NetworkはFlannelを使うことにする。

```bash:bash
vagrant@vagrant:~$ sudo su -
# install kubernetes
# x.x.x.xはapi-serverが待ち受けるnicのip addressを指定する。
root@vagrant:~# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=x.x.x.x --ignore-preflight-errors all

# To make kubectl work for your non-root user
root@vagrant:~# exit
vagrant@vagrant:~$ mkdir -p $HOME/.kube
vagrant@vagrant:~$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
vagrant@vagrant:~$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

# install CNI (Flannel)
vagrant@vagrant:~$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml

# To be able to schedule pods on the control-plane node
vagrant@vagrant:~$ kubectl taint nodes --all node-role.kubernetes.io/master-
```