从 .json 文件创建 kubernetes secret

Mr.*_*tle 6 kubernetes

我有一个带有一些键的 json 文件,如下所示:

{
  "a":"someval"
  "b":"someval"
  .. more keys
}
Run Code Online (Sandbox Code Playgroud)

如何将这些密钥添加到 kubernetes 的秘密中?

当我尝试$ kubectl create secret generic mysecret --from-file=file.json它返回一个包含文件的秘密时,但我想将文件的内容映射到秘密,而不是将文件添加为秘密。

输出:

$ kubectl get secret mysecret -o yaml

apiVersion: v1
data:
  file.json: #base64 encoded stuff here.
kind: Secret
Run Code Online (Sandbox Code Playgroud)

想要的输出:

$ kubectl get secret mysecret -o yaml

apiVersion: v1
data:
  a: someval
  b: someval
kind: Secret
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Vas*_*pov 7

如果您有平面(非嵌套)JSON,请尝试此操作(假设您已jq安装工具):

kubectl create secret generic test --from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" YOUR_FILE.json)
Run Code Online (Sandbox Code Playgroud)


P E*_*ram 5

试试这些步骤

kubectl proxy --port 8000 &  
curl localhost:8000/api/v1/namespaces/default/secrets 

curl localhost:8000/api/v1/namespaces/default/secrets \
  -X POST -H "Content-Type: application/json" \
  --data '{"metadata":{"name":"mytest"},"stringData":{"a":"test","b":"test","c":"test"}}'

master $ curl localhost:8000/api/v1/namespaces/default/secrets/mytest{
  "kind": "Secret",
  "apiVersion": "v1",
  "metadata": {
    "name": "mytest",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/secrets/mytest",
    "uid": "55736602-725e-11e9-b3a2-0242ac110034",
    "resourceVersion": "2948",
    "creationTimestamp": "2019-05-09T13:28:29Z"
  },
  "data": {
    "a": "dGVzdA==",
    "b": "dGVzdA==",
    "c": "dGVzdA=="
  },
  "type": "Opaque"
}
Run Code Online (Sandbox Code Playgroud)