当前位置:   article > 正文

ubuntu22.04安装kubernetes1.28

ubuntu22.04安装kubernetes

一、环境介绍

注意:一般3个节点就行,搭建好后,根据需要节点可以再扩充

二、所有节点都要执行

1、虚拟机基础环境配置

  1. # 1 修改为静态ip地址
  2. ssh ziu@192.168.48.x
  3. sudo su
  4. cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bbk
  5. vim /etc/netplan/00-installer-config.yaml # ubuntu22.04的设置方法
  6. network:
  7. ethernets:
  8. ens33:
  9. dhcp4: false
  10. addresses: [192.168.48.210/24]
  11. optional: true
  12. routes:
  13. - to: default
  14. via: 192.168.48.2
  15. nameservers:
  16. addresses: [192.168.48.2]
  17. version: 2
  18. netplan apply
  19. # 2 修改为root登录
  20. sudo su
  21. passwd root
  22. vim /etc/ssh/sshd_config
  23. ...
  24. PermitRootLogin yes
  25. sudo service ssh restart
  26. # 3 修改主机名和hosts
  27. hostnamectl set-hostname xxx
  28. vim /etc/hosts
  29. ...
  30. 192.168.48.210 kmaster
  31. 192.168.48.211 knode1
  32. 192.168.48.212 knode2
  33. 192.168.48.213 knode3 ##新增的然后同步到其他主机
  34. 192.168.48.214 knode4 ##新增的然后同步到其他主机

2、linux内核参数修改

  1. # 设置转发 IPv4 并让 iptables 看到桥接流量
  2. cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
  3. overlay
  4. br_netfilter
  5. EOF
  6. sudo modprobe overlay
  7. sudo modprobe br_netfilter
  8. # 设置所需的 sysctl 参数,参数在重新启动后保持不变
  9. cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
  10. net.bridge.bridge-nf-call-iptables = 1
  11. net.bridge.bridge-nf-call-ip6tables = 1
  12. net.ipv4.ip_forward = 1
  13. EOF
  14. # 应用 sysctl 参数而不重新启动
  15. sudo sysctl --system
  16. # 通过运行以下指令确认 br_netfilter 和 overlay 模块被加载:
  17. lsmod | grep br_netfilter
  18. lsmod | grep overlay
  19. #通过运行以下指令确认 net.bridge.bridge-nf-call-iptables、net.bridge.bridge-nf-call-ip6tables 和 net.ipv4.ip_forward 系统变量在你的 sysctl 配置中被设置为 1:
  20. sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

3、设置cgroups驱动(此步骤不操作,后面设置)

        有2种设置方式:cgroupfs和systemd,k8s官网建议使用systemd
        要将 systemd 设置为 cgroup 驱动,需编辑 KubeletConfiguration 的 cgroupDriver 选项,并将其设置为 systemd。例如:

  1. apiVersion: kubelet.config.k8s.io/v1beta1
  2. kind: KubeletConfiguration
  3. ...
  4. cgroupDriver: systemd

        说明:从 v1.22 开始,在使用 kubeadm 创建集群时,如果用户没有在 KubeletConfiguration 下设置 cgroupDriver 字段,kubeadm 默认使用 systemd。

4、CRI版本支持

        目的:安装 CNI plugins

        参考下载地址: https://github.com/containernetworking/plugins/releases

  1. #下载出现问题就请到参考地址重新找资源下载
  2. wget -c https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz
  3. #根据官网的安装步骤来,创建一个目录用于存放cni插件
  4. mkdir -p /opt/cni/bin
  5. tar -xzvf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin/

5、安装容器运行时

  1. # 卸载docker相关的配置
  2. for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
  3. sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
  4. sudo rm -rf /var/lib/docker
  5. sudo rm -rf /var/lib/containerd
  6. # Add Docker's official GPG key:
  7. # https://docs.docker.com/engine/install/ubuntu/
  8. sudo apt-get update
  9. sudo apt-get install ca-certificates curl gnupg
  10. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  11. sudo chmod a+r /etc/apt/keyrings/docker.gpg
  12. # Add the repository to Apt sources:
  13. echo \
  14. "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  15. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  16. sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  17. sudo apt-get update
  18. apt-cache madison containerd.io
  19. apt install containerd.io=1.6.24-1
  20. containerd --version
  21. containerd containerd.io 1.6.24 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
  22. systemctl daemon-reload
  23. systemctl enable --now containerd
  24. containerd config default >/etc/containerd/config.toml
  25. vim /etc/containerd/config.toml
  26. ...
  27. sandbox_image = "registry.k8s.io/pause:3.6" # 值修改为registry.aliyuncs.com/google_containers/pause:3.9
  28. ...
  29. SystemdCgroup = true #设置cgroup为systemd,对应前面第2步没执行的,这里设置cgroups驱动
  30. systemctl daemon-reload
  31. systemctl restart containerd.service
  32. #安装runc
  33. #runc是容器运行时,runc实现了容器的init,run,create,ps...我们在运行容器所需要的cmd:
  34. #https://github.com/opencontainers/runc/releases/
  35. curl -LO https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64
  36. install -m 755 runc.amd64 /usr/local/sbin/runc

