k8s笔记——GVK是什么

k8s的GVK是什么

在 Kubernetes 中,GVK 是指 Group、Version 和 Kind 三个字段,用于唯一标识 Kubernetes 资源对象。

  • Group 指的是 Kubernetes API 中的资源组,例如 apps、batch、core 等。
  • Version 指的是资源对象的 API 版本,例如 v1、v1beta1、v2alpha1 等。
  • Kind 指的是资源对象的类型,例如 Pod、Service、Deployment 等。

在 Kubernetes 中,所有的资源对象都必须要有一个 GVK,以便于 Kubernetes 控制器进行操作和管理。对于一个特定的资源对象,可以通过 kubectl api-resources 命令来查看它的 GVK 信息。例如,Pod 资源对象的 GVK 是 core/v1/Pod,其中 core 是资源组,v1 是 API 版本,Pod 是资源对象类型。

查看资源的GVK

查看pod

kubectl explain pod

输出

KIND:       Pod
VERSION:    v1

DESCRIPTION:
    Pod is a collection of containers that can run on a host. This resource is
    created by clients and scheduled onto hosts.

FIELDS:
  apiVersion    <string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind  <string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata      <ObjectMeta>
    Standard object's metadata. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec  <PodSpec>
    Specification of the desired behavior of the pod. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

  status        <PodStatus>
    Most recently observed status of the pod. This data may not be up to date.
    Populated by the system. Read-only. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

查看service

kubectl explain service

输出

KIND:       Service
VERSION:    v1

DESCRIPTION:
    Service is a named abstraction of software service (for example, mysql)
    consisting of local port (for example 3306) that the proxy listens on, and
    the selector that determines which pods will answer requests sent through
    the proxy.

FIELDS:
  apiVersion    <string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind  <string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata      <ObjectMeta>
    Standard object's metadata. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec  <ServiceSpec>
    Spec defines the behavior of a service.
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

  status        <ServiceStatus>
    Most recently observed status of the service. Populated by the system.
    Read-only. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

查看Deployment

kubectl explain Deployment

输出

GROUP:      apps
KIND:       Deployment
VERSION:    v1

DESCRIPTION:
    Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
  apiVersion    <string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind  <string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata      <ObjectMeta>
    Standard object's metadata. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec  <DeploymentSpec>
    Specification of the desired behavior of the Deployment.

  status        <DeploymentStatus>
    Most recently observed status of the Deployment.

查看NameSpace

kubectl explain NameSpace

查看Node

kubectl explain Node

参考资料

k8s基础2之GVK与GVR

在 controller-runtime 库中,可以使用 Client 接口的 List 方法来列出指定类型的资源。以下是一个具体的示例: ```go package main import ( "context" "fmt" "os" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" ) func main() { // 获取 Kubernetes 配置 kubeconfig := config.GetConfigOrDie() // 通过 Kubernetes 配置创建 clientset clientset, err := kubernetes.NewForConfig(kubeconfig) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create clientset: %v\n", err) os.Exit(1) } // 通过 Kubernetes 配置创建 client client, err := client.New(kubeconfig, client.Options{}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create client: %v\n", err) os.Exit(1) } // 定义要列出的资源类型和命名空间 namespace := "default" gvk := schema.GroupVersionKind{Version: "v1", Kind: "Pod"} // 创建一个对象列表来存储结果 objectList := &corev1.PodList{} // 使用 client 的 List 方法列出指定类型的资源 err = client.List(context.Background(), objectList, &client.ListOptions{ Namespace: namespace, Raw: &metav1.ListOptions{}, ResourceVersion: "", }) if err != nil { if errors.IsNotFound(err) { fmt.Fprintf(os.Stderr, "Resource not found: %v\n", err) os.Exit(1) } else { fmt.Fprintf(os.Stderr, "Failed to list resources: %v\n", err) os.Exit(1) } } // 输出结果 fmt.Printf("Found %d pods in namespace %s:\n", len(objectList.Items), namespace) for _, obj := range objectList.Items { fmt.Printf(" %s\n", obj.Name) } } ``` 这个示例演示了如何使用 controller-runtime 库中的 Client 接口来列出指定命名空间中的 Pod 资源。你可以根据自己的需求修改示例代码中的参数来获取不同类型的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值