kubernetesでpodを定義するときのフィールド情報をファイルでコンテナに暴露する
6366 ワード
参照先:https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/
この文書では、podを定義するときのフィールド、例えばラベル、注釈などのいくつかのフィールドをファイル形式でコンテナに露出し、コンテナが実行時にこれらのフィールドを読み取ることができるようにする方法について説明します.ここでは、ファイル形式で環境変数の形式ではないことに注意してください.この特性の主な目的はpod,コンテナを定義する際の情報を運転中のコンテナに容易に伝達することであり,例えばコンテナ運転時のcpu,メモリなどの限定であり,運転中のコンテナはクラスタと対話することなく直接容易にこれらの情報を得ることができる.
Store Pod fields
次のpod定義があると仮定します.
プロファイルにはpodというdownwardAPIというvolumeがあり、コンテナはこのvolumeをディレクトリ/etc/podinfoにマウントしています.volumeの内容に注目し、items配列を見てみましょう.1つ目は、プロファイルの「metadata.labels」パスの下にある「labels」というファイルを示します.同じ2つ目は「annotations」というファイルで、内容はプロファイル内のmetadataです.annotations「パスの下の内容.上記の例では、「labels」でも「annotations」でもpodに関する内容であり、containerの内容ではないことに注意してください.
podを作成するには:
コンテナが動作していることを確認します.
コンテナログの表示:
出力には、ファイル「labels」と「annotations」の内容が表示されます.
コンテナのインタラクティブshellを実行するには:
shellでlabelsファイルの内容を表示します.
出力は次のとおりです.
「annotations」ファイルは同じで、省略します.
次のコマンドを実行して、ディレクトリの内容を表示します.
Labelsとannotationsはシンボルリンクファイルであり、元のファイルはホストのディレクトリの下に保存されます.つまりpodのプロファイル変更がリードされると、まずホスト上の元のファイルが更新されますが、podは再作成する必要がなく、labels、annotationsファイルを自動的に更新することができます.
Store Container fields
前節では、pod定義のpodに関連するフィールドをコンテナに露出する方法について説明し、pod定義のcontainerに関連するフィールドを実行中のコンテナに露出する方法について説明します.podの構成ファイルの例を次に示します.
コンフィギュレーション・ファイルから、downwardAPIセクションでは、コンテナ・フィールドに関する参照に1つの制限条件が追加されています.containerNameは、1つのpodに複数のコンテナ定義が含まれるため、このフィールドを追加して、最後に参照されるコンテナの内容を制限します.その他は、前節で説明した内容と同じです.
Capabilities of the Downward API
現在、任意のフィールドがこのように運転中のコンテナに露出できるわけではないようです.
FeildRef、すなわち露出可能なpod定義の項目: spec.nodeName - the node’s name status.hostIP - the node’s IP metadata.name - the pod’s name metadata.namespace - the pod’s namespace status.podIP - the pod’s IP address spec.serviceAccountName - the pod’s service account name metadata.uid - the pod’s UID metadata.labels[''] - the value of the pod’s label (for example, metadata.labels['mylabel']); available in Kubernetes 1.9+ metadata.annotations[''] - the value of the pod’s annotation (for example, metadata.annotations['myannotation']); available in Kubernetes 1.9+
resourceFieldRef、すなわち、container定義内のアイテムを露出することができます. A Container’s CPU limit A Container’s CPU request A Container’s memory limit A Container’s memory request
この文書では、podを定義するときのフィールド、例えばラベル、注釈などのいくつかのフィールドをファイル形式でコンテナに露出し、コンテナが実行時にこれらのフィールドを読み取ることができるようにする方法について説明します.ここでは、ファイル形式で環境変数の形式ではないことに注意してください.この特性の主な目的はpod,コンテナを定義する際の情報を運転中のコンテナに容易に伝達することであり,例えばコンテナ運転時のcpu,メモリなどの限定であり,運転中のコンテナはクラスタと対話することなく直接容易にこれらの情報を得ることができる.
Store Pod fields
次のpod定義があると仮定します.
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: client-container
image: k8s.gcr.io/busybox
command: ["sh", "-c"]
args:
- while true; do
if [[ -e /etc/podinfo/labels ]]; then
echo -en '
'; cat /etc/podinfo/labels; fi;
if [[ -e /etc/podinfo/annotations ]]; then
echo -en '
'; cat /etc/podinfo/annotations; fi;
sleep 5;
done;
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations
プロファイルにはpodというdownwardAPIというvolumeがあり、コンテナはこのvolumeをディレクトリ/etc/podinfoにマウントしています.volumeの内容に注目し、items配列を見てみましょう.1つ目は、プロファイルの「metadata.labels」パスの下にある「labels」というファイルを示します.同じ2つ目は「annotations」というファイルで、内容はプロファイル内のmetadataです.annotations「パスの下の内容.上記の例では、「labels」でも「annotations」でもpodに関する内容であり、containerの内容ではないことに注意してください.
podを作成するには:
kubectl create -f https://k8s.io/examples/pods/inject/dapi-volume.yaml
コンテナが動作していることを確認します.
kubectl get pods
コンテナログの表示:
kubectl logs kubernetes-downwardapi-volume-example
出力には、ファイル「labels」と「annotations」の内容が表示されます.
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
build="two"
builder="john-doe"
コンテナのインタラクティブshellを実行するには:
kubectl exec -it kubernetes-downwardapi-volume-example -- sh
shellでlabelsファイルの内容を表示します.
/# cat /etc/podinfo/labels
出力は次のとおりです.
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
「annotations」ファイルは同じで、省略します.
次のコマンドを実行して、ディレクトリの内容を表示します.
/# ls -laR /etc/podinfo
drwxr-xr-x ... Feb 6 21:47 ..2982_06_02_21_47_53.299460680
lrwxrwxrwx ... Feb 6 21:47 ..data -> ..2982_06_02_21_47_53.299460680
lrwxrwxrwx ... Feb 6 21:47 annotations -> ..data/annotations
lrwxrwxrwx ... Feb 6 21:47 labels -> ..data/labels
/etc/..2982_06_02_21_47_53.299460680:
total 8
-rw-r--r-- ... Feb 6 21:47 annotations
-rw-r--r-- ... Feb 6 21:47 labels
Labelsとannotationsはシンボルリンクファイルであり、元のファイルはホストのディレクトリの下に保存されます.つまりpodのプロファイル変更がリードされると、まずホスト上の元のファイルが更新されますが、podは再作成する必要がなく、labels、annotationsファイルを自動的に更新することができます.
Store Container fields
前節では、pod定義のpodに関連するフィールドをコンテナに露出する方法について説明し、pod定義のcontainerに関連するフィールドを実行中のコンテナに露出する方法について説明します.podの構成ファイルの例を次に示します.
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example-2
spec:
containers:
- name: client-container
image: k8s.gcr.io/busybox:1.24
command: ["sh", "-c"]
args:
- while true; do
echo -en '
';
if [[ -e /etc/podinfo/cpu_limit ]]; then
echo -en '
'; cat /etc/podinfo/cpu_limit; fi;
if [[ -e /etc/podinfo/cpu_request ]]; then
echo -en '
'; cat /etc/podinfo/cpu_request; fi;
if [[ -e /etc/podinfo/mem_limit ]]; then
echo -en '
'; cat /etc/podinfo/mem_limit; fi;
if [[ -e /etc/podinfo/mem_request ]]; then
echo -en '
'; cat /etc/podinfo/mem_request; fi;
sleep 5;
done;
resources:
requests:
memory: "32Mi"
cpu: "125m"
limits:
memory: "64Mi"
cpu: "250m"
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "cpu_limit"
resourceFieldRef:
containerName: client-container
resource: limits.cpu
- path: "cpu_request"
resourceFieldRef:
containerName: client-container
resource: requests.cpu
- path: "mem_limit"
resourceFieldRef:
containerName: client-container
resource: limits.memory
- path: "mem_request"
resourceFieldRef:
containerName: client-container
resource: requests.memory
コンフィギュレーション・ファイルから、downwardAPIセクションでは、コンテナ・フィールドに関する参照に1つの制限条件が追加されています.containerNameは、1つのpodに複数のコンテナ定義が含まれるため、このフィールドを追加して、最後に参照されるコンテナの内容を制限します.その他は、前節で説明した内容と同じです.
Capabilities of the Downward API
現在、任意のフィールドがこのように運転中のコンテナに露出できるわけではないようです.
FeildRef、すなわち露出可能なpod定義の項目:
resourceFieldRef、すなわち、container定義内のアイテムを露出することができます.