安装ECK

1
2
kubectl create -f https://download.elastic.co/downloads/eck/2.1.0/crds.yaml
kubectl apply -f https://download.elastic.co/downloads/eck/2.1.0/operator.yaml

安装Elasticsearch

 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
tee elasticsearch-7.16.3.yaml << EOF
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: es7163
spec:
  version: 7.16.3
  http:
    tls:
      selfSignedCertificate:
        disabled: true
    service:
      spec:
        type: NodePort
        ports:
        - name: http
          port: 9200
          targetPort: 9200
  nodeSets:
  - name: default
    count: 1
    config:
      node.store.allow_mmap: false
    volumeClaimTemplates:
    - metadata:
        name: elasticsearch-data
      spec:
        accessModes:
        - ReadWriteOnce
        resources:
          requests:
            storage: 2000Gi
        storageClassName: nfs-client
EOF

kubectl apply -f elasticsearch-7.16.3.yaml
1
2
3
4
5
6
7
8
9
kubectl get elasticsearch

USER=elastic

PASSWORD=$(kubectl get secret es7163-es-elastic-user -o go-template='{{.data.elastic | base64decode}}')
#或者
PASSWORD=$(kubectl get secret es7163-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode)

PORT=$(kubectl get service/es7163-es-http -o jsonpath="{.spec.ports[0].nodePort}")

安装Kibana

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
tee kibana-7.16.3.yaml << EOF
apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
  name: quickstart
spec:
  version: 7.16.3
  http:
    service:
      spec:
        type: NodePort
        ports:
        - port: 5601
          targetPort: 5601
  count: 1
  elasticsearchRef:
    name: es7163
EOF

kubectl apply -f kibana-7.16.3.yaml
1
2
3
4
5
6
7
8
kubectl get kibana

USER=elastic

#就是elasticsearch的密码
PASSWORD=$(kubectl get secret es7163-es-elastic-user -o=jsonpath='{.data.elastic}' | base64 --decode; echo)

PORT=$(kubectl get svc quickstart-kb-http -o jsonpath="{.spec.ports[0].nodePort}")

安装Filebeat

试试helm安装filebeat,真香!

1
2
3
4
helm repo add elastic https://helm.elastic.co

wget https://raw.githubusercontent.com/elastic/helm-charts/7.16/filebeat/values.yaml
wget -O example.values.yaml https://github.com/elastic/helm-charts/blob/main/filebeat/examples/security/values.yaml

修改values.yaml中daemonset的内容

  output.elasticsearch:
    host: '${NODE_NAME}'
    hosts: '${ELASTICSEARCH_HOSTS:elasticsearch-master:9200}'

为:

  output.elasticsearch:
    host: '${NODE_NAME}'
    hosts: '${ELASTICSEARCH_HOSTS:elasticsearch-master:9200}'
    username: '${ELASTICSEARCH_USERNAME}'
    password: '${ELASTICSEARCH_PASSWORD}'
 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
#安装
helm install filebeat elastic/filebeat -f values.yaml \
  --set imageTag=7.16.3 \
  --set daemonset.extraEnvs[0].name=ELASTICSEARCH_HOSTS \
  --set daemonset.extraEnvs[0].value=es7163-es-http \
  --set daemonset.extraEnvs[1].name=ELASTICSEARCH_USERNAME \
  --set daemonset.extraEnvs[1].value=elastic \
  --set daemonset.extraEnvs[2].name=ELASTICSEARCH_PASSWORD \
  --set daemonset.extraEnvs[2].value=07eR118z5eQcAWdt9ylui834

#更新
helm upgrade filebeat elastic/filebeat -f values.yaml \
  --set imageTag=7.16.3 \
  --set daemonset.extraEnvs[0].name=ELASTICSEARCH_HOSTS \
  --set daemonset.extraEnvs[0].value=es7163-es-http \
  --set daemonset.extraEnvs[1].name=ELASTICSEARCH_USERNAME \
  --set daemonset.extraEnvs[1].value=elastic \
  --set daemonset.extraEnvs[2].name=ELASTICSEARCH_PASSWORD \
  --set daemonset.extraEnvs[2].value=WsI17nX98Aq1zjDs72S3e5P2

#安装完成有条命令用法提示,检查状态,-w动态监视
kubectl get pods --namespace=default -l app=filebeat-filebeat -w

#卸载
helm uninstall filebeat

踩坑

查看错误

yanyong@newmaster:~/k8s/elastic$ kubectl describe kibana | grep -A 10 Events:
Events:
  Type     Reason                   Age                    From                          Message
  ----     ------                   ----                   ----                          -------
  Warning  AssociationError         5m12s (x4 over 5m12s)  kibana-controller             Association backend for elasticsearch is not configured
  Normal   AssociationStatusChange  5m12s                  kb-es-association-controller  Association status changed from [] to [Pending]

或者,查看错误

yanyong@newmaster:~/k8s/elastic$ kubectl get events | tail
32m         Warning   AssociationError          kibana/quickstart                                                  Association backend for elasticsearch is not configured
32m         Normal    AssociationStatusChange   kibana/quickstart                                                  Association status changed from [] to [Pending]

查看日志

yanyong@newmaster:~/k8s/elastic$ kubectl logs elastic-operator-0 -n elastic-system | tail -1
{"log.level":"error","@timestamp":"2022-04-21T06:53:30.967Z","log.logger":"manager.eck-operator.controller.kb-es-association-controller","message":"Reconciler error","service.version":"2.1.0+02a8d7c7","service.type":"eck","ecs.version":"1.4.0","name":"quickstart","namespace":"default","error":"no port named [http] in service [default/es7163-es-http]","errorCauses":[{"error":"no port named [http] in service [default/es7163-es-http]"}],"error.stack_trace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:266\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:227"}

解决办法

部署elasticsearch时添加name: http

spec:
  version: 7.16.3
  http:
    tls:
      selfSignedCertificate:
        disabled: true
    service:
      spec:
        type: NodePort
        ports:
        - name: http
          port: 9200
          targetPort: 9200

https://www.elastic.co/downloads/elastic-cloud-kubernetes

https://www.elastic.co/guide/en/cloud-on-k8s/2.1/k8s-deploy-elasticsearch.html

https://github.com/elastic/cloud-on-k8s/blob/2.1/config/samples/elasticsearch/elasticsearch.yaml

https://www.elastic.co/guide/en/cloud-on-k8s/2.1/k8s-deploy-kibana.html

https://github.com/elastic/cloud-on-k8s/blob/2.1/config/samples/kibana/kibana_es.yaml

https://www.elastic.co/cn/beats/filebeat

https://www.elastic.co/downloads/beats/filebeat

https://github.com/elastic/helm-charts/tree/main/filebeat

https://github.com/elastic/helm-charts/blob/7.16/filebeat/values.yaml

https://github.com/elastic/helm-charts/blob/main/filebeat/examples/security/values.yaml