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

suzh9iv8  于 5个月前  发布在  Kubernetes
关注(0)|答案(1)|浏览(56)

我正在尝试在Kubernetes中部署InfluxDB。
我可以正确地创建所有内容,但我需要在pod中启动并运行influx时运行下面的自定义命令:

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 conf:

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)

tzdcorbm

tzdcorbm1#

您可以使用kubectl port-forward命令通过本地主机上的隧道暴露InfluxDB端口。
也就是说,如果您的部署具有service定义。

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

字符串
否则,你甚至可以直接在吊舱上做一个隧道。

kubectl port-forward [influx pod name] 8086:8086


然后你应该能够从你的localhost使用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"

相关问题