Implementation of VirtualBox+Ubuntu16 Building Kubernetes Cluster

  • 2021-10-27 10:08:26
  • OfStack

Directory about Kubernetes
Basic environment preparation
Installing VirtualBox
Download Ubuntu 16 System Image
Virtual machine x3
Virtual machine IP
Configure host name
SSH Condensless Connection
Kubernetes cluster building
Installing Docker
Install kubeadm, kubelet, and kubectl
Turn off SWAP
Download the image in advance
Initialize the master node
Join node Node
Distribute kubectl configuration files
Install network plug-ins

Of course, the construction of cluster environment is indispensable for the beginning of Kubernetes operation and maintenance. This article records the whole process of building Kubernetes with VirtualBox + Ubuntu 16 on a machine, including some problems encountered and solutions.

About Kubernetes

The following is an explanation of Kubernetes from Wikipedia:

Kubernetes (commonly referred to as K8s) is an open source system for automatically deploying, extending, and managing "containerized (containerized) applications." The system was designed by Google and donated to Cloud Native Computing Foundation (now the Linux Foundation).
It is intended to provide "a platform for automated deployment, scaling, and running of application containers across host clusters." It supports 1 series of container tools, including Docker and so on.

Kubernetes can provide us with the ability of service discovery and load balancing, storage orchestration, automatic deployment and rollback, automatic boxing calculation, self-repair and key and configuration management.

Basic environment preparation

Installing VirtualBox

VirtualBox is a powerful virtual machine software, and is open source free, this is the download address, install VirtualBox is very simple, I will not repeat here.

Download Ubuntu 16 System Image

Here I chose Ubuntu 16 as the system image, but of course you can also use other systems, such as CentOS, etc., and the download address of Ubuntu 16.

Virtual machine x3

After installing VirtualBox and downloading the image of Ubuntu 16, we first need to build three virtual machines of Ubuntu 16. The process of creating a new virtual machine is also relatively simple, and it is OK to go down step by step. After the new creation is completed, we need to configure each virtual machine accordingly, and the user used in configuration should be root user.

Virtual machine IP

Since we use virtual machines, we will configure network cards for each virtual machine, so that each virtual machine can access the Internet. There are two ways:

Using the bridge network card, the IP of each virtual machine will be the host network segment, and the virtual machine network will be supported Use NAT network + port forwarding, network segment is set by itself, and network on virtual machine is supported

You can use any one of the ways to configure the network card for the virtual machine, so that the virtual machine can surf the Internet.

It should be noted that after the cluster is built, the IP requirements of each node in the cluster remain unchanged, otherwise the nodes need to rejoin.

The simple way is to let the virtual machine go to sleep without shutting down, and just wake up next time.

In the cluster, we use the intranet address, and we can find the intranet address corresponding to each virtual machine through ifconfig or ip addr:


> ifconfig

enp0s3    Link encap:Ethernet  HWaddr 08:00:27:6f:23:2a  
          inet addr:10.0.2.4  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe6f:232a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3277016 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3385793 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1084480916 (1.0 GB)  TX bytes:2079122979 (2.0 GB)

The address of this virtual machine (master) is 10.0. 2.4.

Configure host name

The node name of Kubernetes is determined by the host name, so we can set the host names of three virtual machines as master, node1 and node2 respectively. To modify the host name by modification/etc/hosts, we need to restart the virtual machines:


# /etc/hosts
10.0.2.4 master
10.0.2.5 node1
10.0.2.6 node2

SSH Condensless Connection

After the virtual machines are running, the first thing we have to do is to connect these three virtual machines, that is, configure SSH confidential connection.
First, generate the public and private key of SSH on one of the virtual machines:


ssh-keygen -t rsa -C 'k8scat@gmail.com' -f ~/.ssh/id_rsa -q -N ''

Parameter description for ssh-keygen:

-t rsa specifies the encryption algorithm as RSA -C 'k8scat @ gmail. com' is used to provide a note indicating the generator of the private key -f ~/. ssh/id_rsa Specifies where the private key is generated -q-N ''means that private keys are not cryptographed and silently used

Distribute the public and private keys to the other two virtual machines, and write the contents of the public key (~ /.ssh/id_rsa. pub) into the ~ /.ssh/authorized_keys file on all three virtual machines, and set the permission of the ~ /.ssh/authorized_keys file to 400:


cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 400 ~/.ssh/authorized_keys

After the configuration is completed, we will be able to connect the other virtual machine on one of the virtual machines in the following ways:


#  In  master  Node 
ssh root@node1

Kubernetes cluster building

With three virtual machines in place, we can start building a cluster of Kubernetes with three nodes.

Installing Docker


apt-get update -y
apt-get install -y \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg \
  lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# INSTALL DOCKER ENGINE
apt-get update -y
apt-get install -y docker-ce docker-ce-cli containerd.io

# Configure Docker to start on boot
systemctl enable docker.service
systemctl enable containerd.service

# Start Docker
systemctl start docker

Install kubeadm, kubelet, and kubectl

The image source of Alibaba Cloud is used here:


#  Update  apt  Package index and install and use  Kubernetes apt  Packages required by the warehouse 
apt-get update -y
apt-get install -y apt-transport-https ca-certificates curl

#  Download  Google Cloud  Public signature secret key 
# curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

#  Add  Kubernetes apt  Warehouse 
# echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

#  Update  apt  Package index, installation  kubelet , kubeadm  And  kubectl And lock its version 
apt-get update -y
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

Turn off SWAP

Edit the/etc/fstab file and comment out the configuration of the swap partition:


#/dev/mapper/master--vg-swap_1 none            swap    sw              0       0

Download the image in advance

Get the list of images that kubeadm init needs to use:


> kubeadm config images list

k8s.gcr.io/kube-apiserver:v1.21.1
k8s.gcr.io/kube-controller-manager:v1.21.1
k8s.gcr.io/kube-scheduler:v1.21.1
k8s.gcr.io/kube-proxy:v1.21.1
k8s.gcr.io/pause:3.4.1
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns/coredns:v1.8.0

The mirror source of k8s is out of reach for domestic users, but we can first pull to domestic mirror warehouses or usable mirror warehouses, such as Alibaba Cloud's container mirror service ACR and DockerHub, the official mirror warehouse of Docker.

We can create a new GitHub code warehouse with only one Dockerfile in it, which reads as follows:


FROM k8s.gcr.io/kube-apiserver:v1.21.0

Then create a new image in Alibaba Cloud's container mirroring service ACR, and associate it with this GitHub code warehouse. The constructed image is the k8s image we want, such as k8s. gcr. io/kube-apiserver: v1.21. 1, but it needs to be re-labeled when using it.

With all the required images built in ACR, you can quickly handle the task of tagging the images using the following script:


# /etc/hosts
10.0.2.4 master
10.0.2.5 node1
10.0.2.6 node2
0

Initialize the master node

10.0. 2.4 is the IP address of the master node, setting the pod segment to 192.168. 16.0/20:


# /etc/hosts
10.0.2.4 master
10.0.2.5 node1
10.0.2.6 node2
1

Join node Node


# /etc/hosts
10.0.2.4 master
10.0.2.5 node1
10.0.2.6 node2
2

Distribute kubectl configuration files


scp master:/etc/kubernetes/admin.conf /etc/kubernetes/admin.conf
echo 'export KUBECONFIG="/etc/kubernetes/admin.conf"' >> /etc/profile
source /etc/profile

Install network plug-ins

Here we use Weave Net:


# /etc/hosts
10.0.2.4 master
10.0.2.5 node1
10.0.2.6 node2
4

Related articles: