409主机的APIServer日志:
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#delete-collection-pod-v1-core
kubectl删除namespace下的全部pod时,其实走的还是一个一个删,可以从日志中看到:
[root@master ~]# kubectl get pod -n cicd -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
b1-1-c8477796f-rnz4w 0/1 CrashLoopBackOff 1 5m26s 10.100.219.77 master <none> <none>
b1-10-5bb56df9d7-gsgwt 0/1 CrashLoopBackOff 1 5m26s 10.100.219.110 master <none> <none>
b1-11-bd4ffb6cc-dglgz 0/1 CrashLoopBackOff 1 5m26s 10.100.219.89 master <none> <none>
b1-2-649c5c8b6f-f7jh2 0/1 CrashLoopBackOff 1 5m26s 10.100.219.112 master <none> <none>
b1-3-57f47c8dc7-6mgpp 0/1 CrashLoopBackOff 1 5m26s 10.100.219.95 master <none> <none>
b1-4-5bbfcc49d5-7zpx6 0/1 Completed 0 5m26s 10.100.219.70 master <none> <none>
b1-5-76b54754df-nd5r4 0/1 CrashLoopBackOff 1 5m26s 10.100.219.113 master <none> <none>
b1-6-5b7d8c6577-v797b 0/1 Completed 0 5m26s 10.100.219.87 master <none> <none>
b1-8-844477c99c-47xw9 0/1 Completed 0 5m26s 10.100.219.83 master <none> <none>
b1-9-789d4ddb77-8lwzd 0/1 CrashLoopBackOff 1 5m26s 10.100.219.111 master <none> <none>
busybox-a-86b6df6fb9-k8nhq 0/1 Completed 0 5m25s 10.100.219.71 master <none> <none>
[root@master ~]# kubectl delete pod -n cicd --all --v=7
I0815 22:26:24.013473 26739 loader.go:375] Config loaded from file: /root/.kube/config
I0815 22:26:24.015065 26739 round_trippers.go:420] GET https://172.21.0.15:6443/apis/middleware.hc.middleware/v1alpha1?timeout=32s
I0815 22:26:24.015078 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.015084 26739 round_trippers.go:431] Accept: application/json, */*
I0815 22:26:24.015089 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.022127 26739 round_trippers.go:446] Response Status: 503 Service Unavailable in 7 milliseconds
I0815 22:26:24.030746 26739 request.go:1171] body was not decodable (unable to check for Status): couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }
I0815 22:26:24.030766 26739 cached_discovery.go:78] skipped caching discovery info due to the server is currently unable to handle the request
I0815 22:26:24.031703 26739 round_trippers.go:420] GET https://172.21.0.15:6443/apis/middleware.hc.middleware/v1alpha1?timeout=32s
I0815 22:26:24.031717 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.031723 26739 round_trippers.go:431] Accept: application/json, */*
I0815 22:26:24.031727 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.032463 26739 round_trippers.go:446] Response Status: 503 Service Unavailable in 0 milliseconds
I0815 22:26:24.039493 26739 request.go:1171] body was not decodable (unable to check for Status): couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }
I0815 22:26:24.039511 26739 cached_discovery.go:78] skipped caching discovery info due to the server is currently unable to handle the request
I0815 22:26:24.039547 26739 shortcut.go:89] Error loading discovery information: unable to retrieve the complete list of server APIs: middleware.hc.middleware/v1alpha1: the server is currently unable to handle the request
I0815 22:26:24.040969 26739 round_trippers.go:420] GET https://172.21.0.15:6443/apis/middleware.hc.middleware/v1alpha1?timeout=32s
I0815 22:26:24.041021 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.041040 26739 round_trippers.go:431] Accept: application/json, */*
I0815 22:26:24.041049 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.042800 26739 round_trippers.go:446] Response Status: 503 Service Unavailable in 1 milliseconds
I0815 22:26:24.050095 26739 request.go:1171] body was not decodable (unable to check for Status): couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }
I0815 22:26:24.050111 26739 cached_discovery.go:78] skipped caching discovery info due to the server is currently unable to handle the request
I0815 22:26:24.051372 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods
I0815 22:26:24.051388 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.051397 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.051405 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.053545 26739 round_trippers.go:446] Response Status: 200 OK in 2 milliseconds
I0815 22:26:24.057180 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-1-c8477796f-rnz4w
I0815 22:26:24.057196 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.057202 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.057206 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.057211 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.063420 26739 round_trippers.go:446] Response Status: 200 OK in 6 milliseconds
pod "b1-1-c8477796f-rnz4w" deleted
I0815 22:26:24.063851 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-10-5bb56df9d7-gsgwt
I0815 22:26:24.063858 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.063863 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.063868 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.063872 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.070894 26739 round_trippers.go:446] Response Status: 200 OK in 7 milliseconds
pod "b1-10-5bb56df9d7-gsgwt" deleted
I0815 22:26:24.071311 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-11-bd4ffb6cc-dglgz
I0815 22:26:24.071318 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.071323 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.071328 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.071332 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.086686 26739 round_trippers.go:446] Response Status: 200 OK in 15 milliseconds
pod "b1-11-bd4ffb6cc-dglgz" deleted
I0815 22:26:24.087133 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-2-649c5c8b6f-f7jh2
I0815 22:26:24.087140 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.087145 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.087150 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.087154 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.114167 26739 round_trippers.go:446] Response Status: 200 OK in 26 milliseconds
pod "b1-2-649c5c8b6f-f7jh2" deleted
I0815 22:26:24.114676 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-3-57f47c8dc7-6mgpp
I0815 22:26:24.114684 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.114689 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.114694 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.114699 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.140298 26739 round_trippers.go:446] Response Status: 200 OK in 25 milliseconds
pod "b1-3-57f47c8dc7-6mgpp" deleted
I0815 22:26:24.140787 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-4-5bbfcc49d5-7zpx6
I0815 22:26:24.140795 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.140801 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.140806 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.140810 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.175724 26739 round_trippers.go:446] Response Status: 200 OK in 34 milliseconds
pod "b1-4-5bbfcc49d5-7zpx6" deleted
I0815 22:26:24.176215 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-5-76b54754df-nd5r4
I0815 22:26:24.176223 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.176228 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.176233 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.176237 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.199651 26739 round_trippers.go:446] Response Status: 200 OK in 23 milliseconds
pod "b1-5-76b54754df-nd5r4" deleted
I0815 22:26:24.200156 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-6-5b7d8c6577-v797b
I0815 22:26:24.200163 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.200169 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.200174 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.200178 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.215455 26739 round_trippers.go:446] Response Status: 200 OK in 15 milliseconds
pod "b1-6-5b7d8c6577-v797b" deleted
I0815 22:26:24.221410 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-8-844477c99c-47xw9
I0815 22:26:24.221433 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.221439 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.221445 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.221450 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.228376 26739 round_trippers.go:446] Response Status: 200 OK in 6 milliseconds
pod "b1-8-844477c99c-47xw9" deleted
I0815 22:26:24.228881 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/b1-9-789d4ddb77-8lwzd
I0815 22:26:24.228894 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.228899 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.228904 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.228908 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.239194 26739 round_trippers.go:446] Response Status: 200 OK in 10 milliseconds
pod "b1-9-789d4ddb77-8lwzd" deleted
I0815 22:26:24.239701 26739 round_trippers.go:420] DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods/busybox-a-86b6df6fb9-k8nhq
I0815 22:26:24.239708 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.239714 26739 round_trippers.go:431] Content-Type: application/json
I0815 22:26:24.239719 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.239723 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.245485 26739 round_trippers.go:446] Response Status: 200 OK in 5 milliseconds
pod "busybox-a-86b6df6fb9-k8nhq" deleted
I0815 22:26:24.245752 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-1-c8477796f-rnz4w
I0815 22:26:24.245758 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.245764 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.245768 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.250632 26739 round_trippers.go:446] Response Status: 200 OK in 4 milliseconds
I0815 22:26:24.251035 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-1-c8477796f-rnz4w&resourceVersion=29591860&watch=true
I0815 22:26:24.251049 26739 round_trippers.go:427] Request Headers:
I0815 22:26:24.251056 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:24.251064 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:24.251773 26739 round_trippers.go:446] Response Status: 200 OK in 0 milliseconds
I0815 22:26:32.882791 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-10-5bb56df9d7-gsgwt
I0815 22:26:32.882809 26739 round_trippers.go:427] Request Headers:
I0815 22:26:32.882817 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:32.882824 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:32.887905 26739 round_trippers.go:446] Response Status: 200 OK in 5 milliseconds
I0815 22:26:32.888319 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-10-5bb56df9d7-gsgwt&resourceVersion=29592026&watch=true
I0815 22:26:32.888330 26739 round_trippers.go:427] Request Headers:
I0815 22:26:32.888336 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:32.888344 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:32.892324 26739 round_trippers.go:446] Response Status: 200 OK in 3 milliseconds
I0815 22:26:34.082259 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-11-bd4ffb6cc-dglgz
I0815 22:26:34.082279 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.082286 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.082291 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.084077 26739 round_trippers.go:446] Response Status: 200 OK in 1 milliseconds
I0815 22:26:34.084214 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-2-649c5c8b6f-f7jh2
I0815 22:26:34.084224 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.084229 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.084234 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.088244 26739 round_trippers.go:446] Response Status: 200 OK in 4 milliseconds
I0815 22:26:34.088380 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-3-57f47c8dc7-6mgpp
I0815 22:26:34.088388 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.088396 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.088401 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.090553 26739 round_trippers.go:446] Response Status: 200 OK in 2 milliseconds
I0815 22:26:34.090661 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-4-5bbfcc49d5-7zpx6
I0815 22:26:34.090667 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.090672 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.090677 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.094562 26739 round_trippers.go:446] Response Status: 200 OK in 3 milliseconds
I0815 22:26:34.094667 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-5-76b54754df-nd5r4
I0815 22:26:34.094673 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.094678 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.094683 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.096260 26739 round_trippers.go:446] Response Status: 200 OK in 1 milliseconds
I0815 22:26:34.096355 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-6-5b7d8c6577-v797b
I0815 22:26:34.096361 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.096366 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.096371 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.097290 26739 round_trippers.go:446] Response Status: 200 OK in 0 milliseconds
I0815 22:26:34.097390 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-8-844477c99c-47xw9
I0815 22:26:34.097396 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.097401 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.097405 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.098371 26739 round_trippers.go:446] Response Status: 200 OK in 0 milliseconds
I0815 22:26:34.098454 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-9-789d4ddb77-8lwzd
I0815 22:26:34.098460 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.098464 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.098469 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.102150 26739 round_trippers.go:446] Response Status: 200 OK in 3 milliseconds
I0815 22:26:34.102565 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Db1-9-789d4ddb77-8lwzd&resourceVersion=29592032&watch=true
I0815 22:26:34.102574 26739 round_trippers.go:427] Request Headers:
I0815 22:26:34.102579 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:34.102584 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:34.103099 26739 round_trippers.go:446] Response Status: 200 OK in 0 milliseconds
I0815 22:26:35.277055 26739 round_trippers.go:420] GET https://172.21.0.15:6443/api/v1/namespaces/cicd/pods?fieldSelector=metadata.name%3Dbusybox-a-86b6df6fb9-k8nhq
I0815 22:26:35.277073 26739 round_trippers.go:427] Request Headers:
I0815 22:26:35.277079 26739 round_trippers.go:431] Accept: application/json
I0815 22:26:35.277083 26739 round_trippers.go:431] User-Agent: kubectl/v1.16.3 (linux/amd64) kubernetes/b3cbbae
I0815 22:26:35.282397 26739 round_trippers.go:446] Response Status: 200 OK in 5 milliseconds
[root@master ~]#
所以,kube-APIServer中无批量删除Delete的日志:
[root@master ~]# kubectl logs -n kube-system kube-apiserver-master | grep Delete
[root@master ~]# kubectl logs -n kube-system kube-apiserver-master | grep cicd
[root@master ~]#
go程序测试
pod总数:
[root@master test]# kubectl get pod -n cicd | wc -l
2011
go.mod
module k8s.io/test
go 1.13
require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/imdario/mergo v0.3.11 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
k8s.io/apimachinery v0.17.3
k8s.io/apiserver v0.17.3 // indirect
k8s.io/client-go v0.17.3
)
main.go
package main
import (
"flag"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
flag.Parse()
defer glog.Flush()
glog.Infof("aaaa")
clients, err := createClients()
if err != nil {
glog.Fatalf("create client err: %v", err)
}
err = clients.CoreV1().Pods("cicd").DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{})
if err != nil {
glog.Fatalf("delete pod err:", err)
}
}
type Arg struct {
Master string
KubeConfig string
}
func createClients() (*clientset.Clientset, error) {
kubeconfig, err := clientcmd.BuildConfigFromFlags("", "/root/.kube/config")
if err != nil {
return nil, err
}
//controller-manager的类型为"application/vnd.kubernetes.protobuf"但在这里有问题,导致同步事件AddFunc、UpdateFunc、delFunc出问题,
// 不能加,后续细细研究,
//kubeconfig.ContentConfig.ContentType = s.ContentType
// Override kubeconfig qps/burst settings from flags
kubeconfig.QPS = 20
kubeconfig.Burst = 30
kubeClient, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, "operator-manager"))
if err != nil {
glog.Fatalf("Invalid API configuration: %v", err)
}
return kubeClient, nil
}
[root@master test]# kubectl get pod -n cicd | wc -l
2011
[root@master test]#
[root@master test]# go mod vendor
[root@master test]# go build -a -o main
[root@master test]#
[root@master test]# ll
total 35652
-rw-r--r-- 1 root root 381 Aug 16 08:53 go.mod
-rw-r--r-- 1 root root 33529 Aug 16 08:53 go.sum
-rwxr-xr-x 1 root root 36453104 Aug 16 08:53 main
-rw-r--r-- 1 root root 1313 Aug 16 08:39 main.go
drwxr-xr-x 8 root root 4096 Aug 16 08:39 vendor
[root@master test]# ./main
F0816 08:54:29.073082 7355 main.go:24] delete pod err:%!(EXTRA *errors.StatusError=Timeout: request did not complete within requested timeout 30s)
goroutine 1 [running]:
github.com/golang/glog.stacks(0xc000268600, 0xc0003f2000, 0x96, 0xeb)
/root/gopath/pkg/mod/github.com/golang/glog@v0.0.0-20160126235308-23def4e6c14b/glog.go:769 +0xb8
github.com/golang/glog.(*loggingT).output(0x1da49e0, 0xc000000003, 0xc00023ecb0, 0x1d1f369, 0x7, 0x18, 0x0)
/root/gopath/pkg/mod/github.com/golang/glog@v0.0.0-20160126235308-23def4e6c14b/glog.go:720 +0x372
github.com/golang/glog.(*loggingT).printf(0x1da49e0, 0xc000000003, 0x1327418, 0xf, 0xc0000dff30, 0x1, 0x1)
/root/gopath/pkg/mod/github.com/golang/glog@v0.0.0-20160126235308-23def4e6c14b/glog.go:655 +0x14b
github.com/golang/glog.Fatalf(...)
/root/gopath/pkg/mod/github.com/golang/glog@v0.0.0-20160126235308-23def4e6c14b/glog.go:1148
main.main()
/root/gopath/src/k8s.io/test/main.go:24 +0x2af
执行后,可以看到APIServer中有如下日志:
[root@master test]# kubectl logs -f -n kube-system kube-apiserver-master | grep Delete
I0816 00:54:29.072293 1 trace.go:116] Trace[789103800]: "Delete" url:/api/v1/namespaces/cicd/pods (started: 2020-08-16 00:53:59.071909387 +0000 UTC m=+5740361.632700959) (total time: 30.000366126s):
curl模拟
pod总数:
[root@master test]# kubectl get pod -n cicd | wc -l
2011
模拟:
[root@master test]# curl -k -X DELETE https://172.21.0.15:6443/api/v1/namespaces/cicd/pods
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "Timeout: request did not complete within requested timeout 30s",
"reason": "Timeout",
"details": {
},
"code": 504
}[root@master test]#kubectl logs -n kube-system kube-apiserver-master | grep Deletes
^C
[root@master test]# kubectl logs -n kube-system kube-apiserver-master | grep Delete
I0816 00:54:29.072293 1 trace.go:116] Trace[789103800]: "Delete" url:/api/v1/namespaces/cicd/pods (started: 2020-08-16 00:53:59.071909387 +0000 UTC m=+5740361.632700959) (total time: 30.000366126s):
I0816 01:01:28.789859 1 trace.go:116] Trace[1098869689]: "Delete" url:/api/v1/namespaces/cicd/pods (started: 2020-08-16 01:00:58.789571053 +0000 UTC m=+5740781.350362618) (total time: 30.000266058s):
[root@master test]#
初步结论
APIServer中的日志,在请求超过一定时间才会记录Trace日志。