CC00280.CloudKubernetes——|KuberNetes&运维.V02|——|EFK架构.v02|部署ElasticSearch|

一、部署ElasticSearch
### --- 创建ElasticSearch-service.yaml文件

[root@k8s-master01 EFK]# cat es-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-logging
  namespace: public-service
  labels:
    k8s-app: elasticsearch-logging
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    kubernetes.io/name: "Elasticsearch"
spec:
  ports:
  - port: 9200
    protocol: TCP
    targetPort: db
  selector:
    k8s-app: elasticsearch-logging
二、创建ElasticSearch-service
### --- 创建ElasticSearch-service

[root@k8s-master01 EFK]# kubectl create -f es-service.yaml 
service/elasticsearch-logging created
### --- 查看创建的ElasticSearch-service

[root@k8s-master01 EFK]# kubectl get svc -n public-service -owide
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE    SELECTOR
elasticsearch-logging   ClusterIP   10.107.157.176   <none>        9200/TCP   114s   k8s-app=elasticsearch-logging
[root@k8s-master01 EFK]# kubectl get ep -n public-service -owide
NAME                    ENDPOINTS            AGE
elasticsearch-logging   172.27.14.195:9200   3m
三、创建ElasticSearch-StatefulSet.yaml
### --- 创建ElasticSearch-StatefulSet的yaml文件

[root@k8s-master01 EFK]# cat es-statefulset.yaml 
# RBAC authn and authz
apiVersion: v1
kind: ServiceAccount
metadata:
  name: elasticsearch-logging
  namespace: public-service
  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: public-service
  name: elasticsearch-logging
  labels:
    k8s-app: elasticsearch-logging
    addonmanager.kubernetes.io/mode: Reconcile
subjects:
- kind: ServiceAccount
  name: elasticsearch-logging
  namespace: public-service
  apiGroup: ""
roleRef:
  kind: ClusterRole
  name: elasticsearch-logging
  apiGroup: ""
---
# Elasticsearch deployment itself
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch-logging
  namespace: public-service
  labels:
    k8s-app: elasticsearch-logging
    version: v7.4.2
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  serviceName: elasticsearch-logging
  replicas: 1
  selector:
    matchLabels:
      k8s-app: elasticsearch-logging
      version: v7.4.2
  template:
    metadata:
      labels:
        k8s-app: elasticsearch-logging
        version: v7.4.2
    spec:
      serviceAccountName: elasticsearch-logging
      containers:
      - image: quay.io/fluentd_elasticsearch/elasticsearch:v7.4.2
        name: elasticsearch-logging
        imagePullPolicy: IfNotPresent
        resources:
          # need more cpu upon initialization, therefore burstable class
          limits:
            cpu: 500m
            memory: 2Gi
          requests:
            cpu: 100m
            memory: 2Gi
        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
      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
### --- 注:

~~~     # 注释一:namespace确定
metadata:
  name: elasticsearch-logging
  namespace: public-service
~~~     # 注释二:serviceName:
    spec:
      serviceAccountName: elasticsearch-logging
~~~     # 注释三:副本数:设置为1
spec:
  serviceName: elasticsearch-logging
  replicas: 1                                    // 副本数
~~~     # 注释四:绑定宿主机节点
    spec:   
      serviceAccountName: elasticsearch-logging  // 此行下添加如下参数
      nodeSelector:
         kubernetes.io/hostname: k8s-master03
~~~     # 注释五:健康检查关闭
~~~     注:它是使用k8s集群发现的机制来创建EFK的集群,
~~~     若是使用健康检查机制,它的健康检查一直通不过。集群一直就起不来。

#        livenessProbe:
#          tcpSocket:
#            port: transport
#          initialDelaySeconds: 5
#          timeoutSeconds: 10
#        readinessProbe:
#          tcpSocket:
#            port: transport
#          initialDelaySeconds: 5
#          timeoutSeconds: 10
~~~     # 注释六:es集群
~~~     测试环境是没有挂es的数据盘的,生成环境下,是会有自己的es集群的,es集群规模比较大,有可能是几百台或上千台宿主机的
~~~     所以es集群是不建议部署在k8s集群中的,因为es是非常消耗资源的。
~~~     若是有k8s专用的节点也是可以的

      volumes:                                  
      - name: elasticsearch-logging
        emptyDir: {}
四、创建ElasticSearch-StatefulSet
### --- 创建ElasticSearch-StatefulSet

[root@k8s-master01 EFK]# kubectl create -f es-statefulset.yaml 
serviceaccount/elasticsearch-logging created
clusterrole.rbac.authorization.k8s.io/elasticsearch-logging created
clusterrolebinding.rbac.authorization.k8s.io/elasticsearch-logging created
statefulset.apps/elasticsearch-logging created
### --- 查看创建日志

[root@k8s-master01 EFK]# kubectl logs -f pod/elasticsearch-logging-0 -n public-service
+ export NODE_NAME=elasticsearch-logging-0
+ NODE_NAME=elasticsearch-logging-0
+ export NODE_MASTER=true
+ NODE_MASTER=true
+ export NODE_DATA=true
+ NODE_DATA=true
+ export HTTP_PORT=9200
+ HTTP_PORT=9200
+ export TRANSPORT_PORT=9300
+ TRANSPORT_PORT=9300
+ export MINIMUM_MASTER_NODES=2
+ MINIMUM_MASTER_NODES=2
+ chown -R elasticsearch:elasticsearch /data
+ ./bin/elasticsearch_logging_discovery
### --- 查看创建后结果

[root@k8s-master01 EFK]# kubectl get po -n public-service -owide
NAME                      READY   STATUS    RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
elasticsearch-logging-0   1/1     Running   0          20s   172.27.14.195   k8s-node02   <none>           <none>
五、验证ElasticSearch的实例
### --- 查看创建ElasticSearch的pod实例

[root@k8s-master01 EFK]# kubectl get po -n public-service -owide
NAME                      READY   STATUS    RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
elasticsearch-logging-0   1/1     Running   0          20s   172.27.14.195   k8s-node02   <none>           <none>
### --- 查看创建ElasticSearch的svc实例

[root@k8s-master01 EFK]# kubectl get svc -n public-service -owide
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE    SELECTOR
elasticsearch-logging   ClusterIP   10.107.157.176   <none>        9200/TCP   114s   k8s-app=elasticsearch-logging
### --- 查看创建ElasticSearch的ep实例

[root@k8s-master01 EFK]# kubectl get ep -n public-service -owide
NAME                    ENDPOINTS            AGE
elasticsearch-logging   172.27.14.195:9200   3m
### --- 验证创建ElasticSearch的集群是否正常
~~~     显示green状态说明正常

[root@k8s-master01 EFK]# curl 10.107.157.176:9200/_cluster/health?pretty
{
  "cluster_name" : "kubernetes-logging",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值