使用 Fluentd 进行日志收集

此任务展示如何配置 Istio 以创建自定义日志条目,并将其发送到 Fluentd 守护程序。 Fluentd 是一个开源的日志收集器,它支持许多数据输出并具有可插拔的体系结构。 一个常见的日志收集后端是 Elasticsearch 和作为查看器的 Kibana。 接下来,将启用新的日志流,并把日志发送到示例堆栈 Fluentd / Elasticsearch / Kibana。

整个任务中,使用 Bookinfo 作为示例应用程序。

开始之前

  • 在您的集群中安装 Istio 并部署应用程序。 此任务假定已将 Mixer 设置为默认配置(--configDefaultNamespace=istio-system)。 如果使用其它值,此任务中请更新配置和命令以匹配该值。

安装 Fluentd

在您的集群中,您可能已经在运行 Fluentd 守护程序,例如 此处此处所述的插件, 或其他定制化的相关程序。这可能配置为将日志发送到 Elasticsearch 系统或日志收集程序。

您可以使用这些 Fluentd 守护程序或任何其他已设置的 Fluentd 守护程序,只要它们正在侦听转发的日志, 并且 Istio 的 Mixer 可以连接到它们。为了使 Mixer 连接到正在运行的 Fluentd 守护程序,您可能需要为 Fluentd 添加 service。 以下是侦听转发日志的 Fluentd 配置:

  1. <source>
  2. type forward
  3. </source>

将 Mixer 连接到所有可能的 Fluentd 配置的完整细节不在此任务的讨论范围。

示例堆栈 Fluentd、Elasticsearch、Kibana

出于此任务的目的,您可以部署提供的示例堆栈。该堆栈包括 Fluentd,Elasticsearch 和 Kibana, 它们位于非生产就绪的一组 ServicesDeployments 中, 其全部部署到一个名为 logging 的新 Namespace 中。

将以下内容另存为 logging-stack.yaml

  1. # Logging Namespace. All below are a part of this namespace.
  2. apiVersion: v1
  3. kind: Namespace
  4. metadata:
  5. name: logging
  6. ---
  7. # Elasticsearch Service
  8. apiVersion: v1
  9. kind: Service
  10. metadata:
  11. name: elasticsearch
  12. namespace: logging
  13. labels:
  14. app: elasticsearch
  15. spec:
  16. ports:
  17. - port: 9200
  18. protocol: TCP
  19. targetPort: db
  20. selector:
  21. app: elasticsearch
  22. ---
  23. # Elasticsearch Deployment
  24. apiVersion: apps/v1
  25. kind: Deployment
  26. metadata:
  27. name: elasticsearch
  28. namespace: logging
  29. labels:
  30. app: elasticsearch
  31. spec:
  32. replicas: 1
  33. selector:
  34. matchLabels:
  35. app: elasticsearch
  36. template:
  37. metadata:
  38. labels:
  39. app: elasticsearch
  40. annotations:
  41. sidecar.istio.io/inject: "false"
  42. spec:
  43. containers:
  44. - image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.1.1
  45. name: elasticsearch
  46. resources:
  47. # need more cpu upon initialization, therefore burstable class
  48. limits:
  49. cpu: 1000m
  50. requests:
  51. cpu: 100m
  52. env:
  53. - name: discovery.type
  54. value: single-node
  55. ports:
  56. - containerPort: 9200
  57. name: db
  58. protocol: TCP
  59. - containerPort: 9300
  60. name: transport
  61. protocol: TCP
  62. volumeMounts:
  63. - name: elasticsearch
  64. mountPath: /data
  65. volumes:
  66. - name: elasticsearch
  67. emptyDir: {}
  68. ---
  69. # Fluentd Service
  70. apiVersion: v1
  71. kind: Service
  72. metadata:
  73. name: fluentd-es
  74. namespace: logging
  75. labels:
  76. app: fluentd-es
  77. spec:
  78. ports:
  79. - name: fluentd-tcp
  80. port: 24224
  81. protocol: TCP
  82. targetPort: 24224
  83. - name: fluentd-udp
  84. port: 24224
  85. protocol: UDP
  86. targetPort: 24224
  87. selector:
  88. app: fluentd-es
  89. ---
  90. # Fluentd Deployment
  91. apiVersion: apps/v1
  92. kind: Deployment
  93. metadata:
  94. name: fluentd-es
  95. namespace: logging
  96. labels:
  97. app: fluentd-es
  98. spec:
  99. replicas: 1
  100. selector:
  101. matchLabels:
  102. app: fluentd-es
  103. template:
  104. metadata:
  105. labels:
  106. app: fluentd-es
  107. annotations:
  108. sidecar.istio.io/inject: "false"
  109. spec:
  110. containers:
  111. - name: fluentd-es
  112. image: gcr.io/google-containers/fluentd-elasticsearch:v2.0.1
  113. env:
  114. - name: FLUENTD_ARGS
  115. value: --no-supervisor -q
  116. resources:
  117. limits:
  118. memory: 500Mi
  119. requests:
  120. cpu: 100m
  121. memory: 200Mi
  122. volumeMounts:
  123. - name: config-volume
  124. mountPath: /etc/fluent/config.d
  125. terminationGracePeriodSeconds: 30
  126. volumes:
  127. - name: config-volume
  128. configMap:
  129. name: fluentd-es-config
  130. ---
  131. # Fluentd ConfigMap, contains config files.
  132. kind: ConfigMap
  133. apiVersion: v1
  134. data:
  135. forward.input.conf: |-
  136. # Takes the messages sent over TCP
  137. <source>
  138. type forward
  139. </source>
  140. output.conf: |-
  141. <match **>
  142. type elasticsearch
  143. log_level info
  144. include_tag_key true
  145. host elasticsearch
  146. port 9200
  147. logstash_format true
  148. # Set the chunk limits.
  149. buffer_chunk_limit 2M
  150. buffer_queue_limit 8
  151. flush_interval 5s
  152. # Never wait longer than 5 minutes between retries.
  153. max_retry_wait 30
  154. # Disable the limit on the number of retries (retry forever).
  155. disable_retry_limit
  156. # Use multiple threads for processing.
  157. num_threads 2
  158. </match>
  159. metadata:
  160. name: fluentd-es-config
  161. namespace: logging
  162. ---
  163. # Kibana Service
  164. apiVersion: v1
  165. kind: Service
  166. metadata:
  167. name: kibana
  168. namespace: logging
  169. labels:
  170. app: kibana
  171. spec:
  172. ports:
  173. - port: 5601
  174. protocol: TCP
  175. targetPort: ui
  176. selector:
  177. app: kibana
  178. ---
  179. # Kibana Deployment
  180. apiVersion: apps/v1
  181. kind: Deployment
  182. metadata:
  183. name: kibana
  184. namespace: logging
  185. labels:
  186. app: kibana
  187. spec:
  188. replicas: 1
  189. selector:
  190. matchLabels:
  191. app: kibana
  192. template:
  193. metadata:
  194. labels:
  195. app: kibana
  196. annotations:
  197. sidecar.istio.io/inject: "false"
  198. spec:
  199. containers:
  200. - name: kibana
  201. image: docker.elastic.co/kibana/kibana-oss:6.1.1
  202. resources:
  203. # need more cpu upon initialization, therefore burstable class
  204. limits:
  205. cpu: 1000m
  206. requests:
  207. cpu: 100m
  208. env:
  209. - name: ELASTICSEARCH_URL
  210. value: http://elasticsearch:9200
  211. ports:
  212. - containerPort: 5601
  213. name: ui
  214. protocol: TCP
  215. ---

