You are on page 1of 5

DevOps Certification Training

Module-9: Container Orchestration


using Kubernetes Part - II
Demo Document - 3

© Brain4ce Education Solutions Pvt. Ltd.


Module 9: Container Orchestration using Kubernetes Part - II

DEMO-3: StatefulSets
1. Create 2 persistent volumes for your pods to bind to

Syntax: vi pv.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
name: nginx-pv
labels:
type: local
app: nginx
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/home/edureka/data"

Create another persistent with same settings except with a different metadata name and hostpath

Create the volumes using create command:


Syntax: kubectl create -f pv.yml

Page 1
Module 9: Container Orchestration using Kubernetes Part - II

2. Create a yaml file that contains both the nginx StatefulSet and the service required to access it

Syntax: vi state.yml

apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi

Page 2
Module 9: Container Orchestration using Kubernetes Part - II

Now, execute the yaml file to create the service and the statefulset

Syntax: kubectl create -f state.yml

3. We can check if the statefulset, service and the pods associated are creates successfully

Syntax: kubectl get statefulset


kubectl get svc
kubectl get pods

You can notice that pods are created sequentially and are given unique ID’s which are retained
even after multiple restarts

4. Now to test if the statefulset is working as expected we will check the details of one of the pods,
delete and wait for it to get rescheduled

Syntax: kubectl describe pods web-1

Page 3
Module 9: Container Orchestration using Kubernetes Part - II

Now, delete the pod and wait for it to redeploy

Syntax: kubectl delete pods web-1

We can check the details to see that the pod as retained its identity

Page 4

You might also like