近期在学习Kubernetes,参考的教程是基于linux虚拟机+minikube。考虑到Vagrant较为轻量,因此本文基于Vagrant和minikube从零开始搭建Kubernetes环境。
1. 安装VirtualBox
Vagrant是构建在VirtualBox基础上,因此需要先安装VirtualBox,安装包现在地址如下,本文在Ubuntu 22.04下安装。通过dpkg -i 安装即可。
2. 安装Vagrant
安装教程如下,安装后可到Ubuntu cloud-images网站下载一个Ubuntu 22.04的Vagrant Box.
通过wget下载box:
wget https://cloud-images.ubuntu.com/jammy/20240726/jammy-server-cloudimg-amd64-vagrant.box
将下载的box加入box list:
vagrant box add jammy-server-cloudimg-amd64-vagrant.box --name ubuntu-22.04
vagrant box list # 查看当前可启动的box
创建Vagrant虚拟机环境:
mkdir vagrant_kube_env
cd vagrant_kube_env
vagrant init ubuntu-22.04 # 初始化虚拟机
vagrant up # 启动虚拟机
vagrant ssh # 登录到虚拟机
修改Vagrantfile,挂载共享文件夹,配置内存为8G.
重启虚拟机使配置生效(也列举了一些其他常用命令).
vagrant reload # 重启虚拟机
vagrant halt # 虚拟机关机,vagrant up虚拟器启动后在后台运行时是消耗系统资源的,不用时建议关机
vagrant package # 将虚拟机打包为box,用于分发
vagrant status # 查看虚拟机状态
3. 安装minikube环境依赖
在首次vagrant ssh登入虚拟机后,需要对其进行软件更新和安装:
首先进行apt换源,修改sources.list.
sudo vi /etc/apt/sources.list
以清华源为例,注释掉官方源并在sources.list尾部增加清华源:
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
#deb http://archive.ubuntu.com/ubuntu jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu jammy main restricted
## Major bug fix updates produced after the final release of the
## distribution.
#deb http://archive.ubuntu.com/ubuntu jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu jammy-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
#deb http://archive.ubuntu.com/ubuntu jammy universe
# deb-src http://archive.ubuntu.com/ubuntu jammy universe
#deb http://archive.ubuntu.com/ubuntu jammy-updates universe
# deb-src http://archive.ubuntu.com/ubuntu jammy-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
#deb http://archive.ubuntu.com/ubuntu jammy multiverse
# deb-src http://archive.ubuntu.com/ubuntu jammy multiverse
#deb http://archive.ubuntu.com/ubuntu jammy-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu jammy-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
#deb http://archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
#deb http://security.ubuntu.com/ubuntu jammy-security main restricted
# deb-src http://security.ubuntu.com/ubuntu jammy-security main restricted
#deb http://security.ubuntu.com/ubuntu jammy-security universe
# deb-src http://security.ubuntu.com/ubuntu jammy-security universe
#deb http://security.ubuntu.com/ubuntu jammy-security multiverse
# deb-src http://security.ubuntu.com/ubuntu jammy-security multiverse
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
然后更新并下载常用工具软件:
sudo apt update
sudo apt install -y git vim curl jq
安装docker:
sudo apt install docker.io
sudo service docker start #启动docker服务
sudo usermod -aG docker ${USER} #当前用户加入docker组
更新docker镜像仓库:
sudo vim /etc/docker/daemon.json
加入以下镜像仓库配置:
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"https://registry.docker-cn.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn"
]
}
重启docker:
sudo systemctl daemon-reload
sudo systemctl restart docker
4. 安装minikube
依照其官网指导安装minikube,本文也将相应命令直接复制在下面:
# Intel x86_64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube /usr/local/bin/
启动minikube,基于v1.23.3的Kubernetes:
minikube start --kubernetes-version=v1.23.3
在安装过程中遇到了gcr.io/k8s-minikube/kicbase:v0.0.44这个镜像无法下载的情况。
查阅资料是因为gcr.io无法访问造成的,本文的此刻可用解决方案是使用lank8s.cn进行镜像加速,后续若不能使用可查阅其他镜像加速方案。
# 根据报错日志先下载kicbase镜像,将gcr.io改为gcr.lank8s.cn
docker pull gcr.lank8s.cn/k8s-minikube/kicbase:v0.0.44@sha256:eb04641328b06c5c4a14f4348470e1046bbcf9c2cbc551486e343d3a49db557e
# 通过docker tag为镜像起别名
docker tag gcr.lank8s.cn/k8s-minikube/kicbase:v0.0.44 gcr.io/k8s-minikube/kicbase:v0.0.44@sha256:eb04641328b06c5c4a14f4348470e1046bbcf9c2cbc551486e343d3a49db557e
# 上述gcr.lank8s.cn/k8s-minikube/kicbase:v0.0.44若无法使用,可通过docker image ls查阅并替换为其镜像id
重新启动minikube:
minikube start --image-repository=lank8s.cn --base-image='gcr.io/k8s-minikube/kicbase:v0.0.44' --kubernetes-version=v1.23.3
安装kubectl:
minikube kubectl -- get pods -A
运行minikube指令,可以看到minicube运行成功.