Kubernetes API 列表获取

文章介绍了如何查看Kubernetes集群中的资源类型,包括Binding、ConfigMap、Pod等,以及如何使用kubectl命令获取API路径。同时,提供了获取所有端点的示例和脚本,以展示如何遍历核心API和非核心API资源。
摘要由CSDN通过智能技术生成

概述

熟悉client-go的同学非常清楚,Kubernetes中的资源是按照GVR定义的,即Group+Version+Resource,那么我们如何知道当前Kubernetes集群中有哪些资源的呢?他们的api路径是什么?

有哪些资源?

k api-resources

lua复制代码NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
auditsinks                                     auditregistration.k8s.io       false        AuditSink
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
endpointslices                                 discovery.k8s.io               true         EndpointSlice
events                            ev           events.k8s.io                  true         Event
ingresses                         ing          extensions                     true         Ingress
flowschemas                                    flowcontrol.apiserver.k8s.io   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io   false        PriorityLevelConfiguration
ingressclasses                                 networking.k8s.io              false        IngressClass
ingresses                         ing          networking.k8s.io              true         Ingress
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
runtimeclasses                                 node.k8s.io                    false        RuntimeClass
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
podpresets                                     settings.k8s.io                true         PodPreset
csidrivers                                     storage.k8s.io                 false        CSIDriver
csinodes                                       storage.k8s.io                 false        CSINode
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

快速获取所有端点

