1. #!/bin/bash
    2. # 初始化k8s
    3. kubeadm init --kubernetes-version=1.23.1 \
    4. --apiserver-advertise-address=192.168.56.101 \
    5. --image-repository registry.aliyuncs.com/google_containers \
    6. --service-cidr=10.1.0.0/16 \
    7. --pod-network-cidr=10.244.0.0/16
    8. mkdir -p $HOME/.kube
    9. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    10. sudo chown $(id -u):$(id -g) $HOME/.kube/config
    11. echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
    12. # 安装网络插件 Flannel
    13. cat >>/root/flannel.yml <<EOF
    14. ---
    15. apiVersion: policy/v1beta1
    16. kind: PodSecurityPolicy
    17. metadata:
    18. name: psp.flannel.unprivileged
    19. annotations:
    20. seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    21. seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    22. apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    23. apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
    24. spec:
    25. privileged: false
    26. volumes:
    27. - configMap
    28. - secret
    29. - emptyDir
    30. - hostPath
    31. allowedHostPaths:
    32. - pathPrefix: "/etc/cni/net.d"
    33. - pathPrefix: "/etc/kube-flannel"
    34. - pathPrefix: "/run/flannel"
    35. readOnlyRootFilesystem: false
    36. # Users and groups
    37. runAsUser:
    38. rule: RunAsAny
    39. supplementalGroups:
    40. rule: RunAsAny
    41. fsGroup:
    42. rule: RunAsAny
    43. # Privilege Escalation
    44. allowPrivilegeEscalation: false
    45. defaultAllowPrivilegeEscalation: false
    46. # Capabilities
    47. allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
    48. defaultAddCapabilities: []
    49. requiredDropCapabilities: []
    50. # Host namespaces
    51. hostPID: false
    52. hostIPC: false
    53. hostNetwork: true
    54. hostPorts:
    55. - min: 0
    56. max: 65535
    57. # SELinux
    58. seLinux:
    59. # SELinux is unused in CaaSP
    60. rule: 'RunAsAny'
    61. ---
    62. kind: ClusterRole
    63. apiVersion: rbac.authorization.k8s.io/v1
    64. metadata:
    65. name: flannel
    66. rules:
    67. - apiGroups: ['extensions']
    68. resources: ['podsecuritypolicies']
    69. verbs: ['use']
    70. resourceNames: ['psp.flannel.unprivileged']
    71. - apiGroups:
    72. - ""
    73. resources:
    74. - pods
    75. verbs:
    76. - get
    77. - apiGroups:
    78. - ""
    79. resources:
    80. - nodes
    81. verbs:
    82. - list
    83. - watch
    84. - apiGroups:
    85. - ""
    86. resources:
    87. - nodes/status
    88. verbs:
    89. - patch
    90. ---
    91. kind: ClusterRoleBinding
    92. apiVersion: rbac.authorization.k8s.io/v1
    93. metadata:
    94. name: flannel
    95. roleRef:
    96. apiGroup: rbac.authorization.k8s.io
    97. kind: ClusterRole
    98. name: flannel
    99. subjects:
    100. - kind: ServiceAccount
    101. name: flannel
    102. namespace: kube-system
    103. ---
    104. apiVersion: v1
    105. kind: ServiceAccount
    106. metadata:
    107. name: flannel
    108. namespace: kube-system
    109. ---
    110. kind: ConfigMap
    111. apiVersion: v1
    112. metadata:
    113. name: kube-flannel-cfg
    114. namespace: kube-system
    115. labels:
    116. tier: node
    117. app: flannel
    118. data:
    119. cni-conf.json: |
    120. {
    121. "name": "cbr0",
    122. "cniVersion": "0.3.1",
    123. "plugins": [
    124. {
    125. "type": "flannel",
    126. "delegate": {
    127. "hairpinMode": true,
    128. "isDefaultGateway": true
    129. }
    130. },
    131. {
    132. "type": "portmap",
    133. "capabilities": {
    134. "portMappings": true
    135. }
    136. }
    137. ]
    138. }
    139. net-conf.json: |
    140. {
    141. "Network": "10.244.0.0/16",
    142. "Backend": {
    143. "Type": "vxlan"
    144. }
    145. }
    146. ---
    147. apiVersion: apps/v1
    148. kind: DaemonSet
    149. metadata:
    150. name: kube-flannel-ds
    151. namespace: kube-system
    152. labels:
    153. tier: node
    154. app: flannel
    155. spec:
    156. selector:
    157. matchLabels:
    158. app: flannel
    159. template:
    160. metadata:
    161. labels:
    162. tier: node
    163. app: flannel
    164. spec:
    165. affinity:
    166. nodeAffinity:
    167. requiredDuringSchedulingIgnoredDuringExecution:
    168. nodeSelectorTerms:
    169. - matchExpressions:
    170. - key: kubernetes.io/os
    171. operator: In
    172. values:
    173. - linux
    174. hostNetwork: true
    175. priorityClassName: system-node-critical
    176. tolerations:
    177. - operator: Exists
    178. effect: NoSchedule
    179. serviceAccountName: flannel
    180. initContainers:
    181. - name: install-cni
    182. image: registry.cn-beijing.aliyuncs.com/qingfeng666/flannel:v0.13.0
    183. command:
    184. - cp
    185. args:
    186. - -f
    187. - /etc/kube-flannel/cni-conf.json
    188. - /etc/cni/net.d/10-flannel.conflist
    189. volumeMounts:
    190. - name: cni
    191. mountPath: /etc/cni/net.d
    192. - name: flannel-cfg
    193. mountPath: /etc/kube-flannel/
    194. containers:
    195. - name: kube-flannel
    196. image: registry.cn-beijing.aliyuncs.com/qingfeng666/flannel:v0.13.0
    197. command:
    198. - /opt/bin/flanneld
    199. args:
    200. - --ip-masq
    201. - --kube-subnet-mgr
    202. resources:
    203. requests:
    204. cpu: "100m"
    205. memory: "50Mi"
    206. limits:
    207. cpu: "100m"
    208. memory: "50Mi"
    209. securityContext:
    210. privileged: false
    211. capabilities:
    212. add: ["NET_ADMIN", "NET_RAW"]
    213. env:
    214. - name: POD_NAME
    215. valueFrom:
    216. fieldRef:
    217. fieldPath: metadata.name
    218. - name: POD_NAMESPACE
    219. valueFrom:
    220. fieldRef:
    221. fieldPath: metadata.namespace
    222. volumeMounts:
    223. - name: run
    224. mountPath: /run/flannel
    225. - name: flannel-cfg
    226. mountPath: /etc/kube-flannel/
    227. volumes:
    228. - name: run
    229. hostPath:
    230. path: /run/flannel
    231. - name: cni
    232. hostPath:
    233. path: /etc/cni/net.d
    234. - name: flannel-cfg
    235. configMap:
    236. name: kube-flannel-cfg
    237. EOF
    238. # 检测状态
    239. kubectl apply -f /root/flannel.yml
    240. kubectl get po -n kube-system
    241. kubectl get node -w