Google 컴퓨팅 엔진에서 실행 중인 kubernetes 포드가 메타데이터 서비스에 액세스할 수 없습니다.

Google 컴퓨팅 엔진에서 실행 중인 kubernetes 포드가 메타데이터 서비스에 액세스할 수 없습니다.

Google 컴퓨팅 엔진에서 실행되는 k8 포드 내부에서 Google Cloud Python SDK를 실행하려고 합니다. VM에는 Secrets Manager에 대한 액세스 권한을 제공하는 서비스 계정이 연결되어 있습니다. 호스트에서 Secrets Manager에 액세스할 수 있지만 k8 Pod에서 Python SDK를 실행하면 메타데이터 서비스에 액세스할 수 없다고 불평합니다.

>>> secret_id = 'unskript_test'
>>> name = client.secret_path(project_id, secret_id)
>>> response = client.get_secret(request={"name": name})
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 67, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/grpc/_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/opt/conda/lib/python3.7/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Getting metadata from plugin failed with error: Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Compute Engine Metadata server unavailable"
    debug_error_string = "{"created":"@1630634901.103779641","description":"Getting metadata from plugin failed with error: Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Compute Engine Metadata server unavailable","file":"src/core/lib/security/credentials/plugin/plugin_credentials.cc","file_line":90,"grpc_status":14}"
>

Metadata.google.internal이 k8 포드에서 확인되지 않습니다.

jovyan@jovyan-25ca6c8c-157d-49e5-9366-f9d57fcb7a9f:~$ wget http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
--2021-09-03 02:11:19--  http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
Resolving metadata.google.internal (metadata.google.internal)... failed: Name or service not known.
wget: unable to resolve host address ‘metadata.google.internal’

하지만 호스트가 해결할 수 있습니다.

ubuntu@gcp-test-proxy:~$ wget http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
--2021-09-03 02:11:27--  http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
Resolving metadata.google.internal (metadata.google.internal)... 169.254.169.254
Connecting to metadata.google.internal (metadata.google.internal)|169.254.169.254|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2021-09-03 02:11:27 ERROR 403: Forbidden.

Pod가 Metadata.google.internal을 확인하도록 하려면 어떻게 해야 하나요?

답변1

metadata.google.internal이는 Kubernetes Pod가 DNS 이름을 적절한 IP로 확인할 수 없기 때문에 발생합니다 . 호스트 시스템에는 /etc/hosts해당 도메인을 IP(169.254.169.254)로 하드코딩하는 항목이 있을 수 있습니다.

파일 을 수정하여 포드에서 이를 복제할 수 있어야 합니다 /etc/hosts.

이는 GCP에서 실행되는 VM에서만 작동한다는 점을 기억하세요. 외부에서 IP 주소 169.254.169.254는 특별한 의미가 없는 또 다른 IP 주소일 뿐입니다.

편집: 방금 내 GCP VM 중 하나에서 /etc/hosts를 확인했는데, 내가 찾은 내용은 다음과 같습니다.

$ cat /etc/hosts
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
169.254.169.254 metadata.google.internal metadata

그러니 마지막 줄을 포드에 복사해 보세요 /etc/hosts.

답변2

문제는 microk8s가 호스트 등 호스트 항목을 포드에 복사하지 않는 데 있었습니다. k3로 옮기고 나서 해결됐어요

관련 정보