My Little Kubernetes: Setting Up a Test Cluster in VirtualBox

Over the past couple of years, I have had to deal with Kubernetes quite often, but this usually always happened from a developer's position. View the list of pods, their statuses, logs, etc. In general, nothing complicated.

Recently I decided to study Kubernetes a little deeper in order to better understand how it works and what it can do. But then an obvious problem arose: we need some kind of sandbox, in which one could experiment without fear of breaking something, or even better to be able to roll back at any time.

Usually, you should start learning Kubernetes with minikube , a console utility that can quickly create a cluster from one node using VirtualBox. This is the best option for a quick start.

But there is another option. You can take VirtualBox, create several virtual machines, for example, with Ubuntu Server and set up a small Kubernetes cluster right on your home computer.

I decided to go the second way, but during the setup I started to face various problems, the solution of which was not as obvious to me as I would like. In general, on the Internet you can find a lot of articles in English, where the setup process is described, but, unfortunately, none of them was complete enough for me. I had to collect information piece by piece from different sources.

Here I decided to put everything together and describe the process of setting up a small Kubernetes cluster using VirtualBox in as much detail as I would like to see it myself. Hope this information can help other developers with their experiments.

Watch out for traffic! There are a lot of screenshots in the article.

Training

, , VirtualBox, Kubernetes, c Ubuntu Server 20.04. Windows 10 Home. :

  • VirtualBox 6.1.16 VirtualBox 6.1.16 Extension Pack. .

  • Ubuntu Server 20.04. .

  • kubectl . .

, .NET , , , . - - =)

Hyper-V

, Docker Windows Subsystem for Linux (WLS), Hyper-V. VirtualBox , :

Hyper-V . :

bcdedit /set hypervisorlaunchtype off

:

bcdedit /set hypervisorlaunchtype auto

, .

VirtualBox

VirtualBox VirtualBox Extension Pack. , . File β†’ Preferences β†’ Extensions , Extension Pack :

VirtualBox , , NAT, NAT Network, Bridged, Host-only .. , , . , ( , ).

NAT Host-only.

NAT , ( NAT Network). , IP , .

Host-only , IP , . . VirtualBox , File β†’ Host Network Manager...:

DHCP Server, IP , NAT.

IPv4 Address IP . , IPv4 Network Mask. , IP 192.168.92.1, 255.255.255.0, 192.168.92.2, 192.168.92.10, 192.168.92.42 ..

, VirtualBox . , - , 192.168.10.1:

( , 192.168.1.1 , .. Wi-Fi )

, .. .

, , . , .

Kube Template:

2:

. VDI (VirtualBox Disk Image), 10:

:

. . Machine β†’ Settings... β†’ System β†’ Processor 2, Kubernetes :

Network , NAT:

( ), Host-only , ( ):

, . Ubuntu Server.

Ubuntu Server

. VirtualBox . iso Ubuntu Server 20.04 :

, , . :

:

, , IP , - . :

IP . , IP 192.168.10.1. .

IP :

, . kube-template, username/password test/test:

OpenSSH server. , SSH PuTTY Multi PuTTY Manager. , :

( .)

. , .. :

, , ( cdrom, ):

, , :

IP :

:

, :

curl https://ya.ru

, . snapshot, .

mc

, - . mc:

sudo apt-get update
sudo apt-get install -y mc
sudo mc
mc, , .
  • F3 -

  • F4 - . , mcedit (3).

    • F2 - C .

  • F5 - .

  • F8 - .

  • CTRL+O - .

swap

Kubernetes , swap, .

, swap :

cat /proc/swaps

, swap , /etc, fstab . :

( ) ( /swap.img), :

shutdown -r now

/swap.img:

Docker Kubernetes, . , . , .. .

, , , , .

, .

( ) :

shutdown now

Shared Folders. , :

.

CD VirtualBox Guest Additions, , VirtualBox Extension Pack. Devices β†’ Insert Guest Additions CD image:

. CD:

sudo mkdir /media/cdrom
sudo mount -t iso9660 /dev/cdrom /media/cdrom

, :

sudo apt-get update
sudo apt-get install -y build-essential linux-headers-`uname -r`

:

sudo /media/cdrom/VBoxLinuxAdditions.run

:

shutdown -r now

:

mkdir ~/shared

test, /home/test/shared.

mc /etc/fstab. ( <username> , test):

shared /home/<username>/shared vboxsf defaults 0 0

:

/etc/modules, :

vboxsf

:

:

shutdown -r now

, . :

, ~/shared , :

Docker Kubernetes

Docker. , ( ):

#!/usr/bin/env bash

sudo apt-get update

sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

Kubernetes . , :

#!/usr/bin/env bash

sudo apt-get update

sudo apt-get install -y \
    apt-transport-https \
    curl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

, , . : install-docker.sh install-kubernetes.sh, .

, LF, CRLF.

, , Docker, Kubernetes:

cgroup driver Docker Kubernetes systemd

Linux , Control Groups ( cgroups), , . Docker Kubernetes cgroups, : cgroupfs systemd. , Docker Kubernetes .

systemd, . , Kubernetes .

Docker. /etc/docker daemon.json :

{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

:

Kubernetes /etc/systemd/system/kubelet.service.d/10-kubeadm.conf. :

ExecStart=/usr/bin/kubelet <   >

--cgroup-driver=systemd. :

, :

shutdown now

. . Clone...:

: Kube Master, Kube Node1 Kube Node2.

MAC AddressPolicy Generate new MAC address for all network adapters.

:

hostname IP

, hostname IP .

Kube Master .

hostname. :

sudo hostnamectl set-hostname kube-master

IP Ubuntu Server 20.04 /etc/netplan/00-installer-config.yaml. IP 192.198.10.10:

:

sudo netplan apply

, hostname IP :

hostname && hostname -I

. Kubernetes ( , kubelet) , IP IP , NAT , .

IP , , /etc/systemd/system/kubelet.service.d/10-kubeadm.conf. , --cgroup-driver=systemd. : --node-ip=192.168.10.10:

, .

Kube Node1 kube-node1 IP 192.168.10.11.

Kube Node2 kube-node2 IP 192.168.10.12.

.

master :

sudo kubeadm init \
	--apiserver-advertise-address=192.168.10.10 \
  --pod-network-cidr=10.10.0.0/16 > ~/shared/kubeadm-join.sh

.

--apiserver-advertise-address , IP apiserver. IP , NAT, master . 192.168.10.10.

--pod-network-cidr , IP . , , . 10.10.0.0/16.

master , output kubeadm join ... . , .

, , kubeadm-join.sh:

. :

Kube Node1 Kube Node2, kubeadm-join.sh:

Kubernetes . - . admin.conf /etc/kubernetes, master .

admin.conf config:

sudo cp /etc/kubernetes/admin.conf ~/shared/config

config %UserProfile%/.kube.

:

kubectl get nodes -o wide

, , NotReady.

, , , , Pending:

kubectl get pods -A

, Kubernetes , , Flannel Calico. .

Flannel (.. ).

GitHub , :

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

, .. . kube-flannel.yml .

:

  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }

10.244.0.0 10.10.0.0. , kubectl init.

Flannel :

kubectl apply -f .\Desktop\kube-flannel.yml

, , , Flannel. Running .

Ready:

.

. , , .

I hope this material will be useful for developers who want to set up a test cluster for themselves to learn Kubernetes.




All Articles