本文首发于公众号【我的小碗汤】扫描文末二维码关注,一起交流学习
在云平台开发、中间件容器化时,经常会遇到批量删除 k8s 资源对象的需求,下面记录一下 kubectl 和 golang 发送删除 pvc、pv、pod 请求的例子,便于后续学习查阅
kubectl 发送删除请求
根据 label 批量删除 pod:
kubectl delete pod -n kube-system -l "harmonycloud.cn/statefulset=redis-ll-1010-a"
根据 label 批量删除 pvc:
kubectl delete pvc -n kube-system -l "harmonycloud.cn/statefulset=redis-ll-1010-a"
根据 label 批量删除 pv:
kubectl delete pv -l "harmonycloud.cn/statefulset=redis-ll-1010-a"
golang 发送删除请求
根据 label 批量删除 pvc、pod、pv
注意:启动参数中加入以下参数:
--kubeconfig=/root/.kube/config --v=5
package operator
import (
"flag"
extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/util/logs"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"os"
"testing"
)
type OperatorManagerServer struct {
Master string
Kubeconfig string
}
func NewOMServer() *OperatorManagerServer {s := OperatorManagerServer{}
return &s
}
var s *OperatorManagerServer
func init() {s = NewOMServer()
flag.StringVar(&s.Master, "master", s.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
flag.StringVar(&s.Kubeconfig, "kubeconfig", s.Kubeconfig, "Path to kubeconfig file with authorization and master location information.")
// 初始化 klog 等 flag
logs.InitLogs()
flag.Parse()}
func Test_DeleteCollection(t *testing.T) {if err := Run(s); err != nil {t.Fatalf("%v\n", err)
os.Exit(1)
}
}
func Run(s *OperatorManagerServer) error {
var (
generalLabelKey = "harmonycloud.cn/statefulset"
redisClusterName = "redis-ll-1010"
redisClusterNamespace = "kube-system"
)
kubeClient, _, _, err := createClients(s)
if err != nil {return err}
// 根据 label 批量删除 pod
labelPod := labels.SelectorFromSet(labels.Set(map[string]string{generalLabelKey: redisClusterName}))
listPodOptions := metav1.ListOptions{LabelSelector: labelPod.String(),
}
err = kubeClient.CoreV1().Pods(redisClusterNamespace).DeleteCollection(&metav1.DeleteOptions{}, listPodOptions)
if err != nil {if !errors.IsNotFound(err) {klog.Errorf("Drop RedisCluster: %v/%v pod error: %v", redisClusterNamespace, redisClusterName, err)
return err
}
}
// 根据 label 批量删除 pvc
labelPvc := labels.SelectorFromSet(labels.Set(map[string]string{"app": redisClusterName}))
listPvcOptions := metav1.ListOptions{LabelSelector: labelPvc.String(),
}
err = kubeClient.CoreV1().PersistentVolumeClaims(redisClusterNamespace).DeleteCollection(&metav1.DeleteOptions{}, listPvcOptions)
if err != nil {if !errors.IsNotFound(err) {klog.Errorf("Drop RedisCluster: %v/%v pvc error: %v", redisClusterNamespace, redisClusterName, err)
return err
}
}
// 如果 pv 没有删除掉, 则删除
labelPv := labels.SelectorFromSet(labels.Set(map[string]string{generalLabelKey: redisClusterName}))
listPvOptions := metav1.ListOptions{LabelSelector: labelPv.String(),
}
err = kubeClient.CoreV1().PersistentVolumes().DeleteCollection(&metav1.DeleteOptions{}, listPvOptions)
if err != nil {if !errors.IsNotFound(err) {klog.Errorf("Drop RedisCluster: %v/%v pv error: %v", redisClusterNamespace, redisClusterName, err)
return err
}
}
return nil
}
// 根据 kubeconfig 文件创建客户端
func createClients(s *OperatorManagerServer) (*clientset.Clientset, *extensionsclient.Clientset, *restclient.Config, error) {kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.Kubeconfig)
if err != nil {return nil, nil, nil, err}
kubeconfig.QPS = 100
kubeconfig.Burst = 100
kubeClient, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, "operator-manager"))
if err != nil {klog.Fatalf("Invalid API configuration: %v", err)
}
extensionClient, err := extensionsclient.NewForConfig(restclient.AddUserAgent(kubeconfig, "operator-manager"))
if err != nil {klog.Fatalf("Invalid API configuration: %v", err)
}
return kubeClient, extensionClient, kubeconfig, nil
}
client-go 中提供的
- Delete 方法, 只能删除单个资源对象, 第一个参数往往是资源对象名称, 第二个参数是删除选项,如:优雅终止时间 GracePeriodSeconds、删除传播策略:Foreground 前台删除、后台删除:Background、孤儿删除:Orphan
- DeleteCollection 方法第一个参数是删除选项,第二个参数是删除条件,包括 label Selector、field Selector 等
Delete(name string, options *metav1.DeleteOptions) error
DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error
参考
k8s 官方 API 文档:
https://kubernetes.io/docs/re…
本公众号 免费 提供 csdn 下载服务,海量 IT 学习资源,如果你准备入 IT 坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于 java、go、python、springcloud、elk、嵌入式、大数据、面试资料、前端 等资源。同时我们组建了一个技术交流群,里面有很多大佬,会不定时分享技术文章,如果你想来一起学习提高,可以公众号后台回复【2】,免费邀请加技术交流群互相学习提高,会不定期分享编程 IT 相关资源。
扫码关注,精彩内容第一时间推给你