当前位置:   article > 正文

CentOS安装k8s单机/集群及一些命令

CentOS安装k8s单机/集群及一些命令

目录

前言

1. 安装docker

2. 安装要求

3.准备网络(如果只装单机版可跳过此部)

4. 准备工作

5. 安装

5.1. 配置阿里云yum k8s源

5.2 安装kubeadm、kubectl和kubelet

5.3 初始化,只在master执行,子节点不要执行

5.3.1 一些错误(没有错误直接忽略)

5.4 使用kubectl工具

5.5 子节点加入(单机版可忽略)

5.6 部署CNI网络插件

6. 扩展


前言

        只针对1.24版本以前的k8s, 1.24版本以后删除了内置dockershim插件,原生不再支持docker运行时,需要使用第三方cri接口cri-docker

1. 安装docker

看我上一篇博客

CentOS安装docker及一些命令icon-default.png?t=N7T8https://blog.csdn.net/o_CanDou6/article/details/135505341

2. 安装要求

  • 内存大于等于2G,CPU大于等于2核,硬盘大于等于30G。
  • 禁止swap分区。

3.准备网络(如果只装单机版可跳过此部

新安装的Centos服务器需要配置静态网络:
打开网络配置文件:

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 

将以下内容添加进去,其中BOOTPROTO="static"表示静态网络,NAME和DEVICE填网卡驱动如果没有驱动需要手动安装即可;下面添加IP、子网掩码以及网关、DNS等内容。

  1. TYPE=Ethernet
  2. PROXY_METHOD=none
  3. BROWSER_ONLY=no
  4. BOOTPROTO=static #需要修改
  5. DEFROUTE=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_FAILURE_FATAL=no
  11. NAME=enp0s3
  12. UUID=5c84522d-4102-4260-9a23-4121bd510252
  13. DEVICE=enp0s3
  14. ONBOOT=yes
  15. IPADDR=192.168.2.159 #修改固定ip
  16. NETMASK=255.255.255.0 #同步修改
  17. GATEWAY=192.168.2.1 #同步修改
  18. DNS1=192.168.2.1 #同步修改

准备了两个虚拟机当做演示ip地址如下(按自己的ip为准

角色名称IP
主节点master192.168.2.159
子节点node1192.168.2.64

4. 准备工作

  1. #永久关闭防火墙
  2. systemctl stop firewalld
  3. systemctl disable firewalld
  4. #永久关闭swap
  5. sed -ri 's/.*swap.*/#&/' /etc/fstab
  6. # 单机可以不执行如下命令
  7. ## 二台服务器设置主机名
  8. hostnamectl set-hostname <hostname>
  9. ## 修改hosts
  10. vi /etc/hosts
  11. # 加入如下数据 按自己ip增加
  12. 192.168.2.159 master
  13. 192.168.2.64 node1

5. 安装

5.1. 配置阿里云yum k8s

  1. vi /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  5. enabled=1
  6. gpgcheck=1
  7. repo_gpgcheck=1
  8. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

可查看k8s版本信息

yum list kubelet --showduplicates | sort -r

我们以1.21.0版本为例 (注意k8s版本需要对于特定的docker版本,不然安装不成功

以下是一些常见的k8s与Docker版本对应关系:

  • k8s v1.22.x 对应 Docker 20.10.x
  • k8s v1.21.x 对应 Docker 20.10.x
  • k8s v1.20.x 对应 Docker 19.03.x

5.2 安装kubeadm、kubectl和kubelet

  1. yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0
  2. systemctl enable kubelet

5.3 初始化,只在master执行,子节点不要执行

  1. # --apiserver-advertise-address=本机ip
  2. kubeadm init --kubernetes-version=1.21.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=[本机ip] --ignore-preflight-errors=all --image-repository=registry.aliyuncs.com/google_containers

5.3.1 一些错误(没有错误直接忽略

[WARNING FileExisting-tc]: tc not found in system path 错误

  1. # 解决方法
  2. yum install iproute-tc -y

[WARNING ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0: output: Error response from daemon: pull access denied for registry.aliyuncs.com/google_containers/coredns/coredns, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
, error: exit status 1

  1. # 解决方法
  2. docker pull coredns/coredns:latest
  3. docker tag coredns/coredns:latest registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0

5.4 使用kubectl工具

执行完毕后会出现如下内容红框内容在master(本机)上执行,蓝框的在子节点上执行加入集群

  1. mkdir -p $HOME/.kube
  2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config

执行完可以查看节点了

kubectl get nodes

现在只有自己

5.5 子节点加入(单机版可忽略

子节点不需要执行5.3 初始化的内容,只执行master输出的 kubeadm join 命令加入集群即可

# 执行自己的输出内容不要复制内容不同

kubeadm join 192.168.2.159:6443 --token e5doub.g27604rf65vj02yr \
        --discovery-token-ca-cert-hash sha256:2521d2d4ee37750feba14a00ef0de0dfc390b1141f7abda81b0e259ce01870af 

子节点执行完后再次查看节点

子节点加入进来了但是这时候子节点还不能使用,需要把master服务器/etc/kubernetes/admin.conf复制到子节点的/etc/kubernetes/文件夹中

然后再子节点中执行

  1. mkdir -p $HOME/.kube
  2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config

现在子节点可以正常使用了

5.6 部署CNI网络插件

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

访问不到的可以复制如下内容自己创建文件 使用 kubectl apply -f  执行

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

6. 扩展

在master安装 recommended.yaml 和 dashboard-adminuser.yml 使用图形界面查看 (这两个文件在下方

  1. kubectl apply -f recommended.yaml
  2. kubectl apply -f dashboard-adminuser.yml

安装完成后可以用火狐访问 https://[ip地址]:32508/#/login 如我的为 https://192.168.2.159:32508/#/login

注意不要用高版本Chrome或者edge登录https没有证书访问不了https://[ip地址]:32508/#/login 如我的为 

这里需要token执行下面命令获取token

  1. # 获取登录凭证
  2. kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin | awk '{print $1}')

粘贴后进入

recommended.yaml:

  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. type: NodePort
  36. ports:
  37. - port: 443
  38. targetPort: 8443
  39. nodePort: 32508
  40. selector:
  41. k8s-app: kubernetes-dashboard
  42. ---
  43. apiVersion: v1
  44. kind: Secret
  45. metadata:
  46. labels:
  47. k8s-app: kubernetes-dashboard
  48. name: kubernetes-dashboard-certs
  49. namespace: kubernetes-dashboard
  50. type: Opaque
  51. ---
  52. apiVersion: v1
  53. kind: Secret
  54. metadata:
  55. labels:
  56. k8s-app: kubernetes-dashboard
  57. name: kubernetes-dashboard-csrf
  58. namespace: kubernetes-dashboard
  59. type: Opaque
  60. data:
  61. csrf: ""
  62. ---
  63. apiVersion: v1
  64. kind: Secret
  65. metadata:
  66. labels:
  67. k8s-app: kubernetes-dashboard
  68. name: kubernetes-dashboard-key-holder
  69. namespace: kubernetes-dashboard
  70. type: Opaque
  71. ---
  72. kind: ConfigMap
  73. apiVersion: v1
  74. metadata:
  75. labels:
  76. k8s-app: kubernetes-dashboard
  77. name: kubernetes-dashboard-settings
  78. namespace: kubernetes-dashboard
  79. ---
  80. kind: Role
  81. apiVersion: rbac.authorization.k8s.io/v1
  82. metadata:
  83. labels:
  84. k8s-app: kubernetes-dashboard
  85. name: kubernetes-dashboard
  86. namespace: kubernetes-dashboard
  87. rules:
  88. # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
  89. - apiGroups: [""]
  90. resources: ["secrets"]
  91. resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
  92. verbs: ["get", "update", "delete"]
  93. # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
  94. - apiGroups: [""]
  95. resources: ["configmaps"]
  96. resourceNames: ["kubernetes-dashboard-settings"]
  97. verbs: ["get", "update"]
  98. # Allow Dashboard to get metrics.
  99. - apiGroups: [""]
  100. resources: ["services"]
  101. resourceNames: ["heapster", "dashboard-metrics-scraper"]
  102. verbs: ["proxy"]
  103. - apiGroups: [""]
  104. resources: ["services/proxy"]
  105. resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
  106. verbs: ["get"]
  107. ---
  108. kind: ClusterRole
  109. apiVersion: rbac.authorization.k8s.io/v1
  110. metadata:
  111. labels:
  112. k8s-app: kubernetes-dashboard
  113. name: kubernetes-dashboard
  114. rules:
  115. # Allow Metrics Scraper to get metrics from the Metrics server
  116. - apiGroups: ["metrics.k8s.io"]
  117. resources: ["pods", "nodes"]
  118. verbs: ["get", "list", "watch"]
  119. ---
  120. apiVersion: rbac.authorization.k8s.io/v1
  121. kind: RoleBinding
  122. metadata:
  123. labels:
  124. k8s-app: kubernetes-dashboard
  125. name: kubernetes-dashboard
  126. namespace: kubernetes-dashboard
  127. roleRef:
  128. apiGroup: rbac.authorization.k8s.io
  129. kind: Role
  130. name: kubernetes-dashboard
  131. subjects:
  132. - kind: ServiceAccount
  133. name: kubernetes-dashboard
  134. namespace: kubernetes-dashboard
  135. ---
  136. apiVersion: rbac.authorization.k8s.io/v1
  137. kind: ClusterRoleBinding
  138. metadata:
  139. name: kubernetes-dashboard
  140. roleRef:
  141. apiGroup: rbac.authorization.k8s.io
  142. kind: ClusterRole
  143. name: kubernetes-dashboard
  144. subjects:
  145. - kind: ServiceAccount
  146. name: kubernetes-dashboard
  147. namespace: kubernetes-dashboard
  148. ---
  149. kind: Deployment
  150. apiVersion: apps/v1
  151. metadata:
  152. labels:
  153. k8s-app: kubernetes-dashboard
  154. name: kubernetes-dashboard
  155. namespace: kubernetes-dashboard
  156. spec:
  157. replicas: 1
  158. revisionHistoryLimit: 10
  159. selector:
  160. matchLabels:
  161. k8s-app: kubernetes-dashboard
  162. template:
  163. metadata:
  164. labels:
  165. k8s-app: kubernetes-dashboard
  166. spec:
  167. securityContext:
  168. seccompProfile:
  169. type: RuntimeDefault
  170. containers:
  171. - name: kubernetes-dashboard
  172. image: kubernetesui/dashboard:v2.5.1
  173. imagePullPolicy: Always
  174. ports:
  175. - containerPort: 8443
  176. protocol: TCP
  177. args:
  178. - --auto-generate-certificates
  179. - --namespace=kubernetes-dashboard
  180. # Uncomment the following line to manually specify Kubernetes API server Host
  181. # If not specified, Dashboard will attempt to auto discover the API server and connect
  182. # to it. Uncomment only if the default does not work.
  183. # - --apiserver-host=http://my-address:port
  184. volumeMounts:
  185. - name: kubernetes-dashboard-certs
  186. mountPath: /certs
  187. # Create on-disk volume to store exec logs
  188. - mountPath: /tmp
  189. name: tmp-volume
  190. livenessProbe:
  191. httpGet:
  192. scheme: HTTPS
  193. path: /
  194. port: 8443
  195. initialDelaySeconds: 30
  196. timeoutSeconds: 30
  197. securityContext:
  198. allowPrivilegeEscalation: false
  199. readOnlyRootFilesystem: true
  200. runAsUser: 1001
  201. runAsGroup: 2001
  202. volumes:
  203. - name: kubernetes-dashboard-certs
  204. secret:
  205. secretName: kubernetes-dashboard-certs
  206. - name: tmp-volume
  207. emptyDir: {}
  208. serviceAccountName: kubernetes-dashboard
  209. nodeSelector:
  210. "kubernetes.io/os": linux
  211. # Comment the following tolerations if Dashboard must not be deployed on master
  212. tolerations:
  213. - key: node-role.kubernetes.io/master
  214. effect: NoSchedule
  215. ---
  216. kind: Service
  217. apiVersion: v1
  218. metadata:
  219. labels:
  220. k8s-app: dashboard-metrics-scraper
  221. name: dashboard-metrics-scraper
  222. namespace: kubernetes-dashboard
  223. spec:
  224. ports:
  225. - port: 8000
  226. targetPort: 8000
  227. selector:
  228. k8s-app: dashboard-metrics-scraper
  229. ---
  230. kind: Deployment
  231. apiVersion: apps/v1
  232. metadata:
  233. labels:
  234. k8s-app: dashboard-metrics-scraper
  235. name: dashboard-metrics-scraper
  236. namespace: kubernetes-dashboard
  237. spec:
  238. replicas: 1
  239. revisionHistoryLimit: 10
  240. selector:
  241. matchLabels:
  242. k8s-app: dashboard-metrics-scraper
  243. template:
  244. metadata:
  245. labels:
  246. k8s-app: dashboard-metrics-scraper
  247. spec:
  248. securityContext:
  249. seccompProfile:
  250. type: RuntimeDefault
  251. containers:
  252. - name: dashboard-metrics-scraper
  253. image: kubernetesui/metrics-scraper:v1.0.7
  254. ports:
  255. - containerPort: 8000
  256. protocol: TCP
  257. livenessProbe:
  258. httpGet:
  259. scheme: HTTP
  260. path: /
  261. port: 8000
  262. initialDelaySeconds: 30
  263. timeoutSeconds: 30
  264. volumeMounts:
  265. - mountPath: /tmp
  266. name: tmp-volume
  267. securityContext:
  268. allowPrivilegeEscalation: false
  269. readOnlyRootFilesystem: true
  270. runAsUser: 1001
  271. runAsGroup: 2001
  272. serviceAccountName: kubernetes-dashboard
  273. nodeSelector:
  274. "kubernetes.io/os": linux
  275. # Comment the following tolerations if Dashboard must not be deployed on master
  276. tolerations:
  277. - key: node-role.kubernetes.io/master
  278. effect: NoSchedule
  279. volumes:
  280. - name: tmp-volume
  281. emptyDir: {}

dashboard-adminuser.yml:

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

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

闽ICP备14008679号