thanos解析(一) -- prometheus-operator创建sidecar

在prometheus-prometheus.yaml增加thanos的配置以后:

thanos:
    image: thanosio/thanos:v0.18.0
    objectStorageConfig:
      key: thanos.yaml
      name: thanos-objstore-config

prometheus-operator做了下面的事情:

  • 创建一个thanos的container作为prometheus的sidecar;
  • 为prometheus service增加了1个grpc端口:10901;

创建thanos sidecar

# kubectl describe pod prometheus-k8s-0 -n monitoring
Name:         prometheus-k8s-0
Namespace:    monitoring
Containers:
  ......
  thanos-sidecar:
    Container ID:  docker://e5d123d91752f941b9081d553ce49361154c0a26f9dc7c46dc7975b709575285
    Image:         thanosio/thanos:v0.18.0
    Image ID:      docker-pullable://thanosio/thanos@sha256:b94171aed499b2f1f81b6d3d385e0eeeca885044c59cef28ce6a9a9e8a827217
    Ports:         10902/TCP, 10901/TCP
    Host Ports:    0/TCP, 0/TCP
    Args:
      sidecar
      --prometheus.url=http://localhost:9090/
      --tsdb.path=/prometheus
      --grpc-address=[$(POD_IP)]:10901
      --http-address=[$(POD_IP)]:10902
      --objstore.config=$(OBJSTORE_CONFIG)
    State:          Running
      Started:      Sun, 04 Jul 2021 17:35:11 +0800
  ......

创建sidecar container的代码在prometheus-operator中:

// pkg/prometheus/statefulset.go
func makeStatefulSetSpec(p monitoringv1.Prometheus, c *Config, ruleConfigMapNames []string) (*appsv1.StatefulSetSpec, error) {
    ......
    if p.Spec.Thanos != nil {
        ....
        container := v1.Container{
            Name:                     "thanos-sidecar",
            Image:                    thanosImage,
            TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
            Args:                     thanosArgs,
            Env: []v1.EnvVar{
                {
                    Name: "POD_IP",
                    ValueFrom: &v1.EnvVarSource{
                        FieldRef: &v1.ObjectFieldSelector{
                            FieldPath: "status.podIP",
                        },
                    },
                },
            },
            Ports: []v1.ContainerPort{
                {
                    Name:          "http",
                    ContainerPort: 10902,
                },
                {
                    Name:          "grpc",
                    ContainerPort: 10901,
                },
            },
            VolumeMounts: []v1.VolumeMount{
                {
                    Name:      volName,
                    MountPath: storageDir,
                    SubPath:   subPathForStorage(p.Spec.Storage),
                },
            },
            Resources: p.Spec.Thanos.Resources,
        }
    }
}

增加grpc端口

thanos解析(一) -- prometheus-operator创建sidecar_第1张图片

增加prometheus-operator grpc端口的代码:

// pkg/prometheus/statefulset.go
func makeStatefulSetService(p *monitoringv1.Prometheus, config Config) *v1.Service {
    p = p.DeepCopy()

    if p.Spec.PortName == "" {
        p.Spec.PortName = defaultPortName
    }

    svc := &v1.Service{
        ObjectMeta: metav1.ObjectMeta{
            Name: governingServiceName,
            Labels: config.Labels.Merge(map[string]string{
                "operated-prometheus": "true",
            }),
        },
        Spec: v1.ServiceSpec{
            ClusterIP: "None",
            Ports: []v1.ServicePort{
                {
                    Name:       p.Spec.PortName,
                    Port:       9090,
                    TargetPort: intstr.FromString(p.Spec.PortName),
                },
            },
            Selector: map[string]string{
                "app": "prometheus",
            },
        },
    }

    if p.Spec.Thanos != nil {
        svc.Spec.Ports = append(svc.Spec.Ports, v1.ServicePort{
            Name:       "grpc",
            Port:       10901,
            TargetPort: intstr.FromString("grpc"),
        })
    }
    return svc
}

可以看到10901的端口是hardcoding在代码中的。

你可能感兴趣的