diff --git a/deploy/cloud/manifests/clear-mongo-log.yaml b/deploy/cloud/manifests/clear-mongo-log.yaml new file mode 100644 index 00000000000..0006f57277f --- /dev/null +++ b/deploy/cloud/manifests/clear-mongo-log.yaml @@ -0,0 +1,69 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: clear-mongo-logs-sa + namespace: sealos +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: clear-mongo-logs-role + namespace: sealos +rules: + - apiGroups: ["apps.kubeblocks.io"] + resources: ["clusters"] + verbs: ["get", "list", "watch"] + - apiGroups: [""] + resources: ["pods","pods/exec"] + verbs: ["*"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: clear-mongo-logs-rolebinding + namespace: sealos +subjects: + - kind: ServiceAccount + name: clear-mongo-logs-sa + namespace: sealos +roleRef: + kind: ClusterRole + name: clear-mongo-logs-role + apiGroup: rbac.authorization.k8s.io +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: clear-mongo-logs + namespace: sealos +spec: + schedule: "0 0 * * *" + jobTemplate: + spec: + template: + spec: + serviceAccountName: clear-mongo-logs-sa + containers: + - name: clear-logs + image: bitnami/kubectl:1.28 + command: + - /bin/bash + - -c + - | + clusters=$(kubectl get cluster.apps.kubeblocks.io -A -l clusterdefinition.kubeblocks.io/name=mongodb) + echo "$clusters" | while read -r line; do + namespace=$(echo "$line" | awk '{print $1}') + cluster_name=$(echo "$line" | awk '{print $2}') + pods=$(kubectl get pod -n "${namespace}" -o=custom-columns=NAME:.metadata.name,NODE:.spec.nodeName | grep "${cluster_name}") + echo "$pods" | while read -r pod_line; do + pod_name=$(echo "$pod_line" | awk '{print $1}') + if [ -n "$pod_name" ]; then + kubectl exec -it ${pod_name} -n ${namespace} -- /bin/bash -c 'if [ -d /data/mongodb/logs ]; then cat /dev/null > /data/mongodb/logs/mongodb.log; fi' + fi + done + done + securityContext: + runAsUser: 0 + allowPrivilegeEscalation: false + runAsNonRoot: true + restartPolicy: OnFailure \ No newline at end of file diff --git a/deploy/cloud/scripts/init.sh b/deploy/cloud/scripts/init.sh index e1795eb750a..e4c6cb4f34f 100644 --- a/deploy/cloud/scripts/init.sh +++ b/deploy/cloud/scripts/init.sh @@ -23,6 +23,9 @@ function prepare { # apply notifications crd kubectl apply -f manifests/notifications_crd.yaml + # clear mongo log by cronjob,temporary solution + kubectl apply -f manifests/clear-mongo-log.yaml + # gen mongodb uri gen_mongodbUri