前言

Minikube是一种可以轻松在本地(win系统)运行Kubernetes的工具。Minikube在笔记本电脑的虚拟机(VM)内运行一个单节点Kubernetes集群,以供希望试用Kubernetes或每天使用它开发的用户使用。

搭建流程

本技术文档的虚拟机管理程序使用virtaulbox

查看win主机是否支持虚拟化

打开win终端执行命令:

systeminfo
  • 注: 当出现如下信息时,表示支持虚拟化,如未有,但你的电脑主板是支持虚拟化的,请自行在bios里开启虚拟化支持,这里不再赘述
C:\WINDOWS\system32>systeminfo
... ...
Hyper-V 要求:     虚拟机监视器模式扩展: 是
                  固件中已启用虚拟化: 是
                  二级地址转换: 是
                  数据执行保护可用: 是

或者


C:\WINDOWS\system32>systeminfo
... ...
Hyper-V Requirements:     VM Monitor Mode Extensions: Yes
                          Virtualization Enabled In Firmware: Yes
                          Second Level Address Translation: Yes
                          Data Execution Prevention Available: Yes
安装virtualbox软件

请自行安装,这里不再赘述

win安装minikube工具

win终端下输入命令:

choco install minikube
  • 注: win安装choco请自行处理,这里不再赘述
minikube安装k8s单机版

win终端下输入命令:

minikube start --host-only-cidr=172.16.0.1/24 --vm-driver=virtualbox

会有如下显示,表示安装成功

C:\WINDOWS\system32>minikube start --host-only-cidr=172.16.0.1/24 --vm-driver=virtualbox
* Microsoft Windows 10 Education 10.0.18362 Build 18362 上的 minikube v1.12.0
* 根据用户配置使用 virtualbox 驱动程序
* Starting control plane node minikube in cluster minikube
* Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
! This VM is having trouble accessing https://k8s.gcr.io
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* 正在 Docker 19.03.12 中准备 Kubernetes v1.18.3…
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* 完成!kubectl 已经配置至 "minikube"
注意

当不追加–host-only-cidr,如出现如下情况时

C:\WINDOWS\system32>minikube start --vm-driver=virtualbox
* Microsoft Windows 10 Education 10.0.18362 Build 18362 上的 minikube v1.12.0
* 根据用户配置使用 virtualbox 驱动程序
* Starting control plane node minikube in cluster minikube
* Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
* minikube 1.12.1 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.12.1
* To disable this notice, run: 'minikube config set WantUpdateNotification false'

* 正在删除 virtualbox 中的“minikube”…
! StartHost failed, but will try again: creating host: create: creating: Error setting up host only network on machine start: host-only cidr conflicts with the network address of a host interface
* Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
* Failed to start virtualbox VM. "minikube start" may fix it: creating host: create: creating: Error setting up host only network on machine start: host-only cidr conflicts with the network address of a host interface
*
* [HOST_CIDR_CONFLICT] error provisioning host Failed to start host: creating host: create: creating: Error setting up host only network on machine start: host-only cidr conflicts with the network address of a host interface
* 建议:Specify an alternate --host-only-cidr value, such as 172.16.0.1/24
* Related issue: https://github.com/kubernetes/minikube/issues/3594

此时需要删除虚拟机,并执行上一步骤,删除命令如下:
win终端下输入命令:

C:\WINDOWS\system32>minikube delete
* 正在删除 virtualbox 中的“minikube”…
* Removed all traces of the "minikube" cluster.
查看k8s的集群状态

win终端下输入命令:

C:\WINDOWS\system32>minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
停止k8s

win终端下输入命令:

C:\WINDOWS\system32>minikube stop
* Stopping "minikube" in virtualbox ...
* Node "minikube" stopped.
创建k8s与停止k8s后,启动k8s

win终端下输入命令:

C:\WINDOWS\system32>minikube start
* Microsoft Windows 10 Education 10.0.18362 Build 18362 上的 minikube v1.12.0
* 根据现有的配置文件使用 virtualbox 驱动程序
* Starting control plane node minikube in cluster minikube
* Restarting existing virtualbox VM for "minikube" ...
! This VM is having trouble accessing https://k8s.gcr.io
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* 正在 Docker 19.03.12 中准备 Kubernetes v1.18.3…
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* 完成!kubectl 已经配置至 "minikube"
清理本地状态

win终端下输入命令:

C:\WINDOWS\system32>minikube delete
* 正在删除 virtualbox 中的“minikube”…
* Removed all traces of the "minikube" cluster.
官方文档索引

安装Minikube

部署应用示例
  • 1.minikube创建k8s单机集群

