Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to Secret file in pod

Tags:

kubernetes

I define a Secret:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
stringData:
  config.yaml: |-
    apiUrl: "https://my.api.com/api/v1"
    username: Administrator
    password: NewPasswdTest11

And then creating volume mount in Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-webapp-test
  labels:
    name: k8s-webapp-test
    version: 1.0.4
spec:
  replicas: 2
  selector:
    matchLabels:
      name: k8s-webapp-test
      version: 1.0.4
  template:
    metadata:
      labels:
        name: k8s-webapp-test
        version: 1.0.4
    spec:
      nodeSelector:
        kubernetes.io/os: windows
      volumes:
      - name: secret-volume
        secret:
          secretName: string-data-secret
      containers:
      - name: k8s-webapp-test
        image: dockerstore/k8s-webapp-test:1.0.4
        ports:
        - containerPort: 80
        volumeMounts:
        - name: secret-volume
          mountPath: "/secrets"
          readOnly: false

So, after the deployment, I have 2 pods with volume mounts in C:\secrets (I do use Windows nodes). When I try to edit config.yaml that is located in C:\secrets folder, I get following error:

Access to the path 'c:\secrets\config.yaml' is denied.

Although I marked file as readOnly false I cannot write to it. How can I modify the file?

like image 463
eddyuk Avatar asked Jun 20 '19 12:06

eddyuk


People also ask

Is it possible to mount secrets to pods?

Secrets can be mounted as data volumes or exposed as environment variables to be used by a container in a Pod. Secrets can also be used by other parts of the system, without being directly exposed to the Pod.

How do I add secrets to deployment file?

Add the Secrets to the Deployment as environment variables For the mariadb-root-password Secret, specify the Secret and the key you want by adding an env list/array to the container spec in the Deployment and setting the environment variable value to the value of the key in your Secret.

How do you edit secrets in Kubernetes?

The most direct (and interactive) way should be to execute kubectl edit secret <my secret> . Run kubectl get secrets if you'd like to see the list of secrets managed by Kubernetes.

How do you store secrets in Kubernetes?

When you create a Secret with kubectl create -f secret. yaml , Kubernetes stores it in etcd. The Secrets are stored in clear in etcd unless you define an encryption provider. When you define the provider, before the Secret is stored in etcd and after the values are submitted to the API, the Secrets are encrypted.


Video Answer


1 Answers

As you can see here it is not possible by intention:

Secret, configMap, downwardAPI and projected volumes will be mounted as read-only volumes. Applications that attempt to write to these volumes will receive read-only filesystem errors. Previously, applications were allowed to make changes to these volumes, but those changes were reverted at an arbitrary interval by the system. Applications should be re-configured to write derived files to another location

You can look into using an init container which maps the secret and then copies it to the desired location where you might be able to modify it.

As an alternative to the init container you might also use a container lifecycle hook i.e. a PostStart-hook which executes immediately after a container is created.

lifecycle:
  postStart:
    exec:
      command:
      - "/bin/sh"
      - "-c"
      - >
        cp -r /secrets ~/secrets;
like image 121
papanito Avatar answered Oct 18 '22 06:10

papanito