Monitoring and logging are crucial aspects of managing Kubernetes clusters and applications. They help you keep track of the health of your cluster and the performance of your applications.
Monitoring allows you to observe the state of your applications and infrastructure in real-time, while logging provides a historical record of events and errors. Together, they enable you to: - Identify performance bottlenecks - Troubleshoot issues - Ensure reliability and availability
Prometheus is a popular open-source monitoring tool designed for reliability and scalability. Here's how to set it up in your Kubernetes cluster:
You can use the Prometheus Operator to simplify the deployment. First, create a namespace for monitoring:
kubectl create namespace monitoring
Then, apply the Prometheus Operator manifest:
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/master/bundle.yaml -n monitoring
Create a prometheus.yaml file:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
namespace: monitoring
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
app: my-app
resources:
requests:
cpu: 100m
memory: 400Mi
retention: 10d
Apply the Prometheus instance:
kubectl apply -f prometheus.yaml
To access the Prometheus UI, you can port-forward the service:
kubectl port-forward svc/prometheus-k8s 9090 -n monitoring
Visit http://localhost:9090 in your web browser to see the Prometheus dashboard.
Fluentd is a data collector that helps you unify logging. Elasticsearch is a search and analytics engine that stores logs.
You can deploy Elasticsearch using Helm:
helm repo add elastic https://helm.elastic.co
helm install elasticsearch elastic/elasticsearch -n logging
Create a fluentd-configmap.yaml file to configure Fluentd:
apiVersion: v1
kind: ConfigMap
metadata:
name: fluentd-config
namespace: logging
data:
fluent.conf: |
<source>
@type kubernetes
@id input_kubernetes
@log_level info
<parse>
@type json
</parse>
</source>
<match **>
@type elasticsearch
@id output_elasticsearch
host elasticsearch.logging.svc.cluster.local
port 9200
logstash_format true
</match>
Apply the ConfigMap:
kubectl apply -f fluentd-configmap.yaml
Deploy Fluentd as a DaemonSet:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
namespace: logging
spec:
selector:
matchLabels:
app: fluentd
template:
metadata:
labels:
app: fluentd
spec:
containers:
- name: fluentd
image: fluent/fluentd-kubernetes-daemonset:v1.8.0-debian-ubuntu-20.04
env:
- name: FLUENT_ELASTICSEARCH_HOST
value: "elasticsearch.logging.svc.cluster.local"
volumeMounts:
- name: fluentd-config
mountPath: /fluentd/etc/fluent.conf
subPath: fluent.conf
volumes:
- name: fluentd-config
configMap:
name: fluentd-config
Apply the Fluentd DaemonSet:
kubectl apply -f fluentd-daemonset.yaml
Common Mistake: Not aggregating logs can lead to difficulties in troubleshooting. Always centralize logs for easier access.