From ec787c894f7880b0d4e1b12a73e9e3c07b500f04 Mon Sep 17 00:00:00 2001 From: 24sama Date: Tue, 22 Nov 2022 10:22:01 +0800 Subject: [PATCH] [release-1.1] Fix etcd script to delete other files when there is no space left on device Signed-off-by: 24sama --- apis/kubekey/v1alpha1/default.go | 5 +++-- pkg/cluster/delete/delete.go | 9 ++++++--- pkg/etcd/tmpl/etcdbackup.go | 11 ++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apis/kubekey/v1alpha1/default.go b/apis/kubekey/v1alpha1/default.go index f40e77f74..d20df3753 100644 --- a/apis/kubekey/v1alpha1/default.go +++ b/apis/kubekey/v1alpha1/default.go @@ -21,8 +21,9 @@ import ( "os" "strings" - "github.com/kubesphere/kubekey/pkg/util" log "github.com/sirupsen/logrus" + + "github.com/kubesphere/kubekey/pkg/util" ) const ( @@ -60,7 +61,7 @@ const ( Worker = "worker" K8s = "k8s" DefaultEtcdBackupDir = "/var/backups/kube_etcd" - DefaultEtcdBackupPeriod = 30 + DefaultEtcdBackupPeriod = 720 DefaultKeepBackNumber = 5 DefaultEtcdBackupScriptDir = "/usr/local/bin/kube-scripts" DefaultJoinCIDR = "100.64.0.0/16" diff --git a/pkg/cluster/delete/delete.go b/pkg/cluster/delete/delete.go index f05a441b1..71b0f2f25 100644 --- a/pkg/cluster/delete/delete.go +++ b/pkg/cluster/delete/delete.go @@ -19,7 +19,6 @@ package delete import ( "bufio" "fmt" - "github.com/kubesphere/kubekey/pkg/util" "os" "os/exec" "path/filepath" @@ -27,12 +26,14 @@ import ( "strings" "unicode" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + kubekeyapiv1alpha1 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha1" "github.com/kubesphere/kubekey/pkg/config" + "github.com/kubesphere/kubekey/pkg/util" "github.com/kubesphere/kubekey/pkg/util/executor" "github.com/kubesphere/kubekey/pkg/util/manager" - "github.com/pkg/errors" - log "github.com/sirupsen/logrus" ) func ResetCluster(clusterCfgFile string, logger *log.Logger, verbose bool) error { @@ -263,6 +264,8 @@ var ( "/etc/etcd.env", "/etc/kubernetes", "/etc/systemd/system/etcd.service", + "/etc/systemd/system/backup-etcd.service", + "/etc/systemd/system/backup-etcd.timer", "/var/log/calico", "/etc/cni", "/var/log/pods/", diff --git a/pkg/etcd/tmpl/etcdbackup.go b/pkg/etcd/tmpl/etcdbackup.go index 3712f9815..859724072 100644 --- a/pkg/etcd/tmpl/etcdbackup.go +++ b/pkg/etcd/tmpl/etcdbackup.go @@ -6,16 +6,21 @@ import ( "strconv" "text/template" + "github.com/lithammer/dedent" + kubekeyapiv1alpha1 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha1" "github.com/kubesphere/kubekey/pkg/util" "github.com/kubesphere/kubekey/pkg/util/manager" - "github.com/lithammer/dedent" ) // EtcdBackupScriptTmpl defines the template of etcd backup script. var EtcdBackupScriptTmpl = template.Must(template.New("etcdBackupScript").Parse( dedent.Dedent(`#!/bin/bash +set -o errexit +set -o nounset +set -o pipefail + ETCDCTL_PATH='/usr/local/bin/etcdctl' ENDPOINTS='{{ .Etcdendpoint }}' ETCD_DATA_DIR="/var/lib/etcd" @@ -44,7 +49,7 @@ export ETCDCTL_API=3;$ETCDCTL_PATH --endpoints="$ENDPOINTS" snapshot save $BACKU sleep 3 -cd $BACKUP_DIR/../;ls -lt |awk '{if(NR > '$KEEPBACKUPNUMBER'){print "rm -rf "$9}}'|sh +cd $BACKUP_DIR/../ && ls -lt |awk '{if(NR > '$KEEPBACKUPNUMBER'){print "rm -rf "$9}}'|sh if [[ ! $ETCDBACKUPHOUR ]]; then time="*/$ETCDBACKUPPERIOD * * * *" @@ -82,7 +87,7 @@ func EtcdBackupScript(mgr *manager.Manager, node *kubekeyapiv1alpha1.HostCfg) (s "Hostname": node.Name, "Etcdendpoint": fmt.Sprintf("https://%s:2379", node.InternalAddress), "Backupdir": mgr.Cluster.Kubernetes.EtcdBackupDir, - "KeepbackupNumber": mgr.Cluster.Kubernetes.KeepBackupNumber, + "KeepbackupNumber": mgr.Cluster.Kubernetes.KeepBackupNumber + 1, "EtcdBackupPeriod": mgr.Cluster.Kubernetes.EtcdBackupPeriod, "EtcdBackupScriptDir": mgr.Cluster.Kubernetes.EtcdBackupScriptDir, "EtcdBackupHour": etcdBackupHour,