Exercise
Create a directory structure for a Kustomize project with:
- a base layer
- a development and a production overlays
Create resources in the base layer:
- A Deployment named
webappusingnginx:1.26image with 2 replicas - A Service named
webapp-serviceexposing port 80
- A Deployment named
Create a
kustomization.yamlfile in the base directory that references both resourcesApply the base configuration
Configure the
developmentoverlay:- Changes the image to
nginx:1.28 - Adds a
env: developmentlabel to all resources - Sets the replica count to 1
- Changes the image to
Preview the development overlay using
kubectl kustomizewithout applyingApply the development overlay
Create a
productionoverlay that:- Changes the image to
nginx:1.28-alpine - Adds a
env: productionlabel to all resources - Sets the replica count to 5
- Changes the service type to LoadBalancer
- Changes the image to
Preview the production overlay
Apply the production overlay
Verify the production changes
Delete the resources
Documentation
https://kubectl.docs.kubernetes.io/guides/introduction/kustomize/
Solution
- Create a directory structure for the Kustomize project
mkdir -p base
mkdir -p overlays/development
mkdir -p overlays/production- Create resources in the base layer:
Create the following specifications in the base folder:
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: nginx:1.26
ports:
- containerPort: 80apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
ports:
- port: 80
targetPort: 80
type: ClusterIP- Create a
kustomization.yamlfile in the base directory that references both resources
resources:
- deployment.yaml
- service.yaml- Apply the base configuration
$ kubectl apply -k base
service/webapp-service created
deployment.apps/webapp created- Configure the
developmentoverlay:
In the overlays/development folder, create the following kustomization.yaml file:
resources:
- ../../base
labels:
- pairs:
env: development
patches:
- patch: |-
- op: replace
path: /spec/template/spec/containers/0/image
value: nginx:1.28
- op: replace
path: /spec/replicas
value: 1
target:
kind: Deployment
name: webappThis file specifies:
- the base resources to use
- the labels to add
- the patches that need to be applied to the base resources to change the image and the number of replicas
Instead of defining the patches in the kustomization file, we could also create the deployment-patch.yaml file in the overlays/development:
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 1
template:
spec:
containers:
- name: webapp
image: nginx:1.28And update the kustomization.yaml, so it applies this patch:
resources:
- ../../base
labels:
- pairs:
env: development
patches:
- deployment-patch.yaml- Preview the development overlay:
kubectl has the kustomize built-in command for that purpose:
kubectl kustomize overlays/developmentYou should see the resulting Deployment’s specification is now based on nginx:1.28 and has 1 replica.
- Apply the development overlay:
$ k apply -k overlays/development
service/webapp-service configured
deployment.apps/webapp configured- Configure the production overlay
In the overlays/production folder, create the following kustomization.yaml file:
resources:
- ../../base
labels:
- pairs:
env: production
patches:
- patch: |-
- op: replace
path: /spec/template/spec/containers/0/image
value: nginx:1.28-alpine
- op: replace
path: /spec/replicas
value: 5
target:
kind: Deployment
name: webapp
- patch: |-
- op: replace
path: /spec/type
value: LoadBalancer
target:
kind: Service
name: webapp-servicekustomization.yaml- Preview the production overlay:
kubectl kustomize overlays/production- Apply the production overlay:
kubectl apply -k overlays/production- Verify the production changes:
Checking deployment:
kubectl get deployment webapp -o wide
kubectl get pods -l app=webappYou should see 5 replicas running nginx:1.28-alpine.
Checking service:
kubectl get service webapp-serviceYou should see the Service type is now LoadBalancer.
Checking labels:
kubectl get deployment webapp --show-labels
kubectl get service webapp-service --show-labelsYou should see the env: production label applied to both the Deployment and the Service.
- Clean up:
kubectl delete -k overlays/production