Kubernetes的安装过程还是比较繁琐的,网上也提供了不少的教程,总结下来安装方式大概可以分为三类。

  • 二进制安装(上手难度高,不推荐)
  • kubeadm安装(官方推荐,基于命令行形式,安装过程相对简化)
  • 基于第三方工具(以容器安装容器,安装简单,但是也屏蔽了安装过程,快速体验可以采用这种方式安装)

这里使用腾讯云服务器来安装,准备了以下四台节点。其中三台服务器作为k8s集群节点,一台服务器辅助安装使用,在使用kubeadm安装时非必要,使用第三方辅助工具时候需要用到。

服务器配置 IP地址 节点名称 备注
2核心4G 10.0.4.17 master 集群主节点
2核心4G 10.0.4.5 node1 集群工作节点
2核心4G 10.0.4.8 node2 集群工作节点
2核心2G 10.0.16.15 support 辅助节点(非集群必须)

注:以下安装操作使用节点名称来代替具体的服务器

本篇安装教程主要包含两部分内容,k8s集群的安装方式以及web管理界面的安装方式。

k8s集群安装

KuboardSpray

KuboardSpray是一个图形化的 K8S 集群离线安装、维护工具,可以通过Docker启动,并且提供图形化配置功能,实现一键安装k8s集群。
首先support节点需要提供Docker支持,在support节点上使用Docker安装KuboardSpray,执行以下命令:

  1. docker run -d \
  2. --privileged \
  3. --restart=unless-stopped \
  4. --name=kuboard-spray \
  5. -p 80:80/tcp \
  6. -e TZ=Asia/Shanghai \
  7. -v /var/run/docker.sock:/var/run/docker.sock \
  8. -v ~/kuboard-spray-data:/data \
  9. eipwork/kuboard-spray:latest-amd64
  10. # 如果您是 arm64 环境,请将标签里的 amd64 修改为 arm64,例如 eipwork/kuboard-spray:latest-arm64
  11. # 如果抓不到这个镜像,可以尝试一下这个备用地址:
  12. # swr.cn-east-2.myhuaweicloud.com/kuboard/kuboard-spray:latest-amd64

安装启动,默认登录信息admin/Kuboard123,登录后界面如下所示,可以修改密码
image.png
选择系统设置—>资源包管理,列出了一些列所支持的k8s版本,此处选择v1.23.1。
image.png
等待资源安装过程,安装完成如下所示
image.png
返回主界面,选择集群管理,配置集群信息
image.png
安装前注意事项:

安装后的k8s集群软件版本为:

  • Kubernetes v1.21.x
    • calico 3.17.1
    • nginx-ingress 1.9.1
  • Containerd.io 1.4.3

确保没有问题后,开始准备安装。等待安装过程,取决网络速度,安装containerd/kubelet/kubeadm/kubectl。
image.png
当出现如下提示信息时,k8s集群安装完成。安装内容主要包含

  • master节点
    • kubeadm、kubectl、kubelet
  • node1/node2节点
    • kubectl、kubelet
    • kubectl、kubelet

image.png

Kubeadm

kubeadm 是 Kubernetes 官方支持的安装方式,“二进制” 不是。关于使用kubeadm安装的方式就不再赘述,这里推荐kuboard官方提供的安装教程:

关于安装版本的说明:

  • Kubernetes v1.21 开始,默认移除 docker 的依赖,如果宿主机上安装了 docker 和 containerd,将优先使用 docker 作为容器运行引擎,如果宿主机上未安装 docker 只安装了 containerd,将使用 containerd 作为容器运行引擎
  • 本文使用 containerd 作为容器运行引擎

    Dashboard安装

    k8s提供了强大的集群管理功能,但是对于使用kubectl操作的方式过于专业,因此官方和第三方都提供了web图形化界面支持的工具,下面介绍两种安装方式。

    Kubernetes Dashboard

    Kubernetes Dashboard 是 Kubernetes 的官方 Web UI。使用 Kubernetes Dashboard,您可以:

  • 向 Kubernetes 集群部署容器化应用

  • 诊断容器化应用的问题
  • 管理集群的资源
  • 查看集群上所运行的应用程序
  • 创建、修改Kubernetes 上的资源(例如 Deployment、Job、DaemonSet等)
  • 展示集群上发生的错误