6、其他配置项

  1. # 确保没个节点上的mac地址和product_uuid的唯一性
  2. cat /sys/class/dmi/id/product_uuid
  3. ip link | grep brd
  4. # 关闭swap
  5. swapoff -a
  6. vim /etc/fstab
  7. ...
  8. #/swap.img
  9. free -h
  10. # 更正时区,时间同步
  11. #查看时区,时间
  12. tzselect
  13. cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  14. date
  15. #安装chrony,联网同步时间(非必须,一般没问题,我这里没执行)
  16. apt install chrony -y && systemctl enable --now chrony
  17. # lvm磁盘扩容(系统安装时没有启用lvm可以忽略,目的是将存储资源最大化利用)
  18. lvextend -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
  19. # 关闭ufw和firewalled
  20. systemctl stop ufw
  21. systemctl disable ufw
  22. # 禁用selinux(ubuntu没有启用,centos才默认启用,需要注意一下)
  23. #默认ubunt默认是不安装selinux的,如果没有selinux命令和配置文件则说明没有安装selinux,则下面步骤就不用做了
  24. sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  25. setenforce 0

7、安装kubeadm,kubectl,kubelet(1.28.0)

  1. #安装k8s的3个工具
  2. apt-get update
  3. apt-get install -y apt-transport-https
  4. curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
  5. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  6. deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
  7. EOF
  8. apt-get update
  9. apt install apt-file -y
  10. apt-file update
  11. # 查看所有包,会列出可以安装的所有版本
  12. apt list kubeadm -a
  13. apt install kubeadm=1.28.0-00 kubectl=1.28.0-00 kubelet=1.28.0-00 -y
  14. systemctl enable kubelet

三、master主节点配置

1、准备k8s集群配置文件

  1. # 导出配置
  2. kubeadm config print init-defaults > Kubernetes-cluster.yaml
  1. #文档可以直接复制这个
  2. vim Kubernetes-cluster.yaml
  3. apiVersion: kubeadm.k8s.io/v1beta3
  4. bootstrapTokens:
  5. - groups:
  6. - system:bootstrappers:kubeadm:default-node-token
  7. token: abcdef.0123456789abcdef
  8. ttl: 24h0m0s
  9. usages:
  10. - signing
  11. - authentication
  12. kind: InitConfiguration
  13. localAPIEndpoint:
  14. advertiseAddress: 192.168.48.210 ##改为自己的
  15. bindPort: 6443
  16. nodeRegistration:
  17. criSocket: unix:///run/containerd/containerd.sock #改成这样
  18. imagePullPolicy: IfNotPresent
  19. name: kmaster ##改为自己的主机名
  20. taints: null
  21. ---
  22. apiServer:
  23. timeoutForControlPlane: 4m0s
  24. apiVersion: kubeadm.k8s.io/v1beta3
  25. certificatesDir: /etc/kubernetes/pki
  26. clusterName: kubernetes
  27. controllerManager: {}
  28. dns: {}
  29. etcd:
  30. local:
  31. dataDir: /var/lib/etcd
  32. imageRepository: registry.aliyuncs.com/google_containers #改成阿里的仓库,加快镜像下载速度
  33. kind: ClusterConfiguration
  34. kubernetesVersion: 1.28.0
  35. networking:
  36. dnsDomain: cluster.local
  37. podSubnet: 10.244.0.0/16
  38. serviceSubnet: 10.96.0.0/12
  39. scheduler: {}

