Skip to content

Instantly share code, notes, and snippets.

@dmancloud
Last active October 31, 2024 09:26
Show Gist options
  • Save dmancloud/93947988199273859ea6ff5303f93389 to your computer and use it in GitHub Desktop.
Save dmancloud/93947988199273859ea6ff5303f93389 to your computer and use it in GitHub Desktop.
How to use Kubernetes Secrets

How to use Kubernetes Secrets

Create Kubernetes secrets using kubectl and --from-literal

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

Verify the secret using the following command

kubectl get secret test-secret

Describe The Secret

kubectl describe secret test-secret

Base64 Encoded Kubernetes Secrets

echo -n ‘test-user’ | base64

Using Kubernetes Secrets In A Deployment (mysql)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment