如何批量删除k8s资源对象

本文首发于公众号【我的小碗汤】扫描文末二维码关注,一起交流学习

在云平台开发、中间件容器化时,经常会遇到批量删除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相关资源。

扫码关注,精彩内容第一时间推给你

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章