• 想删除K8S当中的一个名称空间

      使用如下命令删除一直卡着,手动终止之后发现名称空间为”Terminating”状态

    1. [root@k8s-master01 storage]# kubectl get ns -A
    2. NAME STATUS AGE
    3. default Active 3d6h
    4. kube-node-lease Active 3d6h
    5. kube-public Active 3d6h
    6. kube-system Active 3d6h
    7. kubesphere-alerting-system Active 4h12m
    8. kubesphere-controls-system Active 2d5h
    9. kubesphere-devops-system Active 4h12m
    10. kubesphere-logging-system Active 4h12m
    11. kubesphere-monitoring-system Active 4h55m
    12. kubesphere-system Terminating 4h12m
    13. openpitrix-system Active 4h12m
    14. [root@k8s-master01 storage]#
    15. #使用如下命令删除发现名称空间为"Terminating"
    16. [root@k8s-master01 storage]# kubectl delete ns kubesphere-alerting-system
    17. namespace "kubesphere-alerting-system" deleted
    18. ^C
    19. [root@k8s-master01 storage]# kubectl get ns -A
    20. NAME STATUS AGE
    21. default Active 3d6h
    22. kube-node-lease Active 73s
    23. kube-public Active 3d6h
    24. kube-system Active 3d6h
    25. kubesphere-alerting-system Terminating 4h15m
    26. kubesphere-controls-system Active 2d6h
    27. kubesphere-devops-system Active 4h15m
    28. kubesphere-logging-system Active 4h15m
    29. kubesphere-monitoring-system Active 4h58m
    30. kubesphere-system Terminating 4h15m
    31. openpitrix-system Active 4h15m
    32. [root@k8s-master01 storage]#
    33. #试一下强制删除的方法,同样是"Terminating"状态
    34. [root@k8s-master01 storage]# kubectl delete ns kubesphere-controls-system --force --grace-period=0
    35. warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
    36. namespace "kubesphere-controls-system" force deleted
    37. ^C
    38. [root@k8s-master01 storage]#
    39. [root@k8s-master01 storage]# kubectl get ns kubesphere-controls-system
    40. NAME STATUS AGE
    41. kubesphere-controls-system Terminating 2d6h
    42. [root@k8s-master01 storage]#
    • 使用如下方法

      网上搜索了一下,可以使用以下方法删除

    1. kubectl get namespace 命名空间的名字 -o json > devtesting.json
    2. #示例
    3. [root@k8s-master01 storage]# kubectl get ns kubesphere-alerting-system -o json > devtesting.json
    4. [root@k8s-master01 storage]#

    编辑devtesting.json文件,删除specfinalizers这两个字段包含的内容。

    1. "spec": {
    2. "finalizers": [
    3. "kubernetes"
    4. ]
    5. },
    6. #改为
    7. "spec": {
    8. },

    使用kubectl代理,执行命名

    1. [root@k8s-master01 storage]# kubectl proxy --port=8081
    2. Starting to serve on 127.0.0.1:8081
    3. #再开一个窗口试下能不能访问
    4. [root@k8s-master01 storage]# curl http://localhost:8081/api
    5. {
    6. "kind": "APIVersions",
    7. "versions": [
    8. "v1"
    9. ],
    10. "serverAddressByClientCIDRs": [
    11. {
    12. "clientCIDR": "0.0.0.0/0",
    13. "serverAddress": "192.168.1.8:6443"
    14. }
    15. ]
    16. }[root@k8s-master01 storage]#

    使用http接口进行删除

    1. curl -k -H "Content-Type: application/json" -X PUT --data-binary @devtesting.json http://127.0.0.1:8080/api/v1/namespaces/命名空间的名字/finalize
    2. #示例
    3. [root@k8s-master01 storage]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @devtesting.json http://127.0.0.1:8081/api/v1/namespaces/kubesphere-alerting-system/finalize
    4. {
    5. "kind": "Namespace",
    6. "apiVersion": "v1",
    7. "metadata": {
    8. "name": "kubesphere-alerting-system",
    9. "selfLink": "/api/v1/namespaces/kubesphere-alerting-system/finalize",
    10. "uid": "966d12a7-2de6-49ac-956d-811742764f41",
    11. "resourceVersion": "374909",
    12. "creationTimestamp": "2020-11-22T10:09:02Z",
    13. "deletionTimestamp": "2020-11-22T14:22:36Z",
    14. "labels": {
    15. "kubesphere.io/namespace": "kubesphere-alerting-system",
    16. "kubesphere.io/workspace": "system-workspace"
    17. }
    18. },
    19. "spec": {
    20. },
    21. "status": {
    22. "phase": "Terminating",
    23. "conditions": [
    24. {
    25. "type": "NamespaceDeletionDiscoveryFailure",
    26. "status": "True",
    27. "lastTransitionTime": "2020-11-22T14:22:41Z",
    28. "reason": "DiscoveryFailed",
    29. "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request"
    30. },
    31. {
    32. "type": "NamespaceDeletionGroupVersionParsingFailure",
    33. "status": "False",
    34. "lastTransitionTime": "2020-11-22T14:22:41Z",
    35. "reason": "ParsedGroupVersions",
    36. "message": "All legacy kube types successfully parsed"
    37. },
    38. {
    39. "type": "NamespaceDeletionContentFailure",
    40. "status": "False",
    41. "lastTransitionTime": "2020-11-22T14:22:41Z",
    42. "reason": "ContentDeleted",
    43. "message": "All content successfully deleted"
    44. }
    45. ]
    46. }
    47. }
    48. [root@k8s-master01 storage]#
    49. [root@k8s-master01 storage]# kubectl get ns kubesphere-alerting-system
    50. Error from server (NotFound): namespaces "kubesphere-alerting-system" not found
    51. [root@k8s-master01 storage]#
    52. #成功删除