我有一个“卡住”的名称空间,我删除显示在这个永恒的“终止”状态。
当前回答
这是由于名称空间控制器无法删除名称空间中仍然存在的资源。
这个命令(使用kubectl 1.11+)将显示名称空间中保留的资源:
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace>
一旦找到并解析并删除这些名称空间,该名称空间就会被清理
其他回答
最简单的方法就是复制这个bash脚本
#!/bin/bash
###############################################################################
# Copyright (c) 2018 Red Hat Inc
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
###############################################################################
set -eo pipefail
die() { echo "$*" 1>&2 ; exit 1; }
need() {
which "$1" &>/dev/null || die "Binary '$1' is missing but required"
}
# checking pre-reqs
need "jq"
need "curl"
need "kubectl"
PROJECT="$1"
shift
test -n "$PROJECT" || die "Missing arguments: kill-ns <namespace>"
kubectl proxy &>/dev/null &
PROXY_PID=$!
killproxy () {
kill $PROXY_PID
}
trap killproxy EXIT
sleep 1 # give the proxy a second
kubectl get namespace "$PROJECT" -o json | jq 'del(.spec.finalizers[] | select("kubernetes"))' | curl -s -k -H "Content-Type: application/json" -X PUT -o /dev/null --data-binary @- http://localhost:8001/api/v1/namespaces/$PROJECT/finalize && echo "Killed namespace: $PROJECT"
# proxy will get killed by the trap
在deletenamepsace.sh文件中添加上述代码。
然后通过提供命名空间作为参数执行它(linkerd是我想在这里删除的命名空间)
➜ kubectl get namespaces
linkerd Terminating 11d
➜ sh deletenamepsace.sh linkerd
Killed namespace: linkerd
➜ kubectl get namespaces
上面的建议对我很有效。
老实说,我认为 删除命名空间mynamespace——grace-period=0——force 根本不值得一试。
特别感谢Jens Reimann!我认为这个脚本应该被合并到kubectl命令中。
将ambassador替换为您的名称空间
检查名称空间是否卡住
kubectl get ns ambassador
NAME STATUS AGE
ambassador Terminating 110d
这个卡了很久了
打开管理终端/cmd提示符或powershell并运行
kubectl代理
这将启动本地web服务器
打开另一个终端并运行
kubectl get ns ambassador -o json >tmp.json
编辑tmp文件。Json使用vi或nano
从这个
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"ambassador\"}}\n"
},
"creationTimestamp": "2021-01-07T18:23:28Z",
"deletionTimestamp": "2021-04-28T06:43:41Z",
"name": "ambassador",
"resourceVersion": "14572382",
"selfLink": "/api/v1/namespaces/ambassador",
"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"conditions": [
{
"lastTransitionTime": "2021-04-28T06:43:46Z",
"message": "Discovery failed for some groups, 3 failing: unable to retrieve the complete list of server APIs: compose.docker.com/v1alpha3: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1alpha3?timeout=32s\\\": Post https://0.0.0.1:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 0.0.0.0:53284-\u0026gt;0.0.0.0:443: write: broken pipe\") has prevented the request from succeeding, compose.docker.com/v1beta1: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1beta1?timeout=32s\\\": Post https://10.96.0.1:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 0.0.0.0:5284-\u0026gt;10.96.0.1:443: write: broken pipe\") has prevented the request from succeeding, compose.docker.com/v1beta2: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1beta2?timeout=32s\\\": Post https://0.0.0.0:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 1.1.1.1:2284-\u0026gt;0.0.0.0:443: write: broken pipe\") has prevented the request from succeeding",
"reason": "DiscoveryFailed",
"status": "True",
"type": "NamespaceDeletionDiscoveryFailure"
},
{
"lastTransitionTime": "2021-04-28T06:43:49Z",
"message": "All legacy kube types successfully parsed",
"reason": "ParsedGroupVersions",
"status": "False",
"type": "NamespaceDeletionGroupVersionParsingFailure"
},
{
"lastTransitionTime": "2021-04-28T06:43:49Z",
"message": "All content successfully deleted",
"reason": "ContentDeleted",
"status": "False",
"type": "NamespaceDeletionContentFailure"
}
],
"phase": "Terminating"
}
}
to
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"ambassador\"}}\n"
},
"creationTimestamp": "2021-01-07T18:23:28Z",
"deletionTimestamp": "2021-04-28T06:43:41Z",
"name": "ambassador",
"resourceVersion": "14572382",
"selfLink": "/api/v1/namespaces/ambassador",
"uid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"spec": {
"finalizers": []
}
}
通过删除终结器中的状态和kubernetes
现在使用该命令并用您的名称空间替换ambassador
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/ambassador/finalize
运行之前,您将看到另一个json
然后运行命令
kubectl get ns ambassador
Error from server (NotFound): namespaces "ambassador" not found
如果它仍然显示终止或任何其他错误,请确保以适当的方式格式化json,并再次尝试上述步骤。
我写了一个简单的脚本,删除您卡住的名称空间基于@Shreyangi Saxena的解决方案。
cat > delete_stuck_ns.sh << "EOF"
#!/usr/bin/env bash
function delete_namespace () {
echo "Deleting namespace $1"
kubectl get namespace $1 -o json > tmp.json
sed -i 's/"kubernetes"//g' tmp.json
kubectl replace --raw "/api/v1/namespaces/$1/finalize" -f ./tmp.json
rm ./tmp.json
}
TERMINATING_NS=$(kubectl get ns | awk '$2=="Terminating" {print $1}')
for ns in $TERMINATING_NS
do
delete_namespace $ns
done
EOF
chmod +x delete_stuck_ns.sh
该脚本可以检测到所有处于终止状态的命名空间,并将其删除。
PS:
这可能在MacOS中不起作用,因为MacOS中的本机sed与GNU sed不兼容。 你可能需要在MacOS中安装GNU sed,请参考这个答案。 请确认您可以通过命令kubectl访问您的kubernetes集群。 已在kubernetes v1.15.3版本上测试
更新
我找到了一个更简单的解决办法:
kubectl patch RESOURCE NAME -p '{"metadata":{"finalizers":[]}}' --type=merge
kubectl edit namespace ${stucked_namespace}
然后在vi模式下删除终结器并保存。
这对我来说是有效的。
使用实例查看处于“终止”状态的命名空间。 Kubectl获取命名空间 选择一个终止名称空间并查看该名称空间的内容以找出终止器。执行如下命令: Kubectl获取命名空间-o yaml 您的YAML内容可能类似于以下输出:
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: 2019-12-25T17:38:32Z
deletionTimestamp: 2019-12-25T17:51:34Z
name: <terminating-namespace>
resourceVersion: "4779875"
selfLink: /api/v1/namespaces/<terminating-namespace>
uid: ******-****-****-****-fa1dfgerz5
spec:
finalizers:
- kubernetes
status:
phase: Terminating
运行如下命令创建一个临时JSON文件: 获取命名空间-o json >tmp.json 编辑tmp。json文件。从finalizers字段中删除kubernetes值并保存文件。输出如下:
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"creationTimestamp": "2018-11-19T18:48:30Z",
"deletionTimestamp": "2018-11-19T18:59:36Z",
"name": "<terminating-namespace>",
"resourceVersion": "1385077",
"selfLink": "/api/v1/namespaces/<terminating-namespace>",
"uid": "b50c9ea4-ec2b-11e8-a0be-fa163eeb47a5"
},
"spec": {
},
"status": {
"phase": "Terminating"
}
}
使用实例设置临时代理IP和端口。请确保您的终端窗口一直打开,直到您删除卡住的命名空间: kubectl代理 您的代理IP和端口可能类似于以下输出: 开始在127.0.0.1:8001上播放 在一个新的终端窗口中,使用临时代理IP和端口进行API调用:
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/your_terminating_namespace/finalize
你的输出是这样的:
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "<terminating-namespace>",
"selfLink": "/api/v1/namespaces/<terminating-namespace>/finalize",
"uid": "b50c9ea4-ec2b-11e8-a0be-fa163eeb47a5",
"resourceVersion": "1602981",
"creationTimestamp": "2018-11-19T18:48:30Z",
"deletionTimestamp": "2018-11-19T18:59:36Z"
},
"spec": {
},
"status": {
"phase": "Terminating"
}
}
终结器形参被移除。现在,验证终止的命名空间已被删除,运行以下命令: Kubectl获取命名空间
推荐文章
- Printf与std::字符串?
- Kubectl连续日志
- 我的kubernetes豆荚不断崩溃与“CrashLoopBackOff”,但我找不到任何日志
- 如何从kubectl配置中删除集群和上下文?
- Kubernetes支持多个环境(Staging、QA、生产等)
- CSS中*和*|*的区别是什么?
- Kubernetes API -获取特定节点上的pod
- 我如何调试“ImagePullBackOff”?
- Django:“projects”vs“apps”
- 在Kubernetes中更新configmap时重新启动pod ?
- 输入对象的datetime。Datetime没有Datetime属性
- 为部署的Kubernetes服务获取YAML ?
- 如何在kubernetes中切换命名空间
- \(反斜杠)在PHP(5.3+)中做什么?
- 位于另一个名称空间中的服务