Kahibaro
Discord Login Register

Securing and scaling an application

Goals of this Final Exercise

In this part of the final project, you will:

This chapter assumes you already have a working application deployed for the final project and basic CI/CD in place from previous chapters.

Scenario Overview

You are responsible for a simple web application running on OpenShift:

Your tasks:

  1. Harden the runtime security of the application.
  2. Protect configuration and secrets.
  3. Apply network-level protections.
  4. Implement autoscaling and resilience.
  5. Demonstrate that your configuration works (tests, logs, metrics).

1. Preparing the Baseline Deployment

Before adding security and scaling, make sure you have:

Typical starting point (simplified example):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-app
  template:
    metadata:
      labels:
        app: demo-app
    spec:
      containers:
      - name: demo-app
        image: quay.io/example/demo-app:latest
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: "50m"
            memory: "128Mi"
          limits:
            cpu: "200m"
            memory: "256Mi"

You will progressively refine this with security and scaling configuration.

2. Hardening Runtime Security

In this project phase you apply OpenShift security features to your workload. You do not need to design a full security model for the cluster, but you must improve the pod-level configuration.

2.1 Run as Unprivileged User

Your pods should not run as root unless absolutely required.

Tasks:

Example (pod-level):

spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1001
  containers:
  - name: demo-app
    image: quay.io/example/demo-app:latest
    securityContext:
      allowPrivilegeEscalation: false

Verification:

2.2 Minimize Capabilities and Privileges

Avoid privileged containers and unnecessary Linux capabilities.

Tasks:

Example:

containers:
- name: demo-app
  image: quay.io/example/demo-app:latest
  securityContext:
    privileged: false
    allowPrivilegeEscalation: false
    capabilities:
      drop: ["ALL"]

Verification:

2.3 Limit File System Access

Enforce read-only root filesystem if your app can tolerate it.

Tasks:

Example:

spec:
  volumes:
  - name: tmp
    emptyDir: {}
  containers:
  - name: demo-app
    image: quay.io/example/demo-app:latest
    volumeMounts:
    - name: tmp
      mountPath: /tmp
    securityContext:
      readOnlyRootFilesystem: true

Verification:

3. Protecting Configuration and Secrets

In the final project you must demonstrate correct handling of configuration and sensitive data. Here you integrate earlier concepts into a concrete application.

3.1 Move Sensitive Data to Secrets

Tasks:

Example secret:

oc create secret generic demo-db-secret \
  --from-literal=username=demo \
  --from-literal=password=changeMe

Mounting via environment variables:

containers:
- name: demo-app
  env:
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: demo-db-secret
        key: username
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: password
        key: password

Verification:

3.2 Separate Non-Sensitive Configuration into ConfigMaps

Tasks:

Example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-app-config
data:
  LOG_LEVEL: "info"
  FEATURE_X_ENABLED: "true"

Mount via env:

containers:
- name: demo-app
  envFrom:
  - configMapRef:
      name: demo-app-config

Verification:

3.3 Avoid Leaking Secrets in Logs and CI

Tasks for the project report:

Evidence:

4. Network-Level Security

You will implement at least basic network controls around your application.

4.1 Restrict Pod-to-Pod Traffic with NetworkPolicies

Tasks:

Example: allow only traffic from Pods with label role=frontend in the same namespace:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: demo-app-allow-frontend
spec:
  podSelector:
    matchLabels:
      app: demo-app
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 8080

Verification:

4.2 Secure External Access

Tasks:

Example: edge-terminated Route (conceptual snippet):

spec:
  tls:
    termination: edge
    insecureEdgeTerminationPolicy: Redirect

Verification:

5. Enforcing Resource Controls and Quotas

Securing and scaling also involves controlling resource consumption.

5.1 Set Sensible Requests and Limits

Tasks:

Example (per-container):

resources:
  requests:
    cpu: "100m"
    memory: "256Mi"
  limits:
    cpu: "500m"
    memory: "512Mi"

Verification:

5.2 Use ResourceQuotas and LimitRanges (Project-Level)

For the final project you don’t have to design a full multi-team policy, but you should:

Example LimitRange:

apiVersion: v1
kind: LimitRange
metadata:
  name: demo-limits
spec:
  limits:
  - type: Container
    default:
      cpu: "500m"
      memory: "512Mi"
    defaultRequest:
      cpu: "100m"
      memory: "256Mi"

Verification:

6. Implementing Horizontal Scaling

The core of this section is configuring and validating horizontal pod autoscaling (HPA) for your app.

6.1 Create a Horizontal Pod Autoscaler

Prerequisites:

Task:

Example:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: demo-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: demo-app
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

Alternatively, use oc autoscale:

oc autoscale deployment/demo-app \
  --min=2 --max=6 \
  --cpu-percent=60

Verification:

6.2 Generate Load and Observe Scaling

Tasks:

Example (inside a debug pod):

for i in {1..10000}; do curl -s http://demo-app:8080/health > /dev/null; done

Observation:

Evidence for your report:

7. Optional: Vertical Scaling and Right-Sizing

If time permits, explore vertical adjustments based on observed usage.

Tasks:

Report:

8. Making the Application Resilient

Security and scaling are incomplete without resilience. You will use probes and replica strategies to improve availability.

8.1 Health Probes

Tasks:

Example:

containers:
- name: demo-app
  ports:
  - containerPort: 8080
  livenessProbe:
    httpGet:
      path: /healthz
      port: 8080
    initialDelaySeconds: 10
    periodSeconds: 15
  readinessProbe:
    httpGet:
      path: /ready
      port: 8080
    initialDelaySeconds: 5
    periodSeconds: 10

Verification:

8.2 Deployment Strategy and Rollback

Tasks:

Example (snippet):

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1

Experiment:

  1. Deploy a new version that intentionally fails health checks.
  2. Observe the rollout being paused or failing.
  3. Roll back to the previous version:
    • Using oc rollout undo deployment/demo-app.

Evidence:

9. Validation and Reporting Requirements

For this final project component, you will submit a short report or walkthrough describing:

  1. Security changes:
    • Non-root user and privilege settings.
    • Use of Secrets and ConfigMaps.
    • Network policies and Route TLS settings.
  2. Scaling behavior:
    • HPA configuration (YAML snippet or command).
    • Measurements: time to scale out/in under load.
    • Final chosen min/max replicas and rationale.
  3. Resilience:
    • Description of probes and deployment strategy.
    • Evidence of successful recovery from simulated failures.
  4. Trade-offs and Lessons Learned:
    • Where you balanced strict security vs. practicality (e.g., why you did not set readOnlyRootFilesystem in some case).
    • How scaling and security affected each other (e.g., HPA and resource limits, network policies and health checks).

Where possible, attach:

By the end of this exercise, you should have a concrete, working example of an application that is both reasonably hardened and capable of scaling automatically under load, with clear evidence that your configuration behaves as intended.

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!