报错提示如下:
C:\Users\jiangcheng>curl -k -H "Content-Type: application/json" -X PUT --data-binary @dev.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "the object provided is unrecognized (must be of type Namespace): couldn't get version/kind; json parse error: invalid character 'ÿ' looking for beginning of value (fffe7b000d000a0020002000200020002200610070006900560065007200 ...)", "reason": "BadRequest", "code": 400 }
解决方案:
参考github的issue,考虑是json的问题,建议是重新生成
Couldn't get version/kind; json parse error: unexpected end of JSON input · Issue #14396 · openshift/origin · GitHub
那就重新生成json,再执行吧
C:\Users\jiangcheng>rm dev01.json 'rm' is not recognized as an internal or external command, operable program or batch file. C:\Users\jiangcheng>del dev01.json C:\Users\jiangcheng>ls 'ls' is not recognized as an internal or external command, operable program or batch file. C:\Users\jiangcheng>curl -k -H "Content-Type:application/json" -X PUT --data-binary "@dev0001.json" http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "the object provided is unrecognized (must be of type Namespace): couldn't get version/kind; json parse error: invalid character 'ÿ' looking for beginning of value (fffe7b000d000a0020002000200020002200610070006900560065007200 ...)", "reason": "BadRequest", "code": 400 }
如文档所示,出现了问题
invalid character 'ÿ' looking for beginning of value (fffe7b000d000a0020002000200020002200610070006900560065007200 ...)
解决这个问题,先排查json文件的编码问题,参考 powershell 将txt文件从UTF-8转换为ASCII的简单方法 _大数据知识库
解决方案如下:
C:\Users\jiangcheng> (Get-Content -Raw "./dev0001.json") -cmatch '\P{IsBasicLatin}' False C:\Users\jiangcheng> (Get-Content -Raw ./dev0001.json) -cmatch '\P{IsBasicLatin}' False C:\Users\jiangcheng> 'café' | Out-File -Encoding Ascii temp05121830.txt C:\Users\jiangcheng> cat .\temp05121830.txt caf? C:\Users\jiangcheng> (Get-Content -Raw temp05121830.txt) -cmatch '\P{IsBasicLatin}' False C:\Users\jiangcheng> Get-Content -Path "./temp05121830.txt" | Out-File -FilePath "./temp05121830.txt" -Encoding ASCII C:\Users\jiangcheng> (Get-Content -Raw temp05121830.txt) -cmatch '\P{IsBasicLatin}' C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> (Get-Content -Raw ./dev0001.json) -cmatch '\P{IsBasicLatin}' False C:\Users\jiangcheng> Get-Content -Path "./dev0001.json" | Out-File -FilePath "./dev0002.json" -Encoding ASCII C:\Users\jiangcheng> (Get-Content -Path "./dev0002.json") -cmatch '\P{IsBasicLatin}' C:\Users\jiangcheng>
再执行命令
curl -k -H "Content-Type:application/json" -X PUT --data-binary "@dev0002.json" http://127.0.0.1:8081/api/v1/namespaces/dev/finalize
结果如下,改动是有效果的
执行
kubectl get ns
结果如图所示,
结果没有什么改变,说明删除namespace不成功,返回查看可能是命令行的问题,文件名使用了字符串,而字符串不是json,即使没有报错,仍然是不生效的,所以准备改为转义字符
curl -k -H "Content-Type:application/json" -X PUT --data-binary `@dev0002.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize
执行之后,报错如下所示
C:\Users\jiangcheng>curl -k -H "Content-Type:application/json" -X PUT --data-binary `@dev0002.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "the object provided is unrecognized (must be of type Namespace): couldn't get version/kind; json parse error: invalid character '`' looking for beginning of value (6040646576303030322e6a736f6e)", "reason": "BadRequest", "code": 400 }
这次报错提示的是转义字符的问题,需要修改转义字符,指令如下
curl -k -H "Content-Type:application/json" -X PUT --data-binary @dev0002.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize
执行效果如下
C:\Users\jiangcheng>curl -k -H "Content-Type:application/json" -X PUT --data-binary @dev0002.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "Operation cannot be fulfilled on namespaces \"dev\": the object has been modified; please apply your changes to the latest version and try again", "reason": "Conflict", "details": { "name": "dev", "kind": "namespaces" }, "code": 409 }
再修改一下 json文件,修改之前
{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2023-05-12T02:09:08Z", "labels": { "kubernetes.io/metadata.name": "dev" }, "name": "dev", "resourceVersion": "24023", "uid": "51588156-1758-4f51-a7de-f63febda769c" }, "spec": { }, "status": { "phase": "Active" } }
其中的 spec 字段是已经修改过的,现在将这个字段删除,再试试看
{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2023-05-12T02:09:08Z", "labels": { "kubernetes.io/metadata.name": "dev" }, "name": "dev", "resourceVersion": "24023", "uid": "51588156-1758-4f51-a7de-f63febda769c" }, "status": { "phase": "Active" } }
执行之后还是报这个错
根据建议重新生成,并删除spec试试
{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2023-05-12T02:09:08Z", "labels": { "kubernetes.io/metadata.name": "dev" }, "name": "dev", "resourceVersion": "62580", "uid": "51588156-1758-4f51-a7de-f63febda769c" }, "status": { "phase": "Active" } }
执行命令结果如下:
C:\Users\jiangcheng>kubectl get namespace dev -o json > dev.json C:\Users\jiangcheng>notepad dev.json C:\Users\jiangcheng>notepad dev.json C:\Users\jiangcheng>curl -k -H "Content-Type: application/json" -X PUT --data-binary @dev.json http://127.0.0.1:8001/api/v1/namespaces/dev/finalize curl: (7) Failed to connect to 127.0.0.1 port 8001 after 2048 ms: Couldn't connect to server C:\Users\jiangcheng>curl -k -H "Content-Type: application/json" -X PUT --data-binary @dev.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "dev", "uid": "51588156-1758-4f51-a7de-f63febda769c", "resourceVersion": "62580", "creationTimestamp": "2023-05-12T02:09:08Z", "labels": { "kubernetes.io/metadata.name": "dev" }, "managedFields": [ { "manager": "kubectl-create", "operation": "Update", "apiVersion": "v1", "time": "2023-05-12T02:09:08Z", "fieldsType": "FieldsV1", "fieldsV1": {"f:metadata":{"f:labels":{".":{},"f:kubernetes.io/metadata.name":{}}}} } ] }, "spec": { }, "status": { "phase": "Active" } }
执行结果,ns没有被删除
为什么执行成功了,但是namespace仍然没有被删除呢?我判断是状态的原因,因为状态仍然是Active,所以没有被删除掉
接下来,先执行删除命令
kubectl delete namespace dev
执行结果果然是卡住了
这个状态卡住了半天,再执行远程强制删除的命令,结果冲突了
最终我决定重新生成json,并重新执行整个删除操作,先查看namespace的状态,如下
kubectl get ns
状态如图所示
可以看到dev是Terminating的状态,可删除,重新生成json再执行
C:\Users\jiangcheng>del dev.json C:\Users\jiangcheng> C:\Users\jiangcheng>kubectl get namespace dev -o json > dev.json C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng>notepad dev.json C:\Users\jiangcheng>curl -k -H "Content-Type: application/json" -X PUT --data-binary @dev.json http://127.0.0.1:8081/api/v1/namespaces/dev/finalize { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "dev", "uid": "51588156-1758-4f51-a7de-f63febda769c", "resourceVersion": "66013", "creationTimestamp": "2023-05-12T02:09:08Z", "deletionTimestamp": "2023-05-12T11:20:37Z", "labels": { "kubernetes.io/metadata.name": "dev" }, "managedFields": [ { "manager": "kubectl-create", "operation": "Update", "apiVersion": "v1", "time": "2023-05-12T02:09:08Z", "fieldsType": "FieldsV1", "fieldsV1": {"f:metadata":{"f:labels":{".":{},"f:kubernetes.io/metadata.name":{}}}} } ] }, "spec": { }, "status": { "phase": "Terminating" } } C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng> C:\Users\jiangcheng>kubectl get ns NAME STATUS AGE default Active 24h ingress-nginx Active 24h kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h local-path-storage Active 24h
可以看到这次删除终于成功了