在系统/c/Users/用户名(类unix表示方法,win的路径表示为:C:\Users\用户名)目录下,创建一个resolv.conf的文件,把win系统本地的dns解析文件挂载进minikube服务器的/etc/resolv.conf,以使minikube机器可以访问外网,并拉取部署所需要的的镜像,resolv.conf文件内容如下所示,仅供参考

nameserver    xxx.xxx.xxxx.xxx	# 公司内网dns
nameserver    xxx.xxx.xxx.xxx	# 公司外网dns
nameserver    114.114.114.114
nameserver    8.8.8.8

win终端下输入命令:

C:\WINDOWS\system32>minikube start --host-only-cidr=172.16.0.1/24 --cpus=2 --memory=2000mb --disk-size=20000mb --host-dns-resolver=true --mount-string=/c/Users/Raven/resolv.conf:/etc/resolv.conf --vm-driver=virtualbox 

注: 选项详看帮助

C:\WINDOWS\system32>minikube start -h
Starts a local Kubernetes cluster

Options:
      --addons=[]: 启用插件。执行 `minikube addons list` 查看可用插件名称列表
      --apiserver-ips=[]: 一组在为 kubernetes 生成的证书中使用的 apiserver IP
地址。如果您希望将此 apiserver 设置为可从机器外部访问,则可以使用这组 apiserver IP 地址
      --apiserver-name='minikubeCA': The authoritative apiserver hostname for apiserver certificates and connectivity.
This can be used if you want to make the apiserver available from outside the machine
      --apiserver-names=[]: 一组在为 kubernetes 生成的证书中使用的 apiserver 名称。如果您希望将此
apiserver 设置为可从机器外部访问,则可以使用这组 apiserver 名称
      --apiserver-port=8443: apiserver 侦听端口
      --auto-update-drivers=true: 如果设置了,将自动更新驱动到最新版本。默认为 true。

      --base-image='gcr.io/k8s-minikube/kicbase:v0.0.10@sha256:f58e0c4662bac8a9b5dda7984b185bad8502ade5d9fa364bf2755d636ab51438':
The base image to use for docker/podman drivers. Intended for local development.
      --cache-images=true: If true, cache docker images for the current bootstrapper and load them into the machine.
Always false with --driver=none.
       --cni='': CNI plug-in to use. Valid options: auto, bridge, flannel, kindnet, or path to a CNI manifest (default:
auto)
      --container-runtime='docker': The container runtime to be used (docker, cri-o, containerd).
      --cpus=2: Number of CPUs allocated to Kubernetes.
      --cri-socket='': The cri socket path to be used.
      --delete-on-failure=false: If set, delete the current cluster if start fails and try again. Defaults to false.
      --disable-driver-mounts=false: 停用由管理程序提供的文件系统装载
      --disk-size='20000mb': 分配给 minikube 虚拟机的磁盘大小(格式:<数字>[<单位>],其中单位 =
b、k、m 或 g)。
      --dns-domain='cluster.local': The cluster dns domain name used in the Kubernetes cluster
      --dns-proxy=false: 为 NAT DNS 请求启用代理(仅限 virtualbox 驱动程序)
      --docker-env=[]: 传递给 Docker 守护进程的环境变量。(格式:键值对)
      --docker-opt=[]: 指定要传递给 Docker 守护进程的任意标志。(格式:key=value)
      --download-only=false: 如果为 true,仅会下载和缓存文件以备后用 -
不会安装或启动任何项。
      --driver='': Driver is one of: virtualbox, vmwarefusion, hyperv, vmware, docker (defaults to auto-detect)
      --dry-run=false: dry-run mode. Validates configuration, but does not mutate system state
      --embed-certs=false: if true, will embed the certs in kubeconfig.
      --enable-default-cni=false: DEPRECATED: Replaced by --cni=bridge
      --extra-config=: A set of key=value pairs that describe configuration that may be passed to different components.
                The key should be '.' separated, and the first part before the dot is the component to apply the configuration to.
                Valid components are: kubelet, kubeadm, apiserver, controller-manager, etcd, proxy, scheduler
                Valid kubeadm parameters: ignore-preflight-errors, dry-run, kubeconfig, kubeconfig-dir, node-name, cri-socket,
experimental-upload-certs, certificate-key, rootfs, skip-phases, pod-network-cidr
      --feature-gates='': 一组用于描述 alpha 版功能/实验性功能的功能限制的键值对。
      --force=false: 强制 minikube 执行可能有风险的操作
      --force-systemd=false: If set, force the container runtime to use sytemd as cgroup manager. Currently available
for docker and crio. Defaults to false.
      --host-dns-resolver=true: 为 NAT DNS 请求启用主机解析器(仅限 virtualbox 驱动程序)
      --host-only-cidr='192.168.99.1/24': 需要用于 minikube 虚拟机的 CIDR(仅限 virtualbox 驱动程序)
      --host-only-nic-type='virtio': 网卡类型仅用于主机网络。Am79C970A, Am79C973, 82540EM, 82543GC, 82545EM
