Kubernetes API Fundamentals

Finalizers

Create a manifest for a Deployment with a Finalizer:

cat > finalizer-test.yaml<<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: finalizer-test
  namespace: myproject
  labels:
    app: finalizer-test
  finalizers:
    - finalizer.extensions/v1beta1  
spec:
  selector:
    matchLabels:
      app: finalizer-test
  replicas: 3
  template:
    metadata:
      labels:
        app: finalizer-test
    spec:
      containers:
        - name: hieveryone
          image: openshiftkatacoda/blog-django-py
          imagePullPolicy: Always
          ports:
            - name: helloworldport
              containerPort: 8080
EOF

Create the Deployment.

oc create -f finalizer-test.yaml

Verify the Deployment has been created.

oc get deploy

Verify the ReplicaSet has been created:

oc get replicaset

Verify the pods are running:

oc get pods

Attempt to delete the Deployment.

oc delete deployment finalizer-test

Open up another terminal window

Observe the Deployment still exits and has been updated with the deletionGracePeriodSeconds and deletionTimestamp fields.

oc get deployment finalizer-test -o yaml | grep 'deletionGracePeriodSeconds\|deletionTimestamp'

Attempt to scale the Deployment up and down. Although status is updated, pods will not be created/deleted:

oc scale deploy finalizer-test --replicas=5
oc scale deploy finalizer-test --replicas=1
oc get deploy
oc get pods

Update the Deployment with the Finalizer value unset.

cat > finalizer-test-remove.yaml<<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: finalizer-test
  namespace: myproject
  labels:
    app: finalizer-test
  finalizers:
spec:
  selector:
    matchLabels:
      app: finalizer-test
  replicas: 3
  template:
    metadata:
      labels:
        app: finalizer-test
    spec:
      containers:
        - name: hieveryone
          image: openshiftkatacoda/blog-django-py
          imagePullPolicy: Always
          ports:
            - name: helloworldport
              containerPort: 8080
EOF

Replace the Deployment.

oc replace -f finalizer-test-remove.yaml

The Deployment will now be deleted.

oc get deploy
oc get pods

See the following for examples of this behavior in upstream kubernetes:

Here

and Here