在本地环境中安装、配置和验证Kubernetes

55次阅读
没有评论

问题描述

是一个Docker的常规用户,他在本地Win10Pro主机上安装并运行了Docker服务。现在他想要开始使用Kubernetes,所以他使用了gcloud命令行安装Kubernetes,并收到以下消息:

WARNING:   There are older versions of Google Cloud Platform tools on  your system PATH.   Please remove the following to avoid accidentally  invoking these old tools:
C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe

看起来我的Docker安装带来了另一个版本的kubectl,Google建议我删除它。我真的只需要从我的Docker安装中删除该文件吗?

现在似乎安装了两个版本的kubectl.exe,一个是Docker带的,另一个是我刚刚从Google安装的。Docker GUI中的Kubernetes未启用。我应该在Docker设置GUI中启用它吗?

过了一会儿,我尝试安装Minikube并启动它,结果如下:

C:\WINDOWS\system32>minikube start
* minikube v1.5.2 on Microsoft Windows 10 Pro 10.0.17763 Build 17763
E1114 05:07:00.856628    3836 driver_windows.go:74] Can't find VirtualBox registry entries, is VirtualBox really installed properly? The system cannot find the file specified.
* Automatically selected the 'hyperv' driver
* Creating hyperv VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
* Preparing Kubernetes v1.16.2 on Docker '18.09.9' ...
* Pulling images ...
* Launching Kubernetes ...
* Waiting for: apiserver
* Done! kubectl is now configured to use "minikube"!
C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe is version 1.14.7, and is incompatible with Kubernetes 1.16.2. You will need to update C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe or use 'minikube kubectl' to connect with this cluster
C:\WINDOWS\system32>

但尽管上面的消息,我可以正常运行和暴露服务:

C:\WINDOWS\system32>kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
deployment.apps/hello-minikube created
C:\WINDOWS\system32>kubectl expose deployment hello-minikube --type=NodePort --port=8080
service/hello-minikube exposed
C:\WINDOWS\system32>kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
hello-minikube-797f975945-tj4g6   1/1     Running   0          13s
C:\WINDOWS\system32>minikube service hello-minikube --url
http://172.17.100.11:30575
C:\WINDOWS\system32>

该服务在上述URL上显示其输出。您认为我的Kubernetes安装和配置是否正确,还是有什么需要修复的地方?我做得对吗?

我没有删除Docker安装中的kubectl.exe,并且没有被建议安装Minikube,但似乎是正确的。奇怪的是,客户端和服务器现在显示不同的版本:

C:\WINDOWS\system32>kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.7", GitCommit:"8fca2ec50a6133511b771a11559e24191b1aa2b4", GitTreeState:"clean", BuildDate:"2019-09-18T14:47:22Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:09:08Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
C:\WINDOWS\system32>

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

您不需要删除文件,只需从系统的环境变量中删除它。您需要查看系统和帐户环境变量,并查找PATH,然后删除C:\Program Files\Docker\Docker\Resources\bin\
这样做很可能就可以解决问题,我在这台计算机上同时安装了Docker Desktop和gcloud,并且没有将上述文件夹添加到我的PATH中,两个产品都可以正常工作,没有任何关于旧工具的警告。

如果您继续遇到问题,例如在使用Minikube时,使用Get-Command PowerShell命令可以帮助您找出哪个可执行文件被调用:

Get-Command kubectl | Format-List

这将为您提供将运行的可执行文件的完整路径,如果您发出kubectl命令,它将运行的可执行文件的完整路径:

Name            : kubectl.exe
CommandType     : Application
Definition      : C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe
Extension       : .exe
Path            : C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe
FileVersionInfo : File:             C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe
                  InternalName:
                  OriginalFilename:
                  FileVersion:
                  FileDescription:
                  Product:
                  ProductVersion:
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:

方案2

使用脚本或工具来管理容器的启动顺序可能会增加复杂性,并且需要确保容器A和容器B之间的依赖关系正确设置。
另一种方法是编写脚本或使用工具来控制容器的运行顺序。您可以使用docker run命令来手动控制容器的启动顺序,或者使用一些第三方工具来管理容器的依赖关系。

示例:

以下是一个简单的bash脚本示例,可以在容器A启动后启动容器B:

#!/bin/bash
# 启动容器A
docker run -d --name container_a your_image_a
# 等待容器A完全启动
while ! docker exec container_a echo "Container A is ready"; do
  sleep 1
done
# 启动容器B
docker run -d --name container_b your_image_b

在这个示例中,我们首先使用docker run命令启动容器A,并将其命名为container_a。然后,使用一个循环来等待容器A完全启动(这里是通过在容器内运行echo命令来测试)。一旦容器A就绪,我们再使用docker run命令启动容器B,并将其命名为container_b

正文完