Pod, ReplicaSet, Deployment

First create a namespace to work in:

kubectl create namespace myspace
kubectl config set-context --current --namespace=myspace

Pod

Create a naked pod:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: quarkus-demo
spec:
  containers:
  - name: quarkus-demo
    image: quay.io/rhdevelopers/quarkus-demo:v1
EOF

Watch the pod lifecycle:

watch kubectl get pods
NAME           READY   STATUS              RESTARTS   AGE
quarkus-demo   0/1     ContainerCreating   0          10s

From ContainerCreating to Running with Ready 1/1:

NAME           READY   STATUS    RESTARTS   AGE
quarkus-demo   1/1     Running   0          18s

Verify the application in the Pod:

kubectl exec -it quarkus-demo /bin/sh

Run the next command. Notice that as you are inside the container instance, the hostname is localhost.

curl localhost:8080
Supersonic Subatomic Java with Quarkus quarkus-demo:1
exit

Let’s delete the previous Pod:

kubectl delete pod quarkus-demo
watch kubectl get pods
NAME           READY   STATUS        RESTARTS   AGE
quarkus-demo   0/1     Terminating   0          9m35s

No resources found in myspace namespace.

A Naked Pod disappears forever.

ReplicaSet

Create a ReplicaSet:

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: rs-quarkus-demo
spec:
    replicas: 3
    selector:
       matchLabels:
          app: quarkus-demo
    template:
       metadata:
          labels:
             app: quarkus-demo
             env: dev
       spec:
          containers:
          - name: quarkus-demo
            image: quay.io/rhdevelopers/quarkus-demo:v1
EOF

Get the pods with labels:

watch kubectl get pods --show-labels
NAME                    READY   STATUS    RESTARTS   AGE   LABELS
rs-quarkus-demo-jd6jk   1/1     Running   0          58s   app=quarkus-demo,env=dev
rs-quarkus-demo-mlnng   1/1     Running   0          58s   app=quarkus-demo,env=dev
rs-quarkus-demo-t26gt   1/1     Running   0          58s   app=quarkus-demo,env=dev
kubectl get rs
NAME              DESIRED   CURRENT   READY   AGE
rs-quarkus-demo   3         3         3       79s

Describe the RS:

kubectl describe rs rs-quarkus-demo
Name:         rs-quarkus-demo
Namespace:    myspace
Selector:     app=quarkus-demo
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"rs-quarkus-demo","namespace":"myspace"},"spec":{"replicas...
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=quarkus-demo
           env=dev
  Containers:
   quarkus-demo:
    Image:        quay.io/rhdevelopers/quarkus-demo:v1
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  89s   replicaset-controller  Created pod: rs-quarkus-demo-jd6jk
  Normal  SuccessfulCreate  89s   replicaset-controller  Created pod: rs-quarkus-demo-t26gt
  Normal  SuccessfulCreate  89s   replicaset-controller  Created pod: rs-quarkus-demo-mlnng

Pods are "owned" by the ReplicaSet:

kubectl get pod rs-quarkus-demo-mlnng -o json | jq ".metadata.ownerReferences[]"
{
  "apiVersion": "apps/v1",
  "blockOwnerDeletion": true,
  "controller": true,
  "kind": "ReplicaSet",
  "name": "rs-quarkus-demo",
  "uid": "1ed3bb94-dfa5-40ef-8f32-fbc9cf265324"
}

Now delete a pod, while watching pods:

kubectl delete pod rs-quarkus-demo-mlnng

And a new pod will spring to life to replace it:

NAME                    READY   STATUS              RESTARTS   AGE    LABELS
rs-quarkus-demo-2txwk   0/1     ContainerCreating   0          2s     app=quarkus-demo,env=dev
rs-quarkus-demo-jd6jk   1/1     Running             0          109s   app=quarkus-demo,env=dev
rs-quarkus-demo-t26gt   1/1     Running             0          109s   app=quarkus-demo,env=dev

Delete the ReplicaSet to remove all the associated pods:

kubectl delete rs rs-quarkus-demo

Deployment

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: quarkus-demo-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: quarkus-demo
  template:
    metadata:
      labels:
        app: quarkus-demo
        env: dev
    spec:
      containers:
      - name: quarkus-demo
        image: quay.io/rhdevelopers/quarkus-demo:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
EOF
kubectl get pods --show-labels
NAME                                       READY   STATUS    RESTARTS   AGE   LABELS
quarkus-demo-deployment-5979886fb7-c888m   1/1     Running   0          17s   app=quarkus-demo,env=dev,pod-template-hash=5979886fb7
quarkus-demo-deployment-5979886fb7-gdtnz   1/1     Running   0          17s   app=quarkus-demo,env=dev,pod-template-hash=5979886fb7
quarkus-demo-deployment-5979886fb7-grf59   1/1     Running   0          17s   app=quarkus-demo,env=dev,pod-template-hash=5979886f
kubectl exec -it quarkus-demo-deployment-5979886fb7-c888m -- curl localhost:8080
Supersonic Subatomic Java with Quarkus quarkus-demo-deployment-5979886fb7-c888m:1

In the next section, you’ll learn the concept of Service. This is an important element in Kubernetes ecosystem.