下面介绍如何安装,使用下面命令获取安装模板yaml文件,这里使用v2.5.0版本的dashboard。

  1. wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.0/aio/deploy/recommended.yaml

下载yaml文件后,注意修改service kubernetes-dashboard的类型为NodePort,可以在集群外部访问。

  1. # Copyright 2017 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. apiVersion: v1
  15. kind: Namespace
  16. metadata:
  17. name: kubernetes-dashboard
  18. ---
  19. apiVersion: v1
  20. kind: ServiceAccount
  21. metadata:
  22. labels:
  23. k8s-app: kubernetes-dashboard
  24. name: kubernetes-dashboard
  25. namespace: kubernetes-dashboard
  26. ---
  27. kind: Service
  28. apiVersion: v1
  29. metadata:
  30. labels:
  31. k8s-app: kubernetes-dashboard
  32. name: kubernetes-dashboard
  33. namespace: kubernetes-dashboard
  34. spec:
  35. ports:
  36. - port: 443
  37. targetPort: 8443
  38. selector:
  39. k8s-app: kubernetes-dashboard
  40. ---
  41. apiVersion: v1
  42. kind: Secret
  43. metadata:
  44. labels:
  45. k8s-app: kubernetes-dashboard
  46. name: kubernetes-dashboard-certs
  47. namespace: kubernetes-dashboard
  48. type: Opaque
  49. ---
  50. apiVersion: v1
  51. kind: Secret
  52. metadata:
  53. labels:
  54. k8s-app: kubernetes-dashboard
  55. name: kubernetes-dashboard-csrf
  56. namespace: kubernetes-dashboard
  57. type: Opaque
  58. data:
  59. csrf: ""
  60. ---
  61. apiVersion: v1
  62. kind: Secret
  63. metadata:
  64. labels:
  65. k8s-app: kubernetes-dashboard
  66. name: kubernetes-dashboard-key-holder
  67. namespace: kubernetes-dashboard
  68. type: Opaque
  69. ---
  70. kind: ConfigMap
  71. apiVersion: v1
  72. metadata:
  73. labels:
  74. k8s-app: kubernetes-dashboard
  75. name: kubernetes-dashboard-settings
  76. namespace: kubernetes-dashboard
  77. ---
  78. kind: Role
  79. apiVersion: rbac.authorization.k8s.io/v1
  80. metadata:
  81. labels:
  82. k8s-app: kubernetes-dashboard
  83. name: kubernetes-dashboard
  84. namespace: kubernetes-dashboard
  85. rules:
  86. # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
  87. - apiGroups: [""]
  88. resources: ["secrets"]
  89. resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
  90. verbs: ["get", "update", "delete"]
  91. # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
  92. - apiGroups: [""]
  93. resources: ["configmaps"]
  94. resourceNames: ["kubernetes-dashboard-settings"]
  95. verbs: ["get", "update"]
  96. # Allow Dashboard to get metrics.
  97. - apiGroups: [""]
  98. resources: ["services"]
  99. resourceNames: ["heapster", "dashboard-metrics-scraper"]
  100. verbs: ["proxy"]
  101. - apiGroups: [""]
  102. resources: ["services/proxy"]
  103. resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
  104. verbs: ["get"]
  105. ---
  106. kind: ClusterRole
  107. apiVersion: rbac.authorization.k8s.io/v1
  108. metadata:
  109. labels:
  110. k8s-app: kubernetes-dashboard
  111. name: kubernetes-dashboard
  112. rules:
  113. # Allow Metrics Scraper to get metrics from the Metrics server
  114. - apiGroups: ["metrics.k8s.io"]
  115. resources: ["pods", "nodes"]
  116. verbs: ["get", "list", "watch"]
  117. ---
  118. apiVersion: rbac.authorization.k8s.io/v1
  119. kind: RoleBinding
  120. metadata:
  121. labels:
  122. k8s-app: kubernetes-dashboard
  123. name: kubernetes-dashboard
  124. namespace: kubernetes-dashboard
  125. roleRef:
  126. apiGroup: rbac.authorization.k8s.io
  127. kind: Role
  128. name: kubernetes-dashboard
  129. subjects:
  130. - kind: ServiceAccount
  131. name: kubernetes-dashboard
  132. namespace: kubernetes-dashboard
  133. ---
  134. apiVersion: rbac.authorization.k8s.io/v1
  135. kind: ClusterRoleBinding
  136. metadata:
  137. name: kubernetes-dashboard
  138. roleRef:
  139. apiGroup: rbac.authorization.k8s.io
  140. kind: ClusterRole
  141. name: kubernetes-dashboard
  142. subjects:
  143. - kind: ServiceAccount
  144. name: kubernetes-dashboard
  145. namespace: kubernetes-dashboard
  146. ---
  147. kind: Deployment
  148. apiVersion: apps/v1
  149. metadata:
  150. labels:
  151. k8s-app: kubernetes-dashboard
  152. name: kubernetes-dashboard
  153. namespace: kubernetes-dashboard
  154. spec:
  155. replicas: 1
  156. revisionHistoryLimit: 10
  157. selector:
  158. matchLabels:
  159. k8s-app: kubernetes-dashboard
  160. template:
  161. metadata:
  162. labels:
  163. k8s-app: kubernetes-dashboard
  164. spec:
  165. securityContext:
  166. seccompProfile:
  167. type: RuntimeDefault
  168. containers:
  169. - name: kubernetes-dashboard
  170. image: kubernetesui/dashboard:v2.5.0
  171. imagePullPolicy: Always
  172. ports:
  173. - containerPort: 8443
  174. protocol: TCP
  175. args:
  176. - --auto-generate-certificates
  177. - --namespace=kubernetes-dashboard
  178. # Uncomment the following line to manually specify Kubernetes API server Host
  179. # If not specified, Dashboard will attempt to auto discover the API server and connect
  180. # to it. Uncomment only if the default does not work.
  181. # - --apiserver-host=http://my-address:port
  182. volumeMounts:
  183. - name: kubernetes-dashboard-certs
  184. mountPath: /certs
  185. # Create on-disk volume to store exec logs
  186. - mountPath: /tmp
  187. name: tmp-volume
  188. livenessProbe:
  189. httpGet:
  190. scheme: HTTPS
  191. path: /
  192. port: 8443
  193. initialDelaySeconds: 30
  194. timeoutSeconds: 30
  195. securityContext:
  196. allowPrivilegeEscalation: false
  197. readOnlyRootFilesystem: true
  198. runAsUser: 1001
  199. runAsGroup: 2001
  200. volumes:
  201. - name: kubernetes-dashboard-certs
  202. secret:
  203. secretName: kubernetes-dashboard-certs
  204. - name: tmp-volume
  205. emptyDir: {}
  206. serviceAccountName: kubernetes-dashboard
  207. nodeSelector:
  208. "kubernetes.io/os": linux
  209. # Comment the following tolerations if Dashboard must not be deployed on master
  210. tolerations:
  211. - key: node-role.kubernetes.io/master
  212. effect: NoSchedule
  213. ---
  214. kind: Service
  215. apiVersion: v1
  216. metadata:
  217. labels:
  218. k8s-app: dashboard-metrics-scraper
  219. name: dashboard-metrics-scraper
  220. namespace: kubernetes-dashboard
  221. spec:
  222. ports:
  223. - port: 8000
  224. targetPort: 8000
  225. selector:
  226. k8s-app: dashboard-metrics-scraper
  227. ---
  228. kind: Deployment
  229. apiVersion: apps/v1
  230. metadata:
  231. labels:
  232. k8s-app: dashboard-metrics-scraper
  233. name: dashboard-metrics-scraper
  234. namespace: kubernetes-dashboard
  235. spec:
  236. replicas: 1
  237. revisionHistoryLimit: 10
  238. selector:
  239. matchLabels:
  240. k8s-app: dashboard-metrics-scraper
  241. template:
  242. metadata:
  243. labels:
  244. k8s-app: dashboard-metrics-scraper
  245. spec:
  246. securityContext:
  247. seccompProfile:
  248. type: RuntimeDefault
  249. containers:
  250. - name: dashboard-metrics-scraper
  251. image: kubernetesui/metrics-scraper:v1.0.7
  252. ports:
  253. - containerPort: 8000
  254. protocol: TCP
  255. livenessProbe:
  256. httpGet:
  257. scheme: HTTP
  258. path: /
  259. port: 8000
  260. initialDelaySeconds: 30
  261. timeoutSeconds: 30
  262. volumeMounts:
  263. - mountPath: /tmp
  264. name: tmp-volume
  265. securityContext:
  266. allowPrivilegeEscalation: false
  267. readOnlyRootFilesystem: true
  268. runAsUser: 1001
  269. runAsGroup: 2001
  270. serviceAccountName: kubernetes-dashboard
  271. nodeSelector:
  272. "kubernetes.io/os": linux
  273. # Comment the following tolerations if Dashboard must not be deployed on master
  274. tolerations:
  275. - key: node-role.kubernetes.io/master
  276. effect: NoSchedule
  277. volumes:
  278. - name: tmp-volume
  279. emptyDir: {}

