k8s部署consul集群

悠悠 2022-12-05 01:37 473阅读 0赞

k8s以StatefulSet方式部署consul集群:

public-service-ns.yaml

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: public-service

consul-server.yaml

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: consul
  5. namespace: public-service
  6. spec:
  7. rules:
  8. - host: consul.lzxlinux.com
  9. http:
  10. paths:
  11. - path: /
  12. backend:
  13. serviceName: consul-ui
  14. servicePort: 80
  15. ---
  16. apiVersion: v1
  17. kind: Service
  18. metadata:
  19. name: consul-ui
  20. namespace: public-service
  21. labels:
  22. app: consul
  23. component: server
  24. spec:
  25. selector:
  26. app: consul
  27. ports:
  28. - name: http
  29. port: 80
  30. targetPort: 8500
  31. ---
  32. apiVersion: v1
  33. kind: Service
  34. metadata:
  35. name: consul-dns
  36. namespace: public-service
  37. labels:
  38. app: consul
  39. component: dns
  40. spec:
  41. selector:
  42. app: consul
  43. ports:
  44. - name: dns-tcp
  45. protocol: TCP
  46. port: 53
  47. targetPort: dns-tcp
  48. - name: dns-udp
  49. protocol: UDP
  50. port: 53
  51. targetPort: dns-udp
  52. ---
  53. apiVersion: v1
  54. kind: Service
  55. metadata:
  56. name: consul-server
  57. namespace: public-service
  58. labels:
  59. app: consul
  60. component: server
  61. spec:
  62. selector:
  63. app: consul
  64. component: server
  65. ports:
  66. - name: http
  67. port: 8500
  68. targetPort: 8500
  69. - name: dns-tcp
  70. protocol: TCP
  71. port: 8600
  72. targetPort: dns-tcp
  73. - name: dns-udp
  74. protocol: "UDP"
  75. port: 8600
  76. targetPort: dns-udp
  77. - name: serflan-tcp
  78. protocol: TCP
  79. port: 8301
  80. targetPort: 8301
  81. - name: serflan-udp
  82. protocol: UDP
  83. port: 8301
  84. targetPort: 8302
  85. - name: serfwan-tcp
  86. protocol: TCP
  87. port: 8302
  88. targetPort: 8302
  89. - name: serfwan-udp
  90. protocol: UDP
  91. port: 8302
  92. targetPort: 8302
  93. - name: server
  94. port: 8300
  95. targetPort: 8300
  96. publishNotReadyAddresses: true
  97. clusterIP: None
  98. ---
  99. apiVersion: v1
  100. kind: ConfigMap
  101. metadata:
  102. name: consul-server-config
  103. namespace: public-service
  104. data:
  105. ---
  106. apiVersion: policy/v1beta1
  107. kind: PodDisruptionBudget
  108. metadata:
  109. name: consul-server
  110. namespace: public-service
  111. spec:
  112. selector:
  113. matchLabels:
  114. app: consul
  115. component: server
  116. minAvailable: 2
  117. ---
  118. apiVersion: apps/v1
  119. kind: StatefulSet
  120. metadata:
  121. name: consul-server
  122. namespace: public-service
  123. spec:
  124. serviceName: consul-server
  125. replicas: 3
  126. updateStrategy:
  127. type: RollingUpdate
  128. selector:
  129. matchLabels:
  130. app: consul
  131. component: server
  132. template:
  133. metadata:
  134. labels:
  135. app: consul
  136. component: server
  137. spec:
  138. affinity:
  139. podAntiAffinity:
  140. requiredDuringSchedulingIgnoredDuringExecution:
  141. - labelSelector:
  142. matchExpressions:
  143. - key: "componment"
  144. operator: In
  145. values:
  146. - server
  147. topologyKey: "kubernetes.io/hostname"
  148. terminationGracePeriodSeconds: 10
  149. containers:
  150. - name: consul
  151. image: consul:latest
  152. imagePullPolicy: IfNotPresent
  153. ports:
  154. - containerPort: 8500
  155. name: http
  156. - containerPort: 8600
  157. name: dns-tcp
  158. protocol: TCP
  159. - containerPort: 8600
  160. name: dns-udp
  161. protocol: UDP
  162. - containerPort: 8301
  163. name: serflan
  164. - containerPort: 8302
  165. name: serfwan
  166. - containerPort: 8300
  167. name: server
  168. env:
  169. - name: POD_IP
  170. valueFrom:
  171. fieldRef:
  172. fieldPath: status.podIP
  173. - name: NAMESPACE
  174. valueFrom:
  175. fieldRef:
  176. fieldPath: metadata.namespace
  177. args:
  178. - "agent"
  179. - "-server"
  180. - "-advertise=$(POD_IP)"
  181. - "-bind=0.0.0.0"
  182. - "-bootstrap-expect=3"
  183. - "-datacenter=dc1"
  184. - "-config-dir=/consul/userconfig"
  185. - "-data-dir=/consul/data"
  186. - "-disable-host-node-id"
  187. - "-domain=cluster.local"
  188. - "-retry-join=consul-server-0.consul-server.$(NAMESPACE).svc.cluster.local"
  189. - "-client=0.0.0.0"
  190. - "-ui"
  191. resources:
  192. limits:
  193. cpu: "100m"
  194. memory: "128Mi"
  195. requests:
  196. cpu: "100m"
  197. memory: "128Mi"
  198. lifecycle:
  199. preStop:
  200. exec:
  201. command:
  202. - /bin/sh
  203. - -c
  204. - consul leave
  205. volumeMounts:
  206. - name: data
  207. mountPath: /consul/data
  208. - name: user-config
  209. mountPath: /consul/userconfig
  210. volumes:
  211. - name: user-config
  212. configMap:
  213. name: consul-server-config
  214. - name: data
  215. emptyDir: { }
  216. securityContext:
  217. fsGroup: 1000
  218. # volumeClaimTemplates:
  219. # - metadata:
  220. # name: data
  221. # spec:
  222. # accessModes:
  223. # - ReadWriteMany
  224. # storageClassName: "gluster-heketi-2"
  225. # resources:
  226. # requests:
  227. # storage: 10Gi

