πŸ”§ DevOps

Staging

Staging

A test environment before production. Configured to resemble the real environment.

Detailed Explanation

Staging is a test environment where final verification is performed before deploying to the production environment. By configuring it as identically to actual production as possible, problems can be found and fixed before deployment.

A staging environment should use the same infrastructure configuration as production (server specs, network, security settings), a similar data volume, and the same deployment pipeline. Manual testing by the QA team, integration tests, performance tests, security scans, and more are performed in this environment. The goal is to build confidence that "if it succeeded in staging, it will succeed in production too."

Code Example

Environment-Specific Kubernetes Configuration (Kustomize)

# base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
spec:
  selector:
    matchLabels:
      app: api-server
  template:
    metadata:
      labels:
        app: api-server
    spec:
      containers:
      - name: api
        image: myapp/api-server
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: app-config
        - secretRef:
            name: app-secrets
---
# overlays/staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: staging
resources:
- ../../base
replicas:
- name: api-server
  count: 2  # ν”„λ‘œλ•μ…˜λ³΄λ‹€ 적은 λ ˆν”Œλ¦¬μΉ΄
configMapGenerator:
- name: app-config
  literals:
  - ENV=staging
  - LOG_LEVEL=debug
  - DB_HOST=staging-db.internal
  - FEATURE_NEW_UI=true  # μŠ€ν…Œμ΄μ§•μ—μ„œ λ¨Όμ € ν…ŒμŠ€νŠΈ
images:
- name: myapp/api-server
  newTag: staging-latest

GitHub Actions Staging Deployment Pipeline

# .github/workflows/staging-deploy.yaml
name: Deploy to Staging

on:
  push:
    branches: [develop]
  pull_request:
    types: [labeled]

jobs:
  deploy-staging:
    if: github.event_name == 'push' || contains(github.event.pull_request.labels.*.name, 'deploy-staging')
    runs-on: ubuntu-latest
    environment: staging

    steps:
    - uses: actions/checkout@v4

    - name: Build and Push Image
      run: |
        docker build -t ${{ vars.REGISTRY }}/api-server:staging-${{ github.sha }} .
        docker push ${{ vars.REGISTRY }}/api-server:staging-${{ github.sha }}

    - name: Deploy to Staging
      run: |
        kubectl config use-context staging-cluster
        cd k8s/overlays/staging
        kustomize edit set image myapp/api-server=${{ vars.REGISTRY }}/api-server:staging-${{ github.sha }}
        kustomize build | kubectl apply -f -
        kubectl rollout status deployment/api-server -n staging --timeout=5m

    - name: Run Smoke Tests
      run: |
        sleep 30  # μ•ˆμ •ν™” λŒ€κΈ°
        curl -f https://staging.myapp.com/health
        npm run test:smoke -- --env=staging

    - name: Notify on Slack
      if: always()
      uses: slackapi/slack-github-action@v1
      with:
        payload: |
          {
            "text": "Staging 배포 ${{ job.status }}: ${{ github.sha }}"
          }

Staging Data Management

#!/bin/bash
# staging-data-sync.sh - ν”„λ‘œλ•μ…˜ 데이터 읡λͺ…ν™” ν›„ μŠ€ν…Œμ΄μ§•μœΌλ‘œ 볡제

set -e

PROD_DB="prod-db.internal"
STAGING_DB="staging-db.internal"
BACKUP_FILE="/tmp/staging_data_$(date +%Y%m%d).sql"

echo "=== ν”„λ‘œλ•μ…˜ 데이터 덀프 ==="
pg_dump -h $PROD_DB -U readonly \
  --exclude-table=audit_logs \
  --exclude-table=sessions \
  myapp > $BACKUP_FILE

echo "=== 민감 데이터 읡λͺ…ν™” ==="
# 이메일 읡λͺ…ν™”
sed -i "s/[a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]*\.[a-zA-Z]*/user_XXX@example.com/g" $BACKUP_FILE

# μ „ν™”λ²ˆν˜Έ λ§ˆμŠ€ν‚Ή
sed -i "s/010-[0-9]\{4\}-[0-9]\{4\}/010-XXXX-XXXX/g" $BACKUP_FILE

echo "=== μŠ€ν…Œμ΄μ§• DB 볡원 ==="
psql -h $STAGING_DB -U admin myapp < $BACKUP_FILE

echo "=== 정리 ==="
rm $BACKUP_FILE
echo "μŠ€ν…Œμ΄μ§• 데이터 동기화 μ™„λ£Œ"

Practical Conversation

QA Engineer

"We finished testing in staging. Is it okay to deploy to production?"

DevOps engineer

"I'll first check whether the staging environment settings are identical to production. There was a recent change to the production infrastructure."

Dev team lead

"How up to date is the staging data? Performance can vary depending on the data volume."

DevOps engineer

"We anonymize production data and sync it to staging every Sunday. The volume is about 30% of production, so if you need performance testing, it would be better to use a separate load testing environment."

Security team

"You know that staging shouldn't contain real customer data, right?"

DevOps engineer

"Of course. PII data such as emails, phone numbers, and card information is all masked. A script runs automatically on every sync."

Cautions

  • Environment synchronization: If the infrastructure settings of staging and production differ, you get situations like "it worked in staging, but...". Codify your environment configuration with IaC.
  • Data security: When replicating production data, you must anonymize PII (personally identifiable information). Prevent regulatory violations and security incidents.
  • External service isolation: Use staging sandboxes for external services such as payments and email sending. Notifications must not accidentally go out to real customers.
  • Cost management: Keeping staging completely identical to production doubles the cost. Adjust the number of replicas and instance specs appropriately.
  • Access control: Manage access permissions to the staging environment, and add visual distinctions (banners, colors) so it is not confused with production.

Learn More