Minikube安装与配置

在容器编排工具中安装配置最复杂的就是Kubernetes,想要运行一个简单的容器集群环境,对于没有使用过Kubernetes的人来说,需要花费一定的时间去理解各组件的概念和功能,再做大量的安装配置工作才能运行一个kubernetes集群。

从Kubernetes1.3开始提供了一个叫Minikube的强大测试工具,可以在任意主机上运行单节点的小型集群,这个工具默认安装和配置了一个Linux VM,Docker和Kubernetes的相关组件,并且提供Dashboard。目前支持在Linux, OS X及Windows上安装,今天我们介绍的是在Windows上如何安装Minitube。

2020-12-10更新:Windows10下建议直接使用Docker-Desktop On WSL2,不要开启systemd。具体参考:http://blog.gcalls.cn/blog/2020/12/WSL.html

准备

以Windows为例,记录minikube的安装与使用。

Minitube项目地址:https://github.com/kubernetes/minikube

Minikube要求在BIOS中开户了VT-x/AMD-v虚拟化。

Kubernetes版本: v1.19.4

安装virtualbox

安装版本为VirtualBox 6.1.x

安装choco

在Linux世界中,安装一个软件不需要在浏览器中寻找软件的官网,然后将其下载下来,然后双击进行安装。只需要一条简单的命令,就可以完成搜索、安装、更新、卸载等所有操作。其实Windows下,也有这么一个包管理器,功能虽然不及Linux中那些包管理器强大,但是也让Windows下的软件安装方便了不少。这就是Chocolatey。

Chocolatey官网:https://chocolatey.org/

安装choco:

以管理员模式开启PowerShell:

1
2
3
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

setx ChocolateyInstall E:\Chocolatey /M

可以测试是否能正常安装curl:

1
2
3
4
5
6
C:\>choco install curl -y
#测试:
C:\>curl baidu.com
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>

安装minikube

Windows

以管理员模式开启CMD:

1
2
3
4
C:\>choco install minikube -y

C:\>minikube version
minikube version: v1.19.4

如果安装速度慢的话,可以开启代理:

1
2
SET HTTP_PROXY=http://192.168.101.175:1082
SET HTTPS_PROXY=http://192.168.101.175:1082

MacOS

下载对应的文件:

1
2
3
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.19.4/bin/darwin/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

Linux

推荐使用microk8s安装,参考:http://blog.gcalls.cn/blog/2020/12/Kubernetes-Development-Environment.html#microk8s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
yum install bash-completion -y 
~/.bash_profile
alias k=kubectl
source <(kubectl completion bash | sed s/kubectl/k/g)
source /usr/share/bash-completion/bash_completion


cul -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.19.4/bin/linux/amd64/kubectl
chmod +x kubectl && sudo mv kubectl /usr/local/bin/
#启动
#使用driver=none时,不需要安装VirtualBox,但需要安装docker
sudo minikube start --driver=none

#Base on VirtualBox:
yum localinstall VirtualBox-6.1-6.1.16_140961_el7-1.x86_64.rpm
yum localinstall kernel-devel-3.10.0-957.el7.x86_64.rpm
rcvboxdrv setup
su - dev
#Starting minikube
#https://minikube.sigs.k8s.io/docs/handbook/vpn_and_proxy/
export http_proxy="http://192.168.101.175:1082"
export https_proxy=$http_proxy
export no_proxy="127.0.0.1,localhost,10.0.0.0/8,172.0.0.0/8,192.168.0.0/16,*.zerofinance.net,*.aliyun.com,*.163.com,*.docker-cn.com,registry.gcalls.cn"
minikube start --memory=8192 --cpus=4 --kubernetes-version=v1.19.4

WSL

参考:http://blog.gcalls.cn/blog/2020/12/WSL.html

安装docker,参考:http://blog.gcalls.cn/blog/2018/12/ubuntu-os.html#Docker

minikube安装:

参考:https://kubernetes.io/blog/2020/05/21/wsl-docker-kubernetes-on-the-windows-desktop/#minikube-kubernetes-from-everywhere

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Download the latest version of Minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x ./minikube && sudo mv minikube /usr/local/bin/
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.19.4/bin/linux/amd64/kubectl
chmod +x kubectl && sudo mv kubectl /usr/local/bin/

# Check if the KUBECONFIG is not set
echo $KUBECONFIG
# Check if the .kube directory is created > if not, no need to create it
ls $HOME/.kube
# Check if the .minikube directory is created > if yes, delete it
ls $HOME/.minikube
# Create the cluster with sudo
sudo sysctl fs.protected_regular=0
export http_proxy="http://192.168.101.175:1082"
export https_proxy=$http_proxy
export no_proxy="127.0.0.1,localhost,10.0.0.0/8,172.0.0.0/8,192.168.0.0/16,*.zerofinance.net,*.aliyun.com,*.163.com,*.docker-cn.com,registry.gcalls.cn"
sudo minikube start --driver=none
#每次执行以上命令均需要执行以下命令才行(root账户下不需要),否则会报以下错误:
#Unable to connect to the server: dial tcp 172.21.114.172:8443: connect: no route to host
sudo cp -a /root/.kube /root/.minikube $HOME
sudo chown -R $USER $HOME/.kube $HOME/.minikube
#需要修改以上的$HOME/.kube/config中的/root为当前用户目录
sed -i 's;/root/;/home/dave/;g' ~/.kube/config
#minikube start --driver=docker

minikube常用命令

启动minikube:

1
2
3
4
5
6
7
8
9
10
11
12
13
export http_proxy="http://192.168.101.175:1082"
export https_proxy=$http_proxy
export no_proxy="127.0.0.1,localhost,10.0.0.0/8,172.0.0.0/8,192.168.0.0/16,*.zerofinance.net"
C:\>minikube start --memory=8192 --cpus=4 --kubernetes-version=v1.19.4
Starting local Kubernetes v1.7.5 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.

如果是Hyper-V的话:

最新:新版本的minikube不再需要执行以下操作。以下只作备份记录而也。

注意:需要先创建外部网络,具体请参考https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-switch-for-hyper-v-virtual-machines

1
2
minikube.exe start --cpus 2 --memory 2048 --vm-driver="hyperv" --hyperv-virtual-switch="OuterNetwork" --docker-env HTTP_PROXY=http://192.168.10.1:1080 --docker-env HTTPS_PROXY=http://192.168.10.1:1080
minikube start --driver=none --docker-env HTTP_PROXY=http://192.168.101.175:1082 --docker-env HTTPS_PROXY=http://192.168.101.175:1082

注意:要加上代理,否则下载不了grc.io中的镜像。

停止minikube:

1
C:\>minikube stop

删除minikube:

1
C:\>minikube delete

测试minikube:

1
2
3
C:\>kubectl get no
NAME STATUS AGE VERSION
minikube Ready 2h v1.19.4

登录minikube虚拟机:

1
C:\>minikube ssh

查看状态:

1
2
3
4
C:\>minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100

安装一个程序

参考https://kubernetes.io/zh/docs/setup/learning-environment/minikube/

Dashboard

Minikube自带了Kubernetes Dashboard。要浏览这个界面,可以使用内置的minikube dashboard命令:

1
2
C:\> minikube dashboard
Opening kubernetes dashboard in default browser...

dashboard

参考

https://github.com/kubernetes/minikube
https://kubernetes.io/docs/getting-started-guides/minikube/
https://kubernetes.io/zh/docs/setup/learning-environment/minikube/