consul-client.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: consul-client-config
  5. namespace: public-service
  6. data:
  7. ---
  8. apiVersion: apps/v1
  9. kind: DaemonSet
  10. metadata:
  11. name: consul
  12. namespace: public-service
  13. spec:
  14. selector:
  15. matchLabels:
  16. app: consul
  17. component: client
  18. template:
  19. metadata:
  20. labels:
  21. app: consul
  22. component: client
  23. spec:
  24. affinity:
  25. podAntiAffinity:
  26. requiredDuringSchedulingIgnoredDuringExecution:
  27. - labelSelector:
  28. matchExpressions:
  29. - key: "componment"
  30. operator: In
  31. values:
  32. - client
  33. topologyKey: "kubernetes.io/hostname"
  34. terminationGracePeriodSeconds: 10
  35. containers:
  36. - name: consul
  37. image: consul:latest
  38. imagePullPolicy: IfNotPresent
  39. ports:
  40. - containerPort: 8500
  41. name: http
  42. - containerPort: 8600
  43. name: dns-tcp
  44. protocol: TCP
  45. - containerPort: 8600
  46. name: dns-udp
  47. protocol: UDP
  48. - containerPort: 8301
  49. name: serflan
  50. - containerPort: 8302
  51. name: serfwan
  52. - containerPort: 8300
  53. name: server
  54. env:
  55. - name: POD_IP
  56. valueFrom:
  57. fieldRef:
  58. fieldPath: status.podIP
  59. - name: NAMESPACE
  60. valueFrom:
  61. fieldRef:
  62. fieldPath: metadata.namespace
  63. args:
  64. - "agent"
  65. - "-advertise=$(POD_IP)"
  66. - "-bind=0.0.0.0"
  67. - "-datacenter=dc1"
  68. - "-config-dir=/consul/userconfig"
  69. - "-data-dir=/consul/data"
  70. - "-disable-host-node-id=true"
  71. - "-domain=cluster.local"
  72. - "-retry-join=consul-server-0.consul-server.$(NAMESPACE).svc.cluster.local"
  73. - "-client=0.0.0.0"
  74. resources:
  75. limits:
  76. cpu: "50m"
  77. memory: "32Mi"
  78. requests:
  79. cpu: "50m"
  80. memory: "32Mi"
  81. lifecycle:
  82. preStop:
  83. exec:
  84. command:
  85. - /bin/sh
  86. - -c
  87. - consul leave
  88. volumeMounts:
  89. - name: data
  90. mountPath: /consul/data
  91. - name: user-config
  92. mountPath: /consul/userconfig
  93. volumes:
  94. - name: user-config
  95. configMap:
  96. name: consul-client-config
  97. - name: data
  98. emptyDir: { }
  99. securityContext:
  100. fsGroup: 1000
  101. # volumeClaimTemplates:
  102. # - metadata:
  103. # name: data
  104. # spec:
  105. # accessModes:
  106. # - ReadWriteMany
  107. # storageClassName: "gluster-heketi-2"
  108. # resources:
  109. # requests:
  110. # storage: 10Gi

  • PodDisruptionBudget:

