Chuyển đến nội dung chính

Lesson 7: Cloud Native Architecture & Design Patterns

Cloud native principles, microservices vs monolith, service mesh, 12-factor app, immutable infrastructure and cloud native design patterns.

Cloud Native Architecture — Microservices vs Monolith, 12-Factor App

1. Cloud Native — CNCF Definition

According to the CNCF (Cloud Native Computing Foundation), cloud native is an approach to building and running scalable applications in dynamic environments such as public, private, and hybrid clouds, using: containers, microservices, declarative APIs, and immutable infrastructure.

PrincipleMeaningExample
ContainerizedPackage app + dependencies togetherDocker image
Dynamically orchestratedAutomated scheduling, scaling, healingKubernetes
MicroservicesLoose coupling, single responsibilityAuth service, Payment service
Declarative APIsDescribe desired state, not stepskubectl apply -f deployment.yaml
Immutable infrastructureNever modify running; replace insteadNew image version → rolling update

2. Microservices vs Monolith

MONOLITH                          MICROSERVICES
─────────────────────             ──────────────────────────────
┌──────────────────┐              ┌────────┐ ┌────────┐ ┌─────┐
│  Auth    │  UI   │              │  Auth  │ │  Cart  │ │  UI │
│  Cart    │  API  │              │Service │ │Service │ │Svc  │
│  Payment │  DB   │              └────────┘ └────────┘ └─────┘
└──────────────────┘                   │          │         │
  Deploy as 1 unit                     └─── API Gateway ───┘
                                            │
                                       Client/Browser
AspectMonolithMicroservices
DeploymentAll-or-nothingIndependent per service
ScalingScale entire appScale only bottleneck service
ComplexityLow (single codebase)High (distributed)
Fault isolationOne bug crashes allFailure contained in service
TechnologySingle stackPolyglot (best tool per service)

3. 12-Factor App

The 12-factor app methodology defines best practices for cloud native applications:

#FactorCloud Native Practice
1Codebase1 repo per app, many deploys
2DependenciesDeclare explicitly (package.json, go.mod)
3ConfigStore in environment (ConfigMap, Secrets)
4Backing servicesDB, cache = attached resources via URL
5Build/Release/RunStrict separation (CI builds, CD deploys)
6ProcessesStateless processes, store state externally
7Port bindingExport service via port (no web server layer)
8ConcurrencyScale via process model (HPA)
9DisposabilityFast startup, graceful shutdown
10Dev/Prod paritySame tools/services across environments
11LogsTreat as event streams (stdout, not files)
12Admin processesRun one-off admin tasks as Jobs

Exam tip: Factors 3, 6, 9, and 11 frequently appear in KCNA questions. Factor 3 (config in env) → ConfigMap/Secret. Factor 6 (stateless) → the reason for using external storage. Factor 11 (logs as streams) → stdout → log aggregator.

4. Service Mesh

As microservices grow, you need to manage: mTLS, retry, circuit breaker, and observability. A Service Mesh solves this by injecting a sidecar proxy into each Pod.

Without Service Mesh:          With Service Mesh (Istio):
  App A ──────────────► App B    App A ──► [Envoy] ──► [Envoy] ──► App B
  (manual TLS, retry code)         sidecar           sidecar
                                 (auto mTLS, metrics, retry, tracing)
FeatureProvided by Service Mesh
mTLS mutual authenticationAutomatically encrypts traffic between services
Traffic managementCanary, A/B, weighted routing
ObservabilityAuto metrics, tracing, access logs
ResilienceRetry, timeout, circuit breaker

5. Cheat Sheet

Exam questionAnswer
What does the CNCF definition of cloud native include?Containers, microservices, declarative APIs, immutable infra
Where should config be stored according to 12-factor?Environment variables (not hardcoded)
Logs according to 12-factor?Treat as streams (stdout/stderr)
What does a service mesh inject into Pods?Sidecar proxy (Envoy)
What part of microservices is scaled?Only the service with the bottleneck

6. Practice Questions

Q1: According to the 12-factor app methodology, how should an application store its database connection string?

  • A) Hardcoded in the source code
  • B) In a configuration file committed to the repository
  • C) As an environment variable (Kubernetes ConfigMap or Secret) ✓
  • D) In the container image as a build argument

Explanation: Factor 3 (Config) states: "Store config in the environment." In Kubernetes, this means using ConfigMaps for non-sensitive config and Secrets for sensitive values, injected as environment variables.

Q2: What is the primary benefit of using a Service Mesh in a microservices architecture?

  • A) Replace Kubernetes for container orchestration
  • B) Provide infrastructure-level networking features (mTLS, retry, observability) without changing application code ✓
  • C) Store application configuration
  • D) Persist application state across container restarts

Explanation: Service mesh moves cross-cutting concerns (security, observability, resilience) to the infrastructure layer via sidecar proxies. Developers don't need to implement retry logic or mTLS in each service.

Q3: Which characteristic distinguishes "immutable infrastructure" from traditional infrastructure?

  • A) Servers are never rebooted
  • B) Running systems are replaced rather than modified in-place ✓
  • C) Configuration changes require manual approval
  • D) Infrastructure is defined using only YAML files

Explanation: Immutable infrastructure means you never update/patch a running container — you build a new image, deploy it, replace old containers. This eliminates configuration drift and improves repeatability.