kubernetes 部署将机密挂载为文件夹而不是文件 Secrets 与 ConfigMaps将 Secret 挂载为文件预计体积

问题描述

我有一个配置文件作为 kubernetes 中的秘密,我想将它安装到容器内的特定位置。问题是在容器内创建的卷是一个文件夹,而不是一个包含机密内容的文件。有什么办法可以解决吗? 我的部署如下所示:

SELECT
  inboundsms.DateSent 'Reply Date',inboundsms.SendingPhoneNumber 'Replying Number',inboundsms.Body 'Reply Message',outboundsms.DateSent 'Last Reminder',outboundsms.ReceivingPhoneNumber 'Reminder Number',outboundsms.Body 'Reminder Message',FROM #smslogtemp inboundsms
LEFT JOIN (
  SELECT * 
  FROM (
    -- need this sub-query to make sure the rnk column is correct
    SELECT sms.*,ROW_NUMBER() OVER (PARTITION BY sms.ReceivingPhoneNumber ORDER BY sms.DateSent DESC) rnk          
    FROM #smslogtemp sms
    WHERE sms.Direction = 'outbound-api'
  ) subquery 
) outboundsms on CAST(outboundsms.DateSent AS Date) < DATEADD(Day,1,inboundsms.DateSent)
  AND REPLACE(SUBSTRING(outboundsms.ReceivingPhoneNumber,3,10),'-','') = REPLACE(SUBSTRING(inboundsms.SendingPhoneNumber,'')
  AND outboundsms.rnk = 1
WHERE inboundsms.Direction = 'inbound' AND com.linkid_c = c.uniqueid_c AND CAST(inboundsms.DateSent AS Date) BETWEEN @StartDate AND @EndDate ORDER BY 11,inboundsms.Datesent ASC

解决方法

Secrets 与 ConfigMaps

Secrets 可让您存储和管理敏感信息(例如密码、私钥),而 ConfigMaps 用于非敏感配置数据。
正如您在 SecretsConfigMaps 文档中所见:

Secret 是包含少量敏感数据(例如密码、令牌或密钥)的对象。

ConfigMap 允许您将特定于环境的配置与容器映像分离,以便您的应用程序易于移植。

将 Secret 挂载为文件

可以创建Secret并将其作为文件或多个文件传递给Pods
我为您创建了一个简单的例子来说明它是如何工作的。 您可以在下方看到使用此 Secret 的示例 Secret 清单文件和 Deployment
注意:我将 subPathSecrets 一起使用,它按预期工作。

---
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  secret.file1: |
    c2VjcmV0RmlsZTEK
  secret.file2: |
    c2VjcmV0RmlsZTIK
---
apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: secrets-files
          mountPath: "/mnt/secret.file1"  # "secret.file1" file will be created in "/mnt" directory
          subPath: secret.file1
        - name: secrets-files
          mountPath: "/mnt/secret.file2"  # "secret.file2" file will be created in "/mnt" directory
          subPath: secret.file2
      volumes:
        - name: secrets-files
          secret:
            secretName: my-secret # name of the Secret
            

注意: Secret 应该在 Deployment 之前创建。

创建 SecretDeployment 后,我们可以看到它是如何工作的:

$ kubectl get secret,deploy,pod
NAME                         TYPE                                  DATA   AGE
secret/my-secret             Opaque                                2      76s

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           76s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7c67965687-ph7b8   1/1     Running   0          76s

$ kubectl exec nginx-7c67965687-ph7b8 -- ls /mnt
secret.file1
secret.file2
$ kubectl exec nginx-7c67965687-ph7b8 -- cat /mnt/secret.file1
secretFile1
$ kubectl exec nginx-7c67965687-ph7b8 -- cat /mnt/secret.file2
secretFile2

预计体积

我认为实现目标的更好方法是使用 projected volume

投影卷将多个现有卷源映射到同一目录中。

Projected Volume documentation 中,您可以找到详细说明,此外我还创建了一个示例,可以帮助您了解其工作原理。 使用投影卷,我将 secret.file1 中的 secret.file2Secretconfig.file1 中的 ConfigMap 作为文件装入 Pod

---
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  secret.file1: |
    c2VjcmV0RmlsZTEK
  secret.file2: |
    c2VjcmV0RmlsZTIK
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  config.file1: |
    configFile1  
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: all-in-one
      mountPath: "/config-volume"
      readOnly: true
  volumes:
  - name: all-in-one
    projected:
      sources:
      - secret:
          name: my-secret
          items:
            - key: secret.file1
              path: secret-dir1/secret.file1
            - key: secret.file2
              path: secret-dir2/secret.file2
      - configMap:
          name: my-config
          items:
            - key: config.file1
              path: config-dir1/config.file1

我们可以检查它是如何工作的:

$ kubectl exec nginx -- ls /config-volume
config-dir1
secret-dir1
secret-dir2    
$ kubectl exec nginx -- cat /config-volume/config-dir1/config.file1
configFile1
$ kubectl exec nginx -- cat /config-volume/secret-dir1/secret.file1
secretFile1
$ kubectl exec nginx -- cat /config-volume/secret-dir2/secret.file2
secretFile2

如果此回复没有回答您的问题,请提供有关您的 Secret 的更多详细信息以及您想要实现的目标。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...