大网数据广告活动
服务器运维问答

How to delete all namespaces except the kube-system in K3s cluster

· 52CE 我爱测

I have a K3s cluster with system pods (i.e. kube-system namespace) and my application pods: kube-system pod/calico-node-xxxx kube-system pod/calico-kube-controllers-xxxxxx kube-system pod/metrics-server-xxxxx kube-system pod/loca…

问题原文

原帖链接 · solveit · CC BY-SA 4.0

I have a K3s cluster with system pods (i.e. kube-system namespace) and my application pods:

kube-system   pod/calico-node-xxxx                          
kube-system   pod/calico-kube-controllers-xxxxxx   
kube-system   pod/metrics-server-xxxxx
kube-system   pod/local-path-provisioner-xxxxx
kube-system   pod/coredns-xxxxx
app-system    pod/my-app-xxxx
db-system     pod/my-db-xxxxx

I am looking for a shell/kubectl command (for automation script) that can delete my application namespace (want to modify kubectl delete namespace app-system db-system) without mentioning the app namespaces name in the command (as in future if more app namespace would come in cluster, I have to edit this script every time).

That means I want to delete all namespace in cluster except kube-system

something like - kubectl delete namespace -v kube-system (I know -v is not a valid parameter here, just showing how in grep -v is used to except the following words. Similar thing looking for kubectl delete ns... )

已采纳解答

原帖链接 · Valentin Bajrami · CC BY-SA 4.0

I've created the following code , so you can use it as a wrapper. You can name the script whatever you want. For example exclude_ns_removal

#!/usr/bin/env bash

die () 
{ 
    echo "$@" 1>&2
    exit 1
}

usage () 
{ 
    echo "usage: $0 [-h] [-v namespace_to_ignore] " 1>&2
    exit 0
}

inarray () 
{ 
    local n=$1 h
    shift
    for h in "$@"
    do
        [[ $n = "$h" ]] && return
    done
    return 1
}

while getopts ":v:h" opt; do
    case $opt in 
        h)
            usage
        ;;
        v)
            case $OPTARG in 
                '' | *[0-9]*)
                    die "Digits not allowed $OPTARG"
                ;;
                *)
                    val=$OPTARG
                ;;
            esac
        ;;
        :)
            die "argument needed to -$OPTARG"
        ;;
        *)
            die "invalid switch -$OPTARG"
        ;;
    esac
done

shift $((OPTIND - 1))

while IFS='/' read -r _ ns; do
    a+=("$ns")
done < <(kubectl get namespaces --no-headers -o name)

if inarray "$val" "${a[@]}"; then
    unset 'a'
    { 
        while IFS='/' read -r _ ns; do
            a+=("$ns")
            for i in "${!a[@]}"
            do
                if [[ ${a[i]} == $val ]]; then
                    unset 'a[i]'
                fi
            done
        done
    } < <(kubectl get namespaces --no-headers -o name)

    printf '%s\n\n' "Excluding ... $val"
    for namespace in "${a[@]}"
    do
        printf 'Deleting ... %s\n' "$namespace"
    done
else
    die "No namespace found"
fi

Make the script executable:

chmod u+x exclude_ns_removal

Run it as follows:

./exclude_ns_removal -v kube-system

The result will be something like:

Excluding ... kube-system

Deleting ... app-system
Deleting ... db-system

If the output looks good, you should modify this line

printf 'Deleting ... %s\n' "$namespace"

to

kubectl delete namespace "$namespace"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-09-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。