Jusene's Blog

kubernetes 部署 elasticsearch 集群(StatefulSet)

字数统计: 540阅读时长: 3 min
2020/11/12 Share

部署elasticseach集群

elasticsearch作为有状态服务,需要部署集群服务,根据kubernetes statefulset部署步骤开始:

  • headless service
  • statefulset
  • volumeClaimTemplates

构建elasticsearch docker镜像

  • Dockerfile
1
FROM elasticsearch:6.8.13
2
3
WORKDIR /usr/share/elasticsearch
4
5
COPY elasticsearch.yml config/
6
7
RUN chown 1000 config/elasticsearch.yml
  • elasticsearch.yml
1
cluster.name: "elasticsearch-${NAMESPACE}"
2
node.name: "${POD_NAME}"
3
network.host: "${POD_IP}"
4
discovery.zen.ping.unicast.hosts: es-cluster
5
discovery.zen.minimum_master_nodes: 2
6
bootstrap.memory_lock: false
7
gateway.recover_after_nodes: 2
8
gateway.expected_nodes: 3
9
gateway.recover_after_time: 5m
10
discovery.zen.fd.ping_interval: 1s
11
discovery.zen.fd.ping_timeout: 10s
12
discovery.zen.fd.ping_retries: 2

准备volumeClaimTemplates的pv

  • es-pv.yml
1
apiVersion: v1
2
kind: PersistentVolume
3
metadata:
4
  name: nfs-1
5
spec:
6
  storageClassName: manual
7
  capacity:
8
    storage: 10Gi
9
  accessModes:
10
    - ReadWriteOnce
11
  nfs:
12
    server: 192.168.88.90
13
    path: /data/es_data/1
14
15
---
16
apiVersion: v1
17
kind: PersistentVolume
18
metadata:
19
  name: nfs-2
20
spec:
21
  storageClassName: manual
22
  capacity:
23
    storage: 10Gi
24
  accessModes:
25
    - ReadWriteOnce
26
  nfs:
27
    server: 192.168.88.90
28
    path: /data/es_data/2
29
30
---
31
apiVersion: v1
32
kind: PersistentVolume
33
metadata:
34
  name: nfs-3
35
spec:
36
  storageClassName: manual
37
  capacity:
38
    storage: 10Gi
39
  accessModes:
40
    - ReadWriteOnce
41
  nfs:
42
    server: 192.168.88.90
43
    path: /data/es_data/3

准备statefulset

  • es-cluster-sts.yml
1
apiVersion: v1
2
kind: Service
3
metadata:
4
  name: elasticsearch
5
  labels:
6
    app: es
7
spec:
8
  ports:
9
  - name: restful
10
    port: 9200
11
    targetPort: 9200
12
    nodePort: 31000
13
  - name: transport
14
    port: 9300
15
    targetPort: 9300
16
    nodePort: 33000
17
  type: NodePort
18
  selector:
19
    app: es
20
21
---
22
apiVersion: v1
23
kind: Service
24
metadata:
25
  name: es-cluster
26
  labels:
27
    app: es-cluster
28
spec:
29
  ports:
30
  - name: restful
31
    port: 9200
32
  - name: transport
33
    port: 9300
34
  clusterIP: None
35
  selector:
36
    app: es
37
38
---
39
apiVersion: apps/v1
40
kind: StatefulSet
41
metadata:
42
  name: es
43
spec:
44
  serviceName: es-cluster
45
  replicas: 3
46
  selector:
47
    matchLabels:
48
      app: es
49
  template:
50
    metadata:
51
      labels:
52
        app: es
53
    spec:
54
      securityContext:
55
        fsGroup: 1000
56
      nodeSelector:
57
        kubernetes.io/arch: amd64
58
      initContainers:
59
      - name: init
60
        image: busybox
61
        imagePullPolicy: IfNotPresent
62
        command: ["/bin/sh", "-c", "sysctl -w vm.max_map_count=262144; ulimit -n 65536; ulimit -u 2048; ulimit -l ulimited"]
63
        securityContext:
64
          privileged: true
65
        containers:
66
        - name: elasticsearch
67
          image: harbor.zjhw.com/library/elasticsearch:v2
68
          imagePullPolicy: Always
69
          env: 
70
          - name: NAMESPACE
71
            valueFrom:
72
              fieldRef:
73
                fieldPath: metadata.namespace
74
          - name: POD_NAME
75
            valueFrom:
76
              fieldRef:
77
                fieldPath: metadata.name
78
          - name: POD_IP
79
            valueFrom:
80
              fieldRef:
81
                fieldPath: status.podIP
82
          ports:
83
          - containerPort: 9200
84
            name: restful
85
          - containerPort: 9300
86
            name: transport
87
          volumeMounts:
88
          - name: es-data
89
            mountPath: /usr/share/elasticsearch/data
90
  volumeClaimTemplates:
91
  - metadata:
92
      name: es-data
93
    spec:
94
      accessModes: ['ReadWriteOnce']
95
      storageClassName: "manual"
96
      resources:
97
        requests:
98
          storage: 10Gi

kibana

1
apiVersion: apps/v1
2
kind: Deployment
3
metadata:
4
  name: kibana
5
spec:
6
  replicas: 1
7
  selector:
8
    matchLabels:
9
      app: kibana
10
  template:
11
    metadata:
12
      labels:
13
        app: kibana
14
    spec:
15
      nodeSelector:
16
        kubernetes.io/arch: amd64
17
      containers:
18
      - name: kibana
19
        image: harbor.zjhw.com/library/kibana:v2
20
        imagePullPolicy: Always
21
        ports:
22
        - containerPort: 5601
23
---
24
apiVersion: v1
25
kind: Service
26
metadata:
27
  name: kibana
28
  labels:
29
    app: kibana
30
spec:
31
  ports:
32
  - port: 5601
33
    targetPort: 5601
34
    nodePort: 32000
35
  type: NodePort
36
  selector:
37
    app: kibana
CATALOG
  1. 1. 部署elasticseach集群
    1. 1.1. 构建elasticsearch docker镜像
    2. 1.2. 准备volumeClaimTemplates的pv
    3. 1.3. 准备statefulset
    4. 1.4. kibana