olNGIb4NkK5r2x7x4oG3GpEzizVpnY6KNCck9cym

如何獲取 kubectl 命令支援的 Kubernetes 資源及其對應的縮寫、版本編號、命名空間和種類名稱?

使用 "kubectl api-resources" 命令可以列出 kubectl 命令支援的 Kubernetes 資源,以及對應的縮寫(shortname)、版本編號(api version)、命名空間(namespace)和種類名稱(kind)。

容器編排工具 Kubernetes 提供了 kubectl 命令行工具來與 Kubernetes 叢集(Kubernetes Cluster)互動,用於管理 Pod、ReplicaSets、Deployment 與 Service 等 Kubernetes 物件(Kubernetes Object),比如創建資源、刪除資源,以及查詢資源狀態等。

在使用時,為了避免輸入的命令過長,部分的資源有其對應的縮寫(shortname)以方便快速輸入,比如以下的命令是等價的,都可以用於列出目前 Kubernetes 叢集中的 Service 資源:

$ kubectl get svc
$ kubectl get services

其中的 svc> 便是 service 的縮寫,這些在 Kubernetes 中所定義的資源縮寫,可以幫助我們在操作管理 Kubernetes 物件時使用更簡潔的命令。那麼如何獲取 kubectl 命令支援的資源及其縮寫清單呢?

只需要使用 kubectl api-resources 命令即可:

