当前位置:   article > 正文

Kubernetes集群搭建

Kubernetes集群搭建

入职之前闲置在学校,跟着尚硅谷视频分别在云服务器和本地虚拟机各搭建了一套k8s集群(云服务器有点小贵),这里对搭建步骤做个总结。

参考文档:

https://www.yuque.com/leifengyang/oncloud/ghnb83

[前置操作] 安装docker

1、移除以前的docker安装包

  1. sudo yum remove docker \
  2. docker-client \
  3. docker-client-latest \
  4. docker-common \
  5. docker-latest \
  6. docker-latest-logrotate \
  7. docker-logrotate \
  8. docker-engine

2、配置yum源

  1. sudo yum install -y yum-utils
  2. sudo yum-config-manager \
  3. --add-repo \
  4. http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3、安装docker

yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7  containerd.io-1.4.6

4、启动

systemctl enable docker --now

5、配置加速--加速器地址查看自己阿里云账号的[镜像加速]

  1. sudo mkdir -p /etc/docker
  2. sudo tee /etc/docker/daemon.json <<-'EOF'
  3. {
  4. "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  5. "exec-opts": ["native.cgroupdriver=systemd"],
  6. "log-driver": "json-file",
  7. "log-opts": {
  8. "max-size": "100m"
  9. },
  10. "storage-driver": "overlay2"
  11. }
  12. EOF
  13. sudo systemctl daemon-reload
  14. sudo systemctl restart docker

[安装k8s] 

准备工作:

1、2GRAM、2核CPU;

2、节点之间通信需要放开对应端口,或者关闭防火墙;

3、若是虚拟机,则需要设置静态IP,否则重启后IP可能改变;

4、不同主机设置不同的hostname;

5、禁用交换分区

正式配置:

1、基础环境

  1. #各个机器设置自己的域名
  2. hostnamectl set-hostname xxxx
  3. # 将 SELinux 设置为 permissive 模式(相当于将其禁用)
  4. sudo setenforce 0
  5. sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
  6. #关闭swap
  7. swapoff -a
  8. sed -ri 's/.*swap.*/#&/' /etc/fstab
  9. #允许 iptables 检查桥接流量
  10. cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
  11. br_netfilter
  12. EOF
  13. cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
  14. net.bridge.bridge-nf-call-ip6tables = 1
  15. net.bridge.bridge-nf-call-iptables = 1
  16. EOF
  17. sudo sysctl --system

2、安装kubelet、kubeadm、kubectl

  1. cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  5. enabled=1
  6. gpgcheck=0
  7. repo_gpgcheck=0
  8. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  9. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  10. exclude=kubelet kubeadm kubectl
  11. EOF
  12. sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
  13. sudo systemctl enable --now kubelet

3、下载各个机器需要的镜像

  1. sudo tee ./images.sh <<-'EOF'
  2. #!/bin/bash
  3. images=(
  4. kube-apiserver:v1.20.9
  5. kube-proxy:v1.20.9
  6. kube-controller-manager:v1.20.9
  7. kube-scheduler:v1.20.9
  8. coredns:1.7.0
  9. etcd:3.4.13-0
  10. pause:3.2
  11. )
  12. for imageName in ${images[@]} ; do
  13. docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
  14. done
  15. EOF
  16. chmod +x ./images.sh && ./images.sh

4、初始化主节点

  1. #所有机器添加master域名映射,以下需要修改为自己的
  2. echo "172.31.0.4 cluster-endpoint" >> /etc/hosts
  3. #主节点初始化
  4. kubeadm init \
  5. --apiserver-advertise-address=172.31.0.4 \
  6. --control-plane-endpoint=cluster-endpoint \
  7. --image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
  8. --kubernetes-version v1.20.9 \
  9. --service-cidr=10.96.0.0/16 \
  10. --pod-network-cidr=192.168.0.0/16
  11. #所有网络范围不重叠

设置.kube/config

# 执行下述命令

5、安装网络组件-calico

  1. curl https://docs.projectcalico.org/manifests/calico.yaml -O
  2. kubectl apply -f calico.yaml

6、从节点加入集群

  1. kubeadm join cluster-endpoint:6443 --token x5g4uy.wpjjdbgra92s25pp \
  2. --discovery-token-ca-cert-hash sha256:6255797916eaee52bf9dda9429db616fcd828436708345a308f4b917d3457a22

忘了可以生成新令牌:

kubeadm token create --print-join-command

7、部署dashboard

