OpenShift中BuildConfig与DeploymentConfig的区别及ConfigMap配置咨询
OpenShift BuildConfig vs DeploymentConfig: Key Differences & ConfigMap Env Setup
Great questions! Let's break this down step by step to clarify everything you're asking about.
1. Core Differences Between BuildConfig and DeploymentConfig
These two OpenShift resources serve entirely distinct purposes in your application lifecycle:
- BuildConfig: This is OpenShift's dedicated resource for building container images. It defines how to turn source code, a Dockerfile, or a base image into a runnable container image. You'll configure things like build triggers (e.g., code commits, base image updates), build strategies (Source-to-Image, Docker Build, etc.), and where to push the finished image (like an internal OpenShift ImageStream or external registry). It's all about the "build" phase of your app.
- DeploymentConfig: This resource manages the deployment and runtime lifecycle of your application. It defines how many replicas of your container should run, rolling update strategies, health checks, and the image to deploy. Its job is to ensure your app stays in the desired state—for example, automatically rolling out new pods when a new image is pushed, or replacing failed pods. It's focused on the "run" phase.
Kubernetes Equivalents
- BuildConfig: Kubernetes doesn't have a direct native equivalent. To replicate its functionality, you'd typically use CI/CD tools like Tekton (which uses
Pipeline/Taskresources) or combineJobwith tools like Kaniko to build images. BuildConfig is an OpenShift-specific abstraction for image building. - DeploymentConfig: This maps directly to Kubernetes' native
Deploymentresource. Both manage pod deployments and updates, though DeploymentConfig includes some OpenShift-specific extensions (like more granular trigger options).
2. ConfigMap Environment Variables in DeploymentConfig: Your Setup is Correct!
You're right to place the envFrom configuration in your DeploymentConfig. Since environment variables are part of how your app runs (not how its image is built), they belong in the deployment/runtime configuration, not the build configuration.
Full DeploymentConfig Example with ConfigMap Env Vars
Here's a complete, practical example showing how to inject database-related environment variables from your assetextraction ConfigMap:
apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata: name: asset-extraction-app spec: replicas: 2 # Labels to match pods managed by this DeploymentConfig selector: app: asset-extraction template: metadata: labels: app: asset-extraction spec: containers: - name: asset-extraction-container # Replace with your built image reference image: openshift-registry/asset-extraction:latest ports: - containerPort: 3000 # Load ALL key-value pairs from the ConfigMap as env vars envFrom: - configMapRef: name: assetextraction # Optional: If you only need specific keys from the ConfigMap # env: # - name: DB_HOST # valueFrom: # configMapKeyRef: # name: assetextraction # key: db_host # - name: DB_PASSWORD # valueFrom: # configMapKeyRef: # name: assetextraction # key: db_password # Triggers to auto-update the deployment triggers: - type: ConfigChange # Updates when DeploymentConfig itself changes - type: ImageChange # Updates when a new image is pushed to the ImageStream imageChangeParams: automatic: true containerNames: - asset-extraction-container from: kind: ImageStreamTag name: asset-extraction:latest
Key Notes
- Using
envFrom.configMapRefinjects every key-value pair from theassetextractionConfigMap into your container as environment variables—perfect if you want all database-related vars at once. - If you only need a subset of variables, use
env.valueFrom.configMapKeyRefto specify individual keys (commented out in the example) for more control. - This configuration lives inside the pod template (
spec.template.spec.containers), which is where all runtime settings for your app belong in a DeploymentConfig.
内容的提问来源于stack exchange,提问作者jack
相关产品推荐
相关产品推荐

