2020-09-10 06:27:00 +00:00
---
2021-05-11 02:07:40 +00:00
title: Managing Secrets using Configuration File
2020-09-10 06:27:00 +00:00
content_type: task
weight: 20
description: Creating Secret objects using resource configuration file.
---
<!-- overview -->
## {{% heading "prerequisites" %}}
{{< include " task-tutorial-prereqs . md " > }}
<!-- steps -->
2022-07-27 21:54:31 +00:00
## Create the Secret {#create-the-config-file}
2020-09-10 06:27:00 +00:00
2022-07-27 21:54:31 +00:00
You can define the `Secret` object in a manifest first, in JSON or YAML format,
and then create that object. The
2020-09-10 06:27:00 +00:00
[Secret ](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core )
resource contains two maps: `data` and `stringData` .
The `data` field is used to store arbitrary data, encoded using base64. The
`stringData` field is provided for convenience, and it allows you to provide
2022-07-22 18:26:20 +00:00
the same data as unencoded strings.
2020-09-10 06:27:00 +00:00
The keys of `data` and `stringData` must consist of alphanumeric characters,
`-` , `_` or `.` .
2022-07-22 18:26:20 +00:00
The following example stores two strings in a Secret using the `data` field.
2022-07-22 18:35:00 +00:00
1. Convert the strings to base64:
```shell
echo -n 'admin' | base64
echo -n '1f2d1e2e67df' | base64
```
{{< note > }}
2022-07-26 18:17:50 +00:00
The serialized JSON and YAML values of Secret data are encoded as base64 strings. Newlines are not valid within these strings and must be omitted. When using the `base64` utility on Darwin/macOS, users should avoid using the `-b` option to split long lines. Conversely, Linux users *should* add the option `-w 0` to `base64` commands or the pipeline `base64 | tr -d '\n'` if the `-w` option is not available.
{{< / note > }}
2022-07-22 18:35:00 +00:00
The output is similar to:
```
YWRtaW4=
MWYyZDFlMmU2N2Rm
```
2022-07-27 21:54:31 +00:00
1. Create the manifest:
2022-07-22 18:35:00 +00:00
```yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
```
Note that the name of a Secret object must be a valid
[DNS subdomain name ](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names ).
1. Create the Secret using [`kubectl apply` ](/docs/reference/generated/kubectl/kubectl-commands#apply ):
```shell
kubectl apply -f ./secret.yaml
```
The output is similar to:
```
secret/mysecret created
```
2020-09-10 06:27:00 +00:00
2022-07-22 18:39:58 +00:00
To verify that the Secret was created and to decode the Secret data, refer to
[Managing Secrets using kubectl ](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#verify-the-secret ).
2022-07-22 18:32:19 +00:00
### Specify unencoded data when creating a Secret
2020-09-10 06:27:00 +00:00
For certain scenarios, you may wish to use the `stringData` field instead. This
field allows you to put a non-base64 encoded string directly into the Secret,
and the string will be encoded for you when the Secret is created or updated.
A practical example of this might be where you are deploying an application
that uses a Secret to store a configuration file, and you want to populate
parts of that configuration file during your deployment process.
For example, if your application uses the following configuration file:
```yaml
apiUrl: "https://my.api.com/api/v1"
username: "< user > "
password: "< password > "
```
You could store this in a Secret using the following definition:
```yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
stringData:
config.yaml: |
apiUrl: "https://my.api.com/api/v1"
username: < user >
password: < password >
```
2022-07-30 03:19:26 +00:00
2022-07-22 18:39:58 +00:00
When you retrieve the Secret data, the command returns the encoded values,
and not the plaintext values you provided in `stringData` .
2020-09-10 06:27:00 +00:00
2022-07-22 18:43:48 +00:00
For example, if you run the following command:
2020-09-10 06:27:00 +00:00
```shell
kubectl get secret mysecret -o yaml
```
The output is similar to:
```yaml
apiVersion: v1
2021-04-06 05:37:59 +00:00
data:
config.yaml: YXBpVXJsOiAiaHR0cHM6Ly9teS5hcGkuY29tL2FwaS92MSIKdXNlcm5hbWU6IHt7dXNlcm5hbWV9fQpwYXNzd29yZDoge3twYXNzd29yZH19
2020-09-10 06:27:00 +00:00
kind: Secret
metadata:
creationTimestamp: 2018-11-15T20:40:59Z
name: mysecret
namespace: default
resourceVersion: "7225"
uid: c280ad2e-e916-11e8-98f2-025000000001
type: Opaque
```
2022-07-30 03:19:26 +00:00
### Specify both `data` and `stringData`
2020-09-10 06:27:00 +00:00
2022-07-26 18:17:50 +00:00
If you specify a field in both `data` and `stringData` , the value from `stringData` is used.
For example, if you define the following Secret:
2020-09-10 06:27:00 +00:00
```yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
stringData:
username: administrator
```
2022-07-22 18:43:48 +00:00
The `Secret` object is created as follows:
2020-09-10 06:27:00 +00:00
```yaml
apiVersion: v1
2021-04-06 05:37:59 +00:00
data:
username: YWRtaW5pc3RyYXRvcg==
2020-09-10 06:27:00 +00:00
kind: Secret
metadata:
creationTimestamp: 2018-11-15T20:46:46Z
name: mysecret
namespace: default
resourceVersion: "7579"
uid: 91460ecb-e917-11e8-98f2-025000000001
type: Opaque
```
2022-07-22 18:43:48 +00:00
`YWRtaW5pc3RyYXRvcg==` decodes to `administrator` .
2020-09-10 06:27:00 +00:00
2022-07-30 03:19:26 +00:00
## Clean up
2020-09-10 06:27:00 +00:00
2021-02-11 20:51:47 +00:00
To delete the Secret you have created:
2020-09-10 06:27:00 +00:00
```shell
2021-02-01 07:38:04 +00:00
kubectl delete secret mysecret
2020-09-10 06:27:00 +00:00
```
## {{% heading "whatsnext" %}}
- Read more about the [Secret concept ](/docs/concepts/configuration/secret/ )
2022-07-30 03:19:26 +00:00
- Learn how to [manage Secrets using kubectl ](/docs/tasks/configmap-secret/managing-secret-using-kubectl/ )
2021-05-11 02:07:40 +00:00
- Learn how to [manage Secrets using kustomize ](/docs/tasks/configmap-secret/managing-secret-using-kustomize/ )
2020-09-10 06:27:00 +00:00