我有一个“卡住”的名称空间,我删除显示在这个永恒的“终止”状态。
当前回答
你可以运行一些东西。但这通常意味着,命名空间的自动删除无法完成,并且有一个正在运行的进程必须手动删除。要找到它,你可以做这些事情:
把所有的资料都集中到这个名字空间。如果这没有任何结果,继续下一个建议
$ kubectl get all -n your-namespace
一些命名空间附加了apiserivces,删除起来很麻烦。这可以是你想要的任何资源。然后,如果发现任何信息,就删除该资源
$ kubectl get apiservice|grep False
但主要的收获是,可能有一些东西没有完全去除。因此,您可以看到最初在该名称空间中有什么,然后可以看到yaml旋转了哪些内容以查看进程。或者你可以开始谷歌为什么不适当删除服务X,你会发现事情。
其他回答
最简单的方法就是复制这个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命令中。
我尝试了3-5个选项来删除ns,但只有这一个对我有用。
这个sh文件将删除所有处于终止状态的名称空间
$ 我们 force-delete-namespaces.sh
$chmod +x force-delete-namespaces.sh
美元。/ force-delete-namespaces.sh
#!/usr/bin/env bash
set -e
set -o pipefail
kubectl proxy &
proxy_pid="$!"
trap 'kill "$proxy_pid"' EXIT
for ns in $(kubectl get namespace --field-selector=status.phase=Terminating --output=jsonpath="{.items[*].metadata.name}"); do
echo "Removing finalizers from namespace '$ns'..."
curl -H "Content-Type: application/json" -X PUT "127.0.0.1:8001/api/v1/namespaces/$ns/finalize" -d @- \
< <(kubectl get namespace "$ns" --output=json | jq '.spec = { "finalizers": [] }')
echo
echo "Force-deleting namespace '$ns'..."
kubectl delete namespace "$ns" --force --grace-period=0 --ignore-not-found=true
done
将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,并再次尝试上述步骤。
需要移除kubernetes的终结器。
步骤1:
kubectl get namespace <YOUR_NAMESPACE> -o json > <YOUR_NAMESPACE>.json
将kubernetes从finalizers数组中移除
步骤2:
kubectl replace --raw "/api/v1/namespaces/<YOUR_NAMESPACE>/finalize" -f ./<YOUR_NAMESPACE>.json
步骤3:
kubectl get namespace
您可以看到烦人的名称空间消失了。
我发现删除“终止”名称空间的唯一方法是删除“终结器”部分中的条目。我试过——强制删除它和——grace-period=0没有一个工作,但是,这个方法做到了:
在命令行中显示命名空间的信息:
$ kubectl get namespace your-rogue-namespace -o yaml
这将给你yaml输出,寻找类似于这样的一行:
deletionTimestamp: 2018-09-17T13:00:10Z
finalizers:
- Whatever content it might be here...
labels:
然后只需编辑名称空间配置并删除终结器容器中的项。
$ kubectl edit namespace your-rogue-namespace
这将打开一个编辑器(在我的例子中是VI),浏览我想删除的行并删除它,我按D键两次删除整行。
保存它,退出编辑器,就像变魔术一样。rogue-namespace应该消失了。
为了证实这一点:
$ kubectl get namespace your-rogue-namespace -o yaml
推荐文章
- 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+)中做什么?
- 位于另一个名称空间中的服务