Windows 7 下使用 MiniKube 学习 Kubernetes

参考自: https://www.chenshaowen.com/blog/how-to-use-minikube-under-windows7.html

安装Chocolatey,直接打开cmd命令行,贴入以下命令。

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

安装 Kubectl
在官方文档中,详细描述了各种系统中如何安装 kubectl。

$ choco install kubernetes-cli

Chocolatey v0.10.11
Installing the following packages:
kubernetes-cli
By installing you accept licenses for the packages.
Progress: Downloading kubernetes-cli 1.11.3… 100%

$ kubectl version

Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T18:02:47Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"windows/amd64"}

Unable to connect to the server: dial tcp 127.0.0.1:8080: connectex: No connection could be made because the target machine actively refused it.

由于服务没有启动,会提示无法连接。

安装 MiniKube
在 MiniKube 的 GitHub 仓库 中,详细描述了各种系统中如何安装 MiniKube。MiniKube 是使用 Go 语言开发的。在 Windows 上可以直接下载 MiniKube 可执行程序安装,也可以使用 Chocolatey 包管理工具安装。

$ choco install minikube
 Chocolatey v0.10.11
 Installing the following packages:
 minikube
 By installing you accept licenses for the packages.
 Progress: Downloading Minikube 0.28.2… 100%
 Minikube v0.28.2 [Approved]
 minikube package files install completed. Performing other installation steps.
  ShimGen has successfully created a shim for minikube.exe
  The install of minikube was successful.
   Software install location not explicitly set, could be in package or
   default install location if installer.

创建并启动集群

使用 minikube start 命令,可以创建并配置一个运行单节点 Kubernetes 集群的虚拟机,同时配置 kubectl 以与此集群进行通信。注意由 于国内大陆gcr.io被墙了。所以需要用到国内仓库地址–image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

minikube start -p minikube --registry-mirror=https://xxx.mirror.aliyuncs.com  --memory=4096 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
 minikube v1.2.0 on windows (amd64)
 using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
 Creating virtualbox VM (CPUs=2, Memory=4096MB, Disk=20000MB) …
 Found network options:
 NO_PROXY=localhost,127.0.0.1,10.96.0.0/12,192.168.99.0/24,192.168.39.0/24,192.168.99.104
 Configuring environment for Kubernetes v1.15.0 on Docker 18.09.6
 env NO_PROXY=localhost,127.0.0.1,10.96.0.0/12,192.168.99.0/24,192.168.39.0/24,192.168.99.104
 Pulling images …
 Launching Kubernetes …
 Verifying: apiserver proxy etcd scheduler controller dns
 Done! kubectl is now configured to use "minikube" 

Minikube 默认使用 VirtualBox 来驱动安装,使用 –vm-driver 参数可以指定驱动。通过设置环境变量 MINIKUBE_HOME 可以指定安装目录,默认安装目录在 C:\Users\Your User Name.minikube。

检查节点是否正常:

#kubectl get nodes
 NAME       STATUS    ROLES     AGE       VERSION
 minikube   Ready     master    1d        v1.10.0

拉取镜像
如果网络受限,不能打开 gcr.io 页面。有两种方法可以解决镜像拉取不到的问题。由于我的使用环境,能够科学上网,这里只提供一下解决的思路。

第一种,配置代理

minikube start –docker-env HTTP_PROXY=127.0.0.1:1080

如果找到可用的代理,不是件很容易的事,可以试一下第二种方法。

第二种,通过阿里云镜像下载并打 tag

$ minikube ssh
$ docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/kube-addon-manager-amd64:v6.1
$ docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/kube-addon-manager-amd64:v6.1 gcr.io/google-containers/kube-addon-manager:v6.1

第三种,通过阿里云仓库地址

  minikube start  --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers 

测试一下

$ kubectl run kube-nginx --image=nginx:latest --port=80
 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
 deployment.apps/kube-nginx created
#将deployment作为一个服务部署
 $kubectl expose deployment kube-nginx --type=NodePort
 service/kube-nginx exposed
#读取当前的pod列表
 $ kubectl get pods
 NAME                          READY   STATUS    RESTARTS   AGE
 kube-nginx-7c765ffd95-pxjth   1/1     Running   0          37s
#获取服务使用的URL
 $ minikube service kube-nginx --url
 http://192.168.99.106:30102
#删除deployment
$kubectl.exe delete deployment.apps/kube-nginx
 deployment.apps "kube-nginx" deleted
#删除服务
$kubectl.exe delete service kube-nginx
 service "kube-nginx" deleted

MiniKube 基本操作命令

启动集群

$ minikube start

查看状态

$ minikube status

minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100

查看日志

$ minikube logs

关闭集群

$ minikube stop

Stopping local Kubernetes cluster…
Stopping "minikube"…

删除集群

$ minikube delete

查看 IP

$ minikube ip

192.168.99.100

查看 Dashboard

$ minikube dashboard

 Enabling dashboard …
 Verifying dashboard health …
 Launching proxy …
 Verifying proxy health …
 Opening http://127.0.0.1:52666/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/ in your default browser… 

4. 参考

发表评论

电子邮件地址不会被公开。 必填项已用*标注