k8s 部署filebeat sidecar模式


2020-06-12

小試牛刀: 

先在自己本地wmware上嘗試k8s中日志的收集方式。一般有兩種方式:

一:sidecar模式,就是一個pod中部署兩個容器,一個跑應用,一個采集日志,用emptdir的共享目錄形式。

      缺點:一個應用一個收集日志的容器,后期的話資源消耗是個問題。

二:節點模式,一個節點跑一個agent來采集標准輸出和標准錯誤輸出的日志,然后發送給后端。(標准日志:容器內輸出到/dev/std...,或tail -f ,默認會存儲在節點的/var/lib/docker/containers/...路徑下,kubectl logs 能看到的日志就是標准日志。集群系統的日志一般在/var/log/containers/下。)詳情網上查閱資料,這個只是簡單的一個說法,並不嚴謹。

先嘗試第一個模式: sidecar模式

一:創建filebeat的dockerfile,這里我用了centos,做好后鏡像大約300+M,先嘗試看看問題,后期換個基礎鏡像。

FROM centos:latest
WORKDIR /usr/local
ADD filebeat-7.5.0-linux-x86_64.tar.gz .
RUN ln -s filebeat-7.5.0-linux-x86_64  filebeat \
 && cd filebeat       \
 && mkdir  config     \
 && chmod +x filebeat \
 && cp filebeat.yml config/ 

ENTRYPOINT ["/usr/local/filebeat/filebeat","-c","/usr/local/filebeat/config/filebeat.yml"]

 

直接構建鏡像,並推送到私有倉庫

docker build -t 10.0.0.200:5000/centos-fb:v4 . 

docker push 10.0.0.200:5000/centos-fb:v4

可以docker run運行看一下是否正常

二:創建一個deploymen資源,包含兩個容器

[root@k8s-master ~/software]# cat nginx-filebeat-sidecar.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-fb
  namespace: sidecar
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-fb
  template:
    metadata:
      namespace: sidecar
      labels:
        app: nginx-fb
    spec:
      containers:
      - image: nginx:alpine
        name: nginx
        ports: 
        - containerPort: 80
        volumeMounts:
        - name: applog
          mountPath: /var/log/nginx
      - image: 10.0.0.210:5000/centos-fb:v4
        name: filebeat
        volumeMounts:
        - name: applog
          mountPath: /log
        - name: filebeat-config
          mountPath: /usr/local/filebeat/config/
      volumes:
      - name: applog
        emptyDir: {}
      - name: filebeat-config
        configMap:
          name: filebeat-config

 

三:創建好yaml總的namespace和configmap

namespace,metadata里只填寫name就可以

[root@k8s-master ~/software]# kubectl get  namespaces sidecar -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2020-06-10T09:38:02Z"
  name: sidecar
  resourceVersion: "669711"
  selfLink: /api/v1/namespaces/sidecar
  uid: 5b4d0ecc-ff6a-4b62-a41f-a9924efa4e0a
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

configmap:以命令行的方式創建的,看一下filebeat的配置文件,配置文件有些問題,並不標准,下期更新一下

kubectl create configmap filebeat-config --from-file=filebeat.yml --namespace=sidecar       #configmap名字要和上面deployment資源中引用的configmap名字相同,均標了紅色。

[root@k8s-master ~/software]# cat filebeat.yml 
filebeat.inputs:
- type: log
  enabled: true 
  paths:
    - /log/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["access"]

- type: log
  enabled: true 
  paths:
    - /log/error.log
  tags: ["error"]

output.elasticsearch:
  hosts: ["10.0.0.200:9200"]
  indices:
    - index: "nginx_access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "access"
    - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"

setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true

 

四:創建service資源,讓nginx可以被外部訪問

[root@k8s-master ~/software]# cat service-nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: service-nginx
  namespace: sidecar
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080
  selector:
    app: nginx-fb

五:3和4 直接啟動 kubectl creat -f 

nginx 在瀏覽器中可以正常訪問

 

 

ES還並沒有部署在k8s中,在虛擬機中,從filebeat的配置文件可以看出來,可以正常看到兩類日志,沒有問題。

細心的朋友可能看出來了,Index沒有按照filebeat的配置規則來,但是不影響今天的實驗。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM