Minikube 是什么
Minikube是一个可以快速构建一个单节点本地运行Kubernetes环境的一个工具,它专注于让快速创建本地Kubernetes环境,用于学习和开发。
抛去多集群有关的特性,Minukube构建的单节点集群,足以使用户探索和发现Kubernate的绝大多数主要功能。
安装Minikube
Minikube支持安装在OSX, Windows, Linux。
前置条件
资源 | 说明 |
---|---|
CPU | 2 |
内存 | 2G |
硬盘 | 20G |
网络连接 | |
容器 /虚拟机管理器 | Docker, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMWare |
以下就举例在Mac 11.4 上的安装步骤:
安装在本地mac上
brew install minikube
安装docker
目前看只有Docker Desktop for Mac (macOS)可以下载, Docker.dmg
安装hyperkit
brew install hyperkit
安装 kubectl(K8s的CLI工具):
brew install kubectl
运行Minikube
minikube start --driver=hyperkit
你会看到以下信息,表明启动成功。
XXXX % minikube start
😄 minikube v1.21.0 on Darwin 11.4
✨ Using the hyperkit driver based on existing profile
👍 Starting control plane node minikube in cluster minikube
🔄 Restarting existing hyperkit VM for "minikube" ...
🎉 minikube 1.22.0 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.22.0
💡 To disable this notice, run: 'minikube config set WantUpdateNotification false'
❗ This VM is having trouble accessing https://k8s.gcr.io
💡 To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
🐳 Preparing Kubernetes v1.20.7 on Docker 20.10.6 ...
🔎 Verifying Kubernetes components...
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
▪ Using image kubernetesui/dashboard:v2.1.0
▪ Using image kubernetesui/metrics-scraper:v1.0.4
🌟 Enabled addons: storage-provisioner, default-storageclass, dashboard
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
可以使用以下命令,查看集群状态:
kubectl get nodes
查看集群状态:
XXXX % kubectl cluster-info
Kubernetes control plane is running at https://192.168.XX.XX:8443
KubeDNS is running at https://192.168.XX.XX:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
启动 Dashboard
minikube dashboard
你可以看到以下信息,并且浏览器会自动打开dashboard页面
% minikube dashboard
🤔 Verifying dashboard health ...
🚀 Launching proxy ...
🤔 Verifying proxy health ...
🎉 Opening http://127.0.0.1:60595/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
运行sample
kubectl create deployment hello-minikube --image=docker.io/helloworldcontainers/java-spring-boot-hello-world:latest
kubectl expose deployment hello-minikube --type=NodePort --port=8080
这个可能会需要几分钟, 可以查看Pod状态,来查看进度。
kubectl get pods
错误排查
在运行中发现pods pull images无法成功pull到。
错误描述
···
NAME READY STATUS RESTARTS AGE
hello-minikube-7687f858f5-6h7d4 0/1 ErrImagePull 0 20s
hello-minikube-7f4c5f87b4-88zhq 0/1 ImagePullBackOff 0 6m27s
···
使用kubectl describe pods:
kubectl describe pods hello-minikube-7f4c5f87b4-88zhq
得到如下信息吧:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 40s default-scheduler Successfully assigned default/hello-minikube-549948974b-xnd5s to minikube
Warning Failed 30s kubelet Failed to pull image "helloworldcontainers/java-spring-boot-hello-world:latest": rpc error: code = Unknown desc = Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 192.168.64.1:53: read udp 192.168.64.2:58852->192.168.64.1:53: i/o timeout
Normal BackOff 29s kubelet Back-off pulling image "helloworldcontainers/java-spring-boot-hello-world:latest"
Warning Failed 29s kubelet Error: ImagePullBackOff
Normal Pulling 19s (x2 over 41s) kubelet Pulling image "helloworldcontainers/java-spring-boot-hello-world:latest"
Warning Failed 9s (x2 over 30s) kubelet Error: ErrImagePull
Warning Failed 9s kubelet Failed to pull image "helloworldcontainers/java-spring-boot-hello-world:latest": rpc error: code = Unknown desc = Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 192.168.64.1:53: read udp 192.168.64.2:54170->192.168.64.1:53: i/o timeout
尝试了把images 先用docker pull本地,然后修改deployment的imagePullPolicy:IfNotPresent。
docker pull helloworldcontainers/java-spring-boot-hello-world:latest
kubectl edit deployment hello-minikube
spec:
containers:
- image: helloworldcontainers/java-spring-boot-hello-world:latest
imagePullPolicy: IfNotPresent
name: java-spring-boot-hello-world
是种不行, 感觉应该是minukebe和docker之间的网络问题, google了一下,发现了个解决方法,具体可以参考:
检查运行状态
XXX % kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-6c8744f68d-bttlx 1/1 Running 0 9m11s
XXX % kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube NodePort XX.XX.XX.XXX <none> 8080:30086/TCP 34s
kubernetes ClusterIP 10.XX.0.1 <none> 443/TCP 11d
访问pod里的应用
得到host node的IP
kubectl get node -owide
如下
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
minikube Ready control-plane,master 11d v1.20.7 192.168.64.2 <none> Buildroot 2020.02.12 4.19.182 docker://20.10.6
lizhenjun@192 issues-2 % kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube NodePort 10.97.242.173 <none> 8080:30086/TCP 15m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d
curl 服务:
curl http://192.168.64.2:30086
得到如下结果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello, World</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>%
总结
Minikube是个非常方便的工具用于初学者的k8s学习,对于开发者,可以快速进行本地开发和测试k8s应用。总而言之,Minikube是个k8s从业者的启航船。
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/