Skip to content

Latest commit

 

History

History
163 lines (145 loc) · 3.72 KB

example-loki-nginx.md

File metadata and controls

163 lines (145 loc) · 3.72 KB

Store Nginx Access Logs in Grafana Loki with Logging Operator

Add operator chart repository:

helm repo add loki https://grafana.github.io/loki/charts
helm repo add banzaicloud-stable https://kubernetes-charts.banzaicloud.com
helm repo update

Install Loki

helm install --name loki loki/loki

Grafana Loki Documentation

Install Grafana

helm install --name grafana stable/grafana \
 --set "datasources.datasources\\.yaml.apiVersion=1" \
 --set "datasources.datasources\\.yaml.datasources[0].name=Loki" \
 --set "datasources.datasources\\.yaml.datasources[0].type=loki" \
 --set "datasources.datasources\\.yaml.datasources[0].url=http://loki:3100" \
 --set "datasources.datasources\\.yaml.datasources[0].access=proxy"

Install with Helm

Logging Operator

helm install --name logging banzaicloud-stable/logging-operator

You can install logging resource via Helm chart with built-in TLS generation.

Nginx App + Logging Definition

helm install --name nginx-demo banzaicloud-stable/nginx-logging-loki-demo

Install from manifest

Create logging resource

cat <<EOF | kubectl apply -f -
apiVersion: logging.banzaicloud.io/v1beta1
kind: Logging
metadata:
  name: default-logging-simple
spec:
  fluentd: {}
  fluentbit: {}
  controlNamespace: default
EOF

Note: ClusterOutput and ClusterFlow resource will only be accepted in the controlNamespace

Create an Loki output definition

cat <<EOF | kubectl apply -f -
apiVersion: logging.banzaicloud.io/v1beta1
kind: Output
metadata:
  name: loki-output
  namespace: default
spec:
  loki:
    url: http://loki:3100
    buffer:
      path: /tmp/buffer
      timekey: 1m
      timekey_wait: 30s
      timekey_use_utc: true
EOF

Note: For production set-up we recommend using longer timekey interval to avoid generating too many object.

Create flow resource

cat <<EOF | kubectl apply -f -
apiVersion: logging.banzaicloud.io/v1beta1
kind: Flow
metadata:
  name: loki-flow
  namespace: default
spec:
  filters:
    - tag_normaliser: {}
    - parser:
        key_name: message
        remove_key_name_field: true
        reserve_data: true
        parsers:
          - type: nginx
  selectors:
    app: nginx
  outputRefs:
    - loki-output
EOF

Install nginx deployment

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
EOF

Grafana Dashboard

Get Grafana login credantials

kubectl get secrets grafana -o json | jq '.data | map_values(@base64d)'

Forward Grafana Service

kubectl port-forward svc/grafana 3000:80

Gradana Dashboard: http://localhost:3000