[//]: # (Confidential document)
[//]: # (01/05/2025)
[//]: # (v 1.7.3)


# Kubernetes labs v1.7.3 - Core - Workloads

## Namespaces 

Namespaces are a logical cluster or environment. They are the primary method of partitioning a cluster or scoping access.



## Lab 1 : Namespaces 

 - Learn how to create and switch between Kubernetes Namespaces using kubectl
 
 
```
kubectl get namespaces
kubectl config get-contexts
kubectl create namespace dev
kubectl config set-context minikube --namespace=dev
kubectl config get-contexts
```

## Pods

A pod is the atomic unit of Kubernetes. It is the smallest “unit of work” or “management resource” within the system and is the foundational building block of all Kubernetes Workloads.


## Lab 2 : Pods 

 - Examine both single and multi-container Pods; including: viewing their attributes through the cli and their exposed Services through the API Server proxy.
 - Create a simple Pod called pod-example 
 - List, describe Pod
 - Logs, exec Pod
 - Use kubectl proxy to verify the web server
 
```
kubectl create -f manifests/pod-example.yaml
kubectl get po
kubectl get pod pod-example
kubectl describe pod pod-example
kubectl logs pod-example
kubectl exec -it pod-example -- sh

kubectl proxy
```
create a new ssh session and consult the URL

```
ssh -i XXX.pem -L 8001:localhost:8001 ubuntu@IP
```
-  http://127.0.0.1:8001/api/v1/namespaces/dev/pods/pod-example/proxy/

or 

```
kubectl port-forward --address 0.0.0.0 pod/pod-example 8888:80
```
-  http://publicIP:8888

```
kubectl create -f manifests/pod-multi-container-example.yaml
kubectl get po
kubectl describe po multi-container-example
kubectl logs multi-container-example
kubectl logs -c nginx multi-container-example
kubectl logs -c content multi-container-example
kubectl exec -it multi-container-example -- sh
kubectl exec -it -c nginx multi-container-example -- sh

$ kubectl proxy
```

create a new ssh session and consult the URL
```
ssh -i XXX.pem -L 8001:localhost:8001 ubuntu@IP
```
- http://127.0.0.1:8001/api/v1/namespaces/dev/pods/multi-container-example/proxy/



## Labels and Selectors

Labels are key-value pairs that are used to identify, describe and group together related sets of objects or resources.

Selectors use labels to filter or select objects, and are used throughout Kubernetes.

## Lab 3 : Labels and Selectors

 - Explore the methods of labeling objects in addition to filtering them with both equality and set-based selectors
 - labes pods
 - select pods using labels

```
kubectl label pod pod-example app=nginx environment=dev
kubectl label pod multi-container-example  app=nginx environment=prod
kubectl get pods --show-labels
kubectl get pods --selector environment=prod
kubectl get pods -l app=nginx

kubectl delete pods pod-example multi-container-example

```

## ReplicaSets

ReplicaSets are the primary method of managing Pod replicas and their lifecycle. This includes their scheduling, scaling, and deletion.

Their job is simple, always ensure the desired number of replicas that match the selector are running.

## Lab 4 : ReplicaSets 

 - Create and scale a ReplicaSet. Explore and gain an understanding of how the Pods are generated from the Pod template, and how they are targeted with selectors.
 - create a ReplicaSet called rs-example with 3 replicas
 - Scale ReplicaSet rs-example up to 5 replicas
 - Create an independent Pod manually with the same labels (the Pod is created and immediately terminated)
 - Delete the ReplicaSet
 
```
kubectl create -f manifests/rs-example.yaml
kubectl get pods --watch --show-labels
kubectl describe rs rs-example
kubectl scale replicaset rs-example --replicas=5
kubectl describe rs rs-example
kubectl scale rs rs-example --replicas=3
kubectl get pods --show-labels --watch
kubectl create -f manifests/pod-rs-example.yaml
kubectl get pods --show-labels --watch
kubectl describe rs rs-example
kubectl delete rs rs-example
```
##  Deployments

Deployments are a declarative method of managing Pods via ReplicaSets. They provide rollback functionality in addition to more granular update control mechanisms.

## Lab 5 : Deployments

 - Create, update and scale a Deployment as well as explore the relationship of Deployment, ReplicaSet and Pod.
 - Create a Deployment deploy-example
 - Check the status of the Deployment
 - Describe the generated ReplicaSet
 - Update the deploy-example manifest and add a few additional labels to the Pod template
 - Apply the change with the --record flag
 - View the history of a Deployment
 - View the specific revision with the summary of the Pod Template
 - Rollback to older revision
 - Delete the Deployment


```
kubectl create -f manifests/deploy-example.yaml --record
kubectl get deployments
kubectl get rs --show-labels
kubectl describe rs deploy-example-<pod-template-hash>
kubectl get pods --show-labels
kubectl describe pod deploy-example-<pod-template-hash-<random>
```
update the deploy-example.yaml (add version:1.0.0 in the pod template labels)
  template:
    metadata:
      labels:
        app: nginx
        version: 1.0.0

```
kubectl apply -f manifests/deploy-example-update.yaml --record
kubectl get pods --show-labels --watch
kubectl get rs --show-labels
kubectl scale deploy deploy-example --replicas=5
kubectl get rs --show-labels
kubectl describe deploy deploy-example
kubectl describe rs deploy-example-<pod-template-hash>
kubectl describe pod deploy-example-<pod-template-hash-<random>

kubectl rollout history deployment deploy-example
kubectl rollout history deployment deploy-example --revision=1
kubectl rollout history deployment deploy-example --revision=2
kubectl rollout undo deployment deploy-example --to-revision=1
kubectl get pods --show-labels --watch
kubectl describe deployment deploy-example
kubectl delete deploy deploy-example
```

##  DaemonSets

DaemonSets ensure that all nodes matching certain criteria will run an instance of the supplied Pod.

They bypass default scheduling mechanisms and restrictions, and are ideal for cluster wide services such as log forwarding, or health monitoring.

## Lab 6 : DaemonSets
 - Experience creating, updating, and rolling back a DaemonSet. Additionally delve into the process of how they are scheduled and how an update occurs
 - Create DaemonSet ds-example
 - Describe the Ds
 - Label the node with nodeType=edge
 - View the current Pods and display their labels with --show-labels


```
kubectl create -f manifests/ds-example.yaml --record
kubectl get daemonset
kubectl label node minikube nodeType=edge
kubectl get daemonsets
kubectl get pods --show-labels
kubectl delete ds ds-example
```

##  StatefulSets
The StatefulSet controller is tailored to managing Pods that must persist or maintain state. Pod identity including hostname, network, and storage can be considered persistent.

They ensure persistence by making use of three things:

- The StatefulSet controller enforcing predicable naming, and ordered provisioning/updating/deletion.
- A headless service to provide a unique network identity.
- A volume template to ensure stable per-instance storage.


## Lab 7 : StatefulSets
- Create, update, and delete a StatefulSet to gain an understanding of how the StatefulSet lifecycle differs from other workloads with regards to updating, deleting and the provisioning of storage
- Create StatefulSet sts-example 
- View pods
- View the current Persistent Volume Claims
- Delete the sts-example-2 Pod
- Check pods
- Scale and check Persistent Volume Claims
- Delete sts

```
kubectl create -f manifests/sts-example.yaml
kubectl get pods --show-labels --watch
kubectl describe statefulset sts-example
kubectl get pvc
kubectl delete pod sts-example-2
kubectl get pods 

kubectl scale sts sts-example --replicas=5
kubectl get pods 
kubectl get pvc
kubectl scale sts sts-example --replicas=3
kubectl get pods 
kubectl get pvc
kubectl delete sts sts-example
kubectl delete pvc --all
```


## Job
The Job Controller ensures one or more Pods are executed and successfully terminate. Essentially a task executor that can be run in parallel.


## Lab 8 : Job
- Create a Kubernetes Job and work to understand how the Pods are managed with completions and parallelism directives.
- Create job job-example
- Watch the Pods
- Delete the job
```
kubectl create -f manifests/job-example.yaml
kubectl get pods --show-labels --watch
kubectl describe job job-example
kubectl delete job job-example
kubectl get pods
```

##  CronJob

CronJobs are an extension of the Job Controller, and enable Jobs to be run on a schedule.

## Lab 9 : CronJob
- Create a CronJob based off a Job Template. Understand how the Jobs are generated and how to suspend a job in the event of a problem.
- Create CronJob cronjob-example using the cron schedule "*/1 * * * *"
- Watch the Pods

```
kubectl create -f manifests/cronjob-example.yaml
kubectl get jobs
kubectl get jobs
kubectl describe CronJob cronjob-example
kubectl delete cronjob cronjob-example
```


## Clean up

```
kubectl delete -f manifests/

```