解决 Mac 安装最新版 minikube 出现的问题

kubernetes (本文后续简称 k8s ),是一个开源的,用于管理云平台中多个主机上的容器化的应用。Mac 本地运行 k8s 最快的方式,肯定是基于单节点的 minikube 来搭建。人人都说 minikube 搭建 k8s 最简单、快速、只需要傻瓜式输入命令就可以搭建成功,可事实并非如此,比如笔者在搭建的过程中就遇到了 版本代理 的问题。

下面我将对遇到的问题一一进行解答。

1. 硬件环境

  • 系统:MacOS Mojave 10.14.2 (18C54)

2. 软件环境

2.1. 虚拟机

这里我使用的是 VirtualBox,使用版本Virtual Box 5.2.22 r126460 (Qt5.6.3),当然也可以使用其他虚拟软件,参考:https://kubernetes.io/docs/tasks/tools/install-minikube/#install-a-hypervisor

2.2. 安装 kubectl

笔者安装的时候,最新版本为 v1.13.1

有2种安装方式:

  1. 使用 brew 安装,brew install kubernetes-cli

  2. 使用二进制方式安装

    • 下载二进制文件
      curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.13.1/bin/darwin/amd64/kubectl
    • 修改文件权限
      chmod +x ./kubectl
    • 移动文件到用户 bin 目录
      sudo mv ./kubectl /usr/local/bin/kubectl

2.3. 安装 minikube

笔者安装的时候,最新版本为 v0.32.0

有2种安装方式:

  1. 使用 brew 安装,brew cask install minikube

  2. 使用二进制方式安装

    • 下载二进制文件并修改文件权限
      curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.32.0/minikube-darwin-amd64 \ && chmod +x minikube
    • 移动文件到用户 bin 目录
      sudo cp minikube /usr/local/bin && rm minikube

3. 使用 minikube 启动 k8s

minikube start

4. 💣 异常 💣

  1. minikube 默认使用的 k8s 版本与本地 kubecli 版本不一致,部分错误日志如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    $ minikube start
    Starting local Kubernetes v1.12.4 cluster...
    Starting VM...
    Downloading Minikube ISO
    178.88 MB / 178.88 MB [============================================] 100.00% 0s
    Getting VM IP address...
    E0109 17:30:57.090762 47169 start.go:211] Error parsing version semver: Version string empty
    Moving files into cluster...
    Downloading kubeadm v1.12.4
    Downloading kubelet v1.12.4
    Finished Downloading kubeadm v1.12.4
    Finished Downloading kubelet v1.12.4
    Setting up certs...
    Connecting to cluster...
    Setting up kubeconfig...
    Stopping extra container runtimes...
    Starting cluster components...
    E0109 17:38:49.423563 47169 start.go:343] Error starting cluster: kubeadm init error
    sudo /usr/bin/kubeadm init --config /var/lib/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests --ignore-preflight-errors=DirAvailable--data-minikube --ignore-preflight-errors=Port-10250 --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-etcd.yaml --ignore-preflight-errors=Swap --ignore-preflight-errors=CRI
    running command: : Process exited with status 2
    ================================================================================
    An error has occurred. Would you like to opt in to sending anonymized crash
    information to minikube to help prevent future errors?
    To opt out of these messages, run the command:
    minikube config set WantReportErrorPrompt false
    ================================================================================
    Please enter your response [Y/n]: %

    解决:
    https://github.com/kubernetes/minikube/issues/3517#issuecomment-453035044
    因为 minikube 默认使用的 v1.12.x 版本的 k8s,但是我们安装的 kubectl 版本为 v1.13.1,所以可以使用如下命令解决,minikube start --alsologtostderr --kubernetes-version v1.13.1

  2. 代理问题,因为我们伟大的祖国繁荣富强,导致无法下载k8s镜像,部分错误日志如下:

    1
    2
    3
    4
    5
    I0114 10:47:38.727892    4695 utils.go:224] ! error execution phase preflight: [preflight] Some fatal errors occurred:
    I0114 10:47:38.727931 4695 utils.go:224] ! [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver:v1.13.1: output: Error response from daemon: Get https://k8s.gcr.io/v2/: proxyconnect tcp: dial tcp :80: connect: connection refused
    I0114 10:47:38.727941 4695 utils.go:224] ! , error: exit status 1
    I0114 10:47:38.727963 4695 utils.go:224] ! [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager:v1.13.1: output: Error response from daemon: Get https://k8s.gcr.io/v2/: proxyconnect tcp: dial tcp :80: connect: connection refused
    I0114 10:47:38.727975 4695 utils.go:224] ! , error: exit status 1

    解决:
    https://github.com/kubernetes/minikube/issues/3517#issuecomment-453906287
    在启动 minikube 的时候加上代(fan)理(qiang)地址,使用 --docker-env 参数添加系统环境变量,完整命令如下:
    minikube start --alsologtostderr --kubernetes-version v1.13.1 --docker-env HTTP_PROXY=http://10.0.2.2:1087 --docker-env HTTPS_PROXY=http://10.0.2.2:1087 --docker-env NO_PROXY=10.0.2.2,$(minikube ip)
    参数解析:
    10.0.2.2 这个ip其实是因为 virtual box 里虚拟机如果使用NAT模式的网卡的时候 10.0.2.2 默认指向的是宿主机的 127.0.0.1,参考地址:https://forums.docker.com/t/beginner-having-trouble-with-docker-behind-company-proxy/3968

5. 启动

  1. 部分启动成功日志信息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    .....
    I0114 13:35:19.102229 7316 utils.go:224] > Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
    I0114 13:35:19.102246 7316 utils.go:224] > https://kubernetes.io/docs/concepts/cluster-administration/addons/
    I0114 13:35:19.102260 7316 utils.go:224] > You can now join any number of machines by running the following on each node
    I0114 13:35:19.102287 7316 utils.go:224] > as root:
    I0114 13:35:19.102307 7316 utils.go:224] > kubeadm join localhost:8443 --token agf5vb.45h4fw6jqy7o2yff --discovery-token-ca-cert-hash sha256:c60ce3456d2ac3225edc119d1792d1b6c20b40b03cf0812ddf8d2fc867f21bf3
    Verifying kubelet health ...I0114 13:35:19.134467 7316 ssh_runner.go:137] Run with output: sudo systemctl is-active kubelet
    I0114 13:35:19.141912 7316 utils.go:224] > active

    Verifying apiserver health ...I0114 13:35:19.149990 7316 kubeadm.go:99] https://192.168.99.101:8443/healthz response: <nil> &{Status:200 OK StatusCode:200 Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Date:[Mon, 14 Jan 2019 05:35:19 GMT] Content-Length:[2] Content-Type:[text/plain; charset=utf-8]] Body:0xc0004b9d00 ContentLength:2 TransferEncoding:[] Close:false Uncompressed:false Trailer:map[] Request:0xc0002da500 TLS:0xc000354000}
    Kubectl is now configured to use the cluster.
    Loading cached images from config file.


    Everything looks great. Please enjoy minikube!
  2. minikube dashboard 之后,启动 dashboad 截图:

    dashboard

-------------本文结束  感谢您的阅读-------------
xkcoding wechat
欢迎来我的公众号「xkcoding小凯扣丁」逛逛
o(╯□╰)o 赞助一杯咖啡 ~~