如何批量删除k8s资源对象
程序员文章站
2022-05-09 11:30:43
本文首发于公众号【我的小碗汤】扫描文末二维码关注,一起交流学习 在云平台开发、中间件容器化时,经常会遇到批量删除k8s资源对象的需求,下面记录一下kubectl和golang发送删除pvc、pv、pod请求的例子,便于后续学习查阅 kubectl发送删除请求 根据label批量删除pod: 根据la ......
本文首发于公众号【我的小碗汤】扫描文末二维码关注,一起交流学习
在云平台开发、中间件容器化时,经常会遇到批量删除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文档:
本公众号免费提供csdn下载服务,海量it学习资源,如果你准备入it坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java、go、python、springcloud、elk、嵌入式 、大数据、面试资料、前端 等资源。同时我们组建了一个技术交流群,里面有很多大佬,会不定时分享技术文章,如果你想来一起学习提高,可以公众号后台回复【2】,免费邀请加技术交流群互相学习提高,会不定期分享编程it相关资源。
扫码关注,精彩内容第一时间推给你