跳至主要內容

kubernetes常用命令

知识库容器技术kubernetes建设kubernetes大约 4 分钟

Node

## k8s-master2节点需要去污点,允许pod调度,命令如下。
kubectl taint node k8s-master2 node-role.kubernetes.io/master-

NameSpace

## 在尝试以下命令强制删除也不好使
kubectl delete ns <terminating-namespace> --force --grace-period=0

## 1. 运行以下命令以查看处于“Terminating”状态的namespace:
kubectl get namespaces
## 2. 选择一个Terminating namespace,并查看namespace 中的finalizer。运行以下命令:
kubectl get namespace <terminating-namespace> -o yaml
## 3. 导出json格式到tmp.json:
kubectl get namespace <terminating-namespace> -o json >tmp.json
## 4. 编辑tmp.josn,修改 "spec": {"finalizers": []}   finalizers为空数组
kubectl get namespace <terminating-namespace> -o json | jq '.spec.finalizers=[]' > tmp.json
## 5. 开启 proxy :
kubectl proxy
# kubectl proxy --port=8081
## 6. 打开新的terminal 窗口,运行以下命令:
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/<terminating-namespace>/finalize
## 7. 检查该namespace 是否被删除:
kubectl get namespaces


#### 如果上面方法无法删除namespace,可以通过如下方法看下namespace是不是还有什么资源没有清理
## 若命名空间依然无法删除,则查询命名空间哪些资源
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <terminating-namespace>
## 然后删除这些资源:
kubectl -n <terminating-namespace> delete projectalertgroup.management.cattle.io/projectalert-workload-alert --grace-period=0 --force
## 若 Pod 还是无法删除,可以在 Pod 中添加补丁:
kubectl -n <terminating-namespace> patch projectalertgroup.management.cattle.io/projectalert-workload-alert -p '{"metadata":{"finalizers":[]}}' --type='merge'
## 添加补丁后,强制删除:
kubectl -n <terminating-namespace> delete projectalertrule.management.cattle.io/memory-close-to-resource-limited --grace-period=0 --force
## 然后执行下面命令删除namespace
kubectl patch namespace <terminating-namespace> -p '{"metadata":{"finalizers":[]}}' --type='merge'
kubectl delete namespace cattle-system --grace-period=0 --force

Deployment

## 获取hostNetwork网络模式的deployment
kubectl get deployment --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.hostNetwork==true)]} {"namespace: "} {.metadata.namespace} {", name:"} {.metadata.name} {"\n"} {end}'

## 获取特权模式的deployment
kubectl get deployment --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.containers[*].securityContext.privileged==true)]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取控制进程可以获得超出其父进程的特权的deployment
kubectl get deployment --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.containers[*].securityContext.allowPrivilegeEscalation==true)]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取所有deployment的容器名称和namespace
kubectl get deployment --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.containers[*])]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取所有deployment的容器端口
kubectl get deployment --all-namespaces -o jsonpath='{range .items[*]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {", "}{.spec.template.spec.containers[*].ports} {"\n"} {end}'

## 获取配置了hostport的deployment
kubectl get deployment --all-namespaces -o jsonpath='{range .items[*]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {" "}{.spec.template.spec.containers[*].ports} {"\n"} {end}' |grep map | grep hostPort | awk '{print $1 $2 " "$3 $4}'

## 获取配置了capabilities属性的deployment
kubectl get deployment --all-namespaces -o jsonpath='{range .items[*]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {" "}{.spec.template.spec.containers[*].securityContext} {"\n"} {end}' |grep capabilities | awk '{print $1 $2 " "$3 $4}'

DaemonSet

## 重启
kubectl rollout restart daemonset kube-proxy -n kube-system

## 获取所有配置了hostNetwork的DaemonSet
kubectl get ds --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.hostNetwork==true)]} {"namespace: "} {.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取所有配置了hostIPC模式的DaemonSet
kubectl get ds --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.hostIPC==true)]} {"namespace: "} {.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取所有配置了hostPID模式的DaemonSet
kubectl get ds --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.hostPID==true)]} {"namespace: "} {.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取所有配置了allowPrivilegeEscalation模式的DaemonSet
kubectl get ds --all-namespaces -o jsonpath='{range .items[?(@.spec.template.spec.containers[*].securityContext.allowPrivilegeEscalation==true)]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {"\n"} {end}'

## 获取配置了hostport的DaemonSet
kubectl get ds --all-namespaces -o jsonpath='{range .items[*]} {"namespace: "}{.metadata.namespace}{", name:"} {.metadata.name} {" "}{.spec.template.spec.containers[*].ports} {"\n"} {end}' |grep map | grep hostPort | awk '{print $1 $2 " "$3 $4}'

Pod

## 获取所有pod的ip和所在node的ip
kubectl get pods --all-namespaces    -o=jsonpath='{range .items[*]}[nodeip:{.status.hostIP}, podip:{.status.podIP}]{"\n"}{end}'

## 删除有问题的pod
kubectl get pods -A | grep Evicted | awk '{print $1}' | xargs kubectl delete pod

## 查看多个Pod中的日志信息
for p in $(kubectl get pods --namespace=kube-system -l k8s-app=kube-dns -o name); do kubectl logs --namespace=kube-system $p; done

有时候由于节点的内存或者磁盘使用率较高导致集群中产生了大量的 evited 状态 pod,这些 pod 如果不手动删除,会一直存在集群中,这里提供了脚本 clean-evicted-pod.shopen in new window 来一键清理集群中的 evited 状态 pod。

cat >> clean-evicted-pod.sh << EOF
#!/bin/bash

basepath=\$(cd \`dirname \$0\`; pwd)

kubectl get pods -A | grep Evicted | awk -F " " '{print \$1,\$2}'  >> \$basepath/tmp.file

while read line
do
    ns=\`echo \$line | awk -F " " '{print $1}'\`
    podname=\`echo \$line | awk -F " " '{print $2}'\`
    kubectl delete pod \$podname -n \$ns
done < \$basepath/tmp.file

rm \$basepath/tmp.file
EOF
sh clean-evicted-pod.sh

PVC

## 批量删除PVC
kubectl -n default delete pvc `kubectl -n default get pvc | grep -v VOLUME | awk '{print $1}' |  tr '\n' ' '`

Ingress

配置 HTTPS 证书

## 创建密钥
openssl genrsa -out devops.key 2048
openssl req -new -x509 -key devops.key -out devops.crt -subj /C=CN/ST=Shanghai/L=Shanghai/O=DevOps/CN=*.devops.*
## 导入密钥
kubectl create secret tls devops-secret-ingress --cert=devops.crt --key=devops.key -n devops
kubectl describe secret devops-secret-ingress -n devops