[Mac] K8s(MiniKube)搭建过程

前言

用了下公司的K8s系统. 感觉这样的构建方式当真是非常的便利。自己也尝试部署一个单机版本。如果觉得好用,可以扩展到阿里云上。


环境

  • Mac OS: 10.14.6
  • Docker: 2.2.0.5

准备操作

看了下别人的博客. 一般的搭建过程都是需要安装minikubekubectl. 我们同样来安装这2个模块.

# 分解
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/darwin/amd64/kubectl

# 查询最新的稳定版本
curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt 

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.20.4/bin/darwin/amd64/kubectl

# 创建软连接
ln -s /Users/sean/Software/MiniK8s/kubectl /usr/local/bin/
  • 一个小插曲
    在安装kubectl的时候. 我在安装的目录发现Docker自带的kubectl. 但是由于版本较低. 所以我进行了更新. 相关的操作实况和命令如下所示.
# 判断当前kubectl的位置
localhost:usr sean$ which kubectl
/usr/local/bin/kubectl

# 判断版本 可以发现是1.15.5版本
localhost:usr sean$ cd /usr/local/bin/
localhost:bin sean$ kubectl version

Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.5", GitCommit:"20c265fef0741dd71a66480e35bd69f18351daea", GitTreeState:"clean", BuildDate:"2019-10-15T19:16:51Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

# 查看软连接情况. 可以看见真实地址`/Applications/Docker.app/Contents/Resources/bin/kubectl`
localhost:bin sean$ ls -lrt | grep "kubectl"
lrwxr-xr-x  1 root  admin        55  6 27  2020 kubectl -> /Applications/Docker.app/Contents/Resources/bin/kubectl
lrwxr-xr-x  1 root  admin        55  6 27  2020 kubectl.docker -> /Applications/Docker.app/Contents/Resources/bin/kubectl

# 删除软链接
localhost:bin sean$ rm -rf kubectl
localhost:bin sean$ rm -rf kubectl.docker

# 创建软链接并查看
ln -s /Users/sean/Software/MiniK8s/kubectl /usr/local/bin/
localhost:bin sean$ ls -lrt |grep kubectl
lrwxr-xr-x  1 sean  admin        36  3 28 15:04 kubectl -> /Users/sean/Software/MiniK8s/kubectl

# 查看版本
localhost:bin sean$ ./kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
  • 以上的主要要点就是下载并且安装相应版本的minikubekubectl2个应用程序. 并且将其放置到/usr/local/bin下. 为了方便快速访问.

MiniKube 安装

在准备好上面的Minikubekubectl 2个应用程序之后. 我们就可以下载相应的镜像并且启动了. 其中最核心的2个命令是minikube start/minikube stop/minikube delete/minikube dashboard. 但是由于国内的网络环境问题. 这也给我造成了一些困扰 . 困扰的问题, 我会放置到Q&A部分进行记录和解答.

minikube start
localhost:~ sean$ minikube start
😄  Darwin 10.14.6 上的 minikube v1.18.1
✨  自动选择 docker 驱动。其他选项:hyperkit, parallels, ssh
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
💾  Downloading Kubernetes v1.20.2 preload ...
    > preloaded-images-k8s-v9-v1....: 491.22 MiB / 491.22 MiB  100.00% 4.73 MiB
❗  minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.18, but successfully downloaded kicbase/stable:v0.0.18 as a fallback image
🔥  Creating docker container (CPUs=2, Memory=1989MB) ...
❗  This container 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 20.10.3 中准备 Kubernetes v1.20.2…
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v4
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
  • Tips1 注意上述有一串文字说明minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.18, but successfully downloaded kicbase/stable:v0.0.18 as a fallback image. 这是找不到gcr.io/k8s-minikube使用了后面的进行替代. 很多人在安装的过程中都会死在这一步. 这里的等待时间比较长, 其实我们只需要耐心等待即可.
  • Tips2 注意这里在start的时候很多人会加一些其他的参数.
minikube start
 --image-mirror-country cn  这里应该是为了快速下载比较近的资源
 --driver="docker"  虚拟化方式其他还有virtubox hytrix等等
 --image-repository="registry.cn-hangzhou.aliyuncs.com/google_containers"  快速镜像 这里选择的是阿里云镜像
 --kubernetes-version=v1.20.2 这里是你选择的kubernetes版本

minikube start --image-mirror-country cn
–iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.7.3.iso
–docker-env http_proxy=http://192.168.100.20:7890
–docker-env https_proxy=http://192.168.100.20:7890
–docker-env no_proxy=localhost,127.0.0.1,192.168.100.20
–registry-mirror=“https://a90tkz28.mirror.aliyuncs.com”
–image-repository=“registry.cn-hangzhou.aliyuncs.com/google_containers”
–kubernetes-version=v1.18.3
Darwin 10.15.5 上的 minikube v1.14.0
Mac上minikube 安装与简单使用

minikube dashboard
localhost:~ sean$ minikube dashboard
🔌  正在开启 dashboard ...
    ▪ Using image kubernetesui/dashboard:v2.1.0
    ▪ Using image kubernetesui/metrics-scraper:v1.0.4
