Minikube安装成功Kubernetes,一次过!

介绍

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

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

你们在实际开发和体验 Kubernetes时,可能会遇到网络访问的缘由或者其余的坑致使不少朋友没法使用minikube进行实验。所以我经过撰写这篇博客,让你们少走一点弯路。阿里云提供了一个修改版的Minikube,能够从阿里云的镜像地址来获取Docker镜像和配置。linux

坑位盘点

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

    终端报错信息:github

    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出现如上报错。web

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

  2. Minikube doesn't run on VirtualBoxjson

    终端报错信息:ubuntu

    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中被禁用。windows

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

    参考文档: jingyan.baidu.com/article/fc0…

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

    终端报错信息

    [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开发环境的支持 yq.aliyun.com/articles/50…,你们也能够试用

配置

先决条件

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

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

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引擎

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本地环境

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版本

# 安装Kubernetes v1.12.1
minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1
复制代码

打开Kubernetes控制台

minikube dashboard

复制代码

image

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

create

hyper_v

以后,咱们能够用以下命令来建立基于Hyper-V的Kubernetes测试环境

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

复制代码

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

Minikube基本操做

检测集群状态,运行:

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

查看配置文件内容:

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状态:

kubectl get nodes

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

复制代码

使用ssh进入Minikube虚机:

sudo minikube ssh

复制代码
_             _            
            _         _ ( )           ( )           
  ___ ___  (_)  ___  (_)| |/') _ _ | |_ __ /' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\  /'__`\ | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/ (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____) $ 复制代码

中止运行中的kubernetes集群:

$ minikube stop

复制代码

删除本地的kubernetes集群:

$ minikube delete

复制代码

打开Kubernetes控制台

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

$ 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

复制代码

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

$ minikube dashboard

复制代码

获取仪表板的URL

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

复制代码

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

使用Minikube

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

4

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

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

参考链接:

相关文章
相关标签/搜索