Windows 下使用 Minikube 搭建Kubernetes
硬件要求:
- 2核CPU或更高配置
- 2GB内存或更高配置
- 20GB磁盘空间或更多
- 连接互联网
- 容器或虚拟机管理器,例如:Docker、Hyperkit、Hyper-V、KVM、Parallels、Podman、VirtualBox 或 VMware Fusion/Workstation
Windows下使用PowerShell安装
New-Item -Path 'd:\' -Name 'minikube' -ItemType Directory -Force
Invoke-WebRequest -OutFile 'd:\minikube\minikube.exe' -Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' -UseBasicParsing
其中路径可以自定义
设置环境变量
$oldPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
if ($oldPath.Split(';') -inotcontains 'd:\minikube'){ `
[Environment]::SetEnvironmentVariable('Path', $('{0};d:\minikube' -f $oldPath), [EnvironmentVariableTarget]::Machine) `
}
启动集群
minikube start
默认情况下会使用Hyperv
作为驱动
如果要使用Vmware
或者其他,启动的时候需要指定
minikube start --driver vmware
如果报Exiting due to PROVIDER_VMWARE_NOT_FOUND: The 'vmware' provider was not found: exec: "vmrun": executable file not found in $PATH
信息,把vmware
的安装路径加入系统环境变量就行。
下载安装docker-machine-driver-vmware
https://github.com/machine-drivers/docker-machine-driver-vmware/releases/latest
下载然后放到系统中的任何位置都可以,但要把路径配置到环境变量中
再次启动
启动成功
部署应用
创建应用YAML
描述文件,这里以Nginx为例
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
基于YAML文件创建部署
E:\>minikube kubectl -- apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
查看部署信息:
minikube kubectl -- describe deployment nginx-deployment
查看 ReplicaSet 对象的信息:
E:\>minikube kubectl -- get replicasets
NAME DESIRED CURRENT READY AGE
nginx-deployment-9456bbbf9 2 2 2 17m
E:\>minikube kubectl -- describe replicasets
创建公开 Deployment 的 Service 对象:
minikube kubectl -- expose deployment nginx-deployment --type=NodePort --name=web-servicevice
service/web-servicevice exposed
查看Service
信息:
E:\>minikube kubectl -- get service web-servicevice
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web-servicevice NodePort 10.98.221.134 <none> 80:30074/TCP 30s
查看 Service
详情:
minikube kubectl -- describe services web-servicevice
Name: web-servicevice
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.98.221.134
IPs: 10.98.221.134
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30074/TCP
Endpoints: 172.17.0.5:80,172.17.0.6:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
这里因为用了NodePort
的方式暴露外部服务,记住这个端口
查看IP列表:
E:\>minikube kubectl -- get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
minikube Ready control-plane,master 77m v1.23.3 192.168.91.140 <none> Buildroot 2021.02.4 4.19.202 docker://20.10.12
使用INTERNAL-IP
加上NodePort
的端口就可以访问应用了
curl http://http://<internal-ip>:<port>