k8s安装es集群版

2022-09-19 16:45:30 浏览数 (1)

这里使用官方示例 Elasticsearch StatefulSet 将使用 EmptyDir 卷来存储数据。pod 终止时,EmptyDir 将被擦除,后续将存储更改为永久卷声明即可

# 1.编辑es-statefulset.yml

点击查看

代码语言:javascript复制
# RBAC authn and authz
apiVersion: v1
kind: ServiceAccount
metadata:
  name: elasticsearch-logging
  namespace: summer
  labels:
    k8s-app: elasticsearch-logging
    addonmanager.kubernetes.io/mode: Reconcile
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: elasticsearch-logging
  labels:
    k8s-app: elasticsearch-logging
    addonmanager.kubernetes.io/mode: Reconcile
rules:
  - apiGroups:
      - ""
    resources:
      - "services"
      - "namespaces"
      - "endpoints"
    verbs:
      - "get"
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: summer
  name: elasticsearch-logging
  labels:
    k8s-app: elasticsearch-logging
    addonmanager.kubernetes.io/mode: Reconcile
subjects:
  - kind: ServiceAccount
    name: elasticsearch-logging
    namespace: summer
    apiGroup: ""
roleRef:
  kind: ClusterRole
  name: elasticsearch-logging
  apiGroup: ""
---
# Elasticsearch deployment itself
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch-logging
  namespace: summer
  labels:
    k8s-app: elasticsearch-logging
    version: v7.4.3
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  serviceName: elasticsearch-logging
  replicas: 3
  selector:
    matchLabels:
      k8s-app: elasticsearch-logging
      version: v7.4.3
  template:
    metadata:
      labels:
        k8s-app: elasticsearch-logging
        version: v7.4.3
    spec:
      serviceAccountName: elasticsearch-logging
      containers:
        - image: quay.io/fluentd_elasticsearch/elasticsearch:v7.4.3
          name: elasticsearch-logging
          imagePullPolicy: Always
          resources:
            # need more cpu upon initialization, therefore burstable class
            limits:
              cpu: 1000m
              memory: 3Gi
            requests:
              cpu: 100m
              memory: 3Gi
          ports:
            - containerPort: 9200
              name: db
              protocol: TCP
            - containerPort: 9300
              name: transport
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: transport
            initialDelaySeconds: 5
            timeoutSeconds: 10
          readinessProbe:
            tcpSocket:
              port: transport
            initialDelaySeconds: 5
            timeoutSeconds: 10
          volumeMounts:
            - name: elasticsearch-logging
              mountPath: /data
          env:
            - name: "NAMESPACE"
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: "MINIMUM_MASTER_NODES"
              value: "1"
      volumes:
        - name: elasticsearch-logging
          emptyDir: {}
      # Elasticsearch requires vm.max_map_count to be at least 262144.
      # If your OS already sets up this number to a higher value, feel free
      # to remove this init container.
      initContainers:
        - image: alpine:3.6
          command: ["/sbin/sysctl", "-w", "vm.max_map_count=262144"]
          name: elasticsearch-logging-init
          securityContext:
            privileged: true 

# 2.编辑es-service.yml

点击查看

代码语言:javascript复制
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-logging
  namespace: summer
  labels:
    k8s-app: elasticsearch-logging
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    kubernetes.io/name: "Elasticsearch"
spec:
  type: NodePort
  ports:
    - name: db
      port: 9200
      protocol: TCP
      targetPort: 9200
      nodePort: 30000
    - name: transport
      port: 9300
      protocol: TCP
      targetPort: 9300
  publishNotReadyAddresses: true
  selector:
    k8s-app: elasticsearch-logging
  sessionAffinity: None

# 3.执行yml文件

代码语言:javascript复制
[root@master elasticsearch]# kubectl create -f es-service.yml
[root@master elasticsearch]# kubectl create -f es-statefulset.yml

# 4.验证

代码语言:javascript复制
[root@master elasticsearch]# kubectl get statefulset -n summer
NAME                    READY   AGE
elasticsearch-logging   3/3     110m
[root@master elasticsearch]# kubectl get pod -n summer
NAME                                READY   STATUS    RESTARTS   AGE
elasticsearch-logging-0             1/1     Running   0          110m
elasticsearch-logging-1             1/1     Running   0          110m
elasticsearch-logging-2             1/1     Running   0          109m
[root@master elasticsearch]# kubectl get svc -n summer
NAME                    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
elasticsearch-logging   NodePort   10.105.199.132   <none>        9200:30000/TCP,9300:30499/TCP   111m

此时用head工具连接集群任意节点ip:30000验证即可

0 人点赞