经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
K8s部署轻量级日志收集系统EFK(elasticsearch + filebeat + kibana)
来源:cnblogs  作者:山有扶苏QWQ  时间:2023/10/13 8:53:29  对本文有异议

K8s部署EFK(elasticsear + filebeat + kibana)日志收集

一.准备镜像

  1. # 在本机拉取镜像
  2. docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.2
  3. docker pull docker.elastic.co/kibana/kibana:7.17.2
  4. docker pull docker.elastic.co/beats/filebeat:7.17.2
  5. # 对镜像重打标签 将${harbor_url}和${harbor_project}换成自己的harbor私服地址和目录
  6. docker tag docker.elastic.co/elasticsearch/elasticsearch:7.17.2 ${harbor_url}/${harbor_project}/elasticsearch:7.17.2
  7. docker tag docker.elastic.co/kibana/kibana:7.17.2 ${harbor_url}/${harbor_project}/kibana:7.17.2
  8. docker tag docker.elastic.co/beats/filebeat:7.17.2 ${harbor_url}/${harbor_project}/filebeat:7.17.2
  9. # 登陆自己的harbor服务器
  10. docker login -u admin -p ${password} ${harbor_url}
  11. # 上传至harbor仓库
  12. docker push ${harbor_url}/${harbor_project}/elasticsearch:7.17.2
  13. docker push ${harbor_url}/${harbor_project}/kibana:7.17.2
  14. docker push ${harbor_url}/${harbor_project}/filebeat:7.17.2

此处的目的是避免某些服务器从docker外网仓库拉取不了镜像,从而导致pod一直运行不了,当然也可以不用这一步,可以直接使用官方的镜像地址

如果此处的Harbor目录是私有的,则需要在k8s集群中创建docker拉取harbor私库的密钥

  1. # -n 后是指定的空间,根据自己的情况去更改,不加-n,默认是default空间,因为本次EFK安装在kube-system命名空间下,所以-n为kube-system。
  2. kubectl create secret docker-registry harbor-pull-secret --docker-server=${harbor_url} --docker-username=admin --docker-password=${password} -n kube-system

kube-system为集群默认存在的空间,不用手动创建

  1. #检查密钥是否创建成功
  2. kubectl get secrets -n kube-system

image-20231011094101732

二.搭建Elasticsearch + kibana

1.在可执行kubectl命令的服务器准备安装的yml文件

image-20231011095203812

2.在elasticsearch-kibana目录下创建配置文件elasticsearch.yml
  1. cluster.name: my-es
  2. node.name: "node-1"
  3. path.data: /usr/share/elasticsearch/data
  4. #path.logs: /var/log/elasticsearch
  5. bootstrap.memory_lock: false
  6. network.host: 0.0.0.0
  7. http.port: 9200
  8. discovery.seed_hosts: ["127.0.0.1", "[::1]"]
  9. cluster.initial_master_nodes: ["node-1"]
  10. #增加参数,使head插件可以访问es
  11. http.cors.enabled: true
  12. http.cors.allow-origin: "*"
  13. http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
  14. xpack.monitoring.collection.enabled: true
3.创建kibana配置文件kibana.yml
  1. server.port: 5601
  2. server.host: "0.0.0.0"
  3. elasticsearch.hosts: "http://es-kibana-0.es-kibana.kube-system:9200"
  4. kibana.index: ".kibana"
  5. i18n.locale: "zh-CN"
  6. monitoring.ui.elasticsearch.hosts: ["http://es-kibana-0.es-kibana.kube-system:9200"]
  7. monitoring.ui.enabled: true

其中elasticsearch.hosts的地址构成为pod名:es-kibana-0,service名:es-kibana,命名空间:kube-system,以及service中配置的端口号9200

4.在k8s中创建elasticsearch和kibana的配置文件configmap
  1. #在编写yml配置文件的目录执行该命令
  2. kubectl create configmap es-config -n kube-system --from-file=elasticsearch.yml
  3. kubectl create configmap kibana-config -n kube-system --from-file=kibana.yml
