文章目录
环境
- 宿主机:Windows 11
- 虚拟机:Oracle VM VirtualBox
- 虚拟机操作系统:Ubuntu 22.04.3 LTS
- Docker版本:27.0.3
- Kubernetes版本:1.30.0
前提
你已经安装好了一个干净的Ubuntu操作系统,并且apt软件源已经更换到阿里云软件源。
安装步骤
添加Docker和kubernetes阿里云源
# 安装依赖包
sudo apt install apt-transport-https ca-certificates
# 添加阿里云Docker镜像源GPG秘钥
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加阿里云Docker镜像源,最新版本
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 添加阿里云Kubernetes镜像源GPG秘钥
sudo curl -fsSL https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 添加阿里云Kubernetes镜像源,v1.30版本
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
安装Docker
# 更新apt
sudo apt-get update
# 安装Docker
sudo apt install docker-ce docker-ce-cli containerd.io
配置Docker国内镜像仓库
# 阿里云的镜像仓库需要自己去阿里云申请 https://cr.console.aliyun.com/#/accelerator
sudo vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://你的id.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
# 载入新的daemon配置
sudo systemctl daemon-reload
启动Docker
# 启动Docker服务
sudo systemctl start docker
# 如果已经启动了就重新启动
sudo systemctl restart docker
# 查看docker服务状态
systemctl status docker
# 将当前用户添加至用户组
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
安装Kubernetes
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
下载安装Minikube
# 下载Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
# 安装Minikube
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# 下载kicbase
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.44
# 创建Minikube集群
minikube start --image-mirror-country=cn --base-image=registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.44 --registry-mirror=https://docker.mirrors.ustc.edu.cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
此时你应该是启动不成功,出现以下错误,标识kubectl、kubeadm下载失败了。
Exiting due to K8S_INSTALL_FAILED: Failed to update cluster: update primary control-plane node: downloading binaries:
downloading kubeadm: download failed: https://mirror.azure.cn/kubernetes/kubectl/v1.30.0/bin/linux/amd64/kubectl/v1.30.0/bin/linux/amd64/kubeadm?checksum=file:https://mirror.azure.cn/kubernetes/kubectl/v1.30.0/bin/linux/amd64/kubectl/v1.30.0/bin/linux/amd64/kubeadm.sha256: getter: &{Ctx:context.Background Src:https://mirror.azure.cn/kubernetes/kubectl/v1.30.0/bin/linux/amd64/kubectl/v1.30.0/bin/linux/amd64/kubeadm?checksum=file:https://mirror.azure.cn/kubernetes/kubectl/v1.30.0/bin/linux/amd64/kubectl/v1.30.0/bin/linux/amd64/kubeadm.sha256 Dst:/home/用户名/.minikube/cache/linux/amd64/v1.30.0/kubeadm.download Pwd: Mode:2 Umask:---------- Detectors:[0x493b120 0x493b120 0x493b120 0x493b120 0x493b120 0x493b120 0x493b120] Decompressors:map[bz2:0xc00049f718 gz:0xc00049f7a0 tar:0xc00049f750 tar.bz2:0xc00049f760 tar.gz:0xc00049f770 tar.xz:0xc00049f780 tar.zst:0xc00049f790 tbz2:0xc00049f760 tgz:0xc00049f770 txz:0xc00049f780 tzst:0xc00049f790 xz:0xc00049f7a8 zip:0xc00049f7b0 zst:0xc00049f7c0] Getters:map[file:0xc001cd05d0 http:0xc0006c88c0 https:0xc0006c8910] Dir:false ProgressListener:0x48c85e0 Insecure:false DisableSymlinks:false Options:[0x1274940]}: invalid checksum: Error downloading checksum file: bad response code: 404
还记得上面我们已经安装了kubectl、kubeadm吗,此时我们只需要将安装好的命令链接到minikube目录就好了,从报错信息我们知道了要链接的目录是/home/用户名/.minikube/cache/linux/amd64/v1.30.0/kubeadm
# 软连接
ln -s /usr/bin/kubeadm /home/用户名/.minikube/cache/linux/amd64/v1.30.0/kubeadm
ln -s /usr/bin/kubectl /home/用户名/.minikube/cache/linux/amd64/v1.30.0/kubectl
ln -s /usr/bin/kubelet /home/用户名/.minikube/cache/linux/amd64/v1.30.0/kubelet
再次使用上面的命令启动依次,此时会很快输出
😄 minikube v1.33.1 on Ubuntu 22.04 (vbox/amd64)
✨ Using the docker driver based on existing profile
🧯 The requested memory allocation of 1963MiB does not leave room for system overhead (total system memory: 1963MiB). You may face stability issues.
💡 Suggestion: Start minikube with less memory allocated: 'minikube start --memory=1963mb'
👍 Starting "minikube" primary control-plane node in "minikube" cluster
🚜 Pulling base image v0.0.44 ...
🏃 Updating the running docker "minikube" container ...
🐳 Preparing Kubernetes v1.30.0 on Docker 26.1.1 ...
▪ Generating certificates and keys ...
▪ Booting up control plane ...
▪ Configuring RBAC rules ...
🔗 Configuring bridge CNI (Container Networking Interface) ...
🔎 Verifying Kubernetes components...
▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v5
🌟 Enabled addons: storage-provisioner, default-storageclass
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
至此Minikube集群创建成功。
安装Dashboard
minikube dashboard --url
此时可以再另一个窗口中查看镜像安装状态
查看安装状态
kubectl get pods -n kubernetes-dashboard
等了一段时间,可以发现状态都变成了ImagePullBackOff
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-b5fc48f67-csjjn 0/1 ImagePullBackOff 0 9m33s
kubernetes-dashboard-779776cb65-h6c44 0/1 ImagePullBackOff 0 9m33s
出现该问题的原因还是镜像源的问题,解决办法是从国内源拉取镜像
# 进入minikube的docker中
minikube ssh
# 拉取国内镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/dashboard:v2.7.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-scraper:v1.0.8