sidecar这个词通常指带有跨斗的摩托车,在二战时候小日本开着不少这种摩托车,它在原有基础上添加了一个跨斗,以后就能够多载一我的,而对于原来的两轮摩托车没有什么影响,把跨斗拆了也是能够的,对原来的事物没有本质上的破坏,只是扩展了新的功能,这与软件开发里的OCP原则很像,在服务网格的istio里也有这个概念,它把这种组件叫“sidecar”,在istio里sidecar也只是一个概念,具体是由envoy来实现的。nginx
咱们的容器部署到k8s里,经过k8s来管理咱们的容器,实现对容器的生命周期管理,服务发现管理,多副本管理等等;而咱们把这些容器能够理解为一个个的微服务,而这些服务的日志通常先记录在本地,而后推到elasticsearch里,而日志收集工具咱们能够选择fluent
,Filebeat
,Logstash
等等。spring
<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>
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里。json