5.检查是否有StorageClass
  1. kubectl get sc
  2. #如下图所示是有StorageClass的

image-20231011113436713

如果有则跳过第三步,没有则需要按照第三步配置NFS服务器

创建es存储pvc,pv配置文件:es-pvc.yaml

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: es-pv-claim
  5. namespace: kube-system
  6. labels:
  7. app: es
  8. spec:
  9. accessModes:
  10. - ReadWriteMany
  11. storageClassName: "nfs-storage"
  12. resources:
  13. requests:
  14. storage: 20Gi
  1. kubectl apply -f es-pvc.yaml
6.创建es-kibana的yaml配置文件: es-statefulset.yaml
  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. labels:
  5. app: es-kibana
  6. name: es-kibana
  7. namespace: kube-system
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: es-kibana
  13. serviceName: "es-kibana"
  14. template:
  15. metadata:
  16. labels:
  17. app: es-kibana
  18. spec:
  19. containers:
  20. - image: [Harbor私库地址]/elasticsearch:7.17.2
  21. imagePullPolicy: IfNotPresent
  22. name: elasticsearch
  23. resources:
  24. requests:
  25. memory: "800Mi"
  26. cpu: "800m"
  27. limits:
  28. memory: "1Gi"
  29. cpu: "1000m"
  30. volumeMounts:
  31. - name: es-config
  32. mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
  33. subPath: elasticsearch.yml
  34. - name: es-persistent-storage
  35. mountPath: /usr/share/elasticsearch/data
  36. env:
  37. - name: TZ
  38. value: Asia/Shanghai
  39. - image: [Harbor私库地址]/kibana:7.17.2
  40. imagePullPolicy: IfNotPresent
  41. name: kibana
  42. env:
  43. - name: TZ
  44. value: Asia/Shanghai
  45. volumeMounts:
  46. - name: kibana-config
  47. mountPath: /usr/share/kibana/config/kibana.yml
  48. subPath: kibana.yml
  49. volumes:
  50. - name: es-config
  51. configMap:
  52. name: es-config
  53. - name: kibana-config
  54. configMap:
  55. name: kibana-config
  56. - name: es-persistent-storage
  57. persistentVolumeClaim:
  58. claimName: es-pv-claim
  59. #hostNetwork: true
  60. #dnsPolicy: ClusterFirstWithHostNet
  61. nodeSelector:
  62. kubernetes.io/hostname: k8s-uni-node3
  1. #创建pod
  2. kubectl create -f es-statefulset.yaml
  3. # 查看
  4. kubectl get pod -o wide -n kube-system|grep es
  5. # 使用curl命令测试elasticsearch是否正常
  6. kubectl get pod -o wide -n kube-system|grep es

image-20231011120051319

然后在集群内部服务器上测试是否能通信

image-20231011135418126

当然也可以在Rancher上查看pod是否运行成功

image-20231011135532459

这个pod一次运行了两个容器,分别是kibanah和elasticsearch,并且把elasticsearch容器中的/usr/share/elasticsearch/data目录下的内容,挂载到了es-pv-claim下,我们可以在第三步中的NFS服务器共享目录中找到挂载的数据。

image-20231011115625957

7.创建es-kibana cluserip的svc
  1. vi es-cluster-none-svc.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: es-kibana
  6. name: es-kibana
  7. namespace: kube-system
  8. spec:
  9. ports:
  10. - name: es9200
  11. port: 9200
  12. protocol: TCP
  13. targetPort: 9200
  14. - name: es9300
  15. port: 9300
  16. protocol: TCP
  17. targetPort: 9300
  18. clusterIP: None
  19. selector:
  20. app: es-kibana
  21. type: ClusterIP
  1. kubectl apply -f es-cluster-none-svc.yaml

这个配置文件描述了一个名为 es-kibana 的 Kubernetes Service,该 Service 不分配 Cluster IP(ClusterIP: None),它会将流量路由到具有特定标签 app: es-kibana 的 Pod,这些 Pod 的端口 9200 和 9300 将被公开,并且可以通过相应的 targetPort 进行访问。用于集群内部访问

