Kubernetes 常用命令

时间:2023-02-11 17:58:39

Master节点常用命令

重启apiserver

# 获得 apiserver 的 pod 名字
export apiserver_pods=$(kubectl get pods --selector=component=kube-apiserver -n kube-system --output=jsonpath={.items..metadata.name})
# 删除 apiserver 的 pod
kubectl delete pod $apiserver_pods -n kube-system

查看所有的pod

kubectl get pods -A

查看pod启动日志

kubectl --namespace=calico-system  describe pod  tigera-operator-5dc8b759d9-5gnwt
  • --namespace 指定命名空间
  • describe pod 后面跟pod的name

删除pod

kubectl delete pod kubernetes-dashboard-5676d8b865-98ht9 -n kubernetes-dashboard
  • -n 命名空间
  • --grace-period=0 --force 强制删除

查看 namespace 下面所有的资源

kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n {namespace}

删除 namespace 下面所有的资源(慎重)

kubectl delete all --all -n {namespace}

当然也可以删除 namespace 来删除所有资源

kubectl delete ns {namespace}

有时候删除资源会出现卡住,可能是 kubernetes 正在处理,也可能是该资源是其它资源的依赖,导致无法删除
有时候删除资源会出现长时间处于 terminating 状态,可以使用 --force --grace-period=0 (不建议)

kubectl delete po xxx --force --grace-period=0

常见问题

1. k8s删除Terminating状态的命名空间

1. 查看命名空间

[root@kubernetes-master-1 kubernetes]#kubectl  get ns 
NAME STATUS AGE
calico-apiserver Active 2d18h
calico-system Active 2d18h
default Active 3d14h
kube-node-lease Active 3d14h
kube-public Active 3d14h
kube-system Active 3d14h
kubernetes-dashboard Terminating 2d17h
tigera-operator Active 2d18h

发现kubernetes-dashboard一直处于Terminating 状态。无法删除命名空间!!

2.解决方法

查看kubesphere-system的namespace描述

kubectl get ns kubernetes-dashboard  -o json > kubernetes-dashboard.json

编辑json文件,删除spec字段的内存,因为k8s集群时需要认证的。

vi kubesphere-system.json

"spec": {
"finalizers": [
"kubernetes"
]
},

更改为:

"spec": {

},

新开一个窗口运行kubectl proxy跑一个API代理在本地的8081端口

# kubectl proxy --port=8081
Starting to serve on 127.0.0.1:8081

最后运行curl命令进行删除

curl -k -H "Content-Type:application/json" -X PUT --data-binary @kubernetes-dashboard.json http://127.0.0.1:8081/api/v1/namespaces/kubernetes-dashboard/finalize

注意:命令中的kubernetes-dashboard就是命名空间。

调用成功输出:

[root@kubernetes-master-1 kubernetes]# curl -k -H "Content-Type:application/json" -X PUT --data-binary @kubesphere-dashboard.json http://127.0.0.1:8081/api/v1/namespaces/kubernetes-dashboard/finalize
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "kubernetes-dashboard",
"uid": "31b1c662-663b-42e1-a6b8-9abd1cd96911",
"resourceVersion": "54597",
"creationTimestamp": "2022-07-15T08:35:55Z",
"deletionTimestamp": "2022-07-18T01:42:39Z",
"labels": {
"kubernetes.io/metadata.name": "kubernetes-dashboard"
},
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"kubernetes-dashboard\"}}\n"
},
"managedFields": [
{
"manager": "kubectl-client-side-apply",
"operation": "Update",
"apiVersion": "v1",
"time": "2022-07-15T08:35:55Z",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:metadata": {
"f:annotations": {
".": {},
"f:kubectl.kubernetes.io/last-applied-configuration": {}
},
"f:labels": {
".": {},
"f:kubernetes.io/metadata.name": {}
}
}
}
},
{
"manager": "kube-controller-manager",
"operation": "Update",
"apiVersion": "v1",
"time": "2022-07-18T01:42:45Z",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:status": {
"f:conditions": {
".": {},
"k:{\"type\":\"NamespaceContentRemaining\"}": {
".": {},
"f:lastTransitionTime": {},
"f:message": {},
"f:reason": {},
"f:status": {},
"f:type": {}
},
"k:{\"type\":\"NamespaceDeletionContentFailure\"}": {
".": {},
"f:lastTransitionTime": {},
"f:message": {},
"f:reason": {},
"f:status": {},
"f:type": {}
},
"k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": {
".": {},
"f:lastTransitionTime": {},
"f:message": {},
"f:reason": {},
"f:status": {},
"f:type": {}
},
"k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": {
".": {},
"f:lastTransitionTime": {},
"f:message": {},
"f:reason": {},
"f:status": {},
"f:type": {}
},
"k:{\"type\":\"NamespaceFinalizersRemaining\"}": {
".": {},
"f:lastTransitionTime": {},
"f:message": {},
"f:reason": {},
"f:status": {},
"f:type": {}
}
}
}
},
"subresource": "status"
}
]
},
"spec": {},
"status": {
"phase": "Terminating",
"conditions": [
{
"type": "NamespaceDeletionDiscoveryFailure",
"status": "False",
"lastTransitionTime": "2022-07-18T01:42:45Z",
"reason": "ResourcesDiscovered",
"message": "All resources successfully discovered"
},
{
"type": "NamespaceDeletionGroupVersionParsingFailure",
"status": "False",
"lastTransitionTime": "2022-07-18T01:42:45Z",
"reason": "ParsedGroupVersions",
"message": "All legacy kube types successfully parsed"
},
{
"type": "NamespaceDeletionContentFailure",
"status": "True",
"lastTransitionTime": "2022-07-18T01:43:22Z",
"reason": "ContentDeletionFailed",
"message": "Failed to delete all resource types, 1 remaining: unexpected items still remain in namespace: kubernetes-dashboard for gvr: /v1, Resource=pods"
},
{
"type": "NamespaceContentRemaining",
"status": "True",
"lastTransitionTime": "2022-07-18T01:42:45Z",
"reason": "SomeResourcesRemain",
"message": "Some resources are remaining: pods. has 2 resource instances"
},
{
"type": "NamespaceFinalizersRemaining",
"status": "False",
"lastTransitionTime": "2022-07-18T01:42:45Z",
"reason": "ContentHasNoFinalizers",
"message": "All content-preserving finalizers finished"
}
]
}
}

再次查看命名空间

[root@kubernetes-master-1 kubernetes]# kubectl get ns
NAME STATUS AGE
calico-apiserver Active 2d18h
calico-system Active 2d18h
default Active 3d14h
kube-node-lease Active 3d14h
kube-public Active 3d14h
kube-system Active 3d14h
tigera-operator Active 2d18h

发现kubernetes-dashboard命名空间已经消失了。