kubectl get --raw /
{
  "paths": [
    "/api",
    "/api/v1",
    "/apis",
    "/apis/",
    "/apis/admissionregistration.k8s.io",
    "/apis/admissionregistration.k8s.io/v1",
    "/apis/admissionregistration.k8s.io/v1beta1",
    "/apis/apiextensions.k8s.io",
    "/apis/apiextensions.k8s.io/v1",
    "/apis/apiextensions.k8s.io/v1beta1",
    "/apis/apiregistration.k8s.io",
    "/apis/apiregistration.k8s.io/v1",
    "/apis/apiregistration.k8s.io/v1beta1",
    "/apis/apps",
    "/apis/apps/v1",
    "/apis/auditregistration.k8s.io",
    "/apis/auditregistration.k8s.io/v1alpha1",
    "/apis/authentication.k8s.io",
    "/apis/authentication.k8s.io/v1",
    "/apis/authentication.k8s.io/v1beta1",
    "/apis/authorization.k8s.io",
    "/apis/authorization.k8s.io/v1",
    "/apis/authorization.k8s.io/v1beta1",
    "/apis/autoscaling",
    "/apis/autoscaling/v1",
    "/apis/autoscaling/v2beta1",
    "/apis/autoscaling/v2beta2",
    "/apis/batch",
    "/apis/batch/v1",
    "/apis/batch/v1beta1",
    "/apis/batch/v2alpha1",
    "/apis/certificates.k8s.io",
    "/apis/certificates.k8s.io/v1beta1",
    "/apis/coordination.k8s.io",
    "/apis/coordination.k8s.io/v1",
    "/apis/coordination.k8s.io/v1beta1",
    "/apis/discovery.k8s.io",
    "/apis/discovery.k8s.io/v1beta1",
    "/apis/events.k8s.io",
    "/apis/events.k8s.io/v1beta1",
    "/apis/extensions",
    "/apis/extensions/v1beta1",
    "/apis/flowcontrol.apiserver.k8s.io",
    "/apis/flowcontrol.apiserver.k8s.io/v1alpha1",
    "/apis/networking.k8s.io",
    "/apis/networking.k8s.io/v1",
    "/apis/networking.k8s.io/v1beta1",
    "/apis/node.k8s.io",
    "/apis/node.k8s.io/v1alpha1",
    "/apis/node.k8s.io/v1beta1",
    "/apis/policy",
    "/apis/policy/v1beta1",
    "/apis/rbac.authorization.k8s.io",
    "/apis/rbac.authorization.k8s.io/v1",
    "/apis/rbac.authorization.k8s.io/v1alpha1",
    "/apis/rbac.authorization.k8s.io/v1beta1",
    "/apis/scheduling.k8s.io",
    "/apis/scheduling.k8s.io/v1",
    "/apis/scheduling.k8s.io/v1alpha1",
    "/apis/scheduling.k8s.io/v1beta1",
    "/apis/settings.k8s.io",
    "/apis/settings.k8s.io/v1alpha1",
    "/apis/storage.k8s.io",
    "/apis/storage.k8s.io/v1",
    "/apis/storage.k8s.io/v1alpha1",
    "/apis/storage.k8s.io/v1beta1",
    "/healthz",
    "/healthz/autoregister-completion",
    "/healthz/etcd",
    "/healthz/log",
    "/healthz/ping",
    "/healthz/poststarthook/apiserver/bootstrap-system-flowcontrol-configuration",
    "/healthz/poststarthook/apiservice-openapi-controller",
    "/healthz/poststarthook/apiservice-registration-controller",
    "/healthz/poststarthook/apiservice-status-available-controller",
    "/healthz/poststarthook/bootstrap-controller",
    "/healthz/poststarthook/crd-informer-synced",
    "/healthz/poststarthook/generic-apiserver-start-informers",
    "/healthz/poststarthook/kube-apiserver-autoregistration",
    "/healthz/poststarthook/rbac/bootstrap-roles",
    "/healthz/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/healthz/poststarthook/start-apiextensions-controllers",
    "/healthz/poststarthook/start-apiextensions-informers",
    "/healthz/poststarthook/start-cluster-authentication-info-controller",
    "/healthz/poststarthook/start-kube-aggregator-informers",
    "/healthz/poststarthook/start-kube-apiserver-admission-initializer",
    "/livez",
    "/livez/autoregister-completion",
    "/livez/etcd",
    "/livez/log",
    "/livez/ping",
    "/livez/poststarthook/apiserver/bootstrap-system-flowcontrol-configuration",
    "/livez/poststarthook/apiservice-openapi-controller",
    "/livez/poststarthook/apiservice-registration-controller",
    "/livez/poststarthook/apiservice-status-available-controller",
    "/livez/poststarthook/bootstrap-controller",
    "/livez/poststarthook/crd-informer-synced",
    "/livez/poststarthook/generic-apiserver-start-informers",
    "/livez/poststarthook/kube-apiserver-autoregistration",
    "/livez/poststarthook/rbac/bootstrap-roles",
    "/livez/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/livez/poststarthook/start-apiextensions-controllers",
    "/livez/poststarthook/start-apiextensions-informers",
    "/livez/poststarthook/start-cluster-authentication-info-controller",
    "/livez/poststarthook/start-kube-aggregator-informers",
    "/livez/poststarthook/start-kube-apiserver-admission-initializer",
    "/logs",
    "/metrics",
    "/openapi/v2",
    "/readyz",
    "/readyz/autoregister-completion",
    "/readyz/etcd",
    "/readyz/informer-sync",
    "/readyz/log",
    "/readyz/ping",
    "/readyz/poststarthook/apiserver/bootstrap-system-flowcontrol-configuration",
    "/readyz/poststarthook/apiservice-openapi-controller",
    "/readyz/poststarthook/apiservice-registration-controller",
    "/readyz/poststarthook/apiservice-status-available-controller",
    "/readyz/poststarthook/bootstrap-controller",
    "/readyz/poststarthook/crd-informer-synced",
    "/readyz/poststarthook/generic-apiserver-start-informers",
    "/readyz/poststarthook/kube-apiserver-autoregistration",
    "/readyz/poststarthook/rbac/bootstrap-roles",
    "/readyz/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/readyz/poststarthook/start-apiextensions-controllers",
    "/readyz/poststarthook/start-apiextensions-informers",
    "/readyz/poststarthook/start-cluster-authentication-info-controller",
    "/readyz/poststarthook/start-kube-aggregator-informers",
    "/readyz/poststarthook/start-kube-apiserver-admission-initializer",
    "/readyz/shutdown",
    "/version"
  ]
}

API路径获取步骤

开启proxy

kubectl proxy --port=8080

样例

curl -s http://localhost:8080/api/v1
curl -s http://localhost:8080/api/v1/pods
curl -s http://localhost:8080/apis/apps/v1
curl -s http://localhost:8080/apis/apps/v1/statefulsets
curl -s http://localhost:8080/api/v1/pods\?watch\=yes

快速:执行脚本

#!/bin/bash
SERVER="localhost:8080"
# do core resources first, which are at a separate api location
api="core"
CORES=$(curl -s $SERVER/api/v1 | jq -r --arg api "$api" '.resources | .[] | .name ')

for resource in $CORES; do
    if [[ ! $resource =~ .*/.* ]]; then
        uri=$SERVER/api/v1/$resource
        echo $uri
    fi