执行命令,开始应用yaml文件

  1. kubectl apply -f recommended.yaml

创建完成后,查看deployments或pods对应状态。

  1. [root@master ~]# kubectl get deployments -n kubernetes-dashboard
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. dashboard-metrics-scraper 1/1 1 1 6m7s
  4. kubernetes-dashboard 1/1 1 1 6m7s
  5. [root@master ~]# kubectl get pods -n kubernetes-dashboard
  6. NAME READY STATUS RESTARTS AGE
  7. dashboard-metrics-scraper-799d786dbf-p5h8c 1/1 Running 0 6m48s
  8. kubernetes-dashboard-546cbc58cd-p7j7d 1/1 Running 0 6m48s

创建ServiceAccount以及ClusterRoleBinding,新建k8s-auth.yaml文件

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: admin-user
  5. namespace: kubernetes-dashboard
  6. ---
  7. apiVersion: rbac.authorization.k8s.io/v1
  8. kind: ClusterRoleBinding
  9. metadata:
  10. name: admin-user
  11. roleRef:
  12. apiGroup: rbac.authorization.k8s.io
  13. kind: ClusterRole
  14. name: cluster-admin
  15. subjects:
  16. - kind: ServiceAccount
  17. name: admin-user
  18. namespace: kubernetes-dashboard