之一,或 virtio(仅限 VirtualBox 驱动程序)
      --hyperkit-vpnkit-sock='': 用于网络连接的 VPNKit 套接字的位置。如果为空,则停用 Hyperkit
VPNKitSock;如果为“auto”,则将 Docker 用于 Mac VPNKit 连接;否则使用指定的 VSock(仅限
hyperkit 驱动程序)
      --hyperkit-vsock-ports=[]: 应在主机上公开为套接字的访客 VSock 端口列表(仅限 hyperkit
驱动程序)
      --hyperv-external-adapter='': External Adapter on which external switch will be created if no external switch is
found. (hyperv driver only)
      --hyperv-use-external-switch=false: Whether to use external switch over Default Switch if virtual switch not
explicitly specified. (hyperv driver only)
      --hyperv-virtual-switch='': hyperv 虚拟交换机名称。默认为找到的第一个 hyperv
虚拟交换机。(仅限 hyperv 驱动程序)
      --image-mirror-country='':
需要使用的镜像镜像的国家/地区代码。留空以使用全球代码。对于中国大陆用户,请将其设置为
cn。
      --image-repository='': Alternative image repository to pull docker images from. This can be used when you have
limited access to gcr.io. Set it to "auto" to let minikube decide one for you. For Chinese mainland users, you may use
local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers
      --insecure-registry=[]: Insecure Docker registries to pass to the Docker daemon.  The default service CIDR range
will automatically be added.
      --install-addons=true: If set, install addons. Defaults to true.
      --interactive=true: 允许用户提示以获取更多信息

      --iso-url=[https://storage.googleapis.com/minikube/iso/minikube-v1.12.0.iso,https://github.com/kubernetes/minikube/releases/download/v1.12.0/minikube-v1.12.0.iso,https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.12.0.iso]:
Locations to fetch the minikube ISO from.
      --keep-context=false: 这将保留现有 kubectl 上下文并创建 minikube 上下文。
      --kubernetes-version='': The Kubernetes version that the minikube VM will use (ex: v1.2.3, 'stable' for v1.18.3,
'latest' for v1.18.4-rc.0). Defaults to 'stable'.
      --kvm-gpu=false: 在 minikube 中启用实验性 NVIDIA GPU 支持
      --kvm-hidden=false: 向 minikube 中的访客隐藏管理程序签名(仅限 kvm2 驱动程序)
      --kvm-network='default': KVM 网络名称。(仅限 kvm2 驱动程序)
      --kvm-qemu-uri='qemu:///system': KVM QEMU 连接 URI。(仅限 kvm2 驱动程序)
      --memory='': Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g).
      --mount=false: This will start the mount daemon and automatically mount files into minikube.
      --mount-string='C:\Users\Raven:/minikube-host': The argument to pass the minikube mount command on start.
      --nat-nic-type='virtio': 网卡类型仅用于主机网络。Am79C970A, Am79C973, 82540EM, 82543GC, 82545EM
之一,或 virtio(仅限 VirtualBox 驱动程序)
      --native-ssh=true: Use native Golang SSH client (default true). Set to 'false' to use the command line 'ssh'
command when accessing the docker machine. Useful for the machine drivers when they will not start with 'Waiting for
SSH'.
      --network-plugin='': Kubelet network plug-in to use (default: auto)
      --nfs-share=[]: 通过 NFS 装载与访客共享的本地文件夹(仅限 hyperkit 驱动程序)
      --nfs-shares-root='/nfsshares': NFS 共享的根目录位置,默认为 /nfsshares(仅限 hyperkit
驱动程序)
      --no-vtx-check=false: 禁用在启动虚拟机之前检查硬件虚拟化的可用性(仅限 virtualbox
驱动程序)
  -n, --nodes=1: The number of nodes to spin up. Defaults to 1.
      --preload=true: If set, download tarball of preloaded images if available to improve start time. Defaults to true.
      --registry-mirror=[]: 传递给 Docker 守护进程的注册表镜像
      --service-cluster-ip-range='10.96.0.0/12': 需要用于服务集群 IP 的 CIDR。
      --uuid='': 提供虚拟机 UUID 以恢复 MAC 地址(仅限 hyperkit 驱动程序)
      --vm=false: Filter to use only VM Drivers
      --vm-driver='': DEPRECATED, use `driver` instead.
      --wait=[apiserver,system_pods]: comma separated list of Kubernetes components to verify and wait for after
starting a cluster. defaults to "apiserver,system_pods", available options:
"apiserver,system_pods,default_sa,apps_running,node_ready" . other acceptable values are 'all' or 'none', 'true' and
'false'
      --wait-timeout=6m0s: 每个 Kubernetes 核心服务保持健康所需的最长时间。

