Secret
Kubernetes Secrets are used to store sensitive information such as passwords, API keys, and certificates in a secure way. They help separate configuration from application code, improving security and manageability.
Type of Secret
Here are built-in secret type available in kubernetes:
Opaque:
arbitrary user-defined data.
kubernetes.io/service-account-token:
ServiceAccount token.
kubernetes.io/dockercfg:
serialized ~/.dockercfg
file.
kubernetes.io/dockerconfigjson:
serialized ~/.docker/config.json
file.
kubernetes.io/basic-auth:
credentials for basic authentication.
kubernetes.io/ssh-auth:
credentials for SSH authentication.
kubernetes.io/tls:
data for a TLS client or server.
bootstrap.kubernetes.io/token:
bootstrap token data.
Opaque Secret
Previously we already create a postgres service with persistent volume. But we still use plain text as the username and password config. We can use secret
to properly store the sensitive configuration and use it in our postgres deployment.
Create new file called postgres-secret.yaml
and put the secret definition there.
The data
section contains key value pair of our sensitive data. In here we store 2 data for username and password with key POSTGRES_USERNAME
and POSTGRES_PASSWORD
respectively. The value of the pair need to be base64
encoded first. We can use command echo -n "admin" | base64
to encode it.
Lets apply and validate our secret.
As you can see above the secret is created with DATA
length is 2
. We also can describe it to see the details.
Using Secret
Postgres
To use secret in our postgres deployment we need to update our env from plain text to value from secret.
Replace above section with this section below. This will tell kubernetes to look the value from secret
with name postgres-secret
and key POSTGRES_USER
for the user and POSTGRES_PASSWORD
for the password.
Lets apply and validate if the pods running without error. We can do similar testing like before, by doing port forward and then connect using psql
. We should still able to connect and see the data without any error.
Simple V2
Then lets update our apps to write and read from the database. You can just copy paste this and then run go get .
to download all the dependencies.
This app will get user
and password
configuration from environment variable and use it to connect to the server. And to access our service (postgres) from internal cluster we can use this format <service_name>.<namespace>.svc.cluster.local
.
Build our app and tag it as simple-go:v2
.
Update our deployment to use image simple-go:v2
and replace the env
with this one below. This will make POSTGRES_USER
and POSTGRES_PASSWORD
environment variable available in our apps.
Apply and check if the pods running as expected.
To test it we can use minkube service
command to expose our service. Then run curl
to the given url.
We should see our data there. Try to send POST
request to insert new data and GET
again.
We should see new data is properly inserted and returned.
References
Last updated