Kubernetes Project: Deploy a Real Three-Tier App
This project deploys a real three-tier app on Kubernetes. A React frontend talks to a Node API, and the API talks to Postgres. React builds the web pages. Node runs the API code.
Build the two app images
Build each image from inside its own app folder. The image names must match the manifests.
docker build -t three-tier-api:v1 .
docker build -t three-tier-web:v1 .
minikube image load three-tier-api:v1
minikube image load three-tier-web:v1One namespace, one ConfigMap, one Secret
The namespace groups the project. The ConfigMap holds plain settings and the Secret holds the password.
apiVersion: v1
kind: Namespace
metadata:
name: three-tier
---
apiVersion: v1
kind: ConfigMap
metadata:
name: three-tier-config
namespace: three-tier
data:
DB_HOST: three-tier-db
DB_NAME: appdb
---
apiVersion: v1
kind: Secret
metadata:
name: three-tier-secret
namespace: three-tier
type: Opaque
stringData:
DB_PASSWORD: change-me-in-real-usePostgres keeps its data
The claim asks for storage. The Deployment mounts it at the Postgres data folder.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: three-tier-db-data
namespace: three-tier
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: three-tier-db
namespace: three-tier
spec:
replicas: 1
selector:
matchLabels:
app: three-tier-db
template:
metadata:
labels:
app: three-tier-db
spec:
containers:
- name: postgres
image: mirror.gcr.io/library/postgres:16
env:
- name: POSTGRES_DB
valueFrom:
configMapKeyRef:
name: three-tier-config
key: DB_NAME
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: three-tier-secret
key: DB_PASSWORD
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: three-tier-db-dataThe API talks to the database by name
The API reads the database host from the ConfigMap. The Service gives the API a stable name.
apiVersion: apps/v1
kind: Deployment
metadata:
name: three-tier-api
namespace: three-tier
spec:
replicas: 2
selector:
matchLabels:
app: three-tier-api
template:
metadata:
labels:
app: three-tier-api
spec:
containers:
- name: api
image: three-tier-api:v1
imagePullPolicy: Never
ports:
- containerPort: 3000
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: three-tier-config
key: DB_HOST
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: three-tier-secret
key: DB_PASSWORD
---
apiVersion: v1
kind: Service
metadata:
name: three-tier-api
namespace: three-tier
spec:
selector:
app: three-tier-api
ports:
- port: 3000
targetPort: 3000The frontend and the Gateway
The frontend serves the web pages. The Gateway routes web traffic to it.
apiVersion: apps/v1
kind: Deployment
metadata:
name: three-tier-web
namespace: three-tier
spec:
replicas: 2
selector:
matchLabels:
app: three-tier-web
template:
metadata:
labels:
app: three-tier-web
spec:
containers:
- name: web
image: three-tier-web:v1
imagePullPolicy: Never
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: three-tier-web
namespace: three-tier
spec:
selector:
app: three-tier-web
ports:
- port: 80
targetPort: 80
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: three-tier-gateway
namespace: three-tier
spec:
gatewayClassName: nginx
listeners:
- name: http
port: 80
protocol: HTTPExample
kubectl apply -f three-tier.yaml -n three-tier
kubectl get pods -n three-tier
kubectl get svc -n three-tierKey points
- Three-tier app: a frontend tier, an API tier and a database tier, each in its own Deployment
- Use the namespace three-tier for the whole project, so every object stays together
- Use a ConfigMap for the API address and other plain settings
- Use a Secret for the database password, because plain settings do not belong in a Secret
- Use a PersistentVolumeClaim so the database data survives a pod restart
- Use a Service in front of each tier, so pods can find each other by name
- Use a Gateway API Gateway to bring web traffic into the cluster
- stringData: the plain-text values of a Secret, encoded by Kubernetes.
- imagePullPolicy: the rule that says whether Kubernetes pulls the image.
- containerPort: the port a container listens on inside its Pod.
In this example, the first command applies every manifest in the file. The second command lists the pods in the three-tier namespace. The output shows five pods running, with two API pods, one database pod and two frontend pods. The third command lists the Services. The output shows one Service for the API and one for the frontend.
Note
Build the two app images locally and load them with minikube image load before you apply the manifests.
Postgres runs as a Deployment with a PVC to keep this project small. A StatefulSet is the production choice, taught in lesson 34.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which object gives the Postgres pod storage that survives a restart?