写点什么

在虚拟机上搭建单机 k8s 环境

作者:红莲疾风
  • 2022 年 5 月 12 日
  • 本文字数:2622 字

    阅读完需:约 9 分钟

Download images and tools

Install virtualbox

https://www.virtualbox.org/wiki/Downloads

From System Preference -> Security & Privacy, allow Oracle access From Virtualbox -> File -> Host Network Manager, click Create button to create one

Install ubuntu iso from below link

https://releases.ubuntu.com/20.04/

Installation and configuration

Install virtualbox

Open virtualbox manager console

Ensure you have correct host network manager settings

  • properties->vboxnet0->192.168.34.1/24

  • If the subnet is different, you can edit and update it

  • Do not enable dhcp





Boot new VM

  • Click new button

  • Choose OS as ubuntu 64bit and 30G disk, make sure your #CPU>=2

  • Start VM, choose the downloaded ubuntu ISO and follow the installation wizard

  • Specify username/password like cadmin/cadmin (这个可以自定,只要记得密码就好)

  • Install ssh server, enable and start the service

  • Do not install built-in kubenernetes

  • Wait enough long for the os installation complete



Shutdown the OS, and set 2nd network adapter

  • Go to VM->settings->network->adapter 2

  • Enable the adapter and select host only adapter, and choose vboxnet0, vboxnet0 the host network name configured above


Login to the system and set ip for second adapter

vi /etc/netplan/00-installer-config.yaml
network: ethernets: enp0s3: dhcp4: true enp0s8: dhcp4: no addresses: - 192.168.34.2/24 # 这个ip可以按照每个人虚拟机的情况变化,只要在后面的步骤也用这个ip即可 version: 2
复制代码


# 应用一下网络配置netplan apply
复制代码

Network configuration

Now your VM has two adapters:

  • One is NAT which will get an IP automatically, generally it's 10.0.2.15, this interface is for external access from your VM

  • One is host adapter which need create extra ip, which is configured as 192.168.34.2 the reason we need the host adapter and static IP is then we can set this static IP as k8s advertise IP and you can move your VM in different everywhere.(otherwise your VM IP would be changed in different environment)

Swap off

swapoff -avi /etc/fstabremove the line with swap keyword
复制代码


Install docker

apt install docker.io
复制代码

Update cgroupdriver to systemd

vi /etc/docker/daemon.json
# 然后写入以下内容{ "exec-opts": ["native.cgroupdriver=systemd"]}
# 重启dockersystemctl daemon-reloadsystemctl restart docker
复制代码


以上是基础环境搭建部分,完成了虚拟机和 docker 的安装

接下来就是重头戏了:

Install kubernetes

Letting iptables see bridged traffic

$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.confbr_netfilterEOF
$ cat <<EOF | sudo tee /etc/sysctl.d/k8s.confnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOF$ sudo sysctl --system
复制代码

Update the apt package index and install packages needed to use the Kubernetes apt repository:

$ sudo apt-get update$ sudo apt-get install -y apt-transport-https ca-certificates curl
复制代码

Install kubeadm

$ sudo curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
复制代码

Add the Kubernetes apt repository

$ sudo tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF'deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial mainEOF
复制代码

Update apt package index, install kubelet, kubeadm and kubectl

$ sudo apt-get update
# 这里最好指定版本号,我之前在这里坑了好久,我的镜像默认给我装了1.24.0的kubelet# 导致我后面只能装1.24.0的k8s的其他组件$ apt-get install -y kubelet=1.22.2-00 kubeadm=1.22.2-00 kubectl=1.22.2-00
# 这个主要为了保证这几个组件不被升级$ sudo apt-mark hold kubelet kubeadm kubectl
复制代码

kubeadm init

$ echo "192.168.34.2 cncamp.com" >> /etc/hosts
复制代码


$ kubeadm init \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.22.2 \ --pod-network-cidr=192.168.0.0/16 \ --apiserver-advertise-address=192.168.34.2
复制代码

Copy kubeconfig

$ mkdir -p $HOME/.kube$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
复制代码

Untaint master

$ kubectl taint nodes --all node-role.kubernetes.io/master-
复制代码

Install calico cni plugin

https://docs.projectcalico.org/getting-started/kubernetes/quickstart

$ kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml$ kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml
复制代码


至此,安装过程结束,但是可能还要过一会儿才能完成 k8s 的初始化,一开始我用 kubectl get node 得到的结果是这样的:

root@master-node:~# kubectl get nodeNAME          STATUS     ROLES                  AGE     VERSIONmaster-node   NotReady   control-plane,master   3m59s   v1.22.2
复制代码

当时以为失败了,结果机器没关,到了第二天一看,竟然就成功了:

root@master-node:~# kubectl get nodeNAME          STATUS   ROLES                  AGE   VERSIONmaster-node   Ready    control-plane,master   9h    v1.22.2
复制代码


此时用命令查看 k8s 的容器,都在运行中

root@master-node:~# kubectl get po -n kube-systemNAME                                  READY   STATUS    RESTARTS   AGEcoredns-7f6cbbb7b8-s24vp              1/1     Running   0          10hcoredns-7f6cbbb7b8-w7vx8              1/1     Running   0          10hetcd-master-node                      1/1     Running   0          10hkube-apiserver-master-node            1/1     Running   0          10hkube-controller-manager-master-node   1/1     Running   0          10hkube-proxy-wgsn6                      1/1     Running   0          10hkube-scheduler-master-node            1/1     Running   0          10h
复制代码


发布于: 刚刚阅读数: 2
用户头像

红莲疾风

关注

天道酬勤 然则,尽人事,听天命 2021.07.28 加入

1. 多年互联网从业经验 2. 虚心谨慎的终身学习者 3. 相信自己一直有上升的空间

评论

发布
暂无评论
在虚拟机上搭建单机k8s环境_红莲疾风_InfoQ写作社区