2、初始化集群

        注意设置pause的版本,根据提示来,修改为提示建议的版本即可,读取的/etc/containerd/config.toml的设置。

  1. #执行命令
  2. kubeadm init --config Kubernetes-cluster.yaml
  3. #成功后请将以下输出保存
  4. Your Kubernetes control-plane has initialized successfully!
  5. To start using your cluster, you need to run the following as a regular user:
  6. mkdir -p $HOME/.kube
  7. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  8. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  9. Alternatively, if you are the root user, you can run:
  10. export KUBECONFIG=/etc/kubernetes/admin.conf
  11. You should now deploy a pod network to the cluster.
  12. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  13. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  14. Then you can join any number of worker nodes by running the following on each as root:
  15. kubeadm join 192.168.48.210:6443 --token abcdef.0123456789abcdef \
  16. --discovery-token-ca-cert-hash sha256:121e29b7f40844aca55b1b57d3115910d62c48906fa9e56c98e9f204027f60b5
  17. #run the following as a regular user
  18. mkdir -p $HOME/.kube
  19. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  20. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  21. #这里集群状态还是noready,需要设置网络,这里用的flannel,请继续执行

3、设置flannel集群网络

  1. #如果某个节点notready,请检查kubelet的状态,然后启动它以及是否安装网络插件比如flannel
  2. #设置网络flannel
  3. #下载flannel配置文件
  4. wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
  5. cat kube-flannel.yml ##网络地址和配置kubeadm初始化的pod一致
  6. apiVersion: v1
  7. kind: Namespace
  8. metadata:
  9. labels:
  10. k8s-app: flannel
  11. pod-security.kubernetes.io/enforce: privileged
  12. name: kube-flannel
  13. ---
  14. apiVersion: v1
  15. kind: ServiceAccount
  16. metadata:
  17. labels:
  18. k8s-app: flannel
  19. name: flannel
  20. namespace: kube-flannel
  21. ---
  22. apiVersion: rbac.authorization.k8s.io/v1
  23. kind: ClusterRole
  24. metadata:
  25. labels:
  26. k8s-app: flannel
  27. name: flannel
  28. rules:
  29. - apiGroups:
  30. - ""
  31. resources:
  32. - pods
  33. verbs:
  34. - get
  35. - apiGroups:
  36. - ""
  37. resources:
  38. - nodes
  39. verbs:
  40. - get
  41. - list
  42. - watch
  43. - apiGroups:
  44. - ""
  45. resources:
  46. - nodes/status
  47. verbs:
  48. - patch
  49. - apiGroups:
  50. - networking.k8s.io
  51. resources:
  52. - clustercidrs
  53. verbs:
  54. - list
  55. - watch
  56. ---
  57. apiVersion: rbac.authorization.k8s.io/v1
  58. kind: ClusterRoleBinding
  59. metadata:
  60. labels:
  61. k8s-app: flannel
  62. name: flannel
  63. roleRef:
  64. apiGroup: rbac.authorization.k8s.io
  65. kind: ClusterRole
  66. name: flannel
  67. subjects:
  68. - kind: ServiceAccount
  69. name: flannel
  70. namespace: kube-flannel
  71. ---
  72. apiVersion: v1
  73. data:
  74. cni-conf.json: |
  75. {
  76. "name": "cbr0",
  77. "cniVersion": "0.3.1",
  78. "plugins": [
  79. {
  80. "type": "flannel",
  81. "delegate": {
  82. "hairpinMode": true,
  83. "isDefaultGateway": true
  84. }
  85. },
  86. {
  87. "type": "portmap",
  88. "capabilities": {
  89. "portMappings": true
  90. }
  91. }
  92. ]
  93. }
  94. net-conf.json: |
  95. {
  96. "Network": "10.244.0.0/16",
  97. "Backend": {
  98. "Type": "vxlan"
  99. }
  100. }
  101. kind: ConfigMap
  102. metadata:
  103. labels:
  104. app: flannel
  105. k8s-app: flannel
  106. tier: node
  107. name: kube-flannel-cfg
  108. namespace: kube-flannel
  109. ---
  110. apiVersion: apps/v1
  111. kind: DaemonSet
  112. metadata:
  113. labels:
  114. app: flannel
  115. k8s-app: flannel
  116. tier: node
  117. name: kube-flannel-ds
  118. namespace: kube-flannel
  119. spec:
  120. selector:
  121. matchLabels:
  122. app: flannel
  123. k8s-app: flannel
  124. template:
  125. metadata:
  126. labels:
  127. app: flannel
  128. k8s-app: flannel
  129. tier: node
  130. spec:
  131. affinity:
  132. nodeAffinity:
  133. requiredDuringSchedulingIgnoredDuringExecution:
  134. nodeSelectorTerms:
  135. - matchExpressions:
  136. - key: kubernetes.io/os
  137. operator: In
  138. values:
  139. - linux
  140. containers:
  141. - args:
  142. - --ip-masq
  143. - --kube-subnet-mgr
  144. command:
  145. - /opt/bin/flanneld
  146. env:
  147. - name: POD_NAME
  148. valueFrom:
  149. fieldRef:
  150. fieldPath: metadata.name
  151. - name: POD_NAMESPACE
  152. valueFrom:
  153. fieldRef:
  154. fieldPath: metadata.namespace
  155. - name: EVENT_QUEUE_DEPTH
  156. value: "5000"
  157. image: docker.io/flannel/flannel:v0.23.0
  158. name: kube-flannel
  159. resources:
  160. requests:
  161. cpu: 100m
  162. memory: 50Mi
  163. securityContext:
  164. capabilities:
  165. add:
  166. - NET_ADMIN
  167. - NET_RAW
  168. privileged: false
  169. volumeMounts:
  170. - mountPath: /run/flannel
  171. name: run
  172. - mountPath: /etc/kube-flannel/
  173. name: flannel-cfg
  174. - mountPath: /run/xtables.lock
  175. name: xtables-lock
  176. hostNetwork: true
  177. initContainers:
  178. - args:
  179. - -f
  180. - /flannel
  181. - /opt/cni/bin/flannel
  182. command:
  183. - cp
  184. image: docker.io/flannel/flannel-cni-plugin:v1.2.0
  185. name: install-cni-plugin
  186. volumeMounts:
  187. - mountPath: /opt/cni/bin
  188. name: cni-plugin
  189. - args:
  190. - -f
  191. - /etc/kube-flannel/cni-conf.json
  192. - /etc/cni/net.d/10-flannel.conflist
  193. command:
  194. - cp
  195. image: docker.io/flannel/flannel:v0.23.0
  196. name: install-cni
  197. volumeMounts:
  198. - mountPath: /etc/cni/net.d
  199. name: cni
  200. - mountPath: /etc/kube-flannel/
  201. name: flannel-cfg
  202. priorityClassName: system-node-critical
  203. serviceAccountName: flannel
  204. tolerations:
  205. - effect: NoSchedule
  206. operator: Exists
  207. volumes:
  208. - hostPath:
  209. path: /run/flannel
  210. name: run
  211. - hostPath:
  212. path: /opt/cni/bin
  213. name: cni-plugin
  214. - hostPath:
  215. path: /etc/cni/net.d
  216. name: cni
  217. - configMap:
  218. name: kube-flannel-cfg
  219. name: flannel-cfg
  220. - hostPath:
  221. path: /run/xtables.lock
  222. type: FileOrCreate
  223. name: xtables-lock
  224. #应用flannel网络配置
  225. kubectl apply -f kube-flannel.yml