8.创建es-kibana的nodeport类型的svc
  1. vi es-nodeport-svc.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: es-kibana
  6. name: es-kibana-nodeport-svc
  7. namespace: kube-system
  8. spec:
  9. ports:
  10. - name: 9200-9200
  11. port: 9200
  12. protocol: TCP
  13. targetPort: 9200
  14. #nodePort: 9200
  15. - name: 5601-5601
  16. port: 5601
  17. protocol: TCP
  18. targetPort: 5601
  19. #nodePort: 5601
  20. selector:
  21. app: es-kibana
  22. type: NodePort
  1. kubectl apply -f es-nodeport-svc.yaml

这个配置文件创建了一个名为 "es-kibana-nodeport-svc" 的 Kubernetes Service。该 Service 使用 NodePort 类型,允许从集群外部访问服务。Service 公开了两个端口,9200 和 5601,分别将流量路由到具有相应标签的 Pod 的对应端口。Pod 的选择基于标签 app: es-kibana。用于暴露端口,从集群外部访问es和kibana

外网暴露的端口是k8s随机分配的,有两种办法可以查看

  1. #在服务器使用命令查看
  2. kubectl get svc -n kube-system|grep es-kibana

image-20231011141800714

Rancher上查看

image-20231011141916201

可以看到Kibana的端口为31200,然后就能使用nodeip+port访问

image-20231011142531139

检查es是否注册上Kibana,点击侧边栏找到堆栈检测,然后点Nodes

image-20231011142819072

image-20231011142914922

至此,Elasticsearch + kibana已经搭建完成,可以进行第四步。


三.配置NFS服务器

1).安装NFS服务

Ubuntu:

  1. sudo apt update
  2. sudo apt install nfs-kernel-server

Centos:

  1. yum update
  2. yum -y install nfs-utils
  1. # 创建或使用用已有的文件夹作为nfs文件存储点
  2. mkdir -p /home/data/nfs/share
  3. vi /etc/exports

写入如下内容

/home/data/nfs/share *(rw,no_root_squash,sync,no_subtree_check)

image-20230913174358481

  1. # 配置生效并查看是否生效
  2. exportfs -r
  3. exportfs

image-20230913174639129

  1. # 启动rpcbind、nfs服务
  2. #Centos
  3. systemctl restart rpcbind && systemctl enable rpcbind
  4. systemctl restart nfs && systemctl enable nfs
  5. #Ubuntu
  6. systemctl restart rpcbind && systemctl enable rpcbind
  7. systemctl start nfs-kernel-server && systemctl enable nfs-kernel-server
  8. # 查看 RPC 服务的注册状况
  9. rpcinfo -p localhost

image-20230913175507036

  1. # showmount测试
  2. showmount -e localhost

image-20230913175649184

以上都没有问题则说明安装成功

2).k8s注册nfs服务

