Minikube安装成功Kubernetes,一次过!

介绍

Minikube 是 K8S 官方为了开发者能在个人电脑上运行 K8S 而提供的一套工具。实现上是通过 Go 语言编写,通过调用虚拟化管理程序,创建出一个运行在虚拟机内的单节点集群。

注:从这里也可以看出,对于 K8S 集群的基本功能而言,节点数并没有什么限制。只有一个节点同样可以创建集群。

大家在实际开发和体验 :wheel_of_dharma: Kubernetes时,可能会遇到网络访问的原因或者其他的坑导致很多朋友无法使用minikube进行实验。因此我通过撰写这篇博客,让大家少走一点弯路。阿里云提供了一个修改版的Minikube,可以从阿里云的镜像地址来获取Docker镜像和配置。

坑位盘点

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

    终端报错信息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    minikube start

    o minikube v0.35.0 on windows (amd64)
    > Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
    @ Downloading Minikube ISO ...
    ! Unable to start VM: unable to cache ISO: https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: failed to download: failed to download to temp file: download failed: 5 error(s) occurred
    :

    * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no
    t properly respond after a period of time, or established connection failed because connected host has failed to respond.
    * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no
    t properly respond after a period of time, or established connection failed because connected host has failed to respond.
    * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no
    t properly respond after a period of time, or established connection failed because connected host has failed to respond.
    * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no
    t properly respond after a period of time, or established connection failed because connected host has failed to respond.
    * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no
    t properly respond after a period of time, or established connection failed because connected host has failed to respond.

    报错原因: 由于无法从外网下载Minikube ISO出现如上报错。

    解决办法: 修改镜像地址为国内资源,解决网络错误。

  1. Minikube doesn’t run on VirtualBox

    终端报错信息:

    1
    2
    3
    4
    5
    6
    7
    minikube start

    😄 minikube v1.0.1 on linux (amd64) 🤹 Downloading Kubernetes v1.14.1 images in the background ... 🔥 Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...

    💣 Unable to start VM: create: creating: Unable to start the VM: /usr/bin/VBoxManage startvm minikube --type headless failed: VBoxManage: error: The virtual machine 'minikube' has terminated unexpectedly during startup with exit code 1 (0x1) VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine

    😿 Sorry that minikube crashed. If this was unexpected, we would love to hear from you: 👉 https://github.com/kubernetes/minikube/issues/new

    报错原因: VT-x/AMD-v虚拟化在所有CPU中被禁用。

    解决方法: VT-x/AMD-v虚拟化必须在BIOS中开启。

    参考文档: https://jingyan.baidu.com/article/fc07f98976710e12ffe519de.html

  1. [MACHINE_DOES_NOT_EXST] Error getting state for host: machine does not exist

    终端报错信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [sudo] james 的密码: 
    😄 minikube v1.2.0 on linux (amd64)
    ⚠️ Please don't run minikube as root or with 'sudo' privileges. It isn't necessary.
    ✅ using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
    💡 Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
    E0722 14:57:51.637843 5215 start.go:559] StartHost: Error getting state for host: machine does not exist

    💣 Unable to start VM
    ❌ Error: [MACHINE_DOES_NOT_EXST] Error getting state for host: machine does not exist
    💡 Advice: Run 'minikube delete' to delete the stale VM
    ⁉️ Related issues:
    ▪ https://github.com/kubernetes/minikube/issues/3864

    😿 If the above advice does not help, please let us know:
    👉 https://github.com/kubernetes/minikube/issues/new

    报错原因: 已经被使用过VM

    解决办法: minikute delete

注:

  • 本文已更新到 Minikube v1.2.0/Kubernetes v1.15+

  • 如需更新minikube,需要更新 minikube 安装包

    • minikube delete 删除现有虚机,删除 ~/.minikube 目录缓存的文件
    • 重新创建 minikube 环境
  • Docker社区版也为Mac/Windows用户提供了Kubernetes开发环境的支持 https://yq.aliyun.com/articles/508460,大家也可以试用

配置

先决条件

Minikube在不同操作系统上支持不同的驱动

  • macOS
  • Linux
    • VirtualBoxKVM
    • NOTE: Minikube 也支持 --vm-driver=none 选项来在本机运行 Kubernetes 组件,这时候需要本机安装了 Docker。在使用 0.27版本之前的 none 驱动时,在执行 minikube delete 命令时,会移除 /data 目录,请注意,问题说明;另外 none 驱动会运行一个不安全的API Server,会导致安全隐患,不建议在个人工作环境安装。
  • Windows