$ kubectl api-resources
NAME                                SHORTNAMES   APIVERSION                        NAMESPACED   KIND
bindings                                         v1                                true         Binding
componentstatuses                   cs           v1                                false        ComponentStatus
configmaps                          cm           v1                                true         ConfigMap
endpoints                           ep           v1                                true         Endpoints
events                              ev           v1                                true         Event
limitranges                         limits       v1                                true         LimitRange
namespaces                          ns           v1                                false        Namespace
nodes                               no           v1                                false        Node
persistentvolumeclaims              pvc          v1                                true         PersistentVolumeClaim
persistentvolumes                   pv           v1                                false        PersistentVolume
pods                                po           v1                                true         Pod
podtemplates                                     v1                                true         PodTemplate
replicationcontrollers              rc           v1                                true         ReplicationController
resourcequotas                      quota        v1                                true         ResourceQuota
secrets                                          v1                                true         Secret
serviceaccounts                     sa           v1                                true         ServiceAccount
services                            svc          v1                                true         Service
mutatingwebhookconfigurations                    admissionregistration.k8s.io/v1   false        MutatingWebhookConfiguration
validatingadmissionpolicies                      admissionregistration.k8s.io/v1   false        ValidatingAdmissionPolicy
validatingadmissionpolicybindings                admissionregistration.k8s.io/v1   false        ValidatingAdmissionPolicyBinding
validatingwebhookconfigurations                  admissionregistration.k8s.io/v1   false        ValidatingWebhookConfiguration
customresourcedefinitions           crd,crds     apiextensions.k8s.io/v1           false        CustomResourceDefinition
apiservices                                      apiregistration.k8s.io/v1         false        APIService
controllerrevisions                              apps/v1                           true         ControllerRevision
daemonsets                          ds           apps/v1                           true         DaemonSet
deployments                         deploy       apps/v1                           true         Deployment
replicasets                         rs           apps/v1                           true         ReplicaSet
statefulsets                        sts          apps/v1                           true         StatefulSet
selfsubjectreviews                               authentication.k8s.io/v1          false        SelfSubjectReview
tokenreviews                                     authentication.k8s.io/v1          false        TokenReview
localsubjectaccessreviews                        authorization.k8s.io/v1           true         LocalSubjectAccessReview
selfsubjectaccessreviews                         authorization.k8s.io/v1           false        SelfSubjectAccessReview
selfsubjectrulesreviews                          authorization.k8s.io/v1           false        SelfSubjectRulesReview
subjectaccessreviews                             authorization.k8s.io/v1           false        SubjectAccessReview
horizontalpodautoscalers            hpa          autoscaling/v2                    true         HorizontalPodAutoscaler
cronjobs                            cj           batch/v1                          true         CronJob
jobs                                             batch/v1                          true         Job
certificatesigningrequests          csr          certificates.k8s.io/v1            false        CertificateSigningRequest
leases                                           coordination.k8s.io/v1            true         Lease
endpointslices                                   discovery.k8s.io/v1               true         EndpointSlice
events                              ev           events.k8s.io/v1                  true         Event
flowschemas                                      flowcontrol.apiserver.k8s.io/v1   false        FlowSchema
prioritylevelconfigurations                      flowcontrol.apiserver.k8s.io/v1   false        PriorityLevelConfiguration
helmchartconfigs                                 helm.cattle.io/v1                 true         HelmChartConfig
helmcharts                                       helm.cattle.io/v1                 true         HelmChart
addons                                           k3s.cattle.io/v1                  true         Addon
etcdsnapshotfiles                                k3s.cattle.io/v1                  false        ETCDSnapshotFile
nodes                                            metrics.k8s.io/v1beta1            false        NodeMetrics
pods                                             metrics.k8s.io/v1beta1            true         PodMetrics
ingressclasses                                   networking.k8s.io/v1              false        IngressClass
ingresses                           ing          networking.k8s.io/v1              true         Ingress
networkpolicies                     netpol       networking.k8s.io/v1              true         NetworkPolicy
runtimeclasses                                   node.k8s.io/v1                    false        RuntimeClass
poddisruptionbudgets                pdb          policy/v1                         true         PodDisruptionBudget
clusterrolebindings                              rbac.authorization.k8s.io/v1      false        ClusterRoleBinding
clusterroles                                     rbac.authorization.k8s.io/v1      false        ClusterRole
rolebindings                                     rbac.authorization.k8s.io/v1      true         RoleBinding
roles                                            rbac.authorization.k8s.io/v1      true         Role
priorityclasses                     pc           scheduling.k8s.io/v1              false        PriorityClass
csidrivers                                       storage.k8s.io/v1                 false        CSIDriver
csinodes                                         storage.k8s.io/v1                 false        CSINode
csistoragecapacities                             storage.k8s.io/v1                 true         CSIStorageCapacity
storageclasses                      sc           storage.k8s.io/v1                 false        StorageClass
volumeattachments                                storage.k8s.io/v1                 false        VolumeAttachment
ingressroutes                                    traefik.containo.us/v1alpha1      true         IngressRoute
ingressroutetcps                                 traefik.containo.us/v1alpha1      true         IngressRouteTCP
ingressrouteudps                                 traefik.containo.us/v1alpha1      true         IngressRouteUDP
middlewares                                      traefik.containo.us/v1alpha1      true         Middleware
middlewaretcps                                   traefik.containo.us/v1alpha1      true         MiddlewareTCP
serverstransports                                traefik.containo.us/v1alpha1      true         ServersTransport
tlsoptions                                       traefik.containo.us/v1alpha1      true         TLSOption
tlsstores                                        traefik.containo.us/v1alpha1      true         TLSStore
traefikservices                                  traefik.containo.us/v1alpha1      true         TraefikService
ingressroutes                                    traefik.io/v1alpha1               true         IngressRoute
ingressroutetcps                                 traefik.io/v1alpha1               true         IngressRouteTCP
ingressrouteudps                                 traefik.io/v1alpha1               true         IngressRouteUDP
middlewares                                      traefik.io/v1alpha1               true         Middleware
middlewaretcps                                   traefik.io/v1alpha1               true         MiddlewareTCP
serverstransports                                traefik.io/v1alpha1               true         ServersTransport
serverstransporttcps                             traefik.io/v1alpha1               true         ServersTransportTCP
tlsoptions                                       traefik.io/v1alpha1               true         TLSOption
tlsstores                                        traefik.io/v1alpha1               true         TLSStore
traefikservices                                  traefik.io/v1alpha1               true         TraefikService

其中比較常見的資源都有給出縮寫,比如 po 對應 pods 資源、rs 對應 replicasets 資源、svc 對應 services 資源、ing 對應 ingresses 資源等。

當然,並不是所有的資源都有官方的縮寫;而且 Kubernetes 本身也沒有提供自定義資源縮寫的功能;如果要自定義縮寫,筆者一般會在 .bashrc.zshrc 中透過額外定義 Shell Alias 的方式來實現。
較舊最新

張貼留言