简体   繁体   English

获取属于kubernetes复制控制器的Pod

[英]get pods belonging to a kubernetes replication controller

I'm wondering whether there is a way using the kubernetes API to get the the details of the pods that belong to a given replication controller. 我想知道是否有一种使用kubernetes API的方法来获取属于给定复制控制器的Pod的详细信息。 I've looked at the reference and the only way as I see it, is getting the pods list and go through each one checking whether it is belongs to a certain RC by analysing the 'annotations' section. 我已经查看了参考资料,并且看到了它,唯一的方法是获取豆荚列表,并通过分析“注释”部分来检查每个豆荚列表是否属于某个RC。 It's again a hard job since the json specifies the whole 'kubernetes.io/created-by' part as a single string. 由于json将整个“ kubernetes.io/created-by”部分指定为单个字符串,因此这又是一项艰巨的工作。

Every Replication Controller has a selector which defines the set of pods managed by it: 每个Replication Controller都有一个选择器 ,该选择器定义了它所管理的Pod集:

selector:
    label_name_1: some_value
    label_name_2: another_value

You can use the selector to get all the pods with a corresponding set of labels: 您可以使用选择器来获取所有带有相应标签集的窗格:

https://k8s.example.com/api/v1/pods?labelSelector=label_name_1%3Dsome_value,label_name_2%3Danother_value

To get the details of pods belonging to a particular replication controller we need to include selector field in the yaml file that defines the replication controller and matching label fields in the template of the pod to be created. 要获取属于特定复制控制器的Pod的详细信息,我们需要在yaml文件中包括选择器字段,该字段定义复制控制器,并在要创建的Pod模板中包含匹配的标签字段。 An example of a replication controller yaml file is given below: 下面给出了复制控制器yaml文件的示例:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80 

To list out the pod names use the command: 要列出窗格名称,请使用以下命令:

pods=$(kubectl get pods --selector=app=nginx --output=jsonpath={.items..metadata.name})
echo $pods

In the above command the --output=jsonpath option specifies the expression that just gets the name of each pod. 在上面的命令中,--output = jsonpath选项指定仅获取每个pod名称的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM