sidecar這個詞一般指帶有跨斗的摩托車,在二戰時候小日本開着很多這種摩托車,它在原有基礎上添加了一個跨斗,之后就可以多載一個人,而對於原來的兩輪摩托車沒有什么影響,把跨斗拆了也是可以的,對原來的事物沒有本質上的破壞,只是擴展了新的功能,這與軟件開發里的OCP原則很像,在服務網格的istio里也有這個概念,它把這種組件叫“sidecar”,在istio里sidecar也只是一個概念,具體是由envoy來實現的。
具體fluentd功能的sidecar
我們的容器部署到k8s里,通過k8s來管理我們的容器,實現對容器的生命周期管理,服務發現管理,多副本管理等等;而我們把這些容器可以理解為一個個的微服務,而這些服務的日志一般先記錄在本地,然后推到elasticsearch里,而日志收集工具我們可以選擇fluent
,Filebeat
,Logstash
等等。
添加fluentd的sidecar
添加fluentd.config配置
<source>
type tail
format json
path /var/log/*.log
pos_file /var/log/log.pos
tag saas # 這個tag對應match.logstash_prefix,之后在kibana的索引配置里可以找到
</source>
<match **>
@id elasticsearch
@type elasticsearch
@log_level debug
index_name fluentd
type_name fluentd
host elasticsearch.elk
port 9200
include_tag_key true
tag_key @log_name
logstash_format true
logstash_prefix saas
flush_interval 10s
</match>
服務的部署文件添加sidecar
kind: Service
apiVersion: v1
metadata:
name: hello-world
namespace: saas
spec:
selector:
app: hello-world
type: ClusterIP
ports:
- protocol: TCP
targetPort: 9001
port: 80
---
# 構建反射代理
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: hello-world-ingress
namespace: saas
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
tls:
- hosts:
- www.abc.com
secretName: saas-tls
rules:
- host: www.abc.com
http:
paths:
- backend:
serviceName: hello-world
servicePort: 9001
- path: /dotnet
backend:
serviceName: dotnet-hello
servicePort: 80
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: hello-world-deployment
namespace: saas
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: 172.17.0.22:8888/saas/hello-world:latest
imagePullPolicy: Always
ports:
- containerPort: 9001
env:
- name: spring.profiles.active
value: prod
volumeMounts:
- name: varlog
mountPath: /var/log
- name: fluent-sidecar
image: registry.cn-beijing.aliyuncs.com/k8s-mqm/fluentd-elasticsearch:v2.1.0
env:
- name: FLUENTD_ARGS
value: -c /etc/fluentd-config/fluentd.conf
volumeMounts:
- name: varlog
mountPath: /var/log
- name: config-volume
mountPath: /etc/fluentd-config
volumes:
- name: varlog
emptyDir: {}
- name: config-volume
configMap:
name: fluentd-config
當你的hello-world部署到k8s之后,在有日志記錄時它會寫到/var/logs目錄,而fluentd這個sidecar因為是與容器花用的磁盤,所以它也可以讀到日志的內容,然后把日志發到elasticsearch里。