Skip to main content
Version: Cloud

Monitor Nginx in Kubernetes

Overview

Nginx monitoring involves monitoring of the following elements:

  • Nginx Access Logs

  • Nginx Error Logs

  • Nginx Server Health

Pre-reading

Refer to the links below for generic approach to monitoring application metrics and logs in Kubernetes environment

Refer to Nginx monitoring on instances for sfAgent configurations

Configuration

Configure Nginx server to enable monitoring

  1. Configure format of access logs so that it can be parsed by SnappyFlow
  2. Enable Nginx status module to monitor Nginx server health

These configurations can be achieved with the below ConfigMap:

apiVersion: v1 
kind: ConfigMap
metadata:
name: nginx-configmap
labels:
snappyflow/appname: <app_name>
snappyflow/projectname: <project_name>
data:
nginx.conf: |
worker_processes 5;
events {
worker_connections 4096;
}
http {
default_type application/octet-stream;
log_format upstream_time '$remote_addr:$remote_port $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_referer" '
'rt=$request_time uct=$upstream_connect_time uht=$upstream_header_time urt=$upstream_response_time';
server {
listen 80;
error_log /var/log/nginx/error1.log;
access_log /var/log/nginx/access1.log upstream_time;
location /nginx_status {
stub_status;
}
}
}

sfKubeAgent

sfKubeAgent is deployed as sidecar container in the NGINX pod and can be used to monitor Nginx server health as well as Access Logs & Error Logs. Below YAML files provide example for setting up NGINX monitoring with sfKubeAgent.

sfKubeAgent ConfigMap (sfAgent-config.yaml)

apiVersion: v1 
kind: ConfigMap
metadata:
name: sfagent-configmap
labels:
snappyflow/appname: <app_name>
snappyflow/projectname: <project_name>
data:
config.yaml: |+
---
key: "<profile_key>"
metrics:
plugins:
- name: kube-sfagent-nginx
enabled: true
interval: 300
config:
location: nginx_status
port: 80
secure: false
logging:
plugins:
- name: nginx-access
enabled: true
config:
log_path: "/var/log/nginx/access1.log"
- name: nginx-error
enabled: true
config:
log_path: "/var/log/nginx/error1.log"

Pod description YAML running NGINX and sfKubeAgent

apiVersion: v1 
metadata:
name: my-first-pod
labels:
snappyflow/appname: <app_name>
snappyflow/projectname: <project_name>
spec:
containers:
- name: nginx-container
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
volumeMounts:
- name: varlog
mountPath: /var/log/nginx
- name: nginx-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
# Snappyflow's sfkubeagent container
- name: nginx-sfagent
image: snappyflowml/sfagent:latest
imagePullPolicy: Always
command:
- /app/sfagent
- -enable-console-log
env:
- name: APP_NAME
value: <app_name>
- name: PROJECT_NAME
value: <project_name>
volumeMounts:
- name: nginx-sfagent-config
mountPath: /opt/sfagent/config.yaml
subPath: config.yaml
- name: varlog
mountPath: /var/log/nginx
volumes:
- name: nginx-sfagent-config
configMap:
name: sfagent-configmap
- name: nginx-config
configMap:
name: nginx-configmap
- name: varlog
emptyDir: {}

Centralized logging

Log monitoring (both access and error logs) can be implemented through Centralized Logging approach as well which does not require sfKubeAgent. Centralized logging however requires running a busybox container as a sidecar container to stream logs to container’s stdout.

Add the label - snappyflow/component: nginx, which signals to apply Nginx to container’s stdout.

Viewing data and dashboards

  • Data generated by plugin can be viewed in browse data page inside the respective application under plugin=jvm and documentType=jvm
  • Dashboard for this data can be instantiated by Importing dashboard template JVM to the application dashboard.