k8s uses OwnerRef to get all pod hierarchies in the cluster

王林
Release: 2024-02-08 21:45:34
forward
745 people have browsed it

k8s 使用 OwnerRef 获取集群中的所有 pod 层次结构

#php Editor Banana today will introduce to you a method to use OwnerRef to obtain the hierarchy of all Pods in the Kubernetes cluster. Kubernetes is a popular container orchestration platform that helps us manage and schedule containerized applications. In Kubernetes, Pod is the smallest schedulable unit, often composed of one or more containers. Understanding the Pod hierarchy is important to understanding and optimizing applications in a cluster. Let’s learn how to use OwnerRef to get all Pod hierarchies in the cluster!

Question content

We want to get all pods in the cluster, so we use something like this:

pods, err := client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
Copy after login

This code will receive all pods in the cluster.

My question is: Is there a code or library that can bring all pods with owner references to . That is, if the pods are owned by deployment or statfulset etc. you will get all the hierarchy, the trick here is if I need like some recursion Get an extra level like statefulset which is owned by a controller with a custom type

Workaround

As @CoolNetworking suggested, there is a library or code gives you all pods and their owner references, but you can use the k8s API to retrieve the owner reference for each pod. You can then use the k8s API to retrieve the owner object referenced by each owner. This will allow you to create a hierarchy of all Pods in the cluster.

The Kubernetes API is a resource-based (RESTful) programming interface provided over HTTP. It supports retrieving, creating, updating, and deleting primary resources via standard HTTP verbs (POST, PUT, PATCH, DELETE, GET).

Most Kubernetes API resource types are objects: they represent concrete instances of concepts on the cluster, such as Pods or namespaces.

See documentation kubernetes API for more information

The above is the detailed content of k8s uses OwnerRef to get all pod hierarchies in the cluster. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!