下面的例子展示了如何在cluster.yml中配置自定义网络插件与内嵌式插件。

    首先,打开cluster.yml文件,将network参数的现有值修改为 none

    1. network:
    2. plugin: none

    然后,在 cluster.yml中的 addons部分,添加拥有网络插件的集群的 add-on manifest。在下面的例子中,我们通过addons字段添加集群的 add-on manifest,将 Canal 插件替换成 Flannel 插件。

    1. addons: |-
    2. ---
    3. kind: ClusterRoleBinding
    4. apiVersion: rbac.authorization.k8s.io/v1
    5. metadata:
    6. name: flannel
    7. roleRef:
    8. apiGroup: rbac.authorization.k8s.io
    9. kind: ClusterRole
    10. name: flannel
    11. subjects:
    12. - kind: ServiceAccount
    13. name: flannel
    14. namespace: kube-system
    15. ---
    16. kind: ClusterRole
    17. apiVersion: rbac.authorization.k8s.io/v1
    18. metadata:
    19. name: flannel
    20. rules:
    21. - apiGroups:
    22. - ""
    23. resources:
    24. - pods
    25. verbs:
    26. - get
    27. - apiGroups:
    28. - ""
    29. resources:
    30. - nodes
    31. verbs:
    32. - list
    33. - watch
    34. - apiGroups:
    35. - ""
    36. resources:
    37. - nodes/status
    38. verbs:
    39. - patch
    40. ---
    41. kind: ConfigMap
    42. apiVersion: v1
    43. metadata:
    44. name: kube-flannel-cfg
    45. namespace: "kube-system"
    46. labels:
    47. tier: node
    48. app: flannel
    49. data:
    50. cni-conf.json: |
    51. {
    52. "name":"cbr0",
    53. "cniVersion":"0.3.1",
    54. "plugins":[
    55. {
    56. "type":"flannel",
    57. "delegate":{
    58. "forceAddress":true,
    59. "isDefaultGateway":true
    60. }
    61. },
    62. {
    63. "type":"portmap",
    64. "capabilities":{
    65. "portMappings":true
    66. }
    67. }
    68. ]
    69. }
    70. net-conf.json: |
    71. {
    72. "Network": "10.42.0.0/16",
    73. "Backend": {
    74. "Type": "vxlan"
    75. }
    76. }
    77. ---
    78. apiVersion: extensions/v1beta1
    79. kind: DaemonSet
    80. metadata:
    81. name: kube-flannel
    82. namespace: "kube-system"
    83. labels:
    84. tier: node
    85. k8s-app: flannel
    86. spec:
    87. template:
    88. metadata:
    89. labels:
    90. tier: node
    91. k8s-app: flannel
    92. spec:
    93. affinity:
    94. nodeAffinity:
    95. requiredDuringSchedulingIgnoredDuringExecution:
    96. nodeSelectorTerms:
    97. - matchExpressions:
    98. - key: beta.kubernetes.io/os
    99. operator: NotIn
    100. values:
    101. - windows
    102. serviceAccountName: flannel
    103. containers:
    104. - name: kube-flannel
    105. image: rancher/coreos-flannel:v0.10.0-rancher1
    106. imagePullPolicy: IfNotPresent
    107. resources:
    108. limits:
    109. cpu: 300m
    110. memory: 500M
    111. requests:
    112. cpu: 150m
    113. memory: 64M
    114. command: ["/opt/bin/flanneld","--ip-masq","--kube-subnet-mgr"]
    115. securityContext:
    116. privileged: true
    117. env:
    118. - name: POD_NAME
    119. valueFrom:
    120. fieldRef:
    121. fieldPath: metadata.name
    122. - name: POD_NAMESPACE
    123. valueFrom:
    124. fieldRef:
    125. fieldPath: metadata.namespace
    126. volumeMounts:
    127. - name: run
    128. mountPath: /run
    129. - name: cni
    130. mountPath: /etc/cni/net.d
    131. - name: flannel-cfg
    132. mountPath: /etc/kube-flannel/
    133. - name: install-cni
    134. image: rancher/flannel-cni:v0.3.0-rancher1
    135. command: ["/install-cni.sh"]
    136. env:
    137. # The CNI network config to install on each node.
    138. - name: CNI_NETWORK_CONFIG
    139. valueFrom:
    140. configMapKeyRef:
    141. name: kube-flannel-cfg
    142. key: cni-conf.json
    143. - name: CNI_CONF_NAME
    144. value: "10-flannel.conflist"
    145. volumeMounts:
    146. - name: cni
    147. mountPath: /host/etc/cni/net.d
    148. - name: host-cni-bin
    149. mountPath: /host/opt/cni/bin/
    150. hostNetwork: true
    151. tolerations:
    152. - operator: Exists
    153. effect: NoSchedule
    154. - operator: Exists
    155. effect: NoExecute
    156. - key: node.kubernetes.io/not-ready
    157. effect: NoSchedule
    158. operator: Exists
    159. volumes:
    160. - name: run
    161. hostPath:
    162. path: /run
    163. - name: cni
    164. hostPath:
    165. path: /etc/cni/net.d
    166. - name: flannel-cfg
    167. configMap:
    168. name: kube-flannel-cfg
    169. - name: host-cni-bin
    170. hostPath:
    171. path: /opt/cni/bin
    172. updateStrategy:
    173. rollingUpdate:
    174. maxUnavailable: 20%
    175. type: RollingUpdate
    176. ---
    177. apiVersion: v1
    178. kind: ServiceAccount
    179. metadata:
    180. name: flannel
    181. namespace: kube-system

    结果: 为集群创建了自定义的网络插件。