是否可以从Kubernetes的容器中获取容器名称? [英] Is it possible to get the container name from within the container in kubernetes?

查看:1267
本文介绍了是否可以从Kubernetes的容器中获取容器名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下Pod规格.

Say I have the following pod spec.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3
  template:
    metadata:
      labels:
        # Apply this label to pods and default
        # the Deployment label selector to this value
        app: nginx
    spec:
      containers:
      - name: nginx
        # Run this image
        image: nginx:1.10

在这里,容器的名称是nginx.有没有办法从正在运行的容器中获取"nginx"字符串?

Here, the name of the container is nginx. Is there a way to get the "nginx" string from within the running container?

我的意思是,一旦我使用类似

I mean, once I exec into the container with something like

kubectl exec -it <pod-name> -c nginx bash

在pod规范中是否有一种编程方式来获取给定的容器名称?

Is there a programmatic way to get to the given container name in the pod spec ?

请注意,这不一定是在docker ps中打印的docker容器名称. Kubernetes为衍生的Docker容器编写了一个更长的名称.

Note that this is not necessarily the docker container name that gets printed in docker ps. Kubernetes composes a longer name for the spawned docker container.

向下api 在这方面看起来很有希望.但是,在Capabilities of the Downward API部分中未提及container name.

The downward api looks promising in this regard. However container name is not mentioned in the Capabilities of the Downward API section.

推荐答案

通过向下的api无法使用容器名称.您可以使用 yaml锚和别名(引用).不幸的是,它们没有作用域,因此您将不得不为锚定一个唯一的名称-解析的文档中所显示的锚名称并不重要.

The container name is not available trough the downward api. You can use yaml anchors and aliases (references). Unfortunately they are not scoped so you will have to come up with unique names for the anchors - it does not matter what they are as they are not present in the parsed document.

先前序列化的节点的后续出现表示为别名节点.该节点的第一次出现必须由锚点标记,以允许后续出现的出现为别名节点.

Subsequent occurrences of a previously serialized node are presented as alias nodes. The first occurrence of the node must be marked by an anchor to allow subsequent occurrences to be presented as alias nodes.

别名节点由"*"指示符表示. 别名是指具有相同锚点的最近的先前节点.别名节点使用文档中以前未出现的锚点是错误的.指定任何别名节点不使用的锚点不是错误.

An alias node is denoted by the "*" indicator. The alias refers to the most recent preceding node having the same anchor. It is an error for an alias node to use an anchor that does not previously occur in the document. It is not an error to specify an anchor that is not used by any alias node.

First occurrence: &anchor Foo
Second occurrence: *anchor
Override anchor: &anchor Bar
Reuse anchor: *anchor

这是一个完整的示例:

apiVersion: v1
kind: Pod
metadata:
  name: reftest
spec:
  containers:
  - name: &container1name first
    image: nginx:1.10
    env:
    - name: MY_CONTAINER_NAME
      value: *container1name
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
  - name: &container2name second
    image: nginx:1.10
    env:
    - name: MY_CONTAINER_NAME
      value: *container2name
    - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name

这篇关于是否可以从Kubernetes的容器中获取容器名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