Kahibaro
Discord Login Register

Secrets

Types of Secrets in OpenShift

In OpenShift, Secret objects are Kubernetes secrets with some OpenShift-specific integrations and usage patterns. At a high level they all store key–value data, but are used for different purposes.

Opaque (generic) secrets

Example:

oc create secret generic app-credentials \
  --from-literal=api-key='my-api-key' \
  --from-literal=api-secret='my-secret'

Opaque is often the default when no type is specified.

Docker registry (image pull) secrets

Example (create from Docker credentials):

oc create secret docker-registry pull-secret \
  --docker-server=quay.io \
  --docker-username=myuser \
  --docker-password='mypassword' \
  --docker-email=myuser@example.com

TLS secrets

Required keys:

Example:

oc create secret tls my-tls-secret \
  --cert=server.crt \
  --key=server.key

Routes can reference this secret for edge or re-encrypt termination.

Basic authentication secrets

Example:

oc create secret generic db-basic-auth \
  --type=kubernetes.io/basic-auth \
  --from-literal=username=dbuser \
  --from-literal=password='S3cureP@ss'

SSH auth secrets

Example:

oc create secret generic git-ssh-key \
  --type=kubernetes.io/ssh-auth \
  --from-file=ssh-privatekey=id_rsa

Service account tokens (service-account-token)

Creating Secrets in OpenShift

OpenShift provides multiple ways to create secrets, via CLI, web console, and manifests.

Using `oc create secret`

The CLI provides subcommands for common secret types:

  oc create secret generic app-secret \
    --from-literal=token='12345' \
    --from-file=config.yaml=./config.yaml
  oc create secret docker-registry registry-creds \
    --docker-server=registry.example.com \
    --docker-username=user \
    --docker-password='pass' \
    --docker-email=user@example.com
  oc create secret tls tls-cert \
    --cert=./tls.crt \
    --key=./tls.key

From environment files

You can create a secret from a file in KEY=VALUE format:

cat > secret.env <<EOF
DB_USER=myuser
DB_PASS=mypass
EOF
oc create secret generic db-credentials \
  --from-env-file=secret.env

Using YAML manifests

Secrets are Kubernetes API objects. They can be declared in YAML and applied with oc apply -f.

Example Opaque secret:

apiVersion: v1
kind: Secret
metadata:
  name: app-credentials
type: Opaque
data:
  api-key: bXktYXBpLWtleQ==
  api-secret: bXktc2VjcmV0

Key points:

Example using stringData:

apiVersion: v1
kind: Secret
metadata:
  name: app-credentials
type: Opaque
stringData:
  api-key: my-api-key
  api-secret: my-secret

Using the OpenShift web console

Typical workflow:

  1. Navigate to Workloads → Secrets in the developer or admin perspective.
  2. Click Create and choose the secret type (e.g., Key/value, TLS, Image pull secret).
  3. Fill in the key/value pairs or upload files.
  4. Select the Project/Namespace where the secret will live.
  5. Save and then reference the secret from Deployments, BuildConfigs, or Routes.

The console guides you through type-specific fields (e.g., server URL for registry secrets, TLS cert/key upload, etc.).

Using Secrets in Pods

Once a secret exists, it must be explicitly referenced by workloads. Common integration patterns are environment variables, mounted volumes, and image pull secrets.

Exposing secrets as environment variables

Useful for small pieces of configuration (e.g. credentials) read by the application at startup.

From a single secret key

YAML snippet:

env:
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: DB_USER
  - name: DB_PASS
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: DB_PASS

Using oc set env:

oc set env deployment/myapp \
  DB_USER_SECRET_FROM=secretKeyRef:name=db-credentials,key=DB_USER

From all keys in a secret

Exports every key in a secret as an environment variable with the same name:

envFrom:
  - secretRef:
      name: app-credentials

Mounting secrets as volumes

This is the recommended approach for files (certificates, SSH keys, config files, etc.) and for apps that periodically reload secrets.

YAML snippet:

volumes:
  - name: certs
    secret:
      secretName: my-tls-secret
containers:
  - name: web
    image: myimage
    volumeMounts:
      - name: certs
        mountPath: /etc/tls
        readOnly: true

Each key in the secret will appear as a file in the mount directory (/etc/tls/tls.crt, /etc/tls/tls.key, etc. for TLS secrets).

Mapping secret keys to specific file paths

You can customize file names and mount only selected keys:

volumes:
  - name: ssh-key
    secret:
      secretName: git-ssh-key
      items:
        - key: ssh-privatekey
          path: id_rsa
containers:
  - name: builder
    image: my-builder
    volumeMounts:
      - name: ssh-key
        mountPath: /root/.ssh
        readOnly: true

Image pull secrets

When pulling images from private registries, reference a Docker registry secret so the kubelet can authenticate.

On a ServiceAccount

Attach the secret to a ServiceAccount used by Pods:

oc secrets link my-serviceaccount registry-creds --for=pull

Or directly in YAML:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-serviceaccount
imagePullSecrets:
  - name: registry-creds

Any Pod using this ServiceAccount will automatically use the pull secret.

On a Pod/Deployment

You can also define imagePullSecrets directly:

spec:
  imagePullSecrets:
    - name: registry-creds

In OpenShift, the project’s default builder and default ServiceAccounts often already reference secrets for the internal registry.

Secrets in Common OpenShift Workflows

With Routes and TLS termination

Routes can reference TLS secrets for HTTPS:

Example:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: my-secure-route
spec:
  host: app.example.com
  to:
    kind: Service
    name: my-service
  tls:
    termination: edge
    key: |
      -----BEGIN PRIVATE KEY-----
      ...
    certificate: |
      -----BEGIN CERTIFICATE-----
      ...

Alternatively, the console allows you to select a pre-created TLS secret.

With Builds and Source-to-Image (S2I)

Build configurations often need secrets for:

Typical patterns:

  spec:
    source:
      type: Git
      git:
        uri: git@github.com:org/repo.git
      sourceSecret:
        name: git-ssh-key
  spec:
    strategy:
      sourceStrategy:
        from:
          kind: ImageStreamTag
          name: 'my-base-image:latest'
        pullSecret:
          name: registry-creds

With Deployments and StatefulSets

When defining workloads:

Operations teams often standardize secret names and keys across projects to keep manifests portable (e.g., always DB_USER, DB_PASSWORD, SSL_CERT, SSL_KEY).

Namespaces, Access Control, and Secrets

Secrets are namespaced resources:

Impact:

When using oc:

Treat local copies with the same care as the original secret.

Lifecycle and Rotation of Secrets

Updating a secret

Updating secret data can be done via:

By default, Pods will not automatically restart when the secret changes. How updates are propagated depends on how you use the secret:

Secret rotation strategies

Common patterns:

In OpenShift, Operators and controllers often manage their own secrets and rotation (for example, for platform components); application teams typically manage application-level secrets.

Security Considerations Specific to Secrets

Secrets are not a complete security solution by themselves; they are one building block.

Key points in OpenShift:

Additional practices:

Working with Secrets in `oc`

Some useful CLI commands in day-to-day work:

  oc get secrets
  oc get secret app-credentials -o yaml
  oc get secret app-credentials -o jsonpath='{.data.api-key}' | base64 -d
  oc extract secret/app-credentials --to=-
  # or to a directory
  oc extract secret/app-credentials --to=./secrets
  oc patch secret app-credentials \
    -p='{"stringData":{"api-key":"new-api-key"}}'

Use these tools carefully and avoid leaving sensitive data in shell history or shared terminals.

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!