diff --git a/k8s-zero-to-hero/README.md b/k8s-zero-to-hero/README.md new file mode 100644 index 0000000..4f1513d --- /dev/null +++ b/k8s-zero-to-hero/README.md @@ -0,0 +1,294 @@ +``` + ___ .______ ______ __ __ .___________. + / \ | _ \ / __ \ | | | | | | + / ^ \ | |_) | | | | | | | | | `---| |----` + / /_\ \ | _ < | | | | | | | | | | + / _____ \ | |_) | | `--' | | `--' | | | +/__/ \__\ |______/ \______/ \______/ |__| + +``` + + +I took those exercises from a youtube video of Alta3 Research, Inc that you can see [here](https://www.youtube.com/watch?v=5cgpFWVD8ds&t=405s) +The video contains some good tips aobut kubernetes. I think that these exercises cover multiple topics and are good to improve your Kuberntes knowledge. + +## 🚀 How to use this repo + + +``` +1. Before each exercise there is a ***prepare environment*** section with a kubernetes yaml that is ready to create the components required for the exercise (namespaces, deployments, pods...). + +2. Once you have run the script then you are ready to go ahead with the exercise. + +3. Solution for each exercise can be find under the solutions folder. + + +⚠️ **Disclaimer** +All the exercises are provided "as is", with no guarantee of completeness or accuracy, and without warranty of any kind, express or implied, including, but not limited to warranties of performance, merchantability and fitness for a particular purpose. + +## 🎓 Contributing +Contributions are what make this project such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated. + +1. Fork the Project +2. Create your Feature Branch (git checkout -b feature/AmazingFeature) +3. Commit your Changes (git commit -m 'Add some AmazingFeature') +4. Push to the Branch (git push origin feature/AmazingFeature) +5. Open a Pull Request + + +``` + __________ ___ _______ .______ ______ __ _______. _______ _______. +| ____\ \ / / | ____|| _ \ / || | / || ____| / | +| |__ \ V / | |__ | |_) | | ,----'| | | (----`| |__ | (----` +| __| > < | __| | / | | | | \ \ | __| \ \ +| |____ / . \ | |____ | |\ \----.| `----.| | .----) | | |____.----) | +|_______/__/ \__\ |_______|| _| `._____| \______||__| |_______/ |_______|_______/ +``` +--- + +## Example exercise 1 + +Team banana is taking over a webserver from Team Pineapple. No one from Pineapple is around anymore, so who knows where this webserver is. The only thing you know is that the container is named banana-boat. +Search for the correct pod in namespace pineapple and move it to namespace banana. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-1.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-1.md) + +--- +## Example exercise 2 + +Create a single pod in namespace default with the image nginx:1.7.9. The pod should be named alphapod and the container should be named alphapod container. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-2.md) + +--- +## Example exercise 3 + +Missing data has long been a thorn in your company's side. A Tech Lead has whipped up a container named *snooper-con* in deployment *snooper* in namespace *starfruit*. This container mounts a volume and write logs into a file called snooper.log. + +Inside this deployment, create a sidecar container named *kennylogger-con*, image *busybox:1.31.0*. Save your changes as *snooper-new.yaml* but also make sure the deployment is running. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-3.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-3.md) + +--- +## Example exercise 4 + +In namespace coconut there is a single pod named *watermelon*. Convert the pod into a deployment with 5 replicas and deployment name *watermelonslice*. + +Create the deployment and save its YAML under *watermelon-deployment.yaml*. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-4.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-4.md) + +--- +## Example exercise 5 + +There is an existing deployment named *lemon* in namespace *banana*. Apparently the current version is broken. Check the deployment history and rollback to a version that actually works. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-5.yaml +kubectl set image deployment/lemon nginx=nginx:error24 -n banana +``` + +**2. Link to solution:** [Link](./solutions/exercise-5.md) + +--- +## Example exercise 6 + +Team banana needs a job template located at *bananajob.yaml*. This job should execute the commands *sleep 2 && echo done* on image *busybox:1.31.0* in namespace *banana*. Make it execute 3 pods in parallel for 5 total completions. + +Every pod generated by *bananajob* should have the label *eatyour: bananas*. The job should be named *bananajob* and the container named *banana-bin*. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-6.md) + +--- +## Example exercise 7 + +Team melon needs to identify some of their pods in namespace *basket*. They ask you to add a new labbel *safe:sound* to all pods with an existing label *type:citrus* or *type:seedless*. All pods with the label *safe:sound* will then be given the annotation *fruit:good for what ails ya* + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-7.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-7.md) + +--- +## Example exercise 8 + +Create a new PersistenVolume named *strawberry-pv*. It should have a capacity og 2Gi, accessMode *ReadWriteOnce*, hostPath /Data/Berry and the storageClass *exam*. + +Next create a new PersistentVolumeClaim named *strawberry-pvc* in namespace *straw*. It should request 1Gi storage, accessMode *ReadWriteOnce*, with storageClass *exam*. The PVC and PV should bind. + +FInally create a new deployment named *strawberry-deploy* in namespace *straw*. Mount *strawberry-pvc* as a volume /tmp/berry-data. The pods of that deployment should be of image *nginx:1.7.9*. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-8.md) + +--- +## Example exercise 9 + +Create a ConfigMap called *configmap-kiwi* containing the file *web-kiwi.html*. + +The deployment *kiwi-deploy* is already set up to work with this ConfigMap. Confirm that the containers are now running. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-9.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-9.md) + +--- +## Example exercise 10 + +Create a pod called *nginx* with the image *nginx:1.7.9* which forces all processes to run with user ID 1000 and with group ID 2000. Set privilege scalation to false. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-10.md) + +--- +## Example exercise 11 + + +Create a deployment named *apple-deploy* with 4 replicas and the image *nginx:1.7.9*. The containers should be named *apple-bin*. Each container should have a memory request of 15Mi and memory limit of 30Mi. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-11.md) + +--- +## Example exercise 12 + +You will adjust an existing pod named *kiwi-secret-pod* in namespace kiwi. Make a new secret named *juicysecret*. It must contain the key/values *user=kiwis* and *pass=aredelicious*. Make this content available in the pod *kiwi-secret-pod* as environment variables USERKIWI and PASSKIWI*. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-12.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-12.md) + +--- +## Example exercise 13 + +Create a deployment named *banana-peel* with 2 pods of image *nginx:1.9.1.* + +Team banana has its own ServiceAccount *mega-banana-v5*. Make the banana-peel deployment's pods run under this ServiceAccount. The deployment should be in namespace *banana*. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-13.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-13.md) + +--- +## Example exercise 14 + +Create a single pod named *plumpod* in namespace *plum* of image *busybox:1.31.0*. Add a readiness probe to this container executing *cat /tmp/plumpplum*. The probe should initially wait 3 seconds and thereafter wait 6 seconds. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-14.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-14.md) + +--- +## Example exercise 15 + +Create a pod named *grapes* and image *busybox:1.31.0* with the command: "echo I am ape for grapes; sleep 3600". +Copy the logs of the above pod to the pre-made file grape-logs.txt. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-15.md) + +--- +## Example exercise 16 + +Get the CPU usage of all pods in the *default* namespace. Find which pod has the highest CPU usage and write that pod name to the pre-made file cpu-consume.txt file. + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-16.md) + +--- +## Example exercise 17 + +You have used misguided judgement and created a Deployment that is expecting to have three Pods available inside of a namespace called *fail01*. Look at your deployment that is currently running in that namespace. Fix the deployment so that all three expected replicas are running. + + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-17.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-17.md) + +--- +## Example exercise 18 + +Team Coconut needs a service named *project-paradise-svc* in namespace *coconut*. The service should use TCP port redirection of 3423:80. This service should expose a single pod named *lone-coconut* of image *nginx:1.9.1* with the label *coconuts=migrate* (you'll need to create this pod yourself). + +**⒈ Prepare the environment:** +``` +N/A +``` + +**2. Link to solution:** [Link](./solutions/exercise-18.md) + +--- +## Example exercise 19 + +In namespace *cherry* you'll find two deployments named *pit* and *stem*. Both deployments are exposed via a service. Make a NetworkPolicy named *cherry-control* that prevents outgoing traffic from deployment *pit* except to that of deployment *stem*. + +**⒈ Prepare the environment:** +``` +kubectl create -f exercise-19.yaml +``` + +**2. Link to solution:** [Link](./solutions/exercise-19.md) + diff --git a/k8s-zero-to-hero/cpu-consume.txt b/k8s-zero-to-hero/cpu-consume.txt new file mode 100644 index 0000000..e69de29 diff --git a/k8s-zero-to-hero/exercise-1.yaml b/k8s-zero-to-hero/exercise-1.yaml new file mode 100644 index 0000000..ea7bd39 --- /dev/null +++ b/k8s-zero-to-hero/exercise-1.yaml @@ -0,0 +1,32 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: pineapple +spec: {} +status: {} +--- +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: banana +spec: {} +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: banana-boat + name: banana-boat + namespace: pineapple +spec: + containers: + - image: nginx + name: banana-boat + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} diff --git a/k8s-zero-to-hero/exercise-12.yaml b/k8s-zero-to-hero/exercise-12.yaml new file mode 100644 index 0000000..c8a58b1 --- /dev/null +++ b/k8s-zero-to-hero/exercise-12.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: kiwi +spec: {} +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: kiwi-secret-pod + name: kiwi-secret-pod + namespace: kiwi +spec: + containers: + - image: nginx + name: kiwi-secret-pod + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} diff --git a/k8s-zero-to-hero/exercise-13.yaml b/k8s-zero-to-hero/exercise-13.yaml new file mode 100644 index 0000000..b8bc147 --- /dev/null +++ b/k8s-zero-to-hero/exercise-13.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: banana +spec: {} +status: {} +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: mega-banana-v5 diff --git a/k8s-zero-to-hero/exercise-14.yaml b/k8s-zero-to-hero/exercise-14.yaml new file mode 100644 index 0000000..53845b5 --- /dev/null +++ b/k8s-zero-to-hero/exercise-14.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: plum +spec: {} +status: {} diff --git a/k8s-zero-to-hero/exercise-17.yaml b/k8s-zero-to-hero/exercise-17.yaml new file mode 100644 index 0000000..571588f --- /dev/null +++ b/k8s-zero-to-hero/exercise-17.yaml @@ -0,0 +1,33 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: fail01 +spec: {} +status: {} +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: mydeployment + name: mydeployment + namespace: fail01 +spec: + replicas: 3 + selector: + matchLabels: + app: mydeployment + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: mydeployment + spec: + containers: + - image: nginx:5.99.1 + name: nginx + resources: {} +status: {} diff --git a/k8s-zero-to-hero/exercise-19.yaml b/k8s-zero-to-hero/exercise-19.yaml new file mode 100644 index 0000000..9a62ab2 --- /dev/null +++ b/k8s-zero-to-hero/exercise-19.yaml @@ -0,0 +1,80 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: cherry +spec: {} +status: {} +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + name: pit + namespace: cherry +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + selector: + run: pit +status: + loadBalancer: {} +--- +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: pit + name: pit + namespace: cherry +spec: + containers: + - image: nginx + name: pit + ports: + - containerPort: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + name: stem + namespace: cherry +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + selector: + run: stem +status: + loadBalancer: {} +--- +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: stem + name: stem + namespace: cherry +spec: + containers: + - image: nginx + name: stem + ports: + - containerPort: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- diff --git a/k8s-zero-to-hero/exercise-3.yaml b/k8s-zero-to-hero/exercise-3.yaml new file mode 100644 index 0000000..d99608b --- /dev/null +++ b/k8s-zero-to-hero/exercise-3.yaml @@ -0,0 +1,39 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: starfruit +spec: {} +status: {} +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: snooper + name: snooper + namespace: starfruit +spec: + replicas: 1 + selector: + matchLabels: + app: snooper + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: snooper + spec: + containers: + - image: nginx + name: snooper-con + volumeMounts: + - mountPath: /tmp/snooper.log + name: logsfile + volumes: + - name: logsfile + hostPath: + path: /tmp/snooper.log + type: FileOrCreate diff --git a/k8s-zero-to-hero/exercise-4.yaml b/k8s-zero-to-hero/exercise-4.yaml new file mode 100644 index 0000000..5db4390 --- /dev/null +++ b/k8s-zero-to-hero/exercise-4.yaml @@ -0,0 +1,23 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: coconut +spec: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: watermelon + name: watermelon + namespace: coconut +spec: + containers: + - image: nginx + name: watermelon + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} diff --git a/k8s-zero-to-hero/exercise-5.yaml b/k8s-zero-to-hero/exercise-5.yaml new file mode 100644 index 0000000..8370ba7 --- /dev/null +++ b/k8s-zero-to-hero/exercise-5.yaml @@ -0,0 +1,31 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: banana +spec: {} +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: lemon + name: lemon + namespace: banana +spec: + replicas: 1 + selector: + matchLabels: + app: lemon + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: lemon + spec: + containers: + - image: nginx + name: nginx + resources: {} diff --git a/k8s-zero-to-hero/exercise-7.yaml b/k8s-zero-to-hero/exercise-7.yaml new file mode 100644 index 0000000..eb0eb76 --- /dev/null +++ b/k8s-zero-to-hero/exercise-7.yaml @@ -0,0 +1,92 @@ +apiVersion: v1 +kind: Namespace +metadata: + creationTimestamp: null + name: basket +spec: {} +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + type: citrus + name: lemon + namespace: basket +spec: + containers: + - image: nginx + name: lemon + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + type: citrus + name: lime + namespace: basket +spec: + containers: + - image: nginx + name: lime + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + type: seedless + name: pear + namespace: basket +spec: + containers: + - image: nginx + name: pear + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + type: seedless + name: strawberry + namespace: basket +spec: + containers: + - image: nginx + name: strawberry + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + type: seeds + name: watermelon + namespace: basket +spec: + containers: + - image: nginx + name: watermelon + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} diff --git a/k8s-zero-to-hero/exercise-9.yaml b/k8s-zero-to-hero/exercise-9.yaml new file mode 100644 index 0000000..21c71ca --- /dev/null +++ b/k8s-zero-to-hero/exercise-9.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: kiwi-deploy + name: kiwi-deploy +spec: + replicas: 1 + selector: + matchLabels: + app: kiwi-deploy + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: kiwi-deploy + spec: + containers: + - image: nginx + name: nginx + volumeMounts: + - name: myconfigmap + mountPath: "/etc/foo" + volumes: + - name: myconfigmap + configMap: + name: configmap-kiwi diff --git a/k8s-zero-to-hero/grape-logs.txt b/k8s-zero-to-hero/grape-logs.txt new file mode 100644 index 0000000..e69de29 diff --git a/k8s-zero-to-hero/solutions/exercise-1.md b/k8s-zero-to-hero/solutions/exercise-1.md new file mode 100644 index 0000000..c544ab1 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-1.md @@ -0,0 +1,31 @@ +## Exercise 1 + +We have to list all the pods in all namespaces (`--A`) and as we are getting only the container name (not the pod name) we have to display the container name, the pod name and the namespace in custom columns: + +``` +kubectl get pods -A --output=custom-columns='CONTAINER:.spec.containers[0].name,NAME:.metadata.name,NAMESPACE:.metadata.namespace' + +``` + +We cannot edit the pod and change the namespace, so we have to extract the pod yaml to modify it locally: +``` +kubectl get pod banana-boat -n pineapple -o yaml > banana-boat.yaml +``` +We have to modify the field `metadata.namespace` to `banana`. +Then delete the old pod: +``` +kubectl delete pod banana-boat -n pineapple +``` +And apply the new file: +``` +kubectl create -f banana-boat.yaml +``` + + + + + + + + + diff --git a/k8s-zero-to-hero/solutions/exercise-10.md b/k8s-zero-to-hero/solutions/exercise-10.md new file mode 100644 index 0000000..d9f9cf5 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-10.md @@ -0,0 +1,21 @@ +## Exercise 10 + +``` +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx +spec: + securityContext: + runAsUser: 1000 + runAsGroup: 2000 + containers: + - image: nginx:1.7.9 + name: nginx + resources: {} + securityContext: + allowPrivilegeEscalation: false +``` diff --git a/k8s-zero-to-hero/solutions/exercise-11.md b/k8s-zero-to-hero/solutions/exercise-11.md new file mode 100644 index 0000000..8d5cef9 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-11.md @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: apple-deploy + name: apple-deploy +spec: + replicas: 4 + selector: + matchLabels: + app: apple-deploy + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: apple-deploy + spec: + containers: + - image: nginx:1.7.9 + name: apple-bin + resources: + limits: + memory: "30Mi" + requests: + memory: "15Mi" diff --git a/k8s-zero-to-hero/solutions/exercise-12.md b/k8s-zero-to-hero/solutions/exercise-12.md new file mode 100644 index 0000000..b5aca64 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-12.md @@ -0,0 +1,37 @@ +## Exercise 12 + +Create the secret +``` +kubectl create secret generic juicysecret --from-literal=user=kiwis --from-literal=pass=aredelicious -n kiwi +``` +Add the environment variables to the pod + +``` +... + containers: + - image: nginx + name: kiwi-secret-pod + env: + - name: USERKIWI + valueFrom: + secretKeyRef: + name: juicysecret + key: user + - name: PASSKIWI + valueFrom: + secretKeyRef: + name: juicysecret + key: pass +... +``` + +Check that the pod is running +``` +kubectl get pods -n kiwi + +NAME READY STATUS RESTARTS AGE +kiwi-secret-pod 1/1 Running 0 8s + + +``` + diff --git a/k8s-zero-to-hero/solutions/exercise-13.md b/k8s-zero-to-hero/solutions/exercise-13.md new file mode 100644 index 0000000..0985750 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-13.md @@ -0,0 +1,39 @@ +## Exercise 13 + +Create a simple template for the deployment: +``` +kubectl create deployment banana-peel --image=nginx:1.9.1 --replicas=2 -n banana --dry-run=client -o yaml > banana-peel-deploy.yaml + +``` +Add the yaml to include the service account: +``` +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: banana-peel + name: banana-peel + namespace: banana +spec: + replicas: 2 + selector: + matchLabels: + app: banana-peel + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: banana-peel + spec: + serviceAccountName: mega-banana-v5 + containers: + - image: nginx:1.9.1 + name: nginx +``` + +Apply the yaml: +``` +kubectl create -f banana-peel-deploy.yaml +``` diff --git a/k8s-zero-to-hero/solutions/exercise-14.md b/k8s-zero-to-hero/solutions/exercise-14.md new file mode 100644 index 0000000..19abf9e --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-14.md @@ -0,0 +1,39 @@ +## Exercise 14 + +Create a simple template for the pod: +``` +kubectl run plumpod --image=busybox:1.31.0 -n plum --dry-run=client -o yaml > plumpod.yaml +``` + +Edit the yaml to include the readiness probe: +``` +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + run: plumpod + name: plumpod + namespace: plum +spec: + containers: + - image: busybox:1.31.0 + name: plumpod + resources: {} + readinessProbe: + exec: + command: + - cat + - /tmp/plumpplum + initialDelaySeconds: 3 + periodSeconds: 6 + dnsPolicy: ClusterFirst + restartPolicy: Always +status: {} + +``` + +Apply the changes: +``` +kubectl create -f plumpod.yaml +``` diff --git a/k8s-zero-to-hero/solutions/exercise-15.md b/k8s-zero-to-hero/solutions/exercise-15.md new file mode 100644 index 0000000..2a42f86 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-15.md @@ -0,0 +1,24 @@ +## Exercise 15 + +Create a template for the pod: +``` +kubectl run grapes --image=busybox:1.31.0 --dry-run=client -o yaml --command -- /bin/sh -c 'echo I am ape for grapes; sleep 3600' > grapes.yaml +``` + +Apply the file: +``` +kubectl create -f grapes.yaml +``` + +Get the pod logs and redirect the output to the file: +``` +kubectl logs grapes >> grape-logs.txt +``` + +Check that the file contains the requested logs: +``` +cat grape-logs.txt + +I am ape for grapes + +``` diff --git a/k8s-zero-to-hero/solutions/exercise-2.md b/k8s-zero-to-hero/solutions/exercise-2.md new file mode 100644 index 0000000..808059e --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-2.md @@ -0,0 +1,19 @@ +## Exercise 2 + +We can create the pod yaml using the `--dry-run=client` option: +``` +kubectl run alphapod --image=nginx:1.7.9 --dry-run=client -o yaml > alphapod.yaml +``` + +We edit the local yaml to modify the container name (spec.containers.name): +``` +vi alphapod.yaml +``` + +Apply the new yaml file: +``` +kubectl create -f alphapod.yaml +``` + + + diff --git a/k8s-zero-to-hero/solutions/exercise-3.md b/k8s-zero-to-hero/solutions/exercise-3.md new file mode 100644 index 0000000..1a9543d --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-3.md @@ -0,0 +1,39 @@ +## Exercise 3 + +First we get the deployment yaml +``` +kubectl get deployment snooper -n starfruit -o yaml > snooper-new.yaml +``` + +Edit the deployment yaml as follow: +``` +... + + spec: + containers: + - image: nginx + imagePullPolicy: Always + name: snooper-con + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /tmp/snooper.log + name: logsfile + - name: kennylogger-con + image: busybox:1.31.0 + args: [/bin/sh, -c, 'tail -n+1 -f /tmp/snooper.log'] + volumeMounts: + - name: logsfile + mountPath: /tmp/snooper.log +... + + + +``` +Apply the deployment: +``` +kubectl create -f snooper-new.yaml +``` + + diff --git a/k8s-zero-to-hero/solutions/exercise-4.md b/k8s-zero-to-hero/solutions/exercise-4.md new file mode 100644 index 0000000..e47e75d --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-4.md @@ -0,0 +1,44 @@ +## Exercise 4 + +Create a sameple deployment yaml +``` +kubectl create deployment watermelon -n coconut --image=nginx --replicas=5 --dry-run=client -o yaml > watermelon-deployment.yaml +``` + +Now we get the pod yaml to include this in our deployment +``` +kubectl get pod watermelon -n coconut -o yaml +``` + +Copy the content and paste it in our deployment as follow: +``` +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: watermelon + name: watermelon + namespace: coconut +spec: + replicas: 5 + selector: + matchLabels: + app: watermelon + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: watermelon + spec: + containers: + - image: nginx + name: watermelon + +``` +Apply the deployment file and check that it's working properly: + +``` +kubectl create -f watermelon-deployment.yaml +``` diff --git a/k8s-zero-to-hero/solutions/exercise-5.md b/k8s-zero-to-hero/solutions/exercise-5.md new file mode 100644 index 0000000..024df19 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-5.md @@ -0,0 +1,40 @@ +## Exercise 5 + +First we need to check the status of the deployment and the pods: +``` +kubectl rollout status deployment lemon -n banana + +Waiting for deployment "lemon" rollout to finish: 1 old replicas are pending termination... +``` +We can see that the new pods are not available due ImagePullBackOff +``` +kubectl get pods -n banana + +NAME READY STATUS RESTARTS AGE +lemon-55c4b469b9-5jnld 1/1 Running 0 73s +lemon-d956f49f9-wkldc 0/1 ImagePullBackOff 0 57s +``` +Check the deployment history +``` +kubectl rollout history deployment lemon -n banana +``` + +And rollback to the previous version +``` +kubectl rollout undo deployment lemon -n banana + +deployment.apps/lemon rolled back +``` + +Check that the deployment is now running succesfully: +``` +kubectl rollout status deployment lemon -n banana + +deployment "lemon" successfully rolled out +``` + + + + + + diff --git a/k8s-zero-to-hero/solutions/exercise-6.md b/k8s-zero-to-hero/solutions/exercise-6.md new file mode 100644 index 0000000..d7ec823 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-6.md @@ -0,0 +1,39 @@ +## Exercise 6 + +Create your job template as follow: +``` +apiVersion: batch/v1 +kind: Job +metadata: + name: pi + namespace: banana + labels: + eatyour: bananas +spec: + completions: 5 + parallelism: 3 + template: + spec: + containers: + - name: banana-bin + image: busybox:1.31.0 + command: ["/bin/sh", "-c", "sleep 2 && echo done"] + restartPolicy: Never + + +``` + +Apply this template and check that it's working properly: +``` +kubectl create -f bananajob.yaml + +``` + +``` +kubectl get jobs -A + +NAMESPACE NAME COMPLETIONS DURATION AGE +banana pi 5/5 9s 12s + +``` + diff --git a/k8s-zero-to-hero/solutions/exercise-7.md b/k8s-zero-to-hero/solutions/exercise-7.md new file mode 100644 index 0000000..860b9f3 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-7.md @@ -0,0 +1,61 @@ +## Exercise 7 + +First we could start by listing the pods of the namespace showing the labels +``` +kubectl get pods -n basket --show-labels +``` +That give us an idea of the initial status before to apply the changes: + +``` +NAME READY STATUS RESTARTS AGE LABELS +lemon 1/1 Running 0 26h type=citrus +lime 1/1 Running 0 26h type=citrus +pear 1/1 Running 0 26h type=seedless +strawberry 1/1 Running 0 26h type=seedless +watermelon 1/1 Running 0 26h type=seeds + +``` + +Then we can label the pods, we can do it tag by tag or all together, this is up to you + +``` +kubectl label pod --selector='type=citrus' safe=sound -n basket + +pod/lemon labeled +pod/lime labeled +``` +And +``` +kubectl label pod --selector='type=seedless' safe=sound -n basket + +pod/pear labeled +pod/strawberry labeled + +``` + +Check that the label has been applied to the correct pods: + +``` +kubectl get pods -n basket --show-labels + +NAME READY STATUS RESTARTS AGE LABELS +lemon 1/1 Running 0 27h safe=sound,type=citrus +lime 1/1 Running 0 27h safe=sound,type=citrus +pear 1/1 Running 0 27h safe=sound,type=seedless +strawberry 1/1 Running 0 27h safe=sound,type=seedless +watermelon 1/1 Running 0 27h type=seeds + + +``` + +Last step is to annotate the pods with the label `safe:sound` + +``` +kubectl annotate pod --selector='safe=sound' fruit='good for what ails ya' -n basket + +pod/lemon annotated +pod/lime annotated +pod/pear annotated +pod/strawberry annotated + +``` diff --git a/k8s-zero-to-hero/solutions/exercise-8.md b/k8s-zero-to-hero/solutions/exercise-8.md new file mode 100644 index 0000000..150795f --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-8.md @@ -0,0 +1,109 @@ +## Exercise 8 + +Create the persistentVolume in file named *pv.yaml* : +``` +apiVersion: v1 +kind: PersistentVolume +metadata: + name: strawberry-pv +spec: + capacity: + storage: 2Gi + accessModes: + - ReadWriteOnce + storageClassName: exam + hostPath: + path: /Data/Berry + +``` +Apply that file + +``` +kubectl create -f pv.yaml +``` + +Create the pvc yaml file + +``` +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: strawberry-pvc + namespace: straw +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + storageClassName: exam + +``` +and apply that file +``` +kubectl create -f pvc.yaml +``` + +Create deployment: + +``` +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: strawberry-deploy + name: strawberry-deploy + namespace: straw +spec: + replicas: 1 + selector: + matchLabels: + app: strawberry-deploy + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: strawberry-deploy + spec: + volumes: + - name: strawberry-pv + persistentVolumeClaim: + claimName: strawberry-pvc + containers: + - image: nginx:1.7.9 + name: nginx + volumeMounts: + - mountPath: "/tmp/berry-data" + name: strawberry-pv + +``` + +Check that the strawberry pv is bound, the pvc is binded and the pod is running: + +``` +kubectl get pv strawberry-pv +NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE +strawberry-pv 2Gi RWO Retain Bound straw/strawberry-pvc exam 3m6s + + + +kubectl get pvc -n straw +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE +strawberry-pvc Pending exam 14m + + +kubectl get pods -n straw +NAME READY STATUS RESTARTS AGE +strawberry-deploy-6494f99fcd-fv8nx 1/1 Running 0 5m40s + + +``` + + + + + + + diff --git a/k8s-zero-to-hero/solutions/exercise-9.md b/k8s-zero-to-hero/solutions/exercise-9.md new file mode 100644 index 0000000..19f4c22 --- /dev/null +++ b/k8s-zero-to-hero/solutions/exercise-9.md @@ -0,0 +1,13 @@ +## Exercise 9 + +``` + kubectl create configmap configmap-kiwi --from-file=web-kiwi.html +``` + +Check that container is now running: +``` +kubectl get pods + +NAME READY STATUS RESTARTS AGE +kiwi-deploy-7b9bcf5445-xdf6f 1/1 Running 0 2m14s +``` diff --git a/k8s-zero-to-hero/web-kiwi.html b/k8s-zero-to-hero/web-kiwi.html new file mode 100644 index 0000000..36a444f --- /dev/null +++ b/k8s-zero-to-hero/web-kiwi.html @@ -0,0 +1,3 @@ + +Hello +