使用以下文件:

  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. containers:
  166. - name: kubernetes-dashboard
  167. image: kubernetesui/dashboard:v2.3.1
  168. imagePullPolicy: Always
  169. ports:
  170. - containerPort: 8443
  171. protocol: TCP
  172. args:
  173. - --auto-generate-certificates
  174. - --namespace=kubernetes-dashboard
  175. # Uncomment the following line to manually specify Kubernetes API server Host
  176. # If not specified, Dashboard will attempt to auto discover the API server and connect
  177. # to it. Uncomment only if the default does not work.
  178. # - --apiserver-host=http://my-address:port
  179. volumeMounts:
  180. - name: kubernetes-dashboard-certs
  181. mountPath: /certs
  182. # Create on-disk volume to store exec logs
  183. - mountPath: /tmp
  184. name: tmp-volume
  185. livenessProbe:
  186. httpGet:
  187. scheme: HTTPS
  188. path: /
  189. port: 8443
  190. initialDelaySeconds: 30
  191. timeoutSeconds: 30
  192. securityContext:
  193. allowPrivilegeEscalation: false
  194. readOnlyRootFilesystem: true
  195. runAsUser: 1001
  196. runAsGroup: 2001
  197. volumes:
  198. - name: kubernetes-dashboard-certs
  199. secret:
  200. secretName: kubernetes-dashboard-certs
  201. - name: tmp-volume
  202. emptyDir: {}
  203. serviceAccountName: kubernetes-dashboard
  204. nodeSelector:
  205. "kubernetes.io/os": linux
  206. # Comment the following tolerations if Dashboard must not be deployed on master
  207. tolerations:
  208. - key: node-role.kubernetes.io/master
  209. effect: NoSchedule
  210. ---
  211. kind: Service
  212. apiVersion: v1
  213. metadata:
  214. labels:
  215. k8s-app: dashboard-metrics-scraper
  216. name: dashboard-metrics-scraper
  217. namespace: kubernetes-dashboard
  218. spec:
  219. ports:
  220. - port: 8000
  221. targetPort: 8000
  222. selector:
  223. k8s-app: dashboard-metrics-scraper
  224. ---
  225. kind: Deployment
  226. apiVersion: apps/v1
  227. metadata:
  228. labels:
  229. k8s-app: dashboard-metrics-scraper
  230. name: dashboard-metrics-scraper
  231. namespace: kubernetes-dashboard
  232. spec:
  233. replicas: 1
  234. revisionHistoryLimit: 10
  235. selector:
  236. matchLabels:
  237. k8s-app: dashboard-metrics-scraper
  238. template:
  239. metadata:
  240. labels:
  241. k8s-app: dashboard-metrics-scraper
  242. annotations:
  243. seccomp.security.alpha.kubernetes.io/pod: 'runtime/default'
  244. spec:
  245. containers:
  246. - name: dashboard-metrics-scraper
  247. image: kubernetesui/metrics-scraper:v1.0.6
  248. ports:
  249. - containerPort: 8000
  250. protocol: TCP
  251. livenessProbe:
  252. httpGet:
  253. scheme: HTTP
  254. path: /
  255. port: 8000
  256. initialDelaySeconds: 30
  257. timeoutSeconds: 30
  258. volumeMounts:
  259. - mountPath: /tmp
  260. name: tmp-volume
  261. securityContext:
  262. allowPrivilegeEscalation: false
  263. readOnlyRootFilesystem: true
  264. runAsUser: 1001
  265. runAsGroup: 2001
  266. serviceAccountName: kubernetes-dashboard
  267. nodeSelector:
  268. "kubernetes.io/os": linux
  269. # Comment the following tolerations if Dashboard must not be deployed on master
  270. tolerations:
  271. - key: node-role.kubernetes.io/master
  272. effect: NoSchedule
  273. volumes:
  274. - name: tmp-volume
  275. emptyDir: {}

设置访问端口:

kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard

type: ClusterIP 改为 type: NodePort

  1. kubectl get svc -A |grep kubernetes-dashboard
  2. ## 找到端口,在安全组放行

创建访问账号:

  1. #创建访问账号,准备一个yaml文件; vi dash.yaml
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: admin-user
  6. namespace: kubernetes-dashboard
  7. ---
  8. apiVersion: rbac.authorization.k8s.io/v1
  9. kind: ClusterRoleBinding
  10. metadata:
  11. name: admin-user
  12. roleRef:
  13. apiGroup: rbac.authorization.k8s.io
  14. kind: ClusterRole
  15. name: cluster-admin
  16. subjects:
  17. - kind: ServiceAccount
  18. name: admin-user
  19. namespace: kubernetes-dashboard

kubectl apply -f dash.yaml

获取令牌并访问:

  1. #获取访问令牌
  2. kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"

访问: https://集群任意IP:端口 https://139.198.165.238:32759

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

闽ICP备14008679号