done

# now do non-core resources
APIS=$(curl -s $SERVER/apis | jq -r '[.groups | .[].name] | join(" ")')

for api in $APIS; do
    version=$(curl -s $SERVER/apis/$api | jq -r '.preferredVersion.version')
    RESOURCES=$(curl -s $SERVER/apis/$api/$version | jq -r --arg api "$api" '.resources | .[] | .name ')
    for resource in $RESOURCES; do
        if [[ ! $resource =~ .*/.* ]]; then
            uri=$SERVER/apis/$api/$version/$resource
            echo $uri
        fi
    done
done

改编自 stackoverflow答案

k8s v1.18 结果

localhost:8080/api/v1/bindings
localhost:8080/api/v1/componentstatuses
localhost:8080/api/v1/configmaps
localhost:8080/api/v1/endpoints
localhost:8080/api/v1/events
localhost:8080/api/v1/limitranges
localhost:8080/api/v1/namespaces
localhost:8080/api/v1/nodes
localhost:8080/api/v1/persistentvolumeclaims
localhost:8080/api/v1/persistentvolumes
localhost:8080/api/v1/pods
localhost:8080/api/v1/podtemplates
localhost:8080/api/v1/replicationcontrollers
localhost:8080/api/v1/resourcequotas
localhost:8080/api/v1/secrets
localhost:8080/api/v1/serviceaccounts
localhost:8080/api/v1/services
localhost:8080/apis/apiregistration.k8s.io/v1/apiservices
localhost:8080/apis/extensions/v1beta1/ingresses
localhost:8080/apis/apps/v1/controllerrevisions
localhost:8080/apis/apps/v1/daemonsets
localhost:8080/apis/apps/v1/deployments
localhost:8080/apis/apps/v1/replicasets
localhost:8080/apis/apps/v1/statefulsets
localhost:8080/apis/events.k8s.io/v1beta1/events
localhost:8080/apis/authentication.k8s.io/v1/tokenreviews
localhost:8080/apis/authorization.k8s.io/v1/localsubjectaccessreviews
localhost:8080/apis/authorization.k8s.io/v1/selfsubjectaccessreviews
localhost:8080/apis/authorization.k8s.io/v1/selfsubjectrulesreviews
localhost:8080/apis/authorization.k8s.io/v1/subjectaccessreviews
localhost:8080/apis/autoscaling/v1/horizontalpodautoscalers
localhost:8080/apis/batch/v1/jobs
localhost:8080/apis/certificates.k8s.io/v1beta1/certificatesigningrequests
localhost:8080/apis/networking.k8s.io/v1/networkpolicies
localhost:8080/apis/policy/v1beta1/poddisruptionbudgets
localhost:8080/apis/policy/v1beta1/podsecuritypolicies
localhost:8080/apis/rbac.authorization.k8s.io/v1/clusterrolebindings
localhost:8080/apis/rbac.authorization.k8s.io/v1/clusterroles
localhost:8080/apis/rbac.authorization.k8s.io/v1/rolebindings
localhost:8080/apis/rbac.authorization.k8s.io/v1/roles
localhost:8080/apis/settings.k8s.io/v1alpha1/podpresets
localhost:8080/apis/storage.k8s.io/v1/csidrivers
localhost:8080/apis/storage.k8s.io/v1/csinodes
localhost:8080/apis/storage.k8s.io/v1/storageclasses
localhost:8080/apis/storage.k8s.io/v1/volumeattachments
localhost:8080/apis/admissionregistration.k8s.io/v1/mutatingwebhookconfigurations
localhost:8080/apis/admissionregistration.k8s.io/v1/validatingwebhookconfigurations
localhost:8080/apis/apiextensions.k8s.io/v1/customresourcedefinitions
localhost:8080/apis/scheduling.k8s.io/v1/priorityclasses
localhost:8080/apis/coordination.k8s.io/v1/leases
localhost:8080/apis/auditregistration.k8s.io/v1alpha1/auditsinks
localhost:8080/apis/node.k8s.io/v1beta1/runtimeclasses
localhost:8080/apis/discovery.k8s.io/v1beta1/endpointslices
localhost:8080/apis/flowcontrol.apiserver.k8s.io/v1alpha1/flowschemas
localhost:8080/apis/flowcontrol.apiserver.k8s.io/v1alpha1/prioritylevelconfigurations
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值