简体   繁体   English

kubernetes 吊舱名称不断变化

[英]kubernetes pod names Keep changing

I need to make some automation testing on the Kubernetes pod but the pods names keeps changing like at first it was hisham-7cc8f99597 then after some time it is like this hisham-7cc8f99597-8j8lj is there a way I can know what is the name of the pod right now so I can use the name in the automation scenario?我需要对 Kubernetes 吊舱进行一些自动化测试,但吊舱名称一直在变化,就像一开始是hisham-7cc8f99597一样,过了一段时间后它就像这样hisham-7cc8f99597-8j8lj有没有办法我可以知道它的名称是什么现在是 pod,所以我可以在自动化场景中使用该名称?

Pods are cattle, not pets ( devops.stackexchange.com ) . Pod 是牛,不是宠物 ( devops.stackexchange.com ) Thus, pods do not have an identity.因此,豆荚没有身份。 We should therefore never directly communicate with a pod, but rather expose pods through a Service and communicate with the service instead.因此,我们不应该直接与 Pod 通信,而是通过Service公开 Pod 并与服务通信。 The service has a well-defined and constant name.该服务具有定义明确的常量名称。

To get the predictable pod names you must use state full sets要获得可预测的 pod 名称,您必须使用 state 全套

It depends on what your tests do and why do you need to know the Pod's name.这取决于您的测试做什么以及为什么需要知道 Pod 的名称。

If you just need the Pod's name:如果您只需要 Pod 的名称:

Assuming you have access to the cluster and kubectl in your tests, and that your pod is part of a deployment that has a specific label like app=nginx , you can query for its name like this:假设您在测试中可以访问集群和 kubectl,并且您的 pod 是具有特定 label (如app=nginx )的部署的一部分,您可以像这样查询其名称:

kubectl get po --selector=app=nginx-o=name

The output will be something like output 将类似于

pod/yourDeploymentName-7bd6c579fd-7gxzn

You can trim the leading pod/ through awk:您可以通过 awk 修剪前导pod/

kubectl get po --selector=app=nginx-o=name | awk -F "/" '{print $2}'

The variance in the name is due to you using Deployments, and is by design.名称的差异是由于您使用部署,并且是设计使然。

If you really need to use automation, don't go by pod name, go via the deployment.如果您确实需要使用自动化,请不要通过 pod 名称 go,go 通过部署。 So instead of using所以而不是使用

kubectl exec -it -n {namespace} {podname} -- {command}

Use利用

kubectl exec -it -n {namespace} deployments/{name-of-deployment} -- {command}

This will randomly pick one of the deployment pods to run the command.这将随机选择一个部署 pod 来运行命令。 Since all the pods are replicas, each one should behave the same way, so the results should be the same.由于所有 pod 都是副本,因此每个 pod 的行为方式都应该相同,因此结果应该相同。

You can also do some stdout manipulation such as:您还可以进行一些标准输出操作,例如:

kubectl get pods -n {namespace} \
  -l {label1}={value1} --no-headers | \
  awk '{print $1)' | head

To pick the first pod in the listing选择列表中的第一个 pod

You can also use a statefulset instead of a deployment which will name your pods in a known manner: {statefulset}-0 , {statefulset}-1 and so on and you can then go to a specific one for your command.您还可以使用 statefulset 而不是部署,它将以已知方式命名您的 pod: {statefulset}-0{statefulset}-1等等,然后您可以 go 为您的命令指定一个特定的名称。

You can get the pod name dynamically您可以动态获取 pod 名称

MYPOD=$(kubectl get pods \
    -n your_namesoace \
    --selector='your_app_selector' \
    -o \
    jsonpath='{.items[0].metadata.name)

This query will give you the first pod, after filtering it by namespace and release / app selector name.在按命名空间和发布/应用选择器名称过滤后,此查询将为您提供第一个 pod。

You can run in a for loop and execute a dynamic script.您可以在 for 循环中运行并执行动态脚本。

Here just answering your answering as you have asked, otherwise it's good to use the service name for communication not POD name if you are using that.这里只是按照您的要求回答您的回答,否则最好使用服务名称进行通信,而不是使用 POD 名称。

Either you can run your workload as POD however it's not good idea if you are running it as deployment .您可以将工作负载作为POD运行,但是如果将其作为deployment运行则不是一个好主意。

If you are running as POD you can maintain the single name each time you want.如果您作为POD运行,您可以在每次需要时维护单个名称。

Extra :额外

You can also use the metadata or downwardAPI to get the details of the existing POD.您还可以使用 metadata 或 downAPI 来获取现有 POD 的详细信息。

name will get injected into the environment variable (MY_POD_NAME) and your automation can get it automatically, use it with os.env . name将被注入到环境变量 (MY_POD_NAME) 中,您的自动化可以自动获取它,将其与os.env一起使用。

Example:例子:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
          printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
          sleep 10;
        done;
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName

Read more at: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/阅读更多: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

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

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