创建资源:

  1. $ kubectl apply -f logging-stack.yaml
  2. namespace "logging" created
  3. service "elasticsearch" created
  4. deployment "elasticsearch" created
  5. service "fluentd-es" created
  6. deployment "fluentd-es" created
  7. configmap "fluentd-es-config" created
  8. service "kibana" created
  9. deployment "kibana" created

配置 Istio

现在有了一个正在运行的 Fluentd 守护进程,用一个新的日志类型配置 Istio,并将这些日志发送到侦听守护进程。 应用配置 Istio 自动生成和收集日志流的 YAML 文件:

Zip

  1. $ kubectl apply -f @samples/bookinfo/telemetry/fluentd-istio.yaml@

如果您使用的是 Istio 1.1.2 或更早版本,请改用以下配置:

Zip

  1. $ kubectl apply -f @samples/bookinfo/telemetry/fluentd-istio-crd.yaml@

请注意,处理程序配置中的 address: "fluentd-es.logging:24224" 指向我们在示例堆栈中设置的 Fluentd 守护程序。

查看新日志

  1. 将流量发送到示例应用程序。

    对于 Bookinfo 示例, 请在您的浏览器中访问 http://$GATEWAY_URL/productpage,或使用以下命令在命令行中发送请求:

    1. $ curl http://$GATEWAY_URL/productpage
  2. 在 Kubernetes 环境中,通过执行以下命令来设置 Kibana 的端口转发:

    1. $ kubectl -n logging port-forward $(kubectl -n logging get pod -l app=kibana -o jsonpath='{.items[0].metadata.name}') 5601:5601 &

    运行命令以可以访问 Kibana UI,当完成访问时,注意在命令行中用 Ctrl-C 退出。

  3. 导航到 Kibana UI,然后单击右上角的“Set up index patterns”。

  4. 使用 * 指标类型,然后单击“Next step”。

  5. 选择 @timestamp 作为“时间过滤器”字段名称,然后单击“Create index pattern”。

  6. 现在,单击左侧菜单上的“Discover”,然后开始浏览生成的日志。

清除

  • 删除新的遥测配置:

    Zip

    1. $ kubectl delete -f @samples/bookinfo/telemetry/fluentd-istio.yaml@

    如果您使用的是 Istio 1.1.2 或更早版本:

    Zip

    1. $ kubectl delete -f @samples/bookinfo/telemetry/fluentd-istio-crd.yaml@
  • 删除示例堆栈 Fluentd、Elasticsearch 和 Kibana:

    1. $ kubectl delete -f logging-stack.yaml
  • 删除所有可能仍在运行的 kubectl port-forward 进程:

    1. $ killall kubectl
  • 如果您不打算继续探索后续任务,请参考 Bookinfo 清除以关闭应用程序。

相关内容

Mixer 和 SPOF 神话

提高可用,降低延迟。

Mixer 适配器模型

概要说明 Mixer 的插件架构。

Jaeger

了解如何配置代理以向 Jaeger 发送追踪请求。

LightStep

怎样配置代理才能把追踪请求发送到 LightStep。

Mixer 配置模型

描述 Istio 策略执行和遥测机制的配置模型。

TLS Egress 监控和策略配置

描述如何在 TLS Egress 上配置 SNI 监控和策略。