Kubernetes 在生命周期中运行自定义命令

问题描述 投票:0回答:1

我正在尝试在 kubernetes 中部署 InfluxDB。

我可以正确创建所有内容,但当 influx 在 pod 中启动并运行时,我需要运行下面的自定义命令:

influx -username $INFLUXDB_ADMIN_USER -password $INFLUXDB_ADMIN_PASSWORD -execute "CREATE RETENTION POLICY metrics_yearly_rp ON $INFLUXDB_DB DURATION 52w REPLICATION 1"

或者我可以运行下面的curl命令:

curl -XPOST http://localhost:8086/query -u $INFLUXDB_ADMIN_USER:$INFLUXDB_ADMIN_PASSWORD --data-urlencode "q=CREATE RETENTION POLICY metrics_yearly_rp ON $INFLUXDB_DB DURATION 52w REPLICATION 1"

我尝试在 yaml 文件中配置命令,但没有成功。 这是我的 Pod 配置:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: influxdb-monitoringdb
  name: influxdb-monitoringdb
  namespace: mon-grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: influxdb-monitoringdb
  template:
    metadata:
      labels:
        app: influxdb-monitoringdb
      containers:
        - name: influxdb
          image: influxdb:1.8.10
          imagePullPolicy: IfNotPresent
          lifecycle:
            postStart:
              exec:
                command: ["/usr/bin/influx", "-username $(INFLUXDB_ADMIN_USER) -password $(INFLUXDB_ADMIN_PASSWORD) -execute \"CREATE RETENTION POLICY metrics_yearly_rp ON $(INFLUXDB_DB) DURATION 52w REPLICATION 1\""]
          env:
            - name: INFLUXDB_HTTP_AUTH_ENABLED
              value: "true"
            - name: INFLUXDB_DB
              valueFrom:
                secretKeyRef:
                  name: secrets
                  key: INFLUXDB_DB_monitoringdb
            - name: INFLUXDB_USER
              valueFrom:
                secretKeyRef:
                  name: secrets
                  key: INFLUXDB_USER_monitoringdb
            - name: INFLUXDB_USER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secrets
                  key: INFLUXDB_USER_PASSWORD_monitoringdb
            - name: INFLUXDB_ADMIN_USER
              valueFrom:
                secretKeyRef:
                  name: secrets
                  key: INFLUXDB_ADMIN_USER
            - name: INFLUXDB_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secrets
                  key: INFLUXDB_ADMIN_PASSWORD
          ports:
            - containerPort: 8086
              name: http-mondb
              protocol: TCP
          volumeMounts:
            - mountPath: /var/lib/influxdb
              name: influxdb-monitoringdb-pv
            - mountPath: /etc/influxdb/influxdb.conf
              name: configmap
              subPath: influxdb.conf
              readOnly: true
      volumes:
        - name: influxdb-monitoringdb-pv
          persistentVolumeClaim:
            claimName: influxdb-monitoringdb-pvc
        - name: configmap
          configMap:
            name: configmap

当我运行

Kubectl apply -f myfile.yaml
pod 不启动

NAME                          READY   STATUS             RESTARTS        AGE     IP           NODE   NOMINATED NODE   READINESS GATES
pod/influxdb-monitoringdb-0   0/1     CrashLoopBackOff   6 (3m20s ago)   9m50s   10.1.77.28   k8s    <none>           <none>

$ kubectl logs influxdb-monitoringdb-0 -n mon-grafana
ts=2023-12-21T12:15:47.082261Z lvl=info msg="InfluxDB starting" log_id=0mF4h5YG000 version=1.8.10 branch=1.8 commit=688e697c51fd
ts=2023-12-21T12:15:47.082353Z lvl=info msg="Go runtime" log_id=0mF4h5YG000 version=go1.13.8 maxprocs=4

$ kubectl describe pods influxdb-monitoringdb-0 -n mon-grafana
...
Events:
  Type     Reason               Age                    From               Message
  ----     ------               ----                   ----               -------
  Normal   Scheduled            4m48s                  default-scheduler  Successfully assigned mon-grafana/influxdb-monitoringdb-0 to k8s
  Normal   Pulled               3m33s (x4 over 4m46s)  kubelet            Container image "influxdb:1.8.10" already present on machine
  Normal   Created              3m33s (x4 over 4m45s)  kubelet            Created container influxdb
  Normal   Started              3m32s (x4 over 4m44s)  kubelet            Started container influxdb
  Warning  FailedPostStartHook  3m32s (x4 over 4m44s)  kubelet            PostStartHook failed
  Normal   Killing              3m32s (x4 over 4m44s)  kubelet            FailedPostStartHook
  Warning  BackOff              3m31s (x5 over 4m12s)  kubelet            Back-off restarting failed container influxdb in pod influxdb-monitoringdb-0_mon-grafana(f9c64666-9ea2-4d7a-88e6-895ef6274bd2)
kubernetes yaml kubectl
1个回答
0
投票

您可以使用“kubectl port-forward”通过本地主机上的隧道简单地公开 InfluxDB 端口

也就是说,

kubectl port-forward svc/[influx service name] 8086:8086

然后您应该能够按照您的预期使用卷曲。

curl -XPOST http://localhost:8086/query -u $INFLUXDB_ADMIN_USER:$INFLUXDB_ADMIN_PASSWORD --data-urlencode "q=CREATE RETENTION POLICY metrics_yearly_rp ON $INFLUXDB_DB DURATION 52w REPLICATION 1"
© www.soinside.com 2019 - 2024. All rights reserved.