The easiest ways to create the Kubernetes secret is by using the kubectl command and --from-literal flag. For example to understand Kubernetes secret creation we need three things.
- secret-name - test-secret
- username - test-user
- password - testP@ssword
kubectl create secret generic test-secret --from-literal=username=test-user --from-literal=password=testP@ssword
kubectl get secret test-secret
kubectl describe secret test-secret
echo -n ‘test-user’ | base64
Create a secret
apiVersion: v1
kind: Secret
metadata:
name: mysql-test-secret
type: kubernetes.io/basic-auth
stringData:
password: test1234
Create a deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-test-secret
key: password
ports:
- containerPort: 3306
name: mysql