🤔  正在验证 dashboard 运行情况 ...
🚀  Launching proxy ...
🤔  正在验证 proxy 运行状况 ...
🎉  Opening http://127.0.0.1:60094/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
  • 在运行成功后, 会直接跳转到http://127.0.0.1:60094/api/v1/namespaces/kubernetes-dashboard/services. 也就是我们minikube的管理页面.

  • minikube stop

localhost:~ sean$ minikube stop
✋  Stopping node "minikube"  ...
🛑  正在通过 SSH 关闭“minikube”…
🛑  1 nodes stopped.
  • minikube staus
localhost:~ sean$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
timeToStop: Nonexistent

Q&A

这里主要记录一些安装过程中的一些问题.

  • 版本提示
>localhost:~ sean$ minikube start
😄  Darwin 10.14.6 上的 minikube v1.18.1
✨  根据现有的配置文件使用 docker 驱动程序
👍  Starting control plane node minikube in cluster minikube
🔄  Restarting existing docker container for "minikube" ...
🐳  正在 Docker 20.10.3 中准备 Kubernetes v1.20.2…
🔎  Verifying Kubernetes components...
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/dashboard:v2.1.0@sha256:7f80b5ba141bead69c4fee8661464857af300d7d7ed0274cf7beecedc00322e6 (global image repository)
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/metrics-scraper:v1.0.4@sha256:555981a24f184420f3be0c79d4efb6c948a85cfce84034f85a563f4151a81cbf (global image repository)
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-minikube/storage-provisioner:v4 (global image repository)
🌟  Enabled addons: default-storageclass, storage-provisioner, dashboard
❗  /usr/local/bin/kubectl is version 1.15.5, which may have incompatibilites with Kubernetes 1.20.2.
    ▪ Want kubectl v1.20.2? Try 'minikube kubectl -- get pods -A'
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

最后几句. 告诉你现在的版本是1.20.2. 你可以选择最新的版本.

  • 503错误
localhost:~ sean$ minikube start
😄  Darwin 10.14.6 上的 minikube v1.18.1
✨  根据现有的配置文件使用 docker 驱动程序
👍  Starting control plane node minikube in cluster minikube
🏃  Updating the running docker "minikube" container ...
🐳  正在 Docker 20.10.3 中准备 Kubernetes v1.20.2…
🔎  Verifying Kubernetes components...
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-minikube/storage-provisioner:v4 (global image repository)
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/dashboard:v2.1.0@sha256:7f80b5ba141bead69c4fee8661464857af300d7d7ed0274cf7beecedc00322e6 (global image repository)
    ▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/metrics-scraper:v1.0.4@sha256:555981a24f184420f3be0c79d4efb6c948a85cfce84034f85a563f4151a81cbf (global image repository)
🌟  Enabled addons: storage-provisioner, default-storageclass, dashboard
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
localhost:~ sean$ minikube dashboard
🤔  正在验证 dashboard 运行情况 ...
🚀  Launching proxy ...
🤔  正在验证 proxy 运行状况 ...

❌  Exiting due to SVC_URL_TIMEOUT: http://127.0.0.1:56670/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ is not accessible: Temporary Error: unexpected response code: 503

解决办法: 这个基本是网络问题了. 我是选择重新安装解决.

# 删除
minikube stop
minikube delete
rm -rf  ~/.minikube
 rm -rf  ~/.kube
 # 重新安装
 minikube start
  • minikube start --driver="docker" --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
localhost:~ sean$ minikube start  --driver="docker" --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
😄  Darwin 10.14.6 上的 minikube v1.18.1
✨  根据用户配置使用 docker 驱动程序
✅  正在使用镜像存储库 registry.cn-hangzhou.aliyuncs.com/google_containers
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
E0328 16:05:17.971819    9995 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.20.2" -> "/Users/sean/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy_v1.20.2" failed: write: read tcp 10.22.0.10:59230->124.160.145.54:80: read: connection reset by peer
E0328 16:05:18.080946    9995 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/dashboard:v2.1.0" -> "/Users/sean/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/dashboard_v2.1.0" failed: write: read tcp 10.22.0.10:59223->124.160.145.54:80: read: connection reset by peer
E0328 16:05:18.563226    9995 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.20.2" -> "/Users/sean/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager_v1.20.2" failed: write: read tcp 10.22.0.10:59226->124.160.145.54:80: read: connection reset by peer
E0328 16:05:18.570091    9995 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.20.2" -> "/Users/sean/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler_v1.20.2" failed: write: read tcp 10.22.0.10:59232->124.160.145.54:80: read: connection reset by peer
E0328 16:05:18.941179    9995 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0" -> "/Users/sean/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/etcd_3.4.13-0" failed: write: read tcp 10.22.0.10:59224->124.160.145.54:80: read: connection reset by peer
^C

阿里云xx地址访问不到. 去除代理, 使用最简洁的minikube start即可. 大概是阿里云的服务器抽风了.


  • 忙活了2天, 终于装上并且使用了. 可喜可贺.
相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页