Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
loki/operator/internal/manifests/distributor.go

253 lines
7.4 KiB

package manifests
import (
"fmt"
"path"
"github.com/grafana/loki/operator/internal/manifests/internal/config"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// BuildDistributor returns a list of k8s objects for Loki Distributor
func BuildDistributor(opts Options) ([]client.Object, error) {
deployment := NewDistributorDeployment(opts)
if opts.Gates.HTTPEncryption {
if err := configureDistributorHTTPServicePKI(deployment, opts); err != nil {
return nil, err
}
}
if opts.Gates.GRPCEncryption {
if err := configureDistributorGRPCServicePKI(deployment, opts); err != nil {
return nil, err
}
}
if opts.Gates.HTTPEncryption || opts.Gates.GRPCEncryption {
caBundleName := signingCABundleName(opts.Name)
if err := configureServiceCA(&deployment.Spec.Template.Spec, caBundleName); err != nil {
return nil, err
}
}
if err := configureHashRingEnv(&deployment.Spec.Template.Spec, opts); err != nil {
return nil, err
}
if err := configureProxyEnv(&deployment.Spec.Template.Spec, opts); err != nil {
return nil, err
}
return []client.Object{
deployment,
NewDistributorGRPCService(opts),
NewDistributorHTTPService(opts),
newDistributorPodDisruptionBudget(opts),
}, nil
}
// NewDistributorDeployment creates a deployment object for a distributor
func NewDistributorDeployment(opts Options) *appsv1.Deployment {
l := ComponentLabels(LabelDistributorComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
TopologySpreadConstraints: defaultTopologySpreadConstraints(l),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
DefaultMode: &defaultConfigMapMode,
LocalObjectReference: corev1.LocalObjectReference{
Name: lokiConfigMapName(opts.Name),
},
},
},
},
},
Containers: []corev1.Container{
{
Image: opts.Image,
Name: "loki-distributor",
Resources: corev1.ResourceRequirements{
Limits: opts.ResourceRequirements.Distributor.Limits,
Requests: opts.ResourceRequirements.Distributor.Requests,
},
Args: []string{
"-target=distributor",
fmt.Sprintf("-config.file=%s", path.Join(config.LokiConfigMountDir, config.LokiConfigFileName)),
fmt.Sprintf("-runtime-config.file=%s", path.Join(config.LokiConfigMountDir, config.LokiRuntimeConfigFileName)),
"-config.expand-env=true",
},
ReadinessProbe: lokiReadinessProbe(),
LivenessProbe: lokiLivenessProbe(),
Ports: []corev1.ContainerPort{
{
Name: lokiHTTPPortName,
ContainerPort: httpPort,
Protocol: protocolTCP,
},
{
Name: lokiGRPCPortName,
ContainerPort: grpcPort,
Protocol: protocolTCP,
},
{
Name: lokiGossipPortName,
ContainerPort: gossipPort,
Protocol: protocolTCP,
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: configVolumeName,
ReadOnly: false,
MountPath: config.LokiConfigMountDir,
},
},
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: "File",
ImagePullPolicy: "IfNotPresent",
SecurityContext: containerSecurityContext(),
},
},
SecurityContext: podSecurityContext(opts.Gates.RuntimeSeccompProfile),
}
if opts.Stack.Template != nil && opts.Stack.Template.Distributor != nil {
podSpec.Tolerations = opts.Stack.Template.Distributor.Tolerations
podSpec.NodeSelector = opts.Stack.Template.Distributor.NodeSelector
}
if opts.Stack.Replication != nil {
podSpec.TopologySpreadConstraints = append(podSpec.TopologySpreadConstraints, topologySpreadConstraints(*opts.Stack.Replication, LabelDistributorComponent, opts.Name)...)
}
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: appsv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: DistributorName(opts.Name),
Labels: l,
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(opts.Stack.Template.Distributor.Replicas),
Selector: &metav1.LabelSelector{
MatchLabels: labels.Merge(l, GossipLabels()),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("loki-distributor-%s", opts.Name),
Labels: labels.Merge(l, GossipLabels()),
Annotations: a,
},
Spec: podSpec,
},
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.RollingUpdateDeploymentStrategyType,
},
},
}
}
// NewDistributorGRPCService creates a k8s service for the distributor GRPC endpoint
func NewDistributorGRPCService(opts Options) *corev1.Service {
serviceName := serviceNameDistributorGRPC(opts.Name)
labels := ComponentLabels(LabelDistributorComponent, opts.Name)
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: labels,
},
Spec: corev1.ServiceSpec{
ClusterIP: "None",
Ports: []corev1.ServicePort{
{
Name: lokiGRPCPortName,
Port: grpcPort,
Protocol: protocolTCP,
TargetPort: intstr.IntOrString{IntVal: grpcPort},
},
},
Selector: labels,
},
}
}
// NewDistributorHTTPService creates a k8s service for the distributor HTTP endpoint
func NewDistributorHTTPService(opts Options) *corev1.Service {
serviceName := serviceNameDistributorHTTP(opts.Name)
labels := ComponentLabels(LabelDistributorComponent, opts.Name)
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: labels,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: lokiHTTPPortName,
Port: httpPort,
Protocol: protocolTCP,
TargetPort: intstr.IntOrString{IntVal: httpPort},
},
},
Selector: labels,
},
}
}
func configureDistributorHTTPServicePKI(deployment *appsv1.Deployment, opts Options) error {
serviceName := serviceNameDistributorHTTP(opts.Name)
return configureHTTPServicePKI(&deployment.Spec.Template.Spec, serviceName)
}
func configureDistributorGRPCServicePKI(deployment *appsv1.Deployment, opts Options) error {
serviceName := serviceNameDistributorGRPC(opts.Name)
return configureGRPCServicePKI(&deployment.Spec.Template.Spec, serviceName)
}
// newDistributorPodDisruptionBudget returns a PodDisruptionBudget for the LokiStack
// Distributor pods.
func newDistributorPodDisruptionBudget(opts Options) *policyv1.PodDisruptionBudget {
l := ComponentLabels(LabelDistributorComponent, opts.Name)
mu := intstr.FromInt(1)
return &policyv1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
Kind: "PodDisruptionBudget",
APIVersion: policyv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Labels: l,
Name: DistributorName(opts.Name),
Namespace: opts.Namespace,
},
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: l,
},
MinAvailable: &mu,
},
}
}