LarryDpk
发布于 2020-07-02 / 3382 阅读
0

Mac上使用Docker Desktop启动Kubernetes,踩坑后终于搞掂

1 前言

Kubernetes又简称k8s,是Google开源的容器集群管理系统,最近也是火热。闲来无事(为了发文),捣鼓了一下,在Mac上搭建Kubernetes,遇到一些坑,也记录一下。

另外,Docker相关的概念可参考下面文章:Docker入门——理解Docker的核心概念

2 问题Kubernetes is starting

2.1 卡在starting

当Mac安装完Docker Desktop后,可以启动Docker了,而要启动Kubernetes,需要Enable

本以为就如此简单方便,谁料一直显示Kubernetes is starting,卡了很久都没有反应。那是由于网络原因(你懂的),启动Kubernetes所需要的镜像无法下载。

2.2 下载镜像

所需镜像可以手动一个个下载,当然也有人准备好了脚本可以一键下载,脚本在仓库:https://github.com/hummerstudio/k8s-docker-desktop-for-mac 上。

将仓库克隆下来:

$ git clone https://github.com/hummerstudio/k8s-docker-desktop-for-mac.git

然后看一下文件image_list上的版本跟自己安装的版本能不能对得上,建议直接更新Docker Desktop为最新版本。

$ cat image_list
k8s.gcr.io/kube-proxy:v1.16.5=gotok8s/kube-proxy:v1.16.5
k8s.gcr.io/kube-controller-manager:v1.16.5=gotok8s/kube-controller-manager:v1.16.5
k8s.gcr.io/kube-scheduler:v1.16.5=gotok8s/kube-scheduler:v1.16.5
k8s.gcr.io/kube-apiserver:v1.16.5=gotok8s/kube-apiserver:v1.16.5
k8s.gcr.io/coredns:1.6.2=gotok8s/coredns:1.6.2
k8s.gcr.io/pause:3.1=gotok8s/pause:3.1
k8s.gcr.io/etcd:3.3.15-0=gotok8s/etcd:3.3.15-0
k8s.gcr.io/kubernetes-dashboard-amd64=gotok8s/kubernetes-dashboard-amd64:v1.10.1

同为v1.16.5版本:

没有问题就可以执行脚本拉取相关镜像:

$ ./load_images.sh

2.3 重启验证

下载完成后,重启Kubernetes,验证 Kubernetes 集群状态:

$ kubectl cluster-info
$ kubectl get nodes
$ kubectl describe node

3 安装Dashboard

Dashboard并不是必须的,它可以方便我们查看和操作,建议还是安装。将文件( https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml )下载到本地,然后执行以下命令即可:

$ kubectl apply -f recommended.yaml 
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper created

启动:

$ kubectl proxy

建议使用下面命令启动,不然会一直占着终端:

$ nohup kubectl proxy >/dev/null &

启动后可登陆访问:http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/login

界面如下:

需要获取Token,命令如下:

$ kubectl -n kube-system describe secret default| awk '$1=="token:"{print $2}'
eyJhbGciOiJSUzI1NiIsImtpZCI6IjNiT1o4eW5ORHpkVlp6Q2VJakxMOENlZEUxSU16YmRZcHZqV3BfXzhQTm8ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkZWZhdWx0LXRva2VuLTJoNnpkIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImRlZmF1bHQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJhODk5MDkwMS1hOWE4LTQyZTAtOGMxNi1iNmQwZTY1NjgyZTEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06ZGVmYXVsdCJ9.Oo1Sa5GcWC7SknJ_qoGgoIXyGJKIp1nB4ApZqvTsj32DcoAA4mBmo0VB_zqdmlG0x29FO132x_6I2ejOnCwZ9IRlUWBtEYsKCChZb9kUjbAA9FLyIln4AW0K3V7F1ns_YIKLYaudnkHJPWpqE2FXwTVDs-ZpGgUUk2Fs-Sw8KEFOLnHNFzi-lIajsbicmUMyKPimcv51vN-Mgc9gH-_PfvLKBL-h3vbsCep-0xsRE2zOtsfqv9cbaVNhFaYkVvZuYzCBD1TY9v5Q5R6jPsB2IBZzEYMKw8lHGNrPzN0vrKCM_VBRSeUgzUnjAH6MLuVVWbzBmYYiOluCjuAyKf1eWA

就可以通过Token登陆了。Dashboard界面如下所示:

4 总结

至此,安装已经完成,后面再通过一个小案例讲解如何应用。注意我的电脑连了阿里云的镜像仓库,需要设置的请自动Google。想玩k8s还有其它方式搭建环境,如Minikube,以后有机会再折腾吧。