工作环境是Windows,需要使用Docker + K8s,但现在Docker DeskTop收费的原因,无法再继续使用,只能另辟蹊径找解决方案。
起初我的想法是使用MiniKube + HyperV的方式 (minikube start --driver=hyperv),在win下利用k8s虚拟机也可以用。但安装一直各种报错,遂放弃。
然后试着在WSL安装Docker,然后MiniKube + Docker的方式 (minikube start --driver=docker),但还是一直各种报错。无奈,遂放弃。
我的做法,当最一筹莫展的时候 – 去官网搜搜!!!
然后在kubernetes的官网找到一种安装k8s的解决方案 – KIND1。
其使用文档可参考kind官网的快速起步2。
这里有一篇在windows下安装过的体验文章3,可供参考。
下面记录下我的安装过程。
下载下列工具,然后加到系统环境变量PATH中
PS > kind create cluster
创建成功会看到下面截图内容:
建好后,默认的Context是“kind-kind”。验证下,成功会提示以下信息:
PS > kubectl cluster-info --context kind-kind
Kubernetes control plane is running at https://127.0.0.1:56044
CoreDNS is running at https://127.0.0.1:56044/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
PS > kubectl get ns
NAME STATUS AGE
default Active 36m
kube-node-lease Active 36m
kube-public Active 36m
kube-system Active 36m
local-path-storage Active 36m
通过配置文件创建集群
由于我首先是在WSL中搭建的Docker服务,然后基于WSL中的这个Docker创建的K8S Service集群,后面使用的时候会有一个问题,影响到以后通过Service暴露NodePort。针对这种情况,参考kind官网,通过指定一个config文件建立集群6!!!
在这个配置文件中写上需要暴露的端口,后面就可以直接使用这些端口了。配置文件如下:
# cluster-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30001
hostPort: 30001
protocol: TCP
- containerPort: 32000
hostPort: 32000
protocol: TCP
- containerPort: 32001
hostPort: 32001
protocol: TCP
可以一次多加几个端口,以防后面不够用了,由于目前配置文件不支持range语法,只能一个端口一个端口的写上去。写完配置文件,运行下面命令创建集群:
PS > kind create cluster --config=cluster-config.yaml
参考dashboard官网介绍7,安装dashboard直接使用下面命令安装即可。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
但这种默认方式安装的dashboard,每次使用都需要登录。登录密码很长,也没法记。所以我参考网上文章,进行了免登录模式的安装。具体步骤如下。
我使用的是v2.7.0版本的recommended.yaml,下载地址
在recommended.yaml找到下面三处,进行修改:
kubectl apply -f dashboard-deploy-nologin.yaml
新建yaml文件,dashboard-admin-user.yaml,内容如下:
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
执行kubectl,应用该yaml文件
kubectl apply -f dashboard-admin-user.yaml
kubectl -n kubernetes-dashboard create token admin-user
如果需要,或创建账户出错,可用下面命令删除掉后重新执行
kubectl -n kubernetes-dashboard delete serviceaccount admin-user
kubectl -n kubernetes-dashboard delete clusterrolebinding admin-user
# 新建了一个命名空间kubernetes-dashboard
PS > kubectl get ns
NAME STATUS AGE
default Active 82m
kube-node-lease Active 82m
kube-public Active 82m
kube-system Active 82m
kubernetes-dashboard Active 5m29s
local-path-storage Active 82m
# 在命名空间下新建了对应的pod
PS > kubectl get pod -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-7bc864c59-zdpqb 1/1 Running 0 6m32s
kubernetes-dashboard-5fbd7b954d-pz97f 1/1 Running 0 6m32s
浏览器中打开http://localhost:32000/,过一会儿就出现了dashboard界面。
如果不想用了可用下面命令卸载dashboard,并删除k8s集群
kind delete cluster
kubectl -n kubernetes-dashboard delete $(kubectl -n kubernetes-dashboard get pod -o name | grep dashboard)
kubectl delete pod kubernetes-dashboard-59f548c4c7-6b9nj -n kubernetes-dashboard --force --grace-period=0
kubectl delete deployment kubernetes-dashboard -n kubernetes-dashboard
kubectl delete deployment dashboard-metrics-scraper -n kubernetes-dashboard
kubectl delete service kubernetes-dashboard -n kubernetes-dashboard
kubectl delete service dashboard-metrics-scraper -n kubernetes-dashboard
kubectl delete secret kubernetes-dashboard-certs -n kubernetes-dashboard
kubectl delete secret kubernetes-dashboard-csrf -n kubernetes-dashboard
kubectl delete secret kubernetes-dashboard-key-holder -n kubernetes-dashboard
kubectl delete clusterrolebinding kubernetes-dashboard -n kubernetes-dashboard
kubectl delete rolebinding kubernetes-dashboard -n kubernetes-dashboard
kubectl delete clusterrole kubernetes-dashboard -n kubernetes-dashboard
kubectl delete role kubernetes-dashboard -n kubernetes-dashboard
kubectl delete configmaps kubernetes-dashboard-settings -n kubernetes-dashboard
kubectl delete sa kubernetes-dashboard -n kubernetes-dashboard
kubectl delete namespace kubernetes-dashboard
https://kubernetes.io/zh-cn/docs/tasks/tools/ ↩︎
https://kind.sigs.k8s.io/docs/user/quick-start/ ↩︎
https://www.jianshu.com/p/f91dc3faff81 ↩︎
https://github.com/kubernetes-sigs/kind/releases ↩︎
https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-windows/ ↩︎
https://kind.sigs.k8s.io/docs/user/using-wsl2/#accessing-a-kubernetes-service-running-in-wsl2 ↩︎
https://github.com/kubernetes/dashboard/ ↩︎