使用Minikube部署Kubernetes环境
1. Minikube简介
Minikube是一个轻量级的Kubernetes实现,它在本地运行一个Kubernetes集群,可以是单节点或者集群环境,主要用于开发和测试。Minikube支持Kubernetes的所有主要功能,包括Dashboard、DNS、容器运行时(如Docker、containerd、CRI-O)等。
主要功能:
- 快速启动本地Kubernetes集群
- 支持多种容器运行时
- 集成Kubernetes Dashboard
- 提供内置的加载均衡器和Ingress控制器
- 支持持久化存储卷
2. 安装Minikube
2.1 环境要求
- 操作系统:Linux, macOS, Windows
- 最低配置:2 CPUs, 2GB 内存, 20GB 磁盘空间
- 互联网连接
- 容器或者虚拟化管理器: Docker, QEMU, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation
2.2 安装步骤
以lx86架构的linux环境为例,更多的安装选项参考:minikube start | minikube (k8s.io)
-
下载Minikube二进制文件:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
-
安装Minikube:
sudo install minikube-linux-amd64 /usr/local/bin/minikube
3. 启动集群
3.1 启动Minikube
启动Minikube非常简单,只需运行以下命令:
minikube start
如果minikube启动失败,可以参考Drivers | minikube (k8s.io)安装一个虚拟机或容器环境。
集群启动成功后,创建了一个当前最新的单节点K8S 1.30版本的集群:
test@ubuntu-svr:~$ kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
minikube Ready control-plane 31m v1.30.0 192.168.49.2 <none> Ubuntu 22.04.4 LTS 5.15.0-107-generic docker://26.1.1
test@ubuntu-svr:~$ kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-7db6d8ff4d-8c7h8 1/1 Running 0 4m33s
kube-system coredns-7db6d8ff4d-xmd7b 1/1 Running 0 4m33s
kube-system etcd-minikube 1/1 Running 0 4m47s
kube-system kube-apiserver-minikube 1/1 Running 0 4m47s
kube-system kube-controller-manager-minikube 1/1 Running 0 4m47s
kube-system kube-proxy-c6jdl 1/1 Running 0 4m33s
kube-system kube-scheduler-minikube 1/1 Running 0 4m47s
kube-system storage-provisioner 1/1 Running 0 4m46s
备注:
官方建议使用非root用户启动集群,如果要使用root运行,需要加
--force
如果拉取镜像失败,可以通过设置代理服务器进行,命令如下:
export HTTP_PROXY=http://<proxy hostname:port> export HTTPS_PROXY=https://<proxy hostname:port> export NO_PROXY=localhost,127.0.0.1,10.96.0.0/12,192.168.59.0/24,192.168.49.0/24,192.168.39.0/24minikube start
3.2 节点运行说明
本机使用docker作为底层driver,上述创建了一个单节点的k8s集群,该k8s节点运行在docker容器中。minikube所在的宿主机ip为192.168.0.157,docker使用kicbase启动了一个容器作为k8s的节点(ip为192.168.49.2),这种玩法用到了容器嵌套的技术。
test@ubuntu-svr:~$ ip add | grep 192.inet 192.168.0.157/24 metric 100 brd 192.168.0.255 scope global dynamic ens33
test@ubuntu-svr:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d565ab58c23 gcr.io/k8s-minikube/kicbase:v0.0.44 "/usr/local/bin/entr…" 2 hours ago Up 2 hours 127.0.0.1:32772->22/tcp, 127.0.0.1:32771->2376/tcp, 127.0.0.1:32770->5000/tcp, 127.0.0.1:32769->8443/tcp, 127.0.0.1:32768->32443/tcp minikube
# 容器ip为minikube节点的INTERNAL-IP
test@ubuntu-svr:~$ docker inspect 9d565ab58c23 | grep -i ipaddr"SecondaryIPAddresses": null,"IPAddress": "","IPAddress": "192.168.49.2",
# 进入kicbase容器内部,里面运行了minikube k8s环境的管理面容器和其他容器
test@ubuntu-svr:~$ docker exec -it 9d565ab58c23 sh
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fead4e8669bc kicbase/echo-server "/bin/echo-server" 13 minutes ago Up 13 minutes k8s_echo-server_hello-minikube-5c898d8489-ngvc6_default_70e327a3-9926-4615-8e63-b9b5e93c04d3_0
c0dfae68f544 registry.k8s.io/pause:3.9 "/pause" 13 minutes ago Up 13 minutes k8s_POD_hello-minikube-5c898d8489-ngvc6_default_70e327a3-9926-4615-8e63-b9b5e93c04d3_0
89fc270a8b21 kubernetesui/dashboard "/dashboard --insecu…" 33 minutes ago Up 33 minutes k8s_kubernetes-dashboard_kubernetes-dashboard-779776cb65-h8gql_kubernetes-dashboard_b638ee89-615c-462c-a877-49d78471e24c_0
...
4. 集群交互
如果已经安装了kubectl( 安装教程),可以直接使用:
kubectl get po -A
也可以使用minikube下载合适的kubectl版本:
minikube kubectl -- get po -A
可以设置命令别名,详情参考 kubectl:
alias kubectl="minikube kubectl --"
minikube同时集成了dashboard,可以通过下面的命令使用:
test@ubuntu-svr:~$ minikube dashboard
* Enabling dashboard ...- Using image docker.io/kubernetesui/metrics-scraper:v1.0.8- Using image docker.io/kubernetesui/dashboard:v2.7.0
* Some dashboard features require the metrics-server addon. To enable all features please run:minikube addons enable metrics-server* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
* Opening http://127.0.0.1:34747/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...http://127.0.0.1:34747/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
配置ssh端口转发,将本地的34747端口转发到上述宿主机节点的34747端口:ssh -f -N -L 34747:localhost:34747 test@192.168.0.157
成功配置后,本地浏览器访问http://127.0.0.1:34747/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
即可打开dashboard面板。
kubeconfig文件路径如下,注意server指向的地址:
test@ubuntu-svr:~$ cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:certificate-authority: /home/test/.minikube/ca.crtextensions:- extension:last-update: Sun, 16 Jun 2024 05:43:41 UTCprovider: minikube.sigs.k8s.ioversion: v1.33.1name: cluster_infoserver: https://192.168.49.2:8443name: minikube
contexts:
- context:cluster: minikubeextensions:- extension:last-update: Sun, 16 Jun 2024 05:43:41 UTCprovider: minikube.sigs.k8s.ioversion: v1.33.1name: context_infonamespace: defaultuser: minikubename: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikubeuser:client-certificate: /home/test/.minikube/profiles/minikube/client.crtclient-key: /home/test/.minikube/profiles/minikube/client.key
5. 部署和管理应用
5.1 部署一个示例应用
使用以下命令部署一个示例应用(如Nginx):
kubectl create deployment hello-minikube --image=kicbase/echo-server:1.0
5.2 暴露服务
创建一个服务来暴露部署的应用:
kubectl expose deployment hello-minikube --type=NodePort --port=8080
kubectl get services hello-minikube
5.3 访问应用
你可以通过Minikube的服务URL来访问应用:
test@ubuntu-svr:~$ minikube service hello-minikube
|-----------|----------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|----------------|-------------|---------------------------|
| default | hello-minikube | 8080 | http://192.168.49.2:30984 |
|-----------|----------------|-------------|---------------------------|
* Opening service default/hello-minikube in default browser...http://192.168.49.2:30984
使用kubectl配置端口转发,将宿主机的7080端口转发到hello-minikube应用的8080端口:
kubectl port-forward service/hello-minikube 7080:8080
# 宿主机访问hello-minikube应用
test@ubuntu-svr:~$ curl http://localhost:7080/
Request served by hello-minikube-5c898d8489-ngvc6HTTP/1.1 GET /Host: localhost:7080
Accept: */*
User-Agent: curl/7.81.0
宿主机为VMware workstation的一台虚拟机,可以进一步配置端口转发,再次在PC本地配置ssh转发,转发客户端的7080端口到宿主机的7080端口。
ssh -f -N -L 7080:localhost:7080 test@192.168.0.157
使用pc浏览器访问 http://localhost:7080/即可查看hello-minikube应用。
6. 管理集群
查看集群状态
minikube status
暂停集群:
minikube pause
恢复集群:
minikube unpause
停止Minikube
minikube stop
设置默认的内存资源限制(需要重启):
minikube config set memory 4096
查看安装的k8s服务列表,minikube addons list
:
test@ubuntu-svr:~$ minikube addons list
|-----------------------------|----------|--------------|--------------------------------|
| ADDON NAME | PROFILE | STATUS | MAINTAINER |
|-----------------------------|----------|--------------|--------------------------------|
| ambassador | minikube | disabled | 3rd party (Ambassador) |
| auto-pause | minikube | disabled | minikube |
| cloud-spanner | minikube | disabled | Google |
| csi-hostpath-driver | minikube | disabled | Kubernetes |
| dashboard | minikube | enabled ✅ | Kubernetes |
| default-storageclass | minikube | enabled ✅ | Kubernetes |
| efk | minikube | disabled | 3rd party (Elastic) |
| freshpod | minikube | disabled | Google |
| gcp-auth | minikube | disabled | Google |
| gvisor | minikube | disabled | minikube |
| headlamp | minikube | disabled | 3rd party (kinvolk.io) |
| helm-tiller | minikube | disabled | 3rd party (Helm) |
| inaccel | minikube | disabled | 3rd party (InAccel |
| | | | [info@inaccel.com]) |
| ingress | minikube | disabled | Kubernetes |
| ingress-dns | minikube | disabled | minikube |
| inspektor-gadget | minikube | disabled | 3rd party |
| | | | (inspektor-gadget.io) |
| istio | minikube | disabled | 3rd party (Istio) |
| istio-provisioner | minikube | disabled | 3rd party (Istio) |
| kong | minikube | disabled | 3rd party (Kong HQ) |
| kubeflow | minikube | disabled | 3rd party |
| kubevirt | minikube | disabled | 3rd party (KubeVirt) |
| logviewer | minikube | disabled | 3rd party (unknown) |
| metallb | minikube | disabled | 3rd party (MetalLB) |
| metrics-server | minikube | disabled | Kubernetes |
| nvidia-device-plugin | minikube | disabled | 3rd party (NVIDIA) |
| nvidia-driver-installer | minikube | disabled | 3rd party (Nvidia) |
| nvidia-gpu-device-plugin | minikube | disabled | 3rd party (Nvidia) |
| olm | minikube | disabled | 3rd party (Operator Framework) |
| pod-security-policy | minikube | disabled | 3rd party (unknown) |
| portainer | minikube | disabled | 3rd party (Portainer.io) |
| registry | minikube | disabled | minikube |
| registry-aliases | minikube | disabled | 3rd party (unknown) |
| registry-creds | minikube | disabled | 3rd party (UPMC Enterprises) |
| storage-provisioner | minikube | enabled ✅ | minikube |
| storage-provisioner-gluster | minikube | disabled | 3rd party (Gluster) |
| storage-provisioner-rancher | minikube | disabled | 3rd party (Rancher) |
| volumesnapshots | minikube | disabled | Kubernetes |
| yakd | minikube | disabled | 3rd party (marcnuri.com) |
创建一个老版本的k8s集群:
minikube start -p aged --kubernetes-version=v1.16.1
删除Minikube集群
minikube delete --all
7. 高级配置和常见问题
7.1 配置资源限制
你可以在启动Minikube时配置CPU和内存资源:
minikube start --cpus=4 --memory=8192
7.2 配置容器运行时
Minikube支持多种容器运行时,可以使用--container-runtime
选项来指定:
minikube start --container-runtime=cri-o
7.3 常见问题
Q: 启动Minikube时遇到虚拟化问题?
A: 确保你的系统支持虚拟化,并且已启用。你可以在BIOS设置中启用虚拟化技术(如Intel VT-x或AMD-V)。
Q: 如何重新配置Minikube?
A: 你可以使用minikube config
命令来设置和查看Minikube的配置。例如,设置默认的内存大小:
minikube config set memory 8192
Q: Minikube是否支持高可用集群及多节点配置?
A:支持,详情参考:
Using Multi-Control Plane - HA Clusters | minikube (k8s.io)
Using Multi-Node Clusters | minikube (k8s.io)
8. 总结
Minikube是一个强大的工具,可以在本地快速搭建一个Kubernetes集群,帮助开发者在本地进行开发和测试。通过本文的介绍,你应该能够顺利安装、配置和使用Minikube,并了解一些高级配置和常见问题的解决方法。希望这篇文章对你有所帮助,祝你在Kubernetes的学习和使用中取得成功!
更多信息请访问 Minikube官方文档。