应用RBAC yaml文件

  1. [root@master ~]# kubectl apply -f k8s-auth.yaml
  2. serviceaccount/admin-user created
  3. clusterrolebinding.rbac.authorization.k8s.io/admin-user unchanged

然后通过浏览器访问Kubernetes Dashboard,如果出现不能访问的情况:可能跟集群使用的证书有关。查看名称空间所应用的证书,kubernetes-dashboard-certs对应的DATA为0,因此这种场景下就需要自签发证书了。

  1. [root@master ~]# kubectl get secrets -n kubernetes-dashboard
  2. NAME TYPE DATA AGE
  3. admin-user-token-zqfhp kubernetes.io/service-account-token 3 101s
  4. default-token-p2szn kubernetes.io/service-account-token 3 11m
  5. kubernetes-dashboard-certs Opaque 0 11m
  6. kubernetes-dashboard-csrf Opaque 1 11m
  7. kubernetes-dashboard-key-holder Opaque 2 11m
  8. kubernetes-dashboard-token-dqhdt kubernetes.io/service-account-token 3 11m

删除集群中kubernetes-dashboard名称空间下的证书

  1. kubectl delete secret kubernetes-dashboard-certs -n kubernetes-dashboard

生成证书

  1. openssl genrsa -out dashboard.key 2048
  2. openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN=your ip address'
  3. openssl x509 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt

生成新的secret

  1. kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kubernetes-dashboard

删除原有pod,由于使用development创建资源,会自动创建新Pod资源。

  1. [root@master ~]# kubectl delete pod kubernetes-dashboard-546cbc58cd-p7j7d -n kubernetes-dashboard
  2. pod "kubernetes-dashboard-546cbc58cd-p7j7d" deleted

查看新Pod创建状况,kubernetes-dashboard-546cbc58cd-pgs8s已经处于运行状态。

  1. [root@master ~]# kubectl get pods -n kubernetes-dashboard
  2. NAME READY STATUS RESTARTS AGE
  3. dashboard-metrics-scraper-799d786dbf-p5h8c 1/1 Running 0 22m
  4. kubernetes-dashboard-546cbc58cd-pgs8s 1/1 Running 0 72s

