macOS安装Kubernetes

这里使用Docker自带的Kubernetes,版本信息如下:

docker-version

安装Docker

brew cask install docker

安装完成之后,已经有了Kubernetes的命令行工具Kubectl等,Docker默认是不启动Kubernetes的,需要自己手动。同时启动Kubernetes所需要的镜像也都不会下载,在启动的时候会自己下载。

开启Docker的Kubernetes功能

docker-preference

点击Apply & Restart之后会有一段等待时间,此时Kubernetes在下载所有需要的镜像。

REPOSITORY                           TAG                                              IMAGE ID            CREATED             SIZE
docker/desktop-storage-provisioner   v1.1                                             e704287ce753        3 months ago        41.8MB
docker/desktop-vpnkit-controller     v1.0                                             79da37e5a3aa        4 months ago        36.6MB
docker/desktop-kubernetes            kubernetes-v1.16.5-cni-v0.7.5-critools-v1.15.0   a86647f0b376        5 months ago        279MB
k8s.gcr.io/kube-apiserver            v1.16.5                                          fc838b21afbb        5 months ago        159MB
k8s.gcr.io/kube-scheduler            v1.16.5                                          b4d073a9efda        5 months ago        83.5MB
k8s.gcr.io/kube-proxy                v1.16.5                                          0ee1b8a3ebe0        5 months ago        82.7MB
k8s.gcr.io/kube-controller-manager   v1.16.5                                          441835dd2301        5 months ago        151MB
docker/kube-compose-controller       v0.4.25-alpha1                                   129151cdf35f        8 months ago        35.6MB
docker/kube-compose-api-server       v0.4.25-alpha1                                   989749268895        8 months ago        50.7MB
docker/kube-compose-installer        v0.4.25-alpha1                                   2a71ac5a1359        8 months ago        42.3MB
k8s.gcr.io/etcd                      3.3.15-0                                         b2756210eeab        10 months ago       247MB
k8s.gcr.io/coredns                   1.6.2                                            bf261d157914        10 months ago       44.1MB
k8s.gcr.io/pause                     3.1                                              da86e6ba6ca1        2 years ago         742kB

镜像下载完成之后,Docker控制面板上的Kubernetes会显示running。

安装Kubernetes Dashboard

可以在Dashborad查看最新版的Kubernetes Dashboard安装信息。这里直接Kubectl工具直接安装,执行:

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/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

访问Kubernetes Dashboard

为了从本地访问Dashboard,需要创建一个安全的通道到Kubernetes集群,运行:

$ kubectl proxy
Starting to serve on 127.0.0.1:8001

此时就可以通过以下链接访问Dashboard了。

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

打开之后会发现,登录Dashboard需要Token或者kubeconfig文件。

kubernetes-dashboard-login

这里使用token登录。

创建Kubernetes Dashboard管理账户

为了登录Dashboard,首先需要创建账户,创建账户的详细教程可以参考Creating sample user

用官方最简单的例子,首先创建一个dashboard-adminuser.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 apply -f dashboard-adminuser.yaml
serviceaccount/admin-user created
clusterrolebinding.rbac.authorization.k8s.io/admin-user created

然后再执行以下命令就能获取登录Token了:

$ kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
Name:         admin-user-token-tl795
Namespace:    kubernetes-dashboard
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: admin-user
              kubernetes.io/service-account.uid: d8c079a2-2b74-4146-beff-763ac306725b

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  20 bytes
token:      eyJhbGciOiJW50Iiwia3ViZXt1YmVybmV0ZUiOiJhZG1pbi......XRsNzk1Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY2dA

复制Token到登录界面,点击登录就可以进入Dashboard了。

kubernetes-dashboard

Categories: kubernetes
Date: 2020-07-05
Lastmod: 2020-07-05
License: BY-NC-ND 4.0