注:

  • 由于minikube复用了docker-machine,在其软件包中已经支持了相应的VirtualBox, VMware Fusion驱动
  • VT-x/AMD-v 虚拟化必须在 BIOS 中开启
  • 在Windows环境下,如果开启了Hyper-V,不支持VirtualBox方式

Kubernetes 1.14+ release

我们提供了最新的Minikube修改版的文件,可以直接下载使用

Mac OSX

1
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

Linux

1
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

Windows

下载 minikube-windows-amd64.exe 文件,并重命名为 minikube.exe

自己构建

也可以从Github上获取相应的项目自行构建。

注:需要本地已经安装配置好 Golang 开发环境和Docker引擎

1
2
3
4
5
git clone https://github.com/AliyunContainerService/minikube
cd minikube
git checkout aliyun-v1.2.0
make
sudo cp out/minikube /usr/local/bin/

启动

缺省Minikube使用VirtualBox驱动来创建Kubernetes本地环境

1
2
3
4
5
6
7
8
9
10
11
minikube start --registry-mirror=https://registry.docker-cn.com

😄 minikube v1.2.0 on linux (amd64)
⚠️ Please don't run minikube as root or with 'sudo' privileges. It isn't necessary.
✅ using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
🔥 Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
🐳 Configuring environment for Kubernetes v1.15.0 on Docker 18.09.6
🚜 Pulling images ...
🚀 Launching Kubernetes ...
⌛ Verifying: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"

支持不同的Kubernetes版本

1
2
# 安装Kubernetes v1.12.1
minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1

打开Kubernetes控制台

1
minikube dashboard

image

对于使用Hyper-V环境的用户,首先应该打开Hyper-V管理器创建一个外部虚拟交换机,

create

hyper_v

之后,我们可以用如下命令来创建基于Hyper-V的Kubernetes测试环境

1
.\minikube.exe start --registry-mirror=https://registry.docker-cn.com --vm-driver="hyperv" --memory=4096 --hyperv-virtual-switch="MinikubeSwitch"

注:需要管理员权限来创建Hyper-V虚拟机

Minikube基本操作

检测集群状态,运行:

1
2
3
4
5
6
 kubectl cluster-info

Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Ubuntu下,Minikube的配置文件在如下路径

~/.minikube/machines/minikube/config.json

查看配置文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
kubectl config view

apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/james/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /home/james/.minikube/client.crt
client-key: /home/james/.minikube/client.key

检验Node状态:

1
2
3
4
kubectl get nodes

NAME STATUS ROLES AGE VERSION
minikube Ready master 11m v1.15.0

使用ssh进入Minikube虚机:

1
sudo minikube ssh
1
2
3
4
5
6
7
8
                         _             _            
_ _ ( ) ( )
___ ___ (_) ___ (_)| |/') _ _ | |_ __
/' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$

停止运行中的kubernetes集群:

1
$ minikube stop

删除本地的kubernetes集群:

1
$ minikube delete

打开Kubernetes控制台

Kubernete附带一个web,允许您在不与命令行交互的情况下管理集群。在minikube上默认安装并启用仪表板插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ minikube addons list

- addon-manager: enabled
- coredns: disabled
- dashboard: enabled
- default-storageclass: enabled
- efk: disabled
- freshpod: disabled
- heapster: disabled
- ingress: disabled
- kube-dns: enabled
- metrics-server: disabled
- registry: disabled
- registry-creds: disabled
- storage-provisioner: enabled

要直接在默认浏览器上打开,请使用:

1
$ minikube dashboard

获取仪表板的URL

1
2
$ minikube dashboard --url
http://192.168.39.117:30000

通过打开您最喜欢的浏览器上的URL访问Kubernetes Dashboard。进一步阅读,请查看:

使用Minikube

Minikube利用本地虚拟机环境部署Kubernetes,其基本架构如下图所示。
4

用户使用Minikube CLI管理虚拟机上的Kubernetes环境,比如:启动,停止,删除,获取状态等。一旦Minikube虚拟机启动,用户就可以使用熟悉的Kubectl CLI在Kubernetes集群上执行操作。

好了,开始探索Kubernetes的世界吧!:-)

参考连接:

------ 本文结束感谢您的阅读-------------
给我加点油吧!