mac 单机搭建 k8s 测试环境

Install brew

brew 是 Mac 程序员的必备工具,安装如下:

  1. /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

更多的内容可以到官网学习 (自带中文): https://brew.sh/index_zh-cn

Install minikube

底层采用 hyperkit 虚拟机(也可以用其他 比如 VirtualBox、VMware Fusion)

brew install hyperkit

安装

brew install minikube

启动

官方的推荐文档里面是很简单的一句话:

minikube start

但是在安装的过程中就会出现如下的错误

unable to cache ISO: connection failed because connected host has failed to respond.

报错原因: 由于无法从外网下载 Minikube ISO 出现如上报错。
解决办法: 修改镜像地址为国内资源,解决网络错误。
这些参数可以通过minikube help start获取详细介绍
默认:—iso-url=’https://storage.googleapis.com/minikube/iso/minikube-v1.7.3.iso:‘ Location of the minikube iso.
通过 --iso-url='https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.7.3.iso' 更改到阿里镜像
所以启动改成如下配置:

minikube start --iso-url='https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.7.3.iso'

这个时候又会遇到新的问题,域名没办法解析

⚠️  VM is unable to access k8s.gcr.io, you may need to configure a proxy or set --image-repository
🐳  Preparing Kubernetes v1.17.3 on Docker 19.03.6 ...
E0229 14:40:59.427587    2227 cache.go:62] save image to file "k8s.gcr.io/coredns:1.6.5" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/coredns_1.6.5" failed: nil image for k8s.gcr.io/coredns:1.6.5: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.438578    2227 cache.go:62] save image to file "gcr.io/k8s-minikube/storage-provisioner:v1.8.1" -> "/Users/astaxie/.minikube/cache/images/gcr.io/k8s-minikube/storage-provisioner_v1.8.1" failed: nil image for gcr.io/k8s-minikube/storage-provisioner:v1.8.1: Get https://gcr.io/v2/: dial tcp 108.177.125.82:443: i/o timeout
E0229 14:40:59.508189    2227 cache.go:62] save image to file "k8s.gcr.io/kube-scheduler:v1.17.3" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/kube-scheduler_v1.17.3" failed: nil image for k8s.gcr.io/kube-scheduler:v1.17.3: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.556392    2227 cache.go:62] save image to file "k8s.gcr.io/pause:3.1" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/pause_3.1" failed: nil image for k8s.gcr.io/pause:3.1: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.726322    2227 cache.go:62] save image to file "k8s.gcr.io/kube-controller-manager:v1.17.3" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/kube-controller-manager_v1.17.3" failed: nil image for k8s.gcr.io/kube-controller-manager:v1.17.3: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.732730    2227 cache.go:62] save image to file "k8s.gcr.io/etcd:3.4.3-0" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/etcd_3.4.3-0" failed: nil image for k8s.gcr.io/etcd:3.4.3-0: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.751679    2227 cache.go:62] save image to file "k8s.gcr.io/kube-proxy:v1.17.3" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/kube-proxy_v1.17.3" failed: nil image for k8s.gcr.io/kube-proxy:v1.17.3: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.776561    2227 cache.go:62] save image to file "k8s.gcr.io/kube-apiserver:v1.17.3" -> "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/kube-apiserver_v1.17.3" failed: nil image for k8s.gcr.io/kube-apiserver:v1.17.3: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout
E0229 14:40:59.776665    2227 cache.go:88] Error caching images:  Caching images for kubeadm: caching images: caching image "/Users/astaxie/.minikube/cache/images/k8s.gcr.io/coredns_1.6.5": nil image for k8s.gcr.io/coredns:1.6.5: Get https://k8s.gcr.io/v2/: dial tcp 64.233.189.82:443: i/o timeout

通过错误提示可以看出来很多镜像去拉取了 k8s.gcr.io 上面的,心想要解决这个问题必然是修改 image 地址了,继续打开 help 查看,发现了这个参数和这句话

--image-mirror-country='': Country code of the image mirror to be used. Leave empty to use the global one. For
Chinese mainland users, set it to cn.

官方针对中国特意写了这个 cn,大家一下子就懂了,这个时候 iso 已经下载完成了,所以启动就不带 iso 了,然后直接通过如下参数启动了

minikube start --image-mirror-country cn

等待两三分钟之后终于看到了这句话:

😄  minikube v1.7.3 on Darwin 10.15.3
✨  Using the hyperkit driver based on existing profile
⌛  Reconfiguring existing host ...
🔄  Starting existing hyperkit VM for "minikube" ...
⚠️  VM is unable to access k8s.gcr.io, you may need to configure a proxy or set --image-repository
🐳  Preparing Kubernetes v1.17.3 on Docker 19.03.6 ...
🚀  Launching Kubernetes ...
🌟  Enabling addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube"

打开 Kubernetes 控制台

minikube dashboard


k8s - 图1

总结

最后附上 minikube 的架构图

k8s - 图2
用户使用 Minikube CLI 管理虚拟机上的 Kubernetes 环境,比如:启动,停止,删除,获取状态等。一旦 Minikube 虚拟机启动,用户就可以使用熟悉的 Kubectl CLI 在 Kubernetes 集群上执行操作。
好了,开始探索 Kubernetes 的世界吧!:-)