使用 Fluentd 进行日志收集

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

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

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

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

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

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

    将以下内容另存为 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. app: fluentd-es
    76. ports:
    77. - name: fluentd-tcp
    78. port: 24224
    79. protocol: TCP
    80. targetPort: 24224
    81. - name: fluentd-udp
    82. port: 24224
    83. protocol: UDP
    84. targetPort: 24224
    85. selector:
    86. app: fluentd-es
    87. ---
    88. # Fluentd Deployment
    89. apiVersion: apps/v1
    90. kind: Deployment
    91. metadata:
    92. name: fluentd-es
    93. namespace: logging
    94. labels:
    95. app: fluentd-es
    96. spec:
    97. replicas: 1
    98. selector:
    99. matchLabels:
    100. app: fluentd-es
    101. template:
    102. metadata:
    103. labels:
    104. app: fluentd-es
    105. annotations:
    106. sidecar.istio.io/inject: "false"
    107. spec:
    108. containers:
    109. - name: fluentd-es
    110. image: gcr.io/google-containers/fluentd-elasticsearch:v2.0.1
    111. env:
    112. - name: FLUENTD_ARGS
    113. value: --no-supervisor -q
    114. resources:
    115. limits:
    116. memory: 500Mi
    117. requests:
    118. cpu: 100m
    119. memory: 200Mi
    120. volumeMounts:
    121. - name: config-volume
    122. mountPath: /etc/fluent/config.d
    123. terminationGracePeriodSeconds: 30
    124. volumes:
    125. - name: config-volume
    126. configMap:
    127. name: fluentd-es-config
    128. ---
    129. # Fluentd ConfigMap, contains config files.
    130. kind: ConfigMap
    131. apiVersion: v1
    132. data:
    133. forward.input.conf: |-
    134. # Takes the messages sent over TCP
    135. <source>
    136. type forward
    137. </source>
    138. output.conf: |-
    139. <match **>
    140. type elasticsearch
    141. log_level info
    142. include_tag_key true
    143. host elasticsearch
    144. port 9200
    145. logstash_format true
    146. # Set the chunk limits.
    147. buffer_chunk_limit 2M
    148. buffer_queue_limit 8
    149. flush_interval 5s
    150. # Never wait longer than 5 minutes between retries.
    151. max_retry_wait 30
    152. # Disable the limit on the number of retries (retry forever).
    153. disable_retry_limit
    154. </match>
    155. metadata:
    156. name: fluentd-es-config
    157. namespace: logging
    158. ---
    159. # Kibana Service
    160. apiVersion: v1
    161. kind: Service
    162. metadata:
    163. name: kibana
    164. namespace: logging
    165. labels:
    166. app: kibana
    167. spec:
    168. ports:
    169. - port: 5601
    170. protocol: TCP
    171. targetPort: ui
    172. selector:
    173. app: kibana
    174. ---
    175. # Kibana Deployment
    176. apiVersion: apps/v1
    177. kind: Deployment
    178. metadata:
    179. name: kibana
    180. namespace: logging
    181. labels:
    182. app: kibana
    183. spec:
    184. replicas: 1
    185. selector:
    186. matchLabels:
    187. app: kibana
    188. template:
    189. metadata:
    190. labels:
    191. app: kibana
    192. annotations:
    193. sidecar.istio.io/inject: "false"
    194. spec:
    195. containers:
    196. - name: kibana
    197. image: docker.elastic.co/kibana/kibana-oss:6.1.1
    198. resources:
    199. # need more cpu upon initialization, therefore burstable class
    200. limits:
    201. cpu: 1000m
    202. requests:
    203. cpu: 100m
    204. env:
    205. - name: ELASTICSEARCH_URL
    206. value: http://elasticsearch:9200
    207. ports:
    208. - containerPort: 5601
    209. name: ui
    210. protocol: TCP
    211. ---

    创建资源:

    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

    现在有了一个正在运行的 Fluentd 守护进程,用一个新的日志类型配置 Istio,并将这些日志发送到侦听守护进程。 应用配置 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. 将流量发送到示例应用程序。

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

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

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

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

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

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

    • 删除新的遥测配置:

      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:

    • 删除所有可能仍在运行的 kubectl port-forward 进程:

      1. $ killall kubectl