新建storageclass-nfs.yaml文件,粘贴如下内容:

  1. ## 创建了一个存储类
  2. apiVersion: storage.k8s.io/v1
  3. kind: StorageClass #存储类的资源名称
  4. metadata:
  5. name: nfs-storage #存储类的名称,自定义
  6. annotations:
  7. storageclass.kubernetes.io/is-default-class: "true" #注解,是否是默认的存储,注意:KubeSphere默认就需要个默认存储,因此这里注解要设置为“默认”的存储系统,表示为"true",代表默认。
  8. provisioner: k8s-sigs.io/nfs-subdir-external-provisioner #存储分配器的名字,自定义
  9. parameters:
  10. archiveOnDelete: "true" ## 删除pv的时候,pv的内容是否要备份
  11. ---
  12. apiVersion: apps/v1
  13. kind: Deployment
  14. metadata:
  15. name: nfs-client-provisioner
  16. labels:
  17. app: nfs-client-provisioner
  18. # replace with namespace where provisioner is deployed
  19. namespace: default
  20. spec:
  21. replicas: 1 #只运行一个副本应用
  22. strategy: #描述了如何用新的POD替换现有的POD
  23. type: Recreate #Recreate表示重新创建Pod
  24. selector: #选择后端Pod
  25. matchLabels:
  26. app: nfs-client-provisioner
  27. template:
  28. metadata:
  29. labels:
  30. app: nfs-client-provisioner
  31. spec:
  32. serviceAccountName: nfs-client-provisioner #创建账户
  33. containers:
  34. - name: nfs-client-provisioner
  35. image: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2 #使用NFS存储分配器的镜像
  36. volumeMounts:
  37. - name: nfs-client-root #定义个存储卷,
  38. mountPath: /persistentvolumes #表示挂载容器内部的路径
  39. env:
  40. - name: PROVISIONER_NAME #定义存储分配器的名称
  41. value: k8s-sigs.io/nfs-subdir-external-provisioner #需要和上面定义的保持名称一致
  42. - name: NFS_SERVER #指定NFS服务器的地址,你需要改成你的NFS服务器的IP地址
  43. value: 192.168.0.0 ## 指定自己nfs服务器地址
  44. - name: NFS_PATH
  45. value: /home/data/nfs/share ## nfs服务器共享的目录 #指定NFS服务器共享的目录
  46. volumes:
  47. - name: nfs-client-root #存储卷的名称,和前面定义的保持一致
  48. nfs:
  49. server: 192.168.0.0 #NFS服务器的地址,和上面保持一致,这里需要改为你的IP地址
  50. path: /home/data/nfs/share #NFS共享的存储目录,和上面保持一致
  51. ---
  52. apiVersion: v1
  53. kind: ServiceAccount #创建个SA账号
  54. metadata:
  55. name: nfs-client-provisioner #和上面的SA账号保持一致
  56. # replace with namespace where provisioner is deployed
  57. namespace: default
  58. ---
  59. #以下就是ClusterRole,ClusterRoleBinding,Role,RoleBinding都是权限绑定配置,不在解释。直接复制即可。
  60. kind: ClusterRole
  61. apiVersion: rbac.authorization.k8s.io/v1
  62. metadata:
  63. name: nfs-client-provisioner-runner
  64. rules:
  65. - apiGroups: [""]
  66. resources: ["nodes"]
  67. verbs: ["get", "list", "watch"]
  68. - apiGroups: [""]
  69. resources: ["persistentvolumes"]
  70. verbs: ["get", "list", "watch", "create", "delete"]
  71. - apiGroups: [""]
  72. resources: ["persistentvolumeclaims"]
  73. verbs: ["get", "list", "watch", "update"]
  74. - apiGroups: ["storage.k8s.io"]
  75. resources: ["storageclasses"]
  76. verbs: ["get", "list", "watch"]
  77. - apiGroups: [""]
  78. resources: ["events"]
  79. verbs: ["create", "update", "patch"]
  80. ---
  81. kind: ClusterRoleBinding
  82. apiVersion: rbac.authorization.k8s.io/v1
  83. metadata:
  84. name: run-nfs-client-provisioner
  85. subjects:
  86. - kind: ServiceAccount
  87. name: nfs-client-provisioner
  88. # replace with namespace where provisioner is deployed
  89. namespace: default
  90. roleRef:
  91. kind: ClusterRole
  92. name: nfs-client-provisioner-runner
  93. apiGroup: rbac.authorization.k8s.io
  94. ---
  95. kind: Role
  96. apiVersion: rbac.authorization.k8s.io/v1
  97. metadata:
  98. name: leader-locking-nfs-client-provisioner
  99. # replace with namespace where provisioner is deployed
  100. namespace: default
  101. rules:
  102. - apiGroups: [""]
  103. resources: ["endpoints"]
  104. verbs: ["get", "list", "watch", "create", "update", "patch"]
  105. ---
  106. kind: RoleBinding
  107. apiVersion: rbac.authorization.k8s.io/v1
  108. metadata:
  109. name: leader-locking-nfs-client-provisioner
  110. # replace with namespace where provisioner is deployed
  111. namespace: default
  112. subjects:
  113. - kind: ServiceAccount
  114. name: nfs-client-provisioner
  115. # replace with namespace where provisioner is deployed
  116. namespace: default
  117. roleRef:
  118. kind: Role
  119. name: leader-locking-nfs-client-provisioner
  120. apiGroup: rbac.authorization.k8s.io

