Back to Loki

Network Policies

operator/docs/user-guides/network_policies.md

3.7.15.8 KB
Original Source

Overview

Network policies provide network segmentation for your LokiStack deployment by controlling ingress and egress traffic between Loki components and external services. When enabled, the Loki Operator automatically creates Kubernetes NetworkPolicy resources that implement a "default deny" security model with explicit allow rules for required communications.

This guide covers how to configure and understand network policies for your LokiStack deployment.

Platform-Specific Notes

Vanilla Kubernetes

On standard Kubernetes clusters:

  • Monitoring: Open access for any Prometheus instance
  • DNS: Support for kube-dns and CoreDNS (port 53)
  • AlertManager: If AlertManager endpoint is configured in the RulerConfig resource then allow egress to the port specified in the endpoint URL. If no port is specified, defaults to 9093

OpenShift

Network policies on OpenShift include additional integrations:

  • Monitoring: Automatic integration with OpenShift Monitoring stack
  • DNS: Support for both standard and OpenShift DNS services (port 5353)
  • AlertManager: Built-in access to cluster monitoring AlertManager

Configuration

Network policies are configured through the networkPolicies field in your LokiStack specification:

yaml
apiVersion: loki.grafana.com/v1
kind: LokiStack
metadata:
  name: lokistack-sample
  namespace: openshift-logging
spec:
  size: 1x.small
  storage:
    secret:
      name: lokistack-object-storage
      type: s3
  storageClassName: gp3-csi
  tenants:
    mode: openshift-logging
  networkPolicies:
    ruleSet: RestrictIngressEgress # Enable network policies

Configuration Options

ConfigurationDescriptionBehavior
networkPolicies: null (omitted)Default - inherit platform and operator defaultsCurrently allows full network access everywhere
networkPolicies.ruleSet: NoneDisabled - no network policies createdFull network access allowed
networkPolicies.ruleSet: RestrictIngressEgressEnabled - network policies enforcedRestricted network access with explicit allow rules

Generated Network Policies

When network policies are enabled, the Loki Operator creates several NetworkPolicy resources to secure different aspects of your LokiStack deployment:

Policy NamePurposeAffected Components
{name}-default-denyBaseline deny-all policyAll LokiStack pods
{name}-loki-allowInter-component communicationAll Loki components
{name}-loki-allow-bucket-egressObject storage accessingester, querier, index-gateway, compactor, ruler
{name}-loki-allow-gateway-ingressGateway access to Loki componentsdistributor, query-frontend, ruler
{name}-gateway-allowGateway external & monitoring accesslokistack-gateway
{name}-ruler-allow-alert-egressRuler egress to AlertManagerruler
{name}-loki-allow-query-frontendQuery frontend external accessquery-frontend (OpenShift network mode)

Flow Matrix

Ingress (Incoming Traffic)

ComponentFrom GatewayFrom ComponentsFrom ExternalFrom Monitoring
distributor
ingester
querier
query-frontend✅*
ruler
compactor
index-gateway
gateway

*Only in OpenShift network mode

Egress (Outgoing Traffic)

ComponentTo ComponentsTo Object StorageTo DNSTo AlertManagerTo API Server
distributor
ingester
querier
query-frontend
ruler
compactor
index-gateway
gateway

Integration with External Systems

For additional integrations (custom dashboards, external alerting), you may need to create supplementary NetworkPolicies. You can select specific components by using the label app.kubernetes.io/component you should always also include the labels app.kubernetes.io/name=lokistack and app.kubernetes.io/instance={name} to avoid collision with other pods deployed in the namespace.

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: custom-dashboard-access
spec:
  podSelector:
    matchLabels:
      app.kubernetes.io/name: lokistack
      app.kubernetes.io/instance: lokistack-dev
      app.kubernetes.io/component: ruler
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          name: monitoring
    - podSelector:
        matchLabels:
          app: custom-alertmanager
    ports:
    - protocol: TCP
      port: 3100

Conclusion

Network policies provide essential security controls for LokiStack deployments by implementing network segmentation and access controls. While they add a layer of complexity, the security benefits make them highly recommended for production environments.

The Loki Operator's network policies are designed to be secure by default while maintaining compatibility across diverse environments.