In Kubernetes, you can define secrets using YAML manifests. Secrets are used to store sensitive information such as passwords, API tokens, and SSH keys securely within Kubernetes. Here's a basic example of how to define a secret:
- Create a YAML manifest file (e.g., my-secret.yaml) with the following content:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: <base64_encoded_username>
password: <base64_encoded_password>
Replace <base64_encoded_username> and <base64_encoded_password> with the Base64-encoded values of your username and password, respectively. You can use the echo command to encode values into Base64:
echo -n 'your_username' | base64
echo -n 'your_password' | base64
Replace your_username and your_password with your actual username and password.
- Apply the YAML manifest to your Kubernetes cluster:
kubectl apply -f my-secret.yaml
This will create a secret named my-secret in your Kubernetes cluster with the specified data.
- Accessing the secret from within a Pod: You can mount the secret into a Pod as a volume or expose it as environment variables. Here's an example of how to mount the secret as a volume:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
volumeMounts:
- name: secret-volume
mountPath: /etc/secret
readOnly: true
volumes:
- name: secret-volume
secret:
secretName: my-secret
In this example, the my-secret secret is mounted as a volume at /etc/secret within the Pod. The contents of the secret are accessible as files within that directory.
Remember that while Kubernetes secrets provide a convenient way to manage sensitive information, they are not encrypted by default. It's essential to follow security best practices to protect sensitive data within your Kubernetes cluster, such as limiting access to secrets, rotating secrets regularly, and using encryption where necessary.