然后通过https://ip:端口访问
image.png
通过命令获取登录token

  1. kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
  2. # 输出结果
  3. Name: admin-user-token-zqfhp
  4. Namespace: kubernetes-dashboard
  5. Labels: <none>
  6. Annotations: kubernetes.io/service-account.name: admin-user
  7. kubernetes.io/service-account.uid: 7b0b0752-cda6-4931-8762-28d1683aab7d
  8. Type: kubernetes.io/service-account-token
  9. Data
  10. ====
  11. token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjY5akgzTk1kTWVQWjR1TUdwUXBGdE53a1kxcWF1TDJTLTRDVU1jSTB1ZkUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXpxZmhwIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI3YjBiMDc1Mi1jZGE2LTQ5MzEtODc2Mi0yOGQxNjgzYWFiN2QiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.QtJ0dY4K97w77cBpS3dJ_7X3s_eu_O6uEIT5aQ6Pi0dq4jIuNtsQEx6dcRcN2rdQH6loDpsaZjA3AFiDkE1tdmgab-A1nhyPFp8TV7aQ4Ga7bMstx4azUo1E71-9ynh43O69JgmP2995p10eRjA8ZA74uwSEjREBKVki5g9daWUfZ6d7vupbjkmqgOcIbb4UCclgBDKBL4UM0CXi7v2eWGJOOOcaTv2UpibifLhHSMqJ-Tl3U96iXGyKkEk41D_fDvtUT7h7PSqwZyvP6amOXRxE4kuI2kwlzv9SNol8oXLYjlE3q5m2OSuZ6YHOX9bH4gFy_H9_tCOcxwKky4hKDA
  12. ca.crt: 1099 bytes
  13. namespace: 20 bytes

在登录页面输入token,进入首页看到集群的资源信息
image.png
配置用户名/密码登录
写入鉴权文件

  1. echo 'admin,admin,1' > /etc/kubernetes/pki/basic_auth_file

修改配置文件

  1. vim /etc/kubernetes/manifests/kube-apiserver.yaml
  2. # 增加如下参数
  3. - --basic-auth-file=/etc/kubernetes/pki/basic_auth_file

修改Kubernetes dashboard配置文件yaml,在args添加配置

  1. - --authentication-mode=basic

绑定用户权限

  1. kubectl create clusterrolebinding login-on-dashboard-with-cluster-admin --clusterrole=cluster-admin --user=admin

重新加载Dashboard

  1. kubectl apply -f kubernetes-dashboard.yaml

Kuboard Dashboard

Kuboard是一款开源的k8s图形化管理软件,提供了一键安装的方式。

  1. kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml
  2. # 您也可以使用下面的指令,唯一的区别是,该指令使用华为云的镜像仓库替代 docker hub 分发 Kuboard 所需要的镜像
  3. # kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3-swr.yaml

执行命令监控安装进度

  1. watch kubectl get pods -n kuboard
  2. Every 2.0s: kubectl get pods -n kuboard master: Wed Mar 30 22:11:22 2022
  3. NAME READY STATUS RESTARTS AGE
  4. kuboard-agent-2-584456bfcb-rrwwf 1/1 Running 1 (2m23s ago) 2m43s
  5. kuboard-agent-64dc57c9b-2d84d 1/1 Running 1 (2m26s ago) 2m43s
  6. kuboard-etcd-69bcb 1/1 Running 0 4m6s
  7. kuboard-questdb-7bcf54c98c-s825d 1/1 Running 0 2m42s
  8. kuboard-v3-56b4b954c9-745zt 1/1 Running 0 4m6s

容器全部进入running状态后,通过浏览器访问

image.png
具体安装细节参考官方网站:https://www.kuboard.cn/install/v3/install-in-k8s.html

摘自Kuboard官网:Kuboard 的定位和 Dashboard 是相似的,主要的区别 在于:

  • Kuboard 关注微服务参考架构的视角对界面进行组织,参考 Kuboard 简介
  • Kuboard 中,不需要手工编写 YAML 文件,进一步降低 K8S 使用难度,提高便捷性
  • Kuboard 可以导出整个微服务架构的部署信息,并在新的名称空间/集群导入配置信息
  • Kuboard 的一个发展方向是,提供内建的 监控套件(目前的全局监控套件成熟度比较高)

参考文档: