k8s~爲服務添加sidecar邊鬥

sidecar這個詞通常指帶有跨斗的摩托車,在二戰時候小日本開着不少這種摩托車,它在原有基礎上添加了一個跨鬥,以後就能夠多載一我的,而對於原來的兩輪摩托車沒有什麼影響,把跨鬥拆了也是能夠的,對原來的事物沒有本質上的破壞,只是擴展了新的功能,這與軟件開發裏的OCP原則很像,在服務網格的istio裏也有這個概念,它把這種組件叫「sidecar」,在istio裏sidecar也只是一個概念,具體是由envoy來實現的。nginx

具體fluentd功能的sidecar

咱們的容器部署到k8s裏,經過k8s來管理咱們的容器,實現對容器的生命週期管理,服務發現管理,多副本管理等等;而咱們把這些容器能夠理解爲一個個的微服務,而這些服務的日誌通常先記錄在本地,而後推到elasticsearch裏,而日誌收集工具咱們能夠選擇fluentFilebeatLogstash等等。spring

添加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裏。json

相關文章
相關標籤/搜索