Kubernetes Workshop

What we will be covering

  • Local Kubernetes Environment
  • Pods
  • Pod Controllers
  • Kubernetes Configuration
  • Helm

Workshop Code

https://github.com/codayblue/kubernetes-workshop

Tools needed for this workshop

When starting rancher desktop for the first time select dockerd (moby)

Rancher Desktop comes with all the other tools we need to get started

Pods

  • Smallest form of a workload
  • Ephemeral
  • Needs a controller to come back if deleted
  • Is made up of 1 or more containers
  • All containers in the pod communicate over 127.0.0.1

Lets run a pod

							
								kubectl run nginx --image=nginx
							
						

You can check pods with the following

							
								kubectl get pods
							
						

You should see something like below

							
								NAME    READY   STATUS    RESTARTS   AGE
								nginx   1/1     Running   0          77s
							
						

Depending on internet connection and speed it might take time get a status of running

Time to clean up and move on to controllers

							
								kubectl delete pod nginx
							
						

Pod Controllers

  • Deployments (Most Common)
  • Daemonset
  • StatefulSet
  • Job
  • CronJob

Lets create a deployment

							
								kubectl create deployment nginx --image=nginx --port=80
							
						

Lets checkout our deployment

							
								kubectl get deployments
							
						

Lets checkout the pod it made

							
								kubectl get pods
							
						

You can change the scale of a deployment

							
								kubectl scale --replicas=3 deployment/nginx
							
						

You can see with the following that it scaled

							
								kubectl get deployments
								kubectl get pods
							
						

Exposing pods

  • NodePort
  • LoadBalancer
  • ClusterIp (Headless)
  • port-forward (debugging)

port forward to connect our computer direct to pod

							
								kubectl port-forward deployment/nginx 8080:80
							
						

When going to localhost:8080 in your broswer you should see the nginx default page

To cancel the port forward and reclaim the shell hit ctrl + c

Lets expose via NodePort

							
								kubectl expose deployment nginx --port=80 --type="NodePort"
							
						

To find the port that got created run the following

							
								kubectl get service
							
						

You should get the following output

							
								NAME         TYPE        CLUSTER-IP   ...  PORT(S)     
								kubernetes   ClusterIP   10.43.0.1    ...  443/TCP     
								nginx        NodePort    10.43.121.75 ...  80:31053/TCP
							
						

The 80:31053 tells you the nodeport selected is 31053

in this case going to localhost:31053 in your browser will get you the nginx default page

Starting clean up lets delete our pod (dont worry it wont come back on its own)

							
								kubectl delete deployment nginx
								# You can verify the pods are gone by running
								kubectl get pods
							
						

To show services are seperated from pods you can see it still exists

							
								kubectl get service
							
						

Since they are seperate objects how are they connected?

							
								kubectl describe service nginx
							
						

describe is a valuable tool, make sure to try it out on other resource types

lets clean up our service

							
								kubectl delete service nginx
							
						

Kubernetes Configuration

  • YAML
  • Used for most container deploys
  • Many Templating Tools
  • Saves Configuration to repo
  • Helm Charts

    • Templatized Kubernetes Config
    • Can be made for reusablilty
    • Packages are called charts
    • Community charts can be found at Artifact Hub

    What does a Kubernetes config look like?

    							
    								helm template ./chart
    							
    						

    Local Development starter

    							
    								# Make some namespaces
    								kubectl create namespace dashboard
    								kubectl create namespace dev
    
    								# Deploy local development
    								tilt up
    							
    						

    What just happened?

    • Made some namespaces
    • tiltfile
    • Our local dev environment is location at http://127-0-0-1.sslip.io/

    What are the resources we just deployed?

    • Namespaces
    • Ingress

    Setting up dashboard

    • Dashboard can be found at http://dashboard.127-0-0-1.sslip.io
    • Setup a dev pane

    Lets deploy a production namespace

    Build Production Image

    							
    								docker build \
    								-t ghcr.io/codayblue/kubernetes-workshop/app:v1.0.0 \
    								-f ./app/Dockerfile \
    								./app
    							
    						

    Normally we would push this to a registry for deployment

    Since our cluster is local we built it on the same runtime so thats not needed

    Creating Production Namespace

    							
    								kubectl create namespace production
    							
    						

    Deploy Production to Production Namespace

    							
    								helm upgrade prod-app-helm ./chart \
    								--install \
    								--namespace production \
    								-f ./chart/prod-values.yaml
    							
    						

    Add Production pane to dashboard

  • Production will be found at http://prod.127-0-0-1.sslip.io/
  • Thank you for coming!

    This is just the start, keep learning!