当前位置:   article > 正文

K8S StatefulSet方式部署elasticsearch集群 —— 筑梦之路_编写 es-statefulset.yaml,通过 yaml 文件构建 elasticsearch

编写 es-statefulset.yaml,通过 yaml 文件构建 elasticsearch 的 statefulset 集

直接上yaml内容

  1. # es-cluster.yaml
  2. apiVersion: apps/v1
  3. # 设置控制器
  4. kind: StatefulSet
  5. metadata:
  6. name: es-cluster
  7. namespace: merry
  8. spec:
  9. # 必须设置
  10. serviceName: es-cluster-svc
  11. # 设置副本数
  12. replicas: 3
  13. # 设置选择器
  14. selector:
  15. # 设置标签
  16. matchLabels:
  17. app: es-net-data
  18. template:
  19. metadata:
  20. # 此处必须要与上面的matchLabels相同
  21. labels:
  22. app: es-net-data
  23. spec:
  24. # 初始化容器
  25. # 初始化容器的作用是在应用容器启动之前做准备工作,每个init容器都必须在下一个启动之前成功完成
  26. initContainers:
  27. - name: increase-vm-max-map
  28. image: busybox:1.32
  29. command: ["sysctl", "-w", "vm.max_map_count=262144"]
  30. securityContext:
  31. privileged: true
  32. - name: increase-fd-ulimit
  33. image: busybox:1.32
  34. command: ["sh", "-c", "ulimit -n 65536"]
  35. securityContext:
  36. privileged: true
  37. # 初始化容器结束后,才能继续创建下面的容器
  38. containers:
  39. - name: es-container
  40. image: elasticsearch:7.6.2
  41. ports:
  42. # 容器内端口
  43. - name: rest
  44. containerPort: 9200
  45. protocol: TCP
  46. # 限制CPU数量
  47. resources:
  48. limits:
  49. cpu: 1000m
  50. requests:
  51. cpu: 100m
  52. # 设置挂载目录
  53. volumeMounts:
  54. - name: es-data
  55. mountPath: /usr/share/elasticsearch/data
  56. # 设置环境变量
  57. env:
  58. # 自定义集群名
  59. - name: cluster.name
  60. value: k8s-es
  61. # 定义节点名,使用metadata.name名称
  62. - name: node.name
  63. valueFrom:
  64. fieldRef:
  65. fieldPath: metadata.name
  66. # 初始化集群时,ES从中选出master节点
  67. - name: cluster.initial_master_nodes
  68. # 对应metadata.name名称加编号,编号从0开始
  69. value: "es-cluster-0,es-cluster-1,es-cluster-2"
  70. - name: discovery.zen.minimum_master_nodes
  71. value: "2"
  72. # 发现节点的地址,discovery.seed_hosts的值应包括所有master候选节点
  73. # 如果discovery.seed_hosts的值是一个域名,且该域名解析到多个IP地址,那么es将处理其所有解析的IP地址。
  74. - name: discovery.seed_hosts
  75. value: "es-cluster-svc"
  76. # 配置内存
  77. - name: ES_JAVA_OPTS
  78. value: "-Xms1g -Xmx1g"
  79. - name: network.host
  80. value: "0.0.0.0"
  81. volumeClaimTemplates:
  82. - metadata:
  83. # 对应容器中volumeMounts.name
  84. name: es-data
  85. labels:
  86. app: es-volume
  87. spec:
  88. # 存储卷可以被单个节点读写
  89. accessModes: [ "ReadWriteOnce" ]
  90. # 对应es-nfs-storage-class.yaml中的metadata.name
  91. storageClassName: es-nfs-storage
  92. # 申请资源的大小
  93. resources:
  94. requests:
  95. storage: 10Gi
  96. ---
  97. # es-service.yaml
  98. apiVersion: v1
  99. kind: Service
  100. metadata:
  101. name: es-cluster-svc
  102. namespace: merry
  103. spec:
  104. selector:
  105. # 注意一定要与"es-cluster.yaml"中spec.selector.matchLabels相同
  106. app: es-net-data
  107. # 设置服务类型
  108. type: NodePort
  109. ports:
  110. - name: rest
  111. # 服务端口
  112. port: 9200
  113. # 应用端口(Pod端口)
  114. targetPort: 9200
  115. # 映射到主机的端口,端口范围是30000~32767
  116. nodePort: 32000
  117. - name: socket9300
  118. port: 9300
  119. targetPort: 9300
  120. nodePort: 39300
  121. ---