k8s可以为每个应用程序创建 PodDisruptionBudget 对象(PDB)。PDB 将限制在同一时间因资源干扰导致的复制应用程序中宕机的 pod 数量。

可以通过两个参数来配置PodDisruptionBudget:

  1. MinAvailable:表示最小可用Pod数,表示应用Pod集群处于运行状态的最小Pod数量,或者是运行状态的Pod数同总Pod数的最小百分比
  2. MaxUnavailable:表示最大不可用Pod数,表示应用Pod集群处于不可用状态的最大Pod数,或者是不可用状态的Pod数同总Pod数的最大百分比

需要注意的是,MinAvailable参数和MaxUnavailable参数只能同时配置一个。

  • 部署:

    kubectl apply -f public-service-ns.yaml

    kubectl apply -f consul-server.yaml

    kubectl get svc -n public-service

    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    consul-dns ClusterIP 10.110.235.63 53/TCP,53/UDP 85s
    consul-server ClusterIP None 8500/TCP,8600/TCP,8600/UDP,8301/TCP,8301/UDP,8302/TCP,8302/UDP,8300/TCP 85s
    consul-ui ClusterIP 10.98.220.223 80/TCP 85s

  1. kubectl get pod -n public-service
  2. NAME READY STATUS RESTARTS AGE
  3. consul-server-0 1/1 Running 0 110s
  4. consul-server-1 1/1 Running 0 107s
  5. consul-server-2 1/1 Running 0 92s
  • 查看集群状态:

    kubectl exec -n public-service consul-server-0 — consul members

    Node Address Status Type Build Protocol DC Segment
    consul-server-0 172.10.135.17:8301 alive server 1.8.3 2 dc1
    consul-server-1 172.10.104.11:8301 alive server 1.8.3 2 dc1
    consul-server-2 172.10.166.136:8301 alive server 1.8.3 2 dc1

  • 访问ui:

添加hosts:consul.lzxlinux.com,访问consul.lzxlinux.com/ui

在这里插入图片描述

可以看到:consul-server-0是leader,集群状态正常。

  • 加入client:

    kubectl apply -f consul-client.yaml

    kubectl get pod -n public-service

    NAME READY STATUS RESTARTS AGE
    consul-8wx22 1/1 Running 0 40s
    consul-glmgs 1/1 Running 0 10s
    consul-server-0 1/1 Running 0 30m
    consul-server-1 1/1 Running 0 30m
    consul-server-2 1/1 Running 0 30m
    consul-vxbj7 1/1 Running 0 61s

    kubectl exec -n public-service consul-server-0 — consul members

    Node Address Status Type Build Protocol DC Segment
    consul-server-0 172.10.135.17:8301 alive server 1.8.3 2 dc1
    consul-server-1 172.10.104.11:8301 alive server 1.8.3 2 dc1
    consul-server-2 172.10.166.136:8301 alive server 1.8.3 2 dc1
    consul-8wx22 172.10.166.138:8301 alive client 1.8.3 2 dc1
    consul-glmgs 172.10.135.19:8301 alive client 1.8.3 2 dc1
    consul-vxbj7 172.10.104.13:8301 alive client 1.8.3 2 dc1

在这里插入图片描述

至此,consul集群(3 server、3client)部署完成。已存放至个人github:kubernetes


发表评论

表情:
评论列表 (有 0 条评论,473人围观)

还没有评论,来说两句吧...

相关阅读

    相关 K8S部署

    一、利用ansible部署kubernetes准备: 集群介绍 本系列文档致力于提供快速部署高可用k8s集群的工具,并且也努力成为k8s实践、使用的参考书;基于二进