四、从节点加入集群

1、第一次加入的

  1. #打开其他节点虚拟机
  2. kubeadm join 192.168.48.210:6443 --token abcdef.0123456789abcdef \
  3. --discovery-token-ca-cert-hash sha256:121e29b7f40844aca55b1b57d3115910d62c48906fa9e56c98e9f204027f60b5
  4. #参考文档
  5. https://blog.csdn.net/qq_45654671/article/details/128238921
 

2、后面加入的(加入token失效时操作步骤)

  1. #在master上重新生成token(对于过了1小时再加入集群的新节点都要执行以下步骤生成新的token)
  2. kubeadm token generate   #生成toke
  3. #会打印出新生成的token
  4. cn4y74.ngzahx3ul01og1et  
  5. kubeadm token create cn4y74.ngzahx3ul01og1et --print-join-command --ttl=0  
  6. #利用上面的token输出添加命令
  7. #会打印出新的join命令
  8. kubeadm join 192.168.48.210:6443 --token cn4y74.ngzahx3ul01og1et --discovery-token-ca-cert-hash sha256:121e29b7f40844aca55b1b57d3115910d62c48906fa9e56c98e9f204027f60b5

​五、检查集群状态

  1. root@kmaster:~# kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. kmaster Ready control-plane 5d17h v1.28.0
  4. knode1 Ready <none> 5d17h v1.28.0
  5. knode2 Ready <none> 5d17h v1.28.0
  6. knode3 Ready <none> 2m40s v1.28.0
  7. knode4 Ready <none> 2m30s v1.28.0


