Minikube安装成功Kubernetes,一次过!
<h2 id="介绍">介绍</h2><p><img src="https://user-gold-cdn.xitu.io/2019/7/22/16c19bbdaba1b047?w=3600&h=3493&f=png&s=326187" alt="" loading="lazy"></p>
<p>Minikube 是 K8S 官方为了开发者能在个人电脑上运行 K8S 而提供的一套工具。实现上是通过 Go 语言编写,通过调用虚拟化管理程序,创建出一个运行在虚拟机内的单节点集群。</p>
<blockquote>
<p>注:从这里也可以看出,对于 K8S 集群的基本功能而言,节点数并没有什么限制。只有一个节点同样可以创建集群。</p>
</blockquote>
<p>大家在实际开发和体验 Kubernetes时,可能会遇到网络访问的原因或者其他的坑导致很多朋友无法使用minikube进行实验。因此我通过撰写这篇博客,让大家少走一点弯路。阿里云提供了一个修改版的Minikube,可以从阿里云的镜像地址来获取Docker镜像和配置。</p>
<h2 id="坑位盘点">坑位盘点</h2>
<ol>
<li>
<p>unable to cache ISO: connection failed because connected host has failed to respond.</p>
<p>终端报错信息:</p>
<pre><code class="language-bash">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.
</code></pre>
<p><strong>报错原因: 由于无法从外网下载Minikube ISO出现如上报错。</strong></p>
<p><strong>解决办法: 修改镜像地址为国内资源,解决网络错误。</strong></p>
</li>
<li>
<p>Minikube doesn't run on VirtualBox</p>
<p>终端报错信息:</p>
<pre><code>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
</code></pre>
<p><strong>报错原因: VT-x/AMD-v虚拟化在所有CPU中被禁用。</strong></p>
<p><strong>解决方法: VT-x/AMD-v虚拟化必须在BIOS中开启。</strong></p>
<blockquote>
<p>参考文档: https://jingyan.baidu.com/article/fc07f98976710e12ffe519de.html</p>
</blockquote>
</li>
<li>
<p> Error getting state for host: machine does not exist</p>
<p>终端报错信息</p>
<pre><code> 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: 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
</code></pre>
<p><strong>报错原因: 已经被使用过VM</strong></p>
<p><strong>解决办法: <code>minikute delete</code></strong></p>
</li>
</ol>
<p>注:</p>
<ul>
<li>
<p>本文已更新到 Minikube v1.2.0/Kubernetes v1.15+</p>
</li>
<li>
<p>如需更新minikube,需要更新 minikube 安装包</p>
<ul>
<li><code>minikube delete</code> 删除现有虚机,删除 <code>~/.minikube</code> 目录缓存的文件</li>
<li>重新创建 minikube 环境</li>
</ul>
</li>
<li>
<p>Docker社区版也为Mac/Windows用户提供了Kubernetes开发环境的支持 https://yq.aliyun.com/articles/508460,大家也可以试用</p>
</li>
</ul>
<h2 id="配置">配置</h2>
<h3 id="先决条件">先决条件</h3>
<ul>
<li>安装 kubectl</li>
</ul>
<p>Minikube在不同操作系统上支持不同的驱动</p>
<ul>
<li>macOS
<ul>
<li>xhyve driver, VirtualBox 或 VMware Fusion</li>
</ul>
</li>
<li>Linux
<ul>
<li>VirtualBox 或 KVM</li>
<li><strong>NOTE:</strong> Minikube 也支持 <code>--vm-driver=none</code> 选项来在本机运行 Kubernetes 组件,这时候需要本机安装了 Docker。在使用 0.27版本之前的 none 驱动时,在执行 <code>minikube delete</code> 命令时,会移除 /data 目录,请注意,问题说明;另外 none 驱动会运行一个不安全的API Server,会导致安全隐患,不建议在个人工作环境安装。</li>
</ul>
</li>
<li>Windows
<ul>
<li>VirtualBox 或 Hyper-V - 请参考下文</li>
</ul>
</li>
</ul>
<p>注:</p>
<ul>
<li>由于minikube复用了docker-machine,在其软件包中已经支持了相应的VirtualBox, VMware Fusion驱动</li>
<li>VT-x/AMD-v 虚拟化必须在 BIOS 中开启</li>
<li>在Windows环境下,如果开启了Hyper-V,不支持VirtualBox方式</li>
</ul>
<h3 id="kubernetes-114-release">Kubernetes 1.14+ release</h3>
<p>我们提供了最新的Minikube修改版的文件,可以直接下载使用</p>
<p><strong>Mac OSX</strong></p>
<pre><code>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/
</code></pre>
<p><strong>Linux</strong></p>
<pre><code>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/
</code></pre>
<p><strong>Windows</strong></p>
<p>下载 minikube-windows-amd64.exe 文件,并重命名为 <code>minikube.exe</code></p>
<h3 id="自己构建">自己构建</h3>
<p>也可以从Github上获取相应的项目自行构建。</p>
<p>注:需要本地已经安装配置好 Golang 开发环境和Docker引擎</p>
<pre><code>git clone https://github.com/AliyunContainerService/minikube
cd minikube
git checkout aliyun-v1.2.0
make
sudo cp out/minikube /usr/local/bin/
</code></pre>
<h3 id="启动">启动</h3>
<p>缺省Minikube使用VirtualBox驱动来创建Kubernetes本地环境</p>
<pre><code class="language-bash">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"
</code></pre>
<p>支持不同的Kubernetes版本</p>
<pre><code># 安装Kubernetes v1.12.1
minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1
</code></pre>
<p>打开Kubernetes控制台</p>
<pre><code>minikube dashboard
</code></pre>
<p><img src="https://user-gold-cdn.xitu.io/2019/7/22/16c19badf02d13cb?w=1409&h=721&f=png&s=71807" alt="image" loading="lazy"></p>
<p>对于使用Hyper-V环境的用户,首先应该打开Hyper-V管理器创建一个外部虚拟交换机,</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/7/22/16c19badf27ae447?w=842&h=846&f=png&s=28307" alt="create" loading="lazy"></p>
<p><img src="https://user-gold-cdn.xitu.io/2019/7/22/16c19badf2810332?w=842&h=846&f=png&s=44932" alt="hyper_v" loading="lazy"></p>
<p>之后,我们可以用如下命令来创建基于Hyper-V的Kubernetes测试环境</p>
<pre><code>.\minikube.exe start --registry-mirror=https://registry.docker-cn.com --vm-driver="hyperv" --memory=4096 --hyperv-virtual-switch="MinikubeSwitch"
</code></pre>
<p>注:需要管理员权限来创建Hyper-V虚拟机</p>
<h2 id="minikube基本操作">Minikube基本操作</h2>
<p>检测集群状态,运行:</p>
<pre><code class="language-bash"> 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'.
</code></pre>
<p>Ubuntu下,Minikube的配置文件在如下路径</p>
<blockquote>
<p>~/.minikube/machines/minikube/config.json</p>
</blockquote>
<p>查看配置文件内容:</p>
<pre><code class="language-bash">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
</code></pre>
<blockquote>
<p>检验Node状态:</p>
</blockquote>
<pre><code class="language-bash">kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 11m v1.15.0
</code></pre>
<blockquote>
<p>使用ssh进入Minikube虚机:</p>
</blockquote>
<pre><code class="language-bash">sudo minikube ssh
</code></pre>
<pre><code class="language-bash"> _ _
_ _ ( ) ( )
___ ___(_)___(_)| |/')_ _ | |_ __
/' _ ` _ `\| |/' _ `\| || , <( ) ( )| '_`\/'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
$
</code></pre>
<blockquote>
<p>停止运行中的kubernetes集群:</p>
</blockquote>
<pre><code>$ minikube stop
</code></pre>
<blockquote>
<p>删除本地的kubernetes集群:</p>
</blockquote>
<pre><code>$ minikube delete
</code></pre>
<h3 id="打开kubernetes控制台">打开Kubernetes控制台</h3>
<p>Kubernete附带一个web,允许您在不与命令行交互的情况下管理集群。在minikube上默认安装并启用仪表板插件</p>
<pre><code class="language-bash">$ 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
</code></pre>
<p>要直接在默认浏览器上打开,请使用:</p>
<pre><code>$ minikube dashboard
</code></pre>
<p>获取仪表板的URL</p>
<pre><code>$ minikube dashboard --url
http://192.168.39.117:30000
</code></pre>
<p>通过打开您最喜欢的浏览器上的URL访问Kubernetes Dashboard。进一步阅读,请查看:</p>
<ul>
<li>
<p>你好Minikube系列: https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/</p>
</li>
<li>
<p>minkube新手指南: https://kubernetes.io/docs/getting-started-guides/minikube/</p>
</li>
</ul>
<h2 id="使用minikube">使用Minikube</h2>
<p>Minikube利用本地虚拟机环境部署Kubernetes,其基本架构如下图所示。<br>
<img src="https://user-gold-cdn.xitu.io/2019/7/22/16c19badf47898f6?w=1024&h=768&f=jpeg&s=103363" alt="4" loading="lazy"></p>
<p>用户使用Minikube CLI管理虚拟机上的Kubernetes环境,比如:启动,停止,删除,获取状态等。一旦Minikube虚拟机启动,用户就可以使用熟悉的Kubectl CLI在Kubernetes集群上执行操作。</p>
<p>好了,开始探索Kubernetes的世界吧!😃</p>
<p>参考连接:</p>
<ul>
<li>https://computingforgeeks.com/how-to-install-minikube-on-ubuntu-18-04/</li>
<li>https://www.virtualbox.org/wiki/Linux_Downloads</li>
<li>https://github.com/kubernetes/minikube/issues/3922</li>
<li>https://stackoverflow.com/questions/55988282/minikube-doesnt-run-on-virtualbox</li>
<li>https://github.com/AliyunContainerService/minikube/releases</li>
<li>https://yq.aliyun.com/articles/221687</li>
<li>https://jingyan.baidu.com/article/fc07f98976710e12ffe519de.html</li>
<li>https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors</li>
<li>https://github.com/kubernetes/minikube/issues/3864</li>
</ul>
</div>
<div id="MySignature" role="contentinfo">
热爱生活,热爱开源!
Java/golang/生活/爱好
日斜柳暗花明,醉卧春色少年
年少,年少,行乐直须尽早<br><br>
来源:https://www.cnblogs.com/sanshengshui/p/11228985.html
頁:
[1]