前提创建了命名空间和nfs

# 1. elasticsearch7-svc.yml

kind: Service
apiVersion: v1
metadata:
  name: elasticsearch7
  namespace: logging
  labels:
    app: elasticsearch7
spec:
  selector:
    app: elasticsearch7
  clusterIP: None
  ports:
    - port: 9200
      name: rest
    - port: 9300
      name: inter-node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 2. elasticsearch7-statefulset.yml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es7-cluster
  namespace: logging
spec:
  serviceName: elasticsearch7
  replicas: 3
  selector:
    matchLabels:
      app: elasticsearch7
  template:
    metadata:
      labels:
        app: elasticsearch7
    spec:
      containers:
      - name: elasticsearch7
        image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
        resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: es7-pv-claim
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: cluster.name
            value: k8s-logs
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.zen.minimum_master_nodes # 含义请参阅官方 Elasticsearch 文档
            value: "2"
          - name: discovery.seed_hosts # 含义请参阅官方 Elasticsearch 文档
            value: "es7-cluster-0.elasticsearch7,es7-cluster-1.elasticsearch7,es7-cluster-2.elasticsearch7"
          - name: cluster.initial_master_nodes # 初始化的 master 节点,旧版本相关配置 discovery.zen.minimum_master_nodes
            value: "es7-cluster-0,es7-cluster-1,es7-cluster-2" # 含义请参阅官方 Elasticsearch 文档
          - name: ES_JAVA_OPTS
            value: "-Xms1g -Xmx2g" # 根据具体资源及需求调整
      initContainers:
      - name: fix-permissions
        image: busybox
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: es7-pv-claim
          mountPath: /usr/share/elasticsearch/data
      - name: increase-vm-max-map
        image: busybox
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: busybox
        command: ["sh", "-c", "ulimit -n 65536"]
  volumeClaimTemplates:
  - metadata:
      name: es7-pv-claim
    spec:
      accessModes: [ "ReadWriteMany","ReadWriteOnce"]
      resources:
        requests:
          storage: 5G
      storageClassName: nfs-storage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

# 参考资料