能看懂的人都能明白!

------------------------------------------------改进版本如下----------------------------------------------------------

主要改进地方:

1. 添加时区的环境变量,修改cluster.name值为docker-cluster

2.初始化容器指定数据目录权限

3.添加无头服务headless

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. namespace: merry
  5. name: es-cluster
  6. spec:
  7. serviceName: elasticsearch-svc-headless
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: elasticsearch
  12. template:
  13. metadata:
  14. labels:
  15. app: elasticsearch
  16. spec:
  17. initContainers:
  18. - name: permissions
  19. image: busybox:latest
  20. command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
  21. securityContext:
  22. privileged: true
  23. volumeMounts:
  24. - name: data
  25. mountPath: /usr/share/elasticsearch/data
  26. - name: increase-vm-max-map
  27. image: busybox:latest
  28. command: ["sysctl", "-w", "vm.max_map_count=262144"]
  29. securityContext:
  30. privileged: true
  31. - name: increase-fd-ulimit
  32. image: busybox:latest
  33. command: ["sh", "-c", "ulimit -n 65536"]
  34. securityContext:
  35. privileged: true
  36. containers:
  37. - name: elasticsearch
  38. image: elasticsearch:7.10.2-analysis-hanlp
  39. resources:
  40. limits:
  41. cpu: 1000m
  42. requests:
  43. cpu: 100m
  44. ports:
  45. - containerPort: 9200
  46. name: rest
  47. protocol: TCP
  48. - containerPort: 9300
  49. name: socket
  50. protocol: TCP
  51. volumeMounts:
  52. - name: data
  53. mountPath: /usr/share/elasticsearch/data
  54. env:
  55. - name: TZ
  56. value: Asia/Shanghai
  57. - name: cluster.name
  58. value: docker-cluster
  59. - name: node.name
  60. valueFrom:
  61. fieldRef:
  62. fieldPath: metadata.name
  63. - name: discovery.seed_hosts
  64. value: "es-cluster-0.elasticsearch-svc-headless,es-cluster-1.elasticsearch-svc-headless,es-cluster-2.elasticsearch-svc-headless"
  65. - name: cluster.initial_master_nodes
  66. value: "es-cluster-0,es-cluster-1,es-cluster-2"
  67. - name: ES_JAVA_OPTS
  68. value: "-Xms512m -Xmx512m"
  69. - name: discovery.zen.minimum_master_nodes
  70. value: "2"
  71. - name: network.host
  72. value: "0.0.0.0"
  73. volumeClaimTemplates:
  74. - metadata:
  75. name: data
  76. labels:
  77. app: elasticsearch
  78. spec:
  79. accessModes: [ "ReadWriteOnce" ]
  80. storageClassName: managed-nfs-storage
  81. resources:
  82. requests:
  83. storage: 1000Gi
  84. ---
  85. kind: Service
  86. apiVersion: v1
  87. metadata:
  88. namespace: merry
  89. name: elasticsearch-svc-headless
  90. labels:
  91. app: elasticsearch
  92. spec:
  93. selector:
  94. app: elasticsearch
  95. clusterIP: None
  96. ports:
  97. - port: 9200
  98. name: rest
  99. - port: 9300
  100. name: socket
  101. ---
  102. kind: Service
  103. apiVersion: v1
  104. metadata:
  105. namespace: merry
  106. name: elasticsearch-svc
  107. labels:
  108. app: elasticsearch
  109. spec:
  110. selector:
  111. app: elasticsearch
  112. type: NodePort
  113. ports:
  114. - port: 9200
  115. name: rest
  116. - port: 9300
  117. name: socket

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

闽ICP备14008679号