Skip to content

Latest commit

 

History

History
122 lines (98 loc) · 4.96 KB

readme.md

File metadata and controls

122 lines (98 loc) · 4.96 KB

Kubernetes Basics Workshop

Requirements

  • Access to a kubernetes cluster. This can be a local KinD (Kubernetes in Docker) cluster or a GKE (Google Kubernetes Engine) cluster.
  • For GKE, we need the google cloud CLI
  • And we need kubectl to be installed

Other useful resources

Let's get started

Kubeconfig and cluster connection

  • Connect to our workshop k8s cluster

    • gcloud container clusters get-credentials k8s-workshop-cluster --region europe-north1 --project robocon2024-workshop
  • What is my current kubectl context?

Create an app

  • Create your namespace

    • kubectl create ns your-demo-ns
  • Export the namespace name as a variable

    • on Windows ⚠️: set MY_NAMESPACE=your-demo-ns
    • on Unix: export MY_NAMESPACE=your-demo-ns
  • Deploy an nginx container with one kubectl command

    • kubectl create deployment nginx --image=nginx -n %MY_NAMESPACE%
  • Have a look at the created deployment object in the cluster

    • kubectl get deploy -n %MY_NAMESPACE%
    • kubectl get deploy -n %MY_NAMESPACE% -o wide for more context
    • kubectl get deploy -n %MY_NAMESPACE% -o yaml for full context
  • Check the status of your pods

    • kubectl get pods -n %MY_NAMESPACE%
    • kubectl get pods -n %MY_NAMESPACE% -o wide for more context
    • kubectl get pods -n %MY_NAMESPACE% -o yaml for full context
  • Port forward to see the website

    • on Windows ⚠️:
      • retrieve name of the pod: kubectl get pods -n %MY_NAMESPACE% -o name
      • copy the name to clipboard and then set it as variable, e.g.: set POD=nginx-000000000000-00000
      • kubectl port-forward %POD% 8080:80 -n %MY_NAMESPACE%
    • on Unix:
      • export POD=$(kubectl get pods -n $MY_NAMESPACE -o name)
      • kubectl port-forward $POD 8080:80 -n $MY_NAMESPACE
    • Access nginx in browser: http://localhost:8080/

Let's customize the app

How is this nginx configured?

Editing index.html

  • Installing vim in the container
    • apt update
    • apt install vim
    • vim /usr/share/nginx/html/index.html

Check your changes

Deleting the pod

  • This will remove your changes as the pod will be re-created from the deployment definition
    • kubectl delete %POD% -n %MY_NAMESPACE%

A permanent way to customization

Inject the index.html via a configmap into the pod

Create a configmap

Create a configmap based on your index.html

  • kubectl create configmap index-html --from-file=index.html -n %MY_NAMESPACE%

Check the config map

  • kubectl get cm index-html -n %MY_NAMESPACE% -o yaml

Kubernetes documentation on configuring pods with configmaps

Inject the config map into the pod via a volume

Kubernetes documentation on using a configmap as a volume

Get the kubernetes manifest of your current deployment

  • kubectl get deploy nginx -n %MY_NAMESPACE% -o yaml > nginx-deployment.yaml

Clean up the yaml from cluster specific content via this online tool kubernetes-manifest-cleaner

Add the volume and the volumeMount to the Kubernetes manifest. Add the volumes under the section spec in the YAML:

      volumes:
      - name: index-html-vol
        configMap:
          name: index-html
          items:
          - key: index-html-file-content
            path: index.html

And volumeMount under the containers section:

        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: index-html-vol

Apply the changes back to the cluster

  • kubectl replace -f nginx-deployment.yaml

Check your pod definition containing your changes

  • kubectl get pods -n %MY_NAMESPACE% -o yaml

Check your changes