【1】minikube初体验
目录
【1】minikube初体验
【2】minikube部署Traefik
【3】minikube离线版安装
minikube 介绍
K8S的迷你版本,运行于单机之上的K8S。当资源并不多,而需要学习或者开发基于k8s集群的组件,minikube也是一个不错的选择。
minikube 官方文档:https://minikube.sigs.k8s.io/docs/start/
安装前准备
官方文档写的非常详细。
大概意思是说,需要准备一台不能小于 2核心、2G内存、20G磁盘、能连接互联网的主机,而且主机需要有容器或者虚拟机环境,比如:docker、QEMU 巴拉巴拉虚拟化软件其中之一,然后安装 minikube 就是一条命令的事:minikube start
我这里直接采用 vmware workstation 创建一台虚拟机来搭建 minikube, 配置信息如下:
挑战下极限,估计给了个最低配置,磁盘100GB不影响。安装好 Centos7.9 准备开搞。
minikube安装部署
系统初始化
最常见的系统初始化还是要做的。
- 关闭 selinux 和 firewalld
- 修改主机名并添加 /etc/hosts 解析
- 校对时间
差不多就这三步吧, 后面遇到再说。这三步就不给出命令了, 都敲烂了。
我主机的主机名及解析
root@minikube(192.168.199.200)~>hostname
minikube
root@minikube(192.168.199.200)~>cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.199.200 minikube
继续跟着官方文档走。
下载minikube
这一步就难死了一大帮兄弟们,看到 google
就头痛。
阿里云发布的minikube地址:https://github.com/kubernetes/minikube/releases 从 release 目录下载最新版的 minikube
别下载错了。
这里要是github再上不去,在国内学习kubernetes 真就得谢谢阿里的同学们,还有一个地方可以使用:
https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.20.0/minikube-linux-amd64
上面这个目前只更新到 v1.20.0
版本了。
上面提供了三种方式下载 minikube
,无论用哪种方法下载的,接下来的操作都是一致的。
部署minikube
root@minikube(192.168.199.200)~>install minikube-linux-amd64 /usr/bin/minikube
root@minikube(192.168.199.200)~>ls /usr/bin/minikube
/usr/bin/minikube*
版本
root@minikube(192.168.199.200)~>minikube version
minikube version: v1.32.0
commit: 8220a6eb95f0a4d75f7f2d7b14cef975f050512d
安装官方文档来说, 试试 minikube start
root@minikube(192.168.199.200)~>minikube start
* minikube v1.32.0 on Centos 7.9.2009
* Unable to pick a default driver. Here is what was considered, in preference order:
* Alternatively you could install one of these drivers:
- docker: Not installed: exec: "docker": executable file not found in $PATH
- kvm2: Not installed: exec: "virsh": executable file not found in $PATH
- podman: Not installed: exec: "podman": executable file not found in $PATH
- qemu2: Not installed: exec: "qemu-system-x86_64": executable file not found in $PATH
- virtualbox: Not installed: unable to find VBoxManage in $PATH
X Exiting due to DRV_NOT_DETECTED: No possible driver was detected. Try specifying --driver, or see https://minikube.sigs.k8s.io/docs/start/
报错了不要慌,认真看报错信息,上面的主要内容无非就是需要指定 --driver
嘛,那么多中选一个就是了。我们本来就是虚拟机,这里直接用 none
这种模式,对应的看 none
后面是啥解释。
- none: Not installed: exec: "docker": executable file not found in $PATH 没有安装 docker,执行文件在环境变量 PATH中找不到。
根据提示先安装docker吧。
安装docker
安装docker有很多种方式。
- 通过yum安装
- 通过二进制包安装
因为有互联网访问,直接采用yum安装更加方便。
添加docker源
root@minikube(192.168.199.200)~>curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
安装docker
root@minikube(192.168.199.200)~>yum -y install docker-ce -y
启动docker
root@minikube(192.168.199.200)~>systemctl enable --now docker
查看docker版本
root@minikube(192.168.199.200)~>docker version
Client: Docker Engine - Community
Version: 25.0.4
API version: 1.44
Go version: go1.21.8
Git commit: 1a576c5
Built: Wed Mar 6 16:33:16 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 25.0.4
API version: 1.44 (minimum version 1.24)
Go version: go1.21.8
Git commit: 061aa95
Built: Wed Mar 6 16:32:11 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.28
GitCommit: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0
注意:这里不要自作主张的修改 Docker Root Dir
的数据目录,minikube初始化会重新设置这个目录。
注意:当执行 minikube start 未成功,下次再次执行前先删除 /root/minikube,因为会缓存上一次失败的文件。
安装完docker后,再次执行,记得要加 --driver=none
我们是虚拟机环境 minikube start --driver=none
先清除
root@minikube(192.168.199.200)~>rm -rf ~/.minikube
再执行
root@minikube(192.168.199.200)~>minikube start --driver=none
* minikube v1.32.0 on Centos 7.9.2009
* Using the none driver based on user configuration
X Exiting due to GUEST_MISSING_CONNTRACK: Sorry, Kubernetes 1.28.3 requires conntrack to be installed in root's path
还是有问题,意思是说:安装 Kubernetes 1.28.3 需要安装 conntrack
在root目录。这里就思考一个问题,这里给出了 k8s安装版本,如果不想用这个版本呢?
这里就通过 minikube start --help
尝试去找找能不能为 minikube 指定 k8s 版本:
root@minikube(192.168.199.200)~>minikube start --help | egrep version
If set, automatically updates drivers to the latest version. Defaults to true.
--kubernetes-version='':
The Kubernetes version that the minikube VM will use (ex: v1.2.3, 'stable' for v1.28.3, 'latest' for v1.28.3). Defaults to 'stable'.
--mount-9p-version='9p2000.L':
Specify the 9p version that the mount should use
看来可以通过 --kubernetes-version=''
来指定版本,默认是安装k8s稳定版,例如我这里需要安装 v1.23.3
版本的 k8s,为什么是 v1.23.3
呢,因为这个版本容器还是用的 docker,后面的版本都使用 containerd
指定版本再次尝试:
先清除
root@minikube(192.168.199.200)~>rm -rf ~/.minikube
再执行
root@minikube(192.168.199.200)~>minikube start --driver=none --kubernetes-version='v1.23.3'
* minikube v1.32.0 on Centos 7.9.2009
* Using the none driver based on user configuration
X Exiting due to GUEST_MISSING_CONNTRACK: Sorry, Kubernetes 1.23.3 requires conntrack to be installed in root's path
看来安装 conntrack
是绕不开了。
安装conntrack
有互联网就是爽,直接yum安装即可。
root@minikube(192.168.199.200)~>yum -y install conntrack
安装完成后,再次尝试:
先清除
root@minikube(192.168.199.200)~>rm -rf ~/.minikube
再执行
root@minikube(192.168.199.200)~>minikube start --driver=none --kubernetes-version='v1.23.3'
* minikube v1.32.0 on Centos 7.9.2009
* Using the none driver based on user configuration
* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=2, Memory=3770MB, Disk=97352MB) ...
* OS release is CentOS Linux 7 (Core)
E0308 10:25:51.159453 15673 start.go:421] unable to disable preinstalled bridge CNI(s): failed to configure non-podman bridge cni configs in "/etc/cni/net.d": sudo find /etc/cni/net.d -maxdepth 1 -type f -name *bridge* -not -name *podman* -not -name *.mk_disabled -printf "%p, " -exec sh -c "sudo sed -i -r -e '/"dst": ".*:.*"/d' -e 's|^(.*)"dst": (.*)[,*]$|\1"dst": \2|g' -e '/"subnet": ".*:.*"/d' -e 's|^(.*)"subnet": ".*"(.*)[,*]$|\1"subnet": "10.244.0.0/16"\2|g' {}" ;: exit status 1
stdout:
stderr:
find: ‘/etc/cni/net.d’: No such file or directory
> kubelet.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
> kubelet: 230.23 KiB / 118.75 MiB [>_____] 0.19% 79.80 KiB p/s ETA 25m2/
看起来已经可以运行了啊,但是这里下载 kubelet / kubeadm / kubectl
速度实在太慢了,有没有其他办法呢? 当然有办法。
安装kubeadm/kubectl/kubelet
重新开启一台虚拟机,配置啥都无所谓能连接到互联网即可,方法是该虚拟机从aliyun源安装对应版本的 kubeadm / kubelet / kubectl 然后拷贝到 minikube 下载目录下即可。
注意:该虚拟机并非安装 minikube 主机!!!
添加 kubernetes源
root@localhost(192.168.199.170)~>cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装minikube需要的版本(注意:注意:该虚拟机并非安装 minikube 主机!!!)
root@localhost(192.168.199.170)~>yum -y install kubelet-1.23.3 kubeadm-1.23.3 kubectl-1.23.3
拷贝kubeadm / kubectl / kubelet 到 minikube指定目录
root@localhost(192.168.199.170)~>scp /usr/bin/kube* 192.168.199.200:/root/.minikube/cache/linux/v1.23.3/
部署minikube
通过上面的步骤已经将 kubelet / kubectl / kubeadm
拷贝到对应目录了, 再次执行操作。因为 minikube 也需要下载镜像文件,而默认的镜像文件也是国外的仓库,修改为阿里云的仓库。
--image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
操作如下:
先清除
root@minikube(192.168.199.200)~>rm -rf ~/.minikube
创建目录
root@minikube(192.168.199.200)~>mkdir -p /root/.minikube/cache/linux/amd64/v1.23.3/
再拷贝
root@localhost(192.168.199.170)~>scp /usr/bin/kube* 192.168.199.200:/root/.minikube/cache/linux/amd64/v1.23.3/
再执行
root@minikube(192.168.199.200)~>minikube start --driver=none --kubernetes-version='v1.23.3' --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
* minikube v1.32.0 on Centos 7.9.2009
* Using the none driver based on user configuration
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=2, Memory=3770MB, Disk=97352MB) ...
* OS release is CentOS Linux 7 (Core)
E0308 13:57:24.345459 7322 start.go:421] unable to disable preinstalled bridge CNI(s): failed to configure non-podman bridge cni configs in "/etc/cni/net.d": sudo find /etc/cni/net.d -maxdepth 1 -type f -name *bridge* -not -name *podman* -not -name *.mk_disabled -printf "%p, " -exec sh -c "sudo sed -i -r -e '/"dst": ".*:.*"/d' -e 's|^(.*)"dst": (.*)[,*]$|\1"dst": \2|g' -e '/"subnet": ".*:.*"/d' -e 's|^(.*)"subnet": ".*"(.*)[,*]$|\1"subnet": "10.244.0.0/16"\2|g' {}" ;: exit status 1
stdout:
stderr:
find: ‘/etc/cni/net.d’: No such file or directory
* Preparing Kubernetes v1.23.3 on Docker 25.0.4 ...
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Configuring local host environment ...
*
! The 'none' driver is designed for experts who need to integrate with an existing VM
* Most users should use the newer 'docker' driver instead, which does not require root!
* For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/
*
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
*
- sudo mv /root/.kube /root/.minikube $HOME
- sudo chown -R $USER $HOME/.kube $HOME/.minikube
*
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Verifying Kubernetes components...
- Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v5
* Enabled addons: default-storageclass, storage-provisioner
* kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
看最后的结果,done,已经完成。看倒数第二行
kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
kubectl 没找到,如果需要执行:minikube kubectl -- get pods -A
尝试执行:
root@minikube(192.168.199.200)~>minikube kubectl -- get po -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-65c54cc984-w9rtf 1/1 Running 0 8m10s
kube-system etcd-minikube 1/1 Running 0 8m23s
kube-system kube-apiserver-minikube 1/1 Running 0 8m23s
kube-system kube-controller-manager-minikube 1/1 Running 0 8m23s
kube-system kube-proxy-qpwsq 1/1 Running 0 8m10s
kube-system kube-scheduler-minikube 1/1 Running 0 8m25s
kube-system storage-provisioner 1/1 Running 0 8m21s
这里没找到 kubectl, 我们让它找到不就完了。
root@minikube(192.168.199.200)~>cp -a ~/.minikube/cache/linux/amd64/v1.23.3/kubectl /usr/bin/
然后在本地想k8s一样的查看pod
root@minikube(192.168.199.200)~>kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-65c54cc984-w9rtf 1/1 Running 0 9m41s
etcd-minikube 1/1 Running 0 9m54s
kube-apiserver-minikube 1/1 Running 0 9m54s
kube-controller-manager-minikube 1/1 Running 0 9m54s
kube-proxy-qpwsq 1/1 Running 0 9m41s
kube-scheduler-minikube 1/1 Running 0 9m56s
storage-provisioner 1/1 Running 0 9m52s
到这里,minikube已经部署完成了,接下来验证下试试看。
开启dashboard
开启dashboard
root@minikube(192.168.199.200)~>minikube addons enable dashboard
查看
root@minikube(192.168.199.200)~>kubectl get pod,svc -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
pod/dashboard-metrics-scraper-7db978b848-zplcm 1/1 Running 0 53m
pod/kubernetes-dashboard-6f4c897964-fw7qj 1/1 Running 0 53m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/dashboard-metrics-scraper ClusterIP 10.97.6.92 <none> 8000/TCP 53m
service/kubernetes-dashboard ClusterIP 10.108.199.93 <none> 80/TCP 53m
访问 dashboard 也是个技术活。
首先,启动dashboard 界面
root@minikube(192.168.199.200)~>minikube dashboard &
[1] 62714
root@minikube(192.168.199.200)~>* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
http://127.0.0.1:34726/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
为了使用非本机的客户端访问 dashboard,需要添加外部代理
kubectl proxy --port=80 --address='192.168.199.200' --accept-hosts='^.*' &
然后就可以将上面的 127.0.0.1:34726
替换为 192.168.199.200
访问了
http://192.168.199.200/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
别看着好奇怪一链接地址,直接浏览器访问就打开了。
验证
运行一个Pod
运行一个pod
root@minikube(192.168.199.200)~>kubectl create deployment ngx --image=nginx:alpine --port=80
deployment.apps/ngx created
为pod添加service
root@minikube(192.168.199.200)~>kubectl expose deployment ngx --type=NodePort --port=80 --target-port=80
service/ngx exposed
查看service
root@minikube(192.168.199.200)~>kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22m
ngx NodePort 10.101.201.161 <none> 80:30429/TCP 37s
映射的随机端口: 30429 , 通过浏览器访问。