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 #这个是es里索引的名称,可以在kibana里建立索引
</source>
<match **>
@id elasticsearch
@type elasticsearch
@log_level debug
index_name fluentd
type_name _doc
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里。
最新评论