​六、设置命令补全

        一般主节点设置即可。

  1. # 设置补充命令
  2. apt install bash-completion -y
  3. echo "source <(kubectl completion bash)" >> ~/.bashrc
  4. source .bashrc

​七、设置dashboard

1、选择dashboard版本

注意:这里大家可以参考github的发布版本,本次实验用的是dashboard2.7.0

GitHub - kubernetes/dashboard: General-purpose web UI for Kubernetes clusters

2、master主节点安装

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

3、设置集群外主机可访问

注意:这里需要删除默认的service,将clusterip设置nodeport

  1. #删除默认创建的service
  2. kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
  3. #创建自定义的service文件
  4. vim dashboard-svc.yaml
  5. kind: Service
  6. apiVersion: v1
  7. metadata:
  8. labels:
  9. k8s-app: kubernetes-dashboard
  10. name: kubernetes-dashboard
  11. namespace: kubernetes-dashboard
  12. spec:
  13. type: NodePort
  14. ports:
  15. - port: 443
  16. targetPort: 8443
  17. selector:
  18. k8s-app: kubernetes-dashboard
  19. #具体过程如下
  20. root@kmaster:~/app/dashboard# kubectl get svc --all-namespaces -owide
  21. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  22. default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d <none>
  23. kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 8d k8s-app=kube-dns
  24. kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.103.119.96 <none> 8000/TCP 8m33s k8s-app=dashboard-metrics-scraper
  25. kubernetes-dashboard kubernetes-dashboard ClusterIP 10.96.109.150 <none> 443/TCP 8m33s k8s-app=kubernetes-dashboard
  26. root@kmaster:~/app/dashboard# kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
  27. service "kubernetes-dashboard" deleted
  28. root@kmaster:~/app/dashboard# vim dashboard_svc.yaml
  29. root@kmaster:~/app/dashboard# kubectl get svc --all-namespaces -owide
  30. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  31. default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d <none>
  32. kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 8d k8s-app=kube-dns
  33. kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.103.119.96 <none> 8000/TCP 28m k8s-app=dashboard-metrics-scraper
  34. root@kmaster:~/app/dashboard# kubectl apply -f dashboard_svc.yaml
  35. service/kubernetes-dashboard created
  36. root@kmaster:~/app/dashboard# kubectl get svc --all-namespaces -owide
  37. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  38. default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d <none>
  39. kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 8d k8s-app=kube-dns
  40. kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.103.119.96 <none> 8000/TCP 29m k8s-app=dashboard-metrics-scraper
  41. kubernetes-dashboard kubernetes-dashboard NodePort 10.103.5.118 <none> 443:31184/TCP 25s k8s-app=kubernetes-dashboard

4、创建dashboard管理员角色

  1. #设置管理员用户文件
  2. vim admin-user.yaml
  3. apiVersion: v1
  4. kind: ServiceAccount
  5. metadata:
  6. name: admin-user
  7. namespace: kubernetes-dashboard
  8. #应用此文件
  9. kubectl apply -f admin-user.yaml
  10. #设置管理员角色连接
  11. vim admin-user-role-binding.yaml
  12. apiVersion: rbac.authorization.k8s.io/v1
  13. kind: ClusterRoleBinding
  14. metadata:
  15. name: admin-user
  16. roleRef:
  17. apiGroup: rbac.authorization.k8s.io
  18. kind: ClusterRole
  19. name: cluster-admin
  20. subjects:
  21. - kind: ServiceAccount
  22. name: admin-user
  23. namespace: kubernetes-dashboard
  24. #应用此文件
  25. kubectl apply -f admin-user-role-binding.yaml
  26. #查看节点以及端口(NodePort)
  27. kubectl get svc --all-namespaces -owide
  28. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  29. default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21d <none>
  30. kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 21d k8s-app=kube-dns
  31. kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.111.53.171 <none> 8000/TCP 22m k8s-app=dashboard-metrics-scraper
  32. kubernetes-dashboard kubernetes-dashboard NodePort 10.106.152.11 <none> 443:32078/TCP 10m k8s-app=kubernetes-dashboard
  33. ##关键步骤-创建登录token的步骤,token就是输出内容
  34. kubectl -n kubernetes-dashboard create token admin-user
  35. ##访问 https://节点ip:port

5、移除admin-user(安全的前提,下次要用界面再创建)

  1. kubectl -n kubernetes-dashboard delete serviceaccount admin-user
  2. kubectl -n kubernetes-dashboard delete clusterrolebinding admin-user

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/601250
推荐阅读
  

闽ICP备14008679号