需要修改的就只有服务器地址和共享的目录

创建StorageClass

  1. kubectl apply -f storageclass-nfs.yaml
  2. # 查看是否存在
  3. kubectl get sc

image-20231011113436713


四.创建filebeat服务

1.创建filebeat主配置文件filebeat.settings.configmap.yml
  1. vi filebeat.settings.configmap.yml
  1. ---
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. namespace: kube-system
  6. name: filebeat-config
  7. labels:
  8. app: filebeat
  9. data:
  10. filebeat.yml: |-
  11. filebeat.inputs:
  12. - type: container
  13. enabled: true
  14. paths:
  15. - /var/log/containers/*.log
  16. multiline:
  17. pattern: ^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}
  18. negate: true
  19. match: after
  20. processors:
  21. - add_kubernetes_metadata:
  22. in_cluster: true
  23. host: ${NODE_NAME}
  24. matchers:
  25. - logs_path:
  26. logs_path: "/var/log/containers/"
  27. - add_cloud_metadata:
  28. - add_kubernetes_metadata:
  29. matchers:
  30. - logs_path:
  31. logs_path: "/var/log/containers/"
  32. - add_docker_metadata:
  33. output.elasticsearch:
  34. hosts: ["http://[节点IP]:32494"]
  35. indices:
  36. - index: "filebeat-demo-%{+yyyy.MM.dd}"
  37. setup.ilm:
  38. policy_file: /etc/indice-lifecycle.json
  1. #执行
  2. kubectl apply -f filebeat.settings.configmap.yml

filebeat.inputs: 定义输入配置,这里配置了从容器日志中收集数据。

  • type: 定义输入类型为 container,表示从容器日志中收集数据。

  • enabled: 启用该输入配置。

  • paths: 指定要监视的日志文件路径,这里是容器日志路径。k8s容器的日志默认是保存在在服务器的/var/log/containers/下的。

  • multiline: 多行日志配置,用于将多行日志合并为单个事件。正则表示如果前面几个数字不是4个数字开头,那么就会合并到一行,解决Java堆栈错误日志收集问题

  • processors: 定义处理器,用于添加元数据。add_kubernetes_metadata:为日志事件添加 Kubernetes 相关的元数据信息,例如 Pod 名称、命名空间、标签等。

output.elasticsearch: 定义输出配置,将收集到的日志发送到 Elasticsearch。

  • hosts: 指定 Elasticsearch 节点的地址和端口。端口号为第二步安装es时,nodeport暴露的端口号。
  • indices: 定义索引模式,这里以日期为后缀,创建每日索引。

setup.ilm: 配置索引生命周期管理 (ILM),用于管理索引的生命周期。

  • policy_file:后面定义的是生命周期配置文件的地址
2.创建Filebeat索引生命周期策略配置文件

为了防止大量的数据存储,可以利用 indice 的生命周期来配置数据保留。 如下所示的文件中,配置成每天或每次超过5GB的时候就对 indice 进行轮转,并删除所有超过30天的 indice 文件。

  1. vi filebeat.indice-lifecycle.configmap.yml
  1. ---
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. namespace: kube-system
  6. name: filebeat-indice-lifecycle
  7. labels:
  8. app: filebeat
  9. data:
  10. indice-lifecycle.json: |-
  11. {
  12. "policy": {
  13. "phases": {
  14. "hot": {
  15. "actions": {
  16. "rollover": {
  17. "max_size": "5GB" ,
  18. "max_age": "1d"
  19. }
  20. }
  21. },
  22. "delete": {
  23. "min_age": "30d",
  24. "actions": {
  25. "delete": {}
  26. }
  27. }
  28. }
  29. }
  30. }
  1. #执行
  2. kubectl apply -f filebeat.indice-lifecycle.configmap.yml
3.Filebeat操作权限
  1. vi filebeat.permission.yml
  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: ClusterRole
  3. metadata:
  4. name: filebeat
  5. rules:
  6. - apiGroups: [""]
  7. resources:
  8. - namespaces
  9. - pods
  10. - nodes
  11. verbs:
  12. - get
  13. - watch
  14. - list
  15. ---
  16. apiVersion: rbac.authorization.k8s.io/v1
  17. kind: ClusterRoleBinding
  18. metadata:
  19. name: filebeat
  20. subjects:
  21. - kind: ServiceAccount
  22. name: filebeat
  23. namespace: kube-system
  24. roleRef:
  25. kind: ClusterRole
  26. name: filebeat
  27. apiGroup: rbac.authorization.k8s.io
  28. ---
  29. apiVersion: v1
  30. kind: ServiceAccount
  31. metadata:
  32. namespace: kube-system
  33. name: filebeat
  1. #执行
  2. kubectl apply -f filebeat.permission.yml
  1. ClusterRole:
    • 作用:定义了一个 ClusterRole,它是一种权限集合,指定了 Filebeat 在集群范围内可以执行的操作,如获取(get)、监视(watch)、列出(list)等。
    • 权限:
      • 可以对命名空间执行 get、watch、list 操作。
      • 可以对 Pod 执行 get、watch、list 操作。
      • 可以对节点执行 get、watch、list 操作。
  2. ClusterRoleBinding:
    • 作用:定义了一个 ClusterRoleBinding,将 ClusterRole (filebeat) 绑定到特定的 ServiceAccount (filebeat)。
    • 意义:将 ClusterRole 与 ServiceAccount 绑定,以使 Filebeat 具有在 Kubernetes 中执行相应操作的权限。
  3. ServiceAccount:
    • 作用:定义了一个 ServiceAccount (filebeat),它是 Kubernetes 中用于身份验证和授权的实体。
    • 意义:创建了一个用于 Filebeat 的身份实体,使得 Filebeat 在 Kubernetes 中能够以其身份运行。
4.Filebeat Daemonset配置文件
  1. vi filebeat.daemonset.yml
  1. ---
  2. apiVersion: apps/v1
  3. kind: DaemonSet
  4. metadata:
  5. namespace: kube-system
  6. name: filebeat
  7. labels:
  8. app: filebeat
  9. spec:
  10. selector:
  11. matchLabels:
  12. app: filebeat
  13. template:
  14. metadata:
  15. labels:
  16. app: filebeat
  17. spec:
  18. serviceAccountName: filebeat
  19. terminationGracePeriodSeconds: 30
  20. containers:
  21. - name: filebeat
  22. image: [Harbor私服地址]/filebeat:7.17.2
  23. args: [
  24. "-c", "/etc/filebeat.yml",
  25. "-e",
  26. ]
  27. env:
  28. - name: NODE_NAME
  29. valueFrom:
  30. fieldRef:
  31. fieldPath: spec.nodeName
  32. securityContext:
  33. runAsUser: 0
  34. resources:
  35. limits:
  36. memory: 200Mi
  37. requests:
  38. cpu: 100m
  39. memory: 100Mi
  40. volumeMounts:
  41. - name: config
  42. mountPath: /etc/filebeat.yml
  43. readOnly: true
  44. subPath: filebeat.yml
  45. - name: filebeat-indice-lifecycle
  46. mountPath: /etc/indice-lifecycle.json
  47. readOnly: true
  48. subPath: indice-lifecycle.json
  49. - name: data
  50. mountPath: /usr/share/filebeat/data
  51. - name: varlog
  52. mountPath: /var/log
  53. readOnly: true
  54. - name: varlibdockercontainers
  55. mountPath: /var/lib/docker/containers
  56. readOnly: true
  57. - name: dockersock
  58. mountPath: /var/run/docker.sock
  59. volumes:
  60. - name: config
  61. configMap:
  62. defaultMode: 0600
  63. name: filebeat-config
  64. - name: filebeat-indice-lifecycle
  65. configMap:
  66. defaultMode: 0600
  67. name: filebeat-indice-lifecycle
  68. - name: varlog
  69. hostPath:
  70. path: /var/log
  71. - name: varlibdockercontainers
  72. hostPath:
  73. path: /var/lib/docker/containers
  74. - name: dockersock
  75. hostPath:
  76. path: /var/run/docker.sock
  77. - name: data
  78. hostPath:
  79. path: /var/lib/filebeat-data
  80. type: DirectoryOrCreate
  1. #执行
  2. kubectl apply -f filebeat.daemonset.yml

volumeMounts配置示意:

  1. name: config:
    • 作用: 创建一个挂载点,将 ConfigMap 中的 filebeat.yml 配置文件挂载到容器中的 /etc/filebeat.yml 路径。
    • readOnly: 设置为 true,表示只读访问该配置文件。
  2. name: filebeat-indice-lifecycle:
    • 作用: 创建一个挂载点,将 ConfigMap 中的 indice-lifecycle.json 文件挂载到容器中的 /etc/indice-lifecycle.json 路径。
    • readOnly: 设置为 true,表示只读访问该文件。
  3. name: data:
    • 作用: 创建一个挂载点,将宿主机的 /var/lib/filebeat-data 目录挂载到容器中的 /usr/share/filebeat/data 路径。
    • type: DirectoryOrCreate: 指定挂载类型为目录,如果该目录不存在则创建。
  4. name: varlog:
    • 作用: 创建一个挂载点,将宿主机的 /var/log 目录挂载到容器中的 /var/log 路径。
    • readOnly: 设置为 true,表示只读访问该目录。
  5. name: varlibdockercontainers:
    • 作用: 创建一个挂载点,将宿主机的 /var/lib/docker/containers 目录挂载到容器中的 /var/lib/docker/containers 路径。
    • readOnly: 设置为 true,表示只读访问该目录。
  6. name: dockersock:
    • 作用: 创建一个挂载点,将宿主机的 Docker socket 文件 /var/run/docker.sock 挂载到容器中的 /var/run/docker.sock 路径。

volumes配置示意:

  1. name: config:
    • 作用: 与volumeMounts中的name: config相对应。
    • defaultMode: 0600: 表示只有文件所有者可读写,其他用户无权限。
    • name:filebeat-config对应第一步创建的filebeat.settings.configmap.yml中的Configmap的名字
  2. name: filebeat-indice-lifecycle:
    • 作用: 与volumeMounts中的filebeat-indice-lifecycle相对应。
    • defaultMode: 0600: 表示只有文件所有者可读写,其他用户无权限。
    • name:filebeat-indice-lifecycle对应第二步创建的filebeat.indice-lifecycle.configmap.yml中的Configmap的名字
  3. name: data
    • 作用:与volumeMounts中的name: data相对应。
    • path:需要挂载的目录路径
    • type:DirectoryOrCreatea表示容器启动时会检查宿主机是否存在该目录,不存在则创建。因为Filebeat在每个节点的宿主机上都会运行,所以直接挂载到宿主机目录

检查是否执行成功

  1. kubectl get pod -o wide -n kube-system|grep filebeat
  2. #如下图,全为Running则表示运行成功

image-20231011160048266

也可以在Rancher上查看

image-20231011160211682

五.检查File beat与es,Kibana是否配置成功

1.首先在侧边栏找到Stack Management

image-20231011160552133

2.选择索引管理,查看是否有以filebeat-demo加时间戳为名的索引

image-20231011160630025

image-20231011160754049

3.创建索引模式

image-20231011160851049

image-20231011160950466

4.查看日志

点击侧边栏,选择discover,就能看到Filebeat收集到的容器日志,可以按照自己的需求进行日志筛选

image-20231011161136329

原文链接:https://www.cnblogs.com/blogof-fusu/p/17757551.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号