![](/img/trans.png)
[英]Modifying gcloud VM service account permissions without stopping the VM
[英]Gcloud - cannot provision many VM using one Service Account
我正在使用 Gcloud 来运行 Prow(持续集成服务器)。 我的一项工作是创建一个虚拟机,执行一些测试,然后删除该实例。 我使用服务帐户来创建 VM、运行测试。
#!/bin/bash
set -o errexit
cleanup() {
gcloud compute instances delete kyma-integration-test-${RANDOM_ID}
}
gcloud config set project ...
gcloud auth activate-service-account --key-file ...
gcloud compute instances create <vm_name> \
--metadata enable-oslogin=TRUE \
--image debian-9-stretch-v20181009 \
--image-project debian-cloud --machine-type n1-standard-4 --boot-disk-size 20 \
trap cleanup exit
gcloud compute scp --strict-host-key-checking=no --quiet <script.sh> <vm_name>:~/<script.sh>
gcloud compute ssh --quiet <vm_name> -- ./<script.sh>
一段时间后,我收到以下错误:
ERROR: (gcloud.compute.scp) INVALID_ARGUMENT: Login profile size exceeds 32 KiB. Delete profile values to make additional space.
实际上,对于该服务帐户, describe
命令会返回大量数据,例如sshPublicKeys
部分中的 ~70 个条目。
gcloud auth activate-service-account --key-file... gcloud compute os-login describe-profile
大多数公钥都引用已删除的 VM 实例。 如何执行此列表的清理? 或者是否有可能根本不存储该公钥?
对我有用的执行上述操作的一种非常粗略的方法是:
for i in $(gcloud compute os-login ssh-keys list); do echo $i; gcloud compute os-login ssh-keys remove --key $i; done
我在删除了几十个键后停止了这个(使用 Control-C),然后它又工作了。
实际上,在 GUI 中的项目元数据中,我没有看到很多关键。 仅有的 :
永久的解决方案是使用--ssh-key-expire-after 30s
。 您仍然需要使用上面的解决方案或更多的命令 kungfu 像这样(没有 grep)清理现有的密钥。
for i in $(gcloud compute os-login ssh-keys list --format="table[no-heading](value.fingerprint)"); do
echo $i;
gcloud compute os-login ssh-keys remove --key $i || true;
done
注意:您必须使用违规帐户。 gcloud config account activate ACCOUNT
和/或gcloud auth activate-service-account --key-file=FILE
或gcloud auth login
在脚本中需要一个新的 ssh 密钥:
# KEYNAME should be something like $HOME/.ssh/google_compute_engine
ssh-keygen -t rsa -N "" -f "${KEYNAME}" -C "${USERNAME}" || true
chmod 400 ${KEYNAME}*
cat > ssh-keys <<EOF
${USERNAME}:$(cat ${KEYNAME}.pub)
EOF
测试此解决方案:
while :; do
USERNAME=testing@test-project.iam.gserviceaccount.com
KEYNAME=~/.ssh/google_compute_engine
rm -f ~/.ssh/google_compute_engine*
ssh-keygen -t rsa -N "" -f "${KEYNAME}" -C "${USERNAME}" || true
chmod 400 ${KEYNAME}*
cat > ssh-keys <<EOF
${USERNAME}:$(cat ${KEYNAME}.pub)
EOF
gcloud --project=test-project compute ssh --ssh-key-expire-after 30s one-bastion-to-rule-them-all -- date
gcloud --project=test-project compute os-login ssh-keys list --format="table[no-heading](value.fingerprint)" \
|wc -l
done
这些密钥存储在您的项目元数据中,您可以通过 Google Console UI 删除它们来删除它们
看到您在问题中提到 OS Login:有一种方法可以使用此命令从用户的配置文件中删除特定的 SSH 密钥。 或者,我建议您不要执行 SCP,就像 John Hanley 所做的那样,将您正在复制的文件放入 Storage 中的实例并通过启动脚本检索它(您也可以使用自定义 Compute 映像)。
就我而言,我使用另一个服务帐户来运行 ssh,所以基本上我使用的是模拟。 如果您也在使用模拟,则需要从您模拟的服务帐户中删除 ssh 密钥列表。
for i in $(gcloud compute os-login ssh-keys list --impersonate-service-account="your_sc@serviceaccount.com" --format="table[no-heading](value.fingerprint)");
do echo $i;
gcloud compute os-login ssh-keys remove --key $i --impersonate-service-account="your_sc@serviceaccount.com" || true;
done
然后添加“--ssh-key-expire-after=7m” 时间量由您的需要定义
gcloud compute ssh ${MY_VM} --zone ${GKE_ZONE} --project ${PROJECT_ID} --tunnel-through-iap --ssh-key-expire-after=7m --impersonate-service-account="your_sc@serviceaccount.com"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.