Usage:
  minikube start [flags] [options]

Use "minikube options" for a list of global command-line options (applies to all commands).
  • 2.查看状态

win终端下输入命令:

C:\WINDOWS\system32>minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
  • 3.部署nginx的deployment控制器,并命名为nginx-minikube

win终端下输入命令:

C:\WINDOWS\system32>kubectl create deployment nginx-minikube --image=nginx
deployment.apps/nginx-minikube created
  • 4.暴露nginx的80端口服务,以使外部能够访问其nginx服务

win终端下输入命令:

C:\WINDOWS\system32>kubectl expose deployment nginx-minikube --type=NodePort --port=80
service/nginx-minikube exposed
  • 5.查看nginx的pod服务

win终端下输入命令:

C:\WINDOWS\system32>kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
nginx-minikube-65fb786d8d-c5zxk   1/1     Running   0          4m6s

注: running状态为服务正常,其它状态值为服务异常,需进行相应排查

查询pod中nginx-minikube的具体情况
win终端下输入命令:

C:\WINDOWS\system32>kubectl describe pod/nginx-minikube-65fb786d8d-c5zxk
... ...
  Normal   BackOff    55s (x7 over 6m27s)  kubelet, minikube  Back-off pulling image "nginx"
  Warning  Failed     55s (x7 over 6m27s)  kubelet, minikube  Error: ImagePullBackOff

由于生效或者minikube服务器网络慢等情况,这个过程可能会持续一段时间,为了加快成功部署,可进入minikube服务器修改resolv.conf文件,操作如下所示
通过virtualbox进入minikube服务器,修改resolv.conf文件,增加如下内容
… …
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx
nameserver 114.114.114.114
nameserver 8.8.8.8


- 6.获取nginx的url,以使外部通过浏览器访问其服务

win终端下输入命令:		

C:\WINDOWS\system32>minikube service nginx-minikube --url
http://172.16.0.108:32072


- 7.本机浏览器访问nginx-minikube暴露的服务

如下图所示,可参考:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200718133911316.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjcxNTIyNQ==,size_16,color_FFFFFF,t_70)
- 8.删除nginx-minikube的services服务

win终端下输入命令:		

C:\WINDOWS\system32>kubectl delete services nginx-minikube
service “nginx-minikube” deleted


- 9.删除nginx-minikube的deployment部署

win终端下输入命令:		
···
C:\WINDOWS\system32>kubectl delete deployment nginx-minikube
deployment.apps "nginx-minikube" deleted
··

- 9.停止k8s集群

win终端下输入命令:

C:\WINDOWS\system32>minikube stop

  • Stopping “minikube” in virtualbox …
  • Node “minikube” stopped.

- 10.删除k8s集群

win终端下输入命令:

C:\WINDOWS\system32>minikube delete

  • 正在删除 virtualbox 中的“minikube”…
  • Removed all traces of the “minikube” cluster.

- 11.获取minikube服务器的可访问ip

win终端下输入命令:

C:\WINDOWS\system32>minikube ip
172.16.0.108


- 12.进入minikube服务器

win终端下输入命令:

C:\WINDOWS\system32>minikube ssh

$ docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
kubernetesui/dashboard                    v2.0.1              85d666cddd04        8 weeks ago         223MB
k8s.gcr.io/kube-proxy                     v1.18.3             3439b7546f29        8 weeks ago         117MB
k8s.gcr.io/kube-apiserver                 v1.18.3             7e28efa976bd        8 weeks ago         173MB
k8s.gcr.io/kube-scheduler                 v1.18.3             76216c34ed0c        8 weeks ago         95.3MB
k8s.gcr.io/kube-controller-manager        v1.18.3             da26705ccb4b        8 weeks ago         162MB
kubernetesui/metrics-scraper              v1.0.4              86262685d9ab        3 months ago        36.9MB
k8s.gcr.io/pause                          3.2                 80d28bedfe5d        5 months ago        683kB
k8s.gcr.io/coredns                        1.6.7               67da37a9a360        5 months ago        43.8MB
k8s.gcr.io/etcd                           3.4.3-0             303ce5db0e90        8 months ago        288MB
gcr.io/k8s-minikube/storage-provisioner   v1.8.1              4689081edb10        2 years ago         80.8MB
  • 13.展示dashboard

win终端下输入命令:

C:\WINDOWS\system32>minikube dashboard
* 正在开启 dashboard ...
* 正在验证 dashboard 运行情况 ...
* Launching proxy ...
* 正在验证 proxy 运行状况 ...
* Opening http://127.0.0.1:53742/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

在这里插入图片描述

结语

k8s官方文档

Logo

开源、云原生的融合云平台

更多推荐