如何从GCR中为图像绘制一张图表

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (9)

我正在尝试为我们的产品编写Helm Charts。图像存储在GCR私人仓库中。所有组件的图表都已准备就绪,但我正在尝试在图表中为imagePullSecrets编写YAML文件。我从这里读过图表提示,

我也知道如何创建imagePullSecret:

kubectl create secret docker-registry mydockercfg \
        --docker-server "https://eu.gcr.io" \
        --docker-username _json_key \
        --docker-email not@val.id \
        --docker-password=$(cat your_service_account.json)

但我不知道如何将“your_service_account.json”的内容填入该图表的values.yaml的密码。我可以更改名称“your_service_account.json”以更新values.yaml的密码。

目前,我的实施如下:

$ cat values.yaml
secretName: gcr-json-key-test
imageCredentials:
  registry: us.gcr.io/xxxxx
  username: _json_key
  password:

secrets.yaml的内容:

$ cat templates/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Values.secretName }}
    labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: kubernetes.io/dockercfg
data:
  .dockerconfigjson: {{ template "imagePullSecret" . }}

_helpers.tpl的内容:

$ cat templates/_helpers.tpl
{{/*
Expand the name of the chart.
*/}}
{{- define "name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited 
to this (by the DNS naming spec).
 */}}
{{- define "fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "imagePullSecret" }}
{{- printf "{\"auths\": {\"%s\": {\"auth\": \"%s\"}}}" .Values.imageCredentials.registry (printf "%s:%s" .Values.imageCredentials.username .Values.imageCredentials.password | b64enc) | b64enc }}
{{- end }}

然后使用

$ helm install ./secrets --set imageCredentials.password "$(cat ./my_service_account.json)"

会导致错误:

错误:此命令需要1个参数:图表名称

我怎么解决这个问题?

提问于
用户回答回答于
用户回答回答于

可以使用以下步骤创建和部署它:

脚步:

  1. 使用docker_username和docker_password创建base64编码的字符串 $ echo -n "docker_username:docker_password" | base64 ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk
  2. 将在步骤1中获得的编码字符串作为auth键的值放在以下Json中,并填写所需的详细信息。 { "https://eu.gcr.io": { "username":"docker_user", "password":"docker_password", "email":"docker@gamil.com", "auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk", } }
  3. 将此json减少为由单引号括起来的字符串'{"https://eu.gcr.io":{"username":"docker_user","password":"docker_password","email":"docker@gamil.com","auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk"}}'
  4. 为上面的Json字符串创建base64编码的字符串,如下所示: $ echo -n '{"https://eu.gcr.io":{"username":"docker_user","password":"docker_password","email":"docker@gamil.com","auth":"ZG9rY2VyX3VzZXI6ZG9ja2VyX3Bhc3N3b3Jk"}}' | base64 eyJodHRwczovL2V1Lmdjci5pbyI6eyJ1c2VybmFtZSI6ImRva2Nlcl91c2VyIiwicGFzc3dvcmQiOiJkb2NrZXJfcGFzc3dvcmQiLCJlbWFpbCI6ImRvY2tlckBnYW1pbC5jb20iLCJhdXRoIjoiWkc5clkyVnlYM1Z6WlhJNlpHOWphMlZ5WDNCaGMzTjNiM0prIn19
  5. 以下列格式创建secret.yml: $ cat templates/secrets.yaml apiVersion: v1 kind: Secret metadata: name: {{ .Values.secretName }} labels: app: {{ template "fullname" . }} chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" release: "{{ .Release.Name }}" heritage: "{{ .Release.Service }}" type: kubernetes.io/dockercfg data: .dockercfg: {{ .Values.dockercfg }}
  6. 步骤4中获得的编码字符串放在value.yaml中: $ cat values.yaml secretName: gcr-json-key-test dockercfg:
  7. 使用以下命令安装图表: $ helm install ./secrets -n realse_name --set dockecfg="eyJodHRwczovL2V1Lmdjci5pbyI6eyJ1c2VybmFtZSI6ImRva2Nlcl91c2VyIiwicGFzc3dvcmQiOiJkb2NrZXJfcGFzc3dvcmQiLCJlbWFpbCI6ImRvY2tlckBnYW1pbC5jb20iLCJhdXRoIjoiWkc5clkyVnlYM1Z6WlhJNlpHOWphMlZ5WDNCaGMzTjNiM0prIn19" --debug 或将其存储在文件(.dockercfg)中并使用以下命令 $ helm install ./secrets -n realse_name --set dockecfg="$(cat ./.dockercfg )"

希望这会有用...... !! :)

扫码关注云+社区

领取腾讯云代金券