This tutorial requires minikube or K3S up and running.
A Kubernetes namespace provides the scope for Pods, Services, and Deployments in the cluster. Users interacting with one namespace do not see the content in another namespace.
Create Namespaces:
- testing
- development
- production
with labels
customer=foo
environment={testing,development,production}
kubectl create -f https://raw.githubusercontent.com/dme86/kube-tutorial/main/namespaces.yml
Apply quotas and limitranges to testing namespace:
kubectl apply -f https://raw.githubusercontent.com/dme86/kube-tutorial/main/limitrange_quota.yml --namespace=testing
Show limitrange from testing namespace:
# only names and creation date
kubectl get limitrange --namespace=testing
# full output
kubectl get limitrange --namespace=testing --output=yaml
Show resourcequota mem-cpu-quota from testing namespace:
# short output
kubectl get resourcequota mem-cpu-quota --namespace=testing
# full yaml output
kubectl get resourcequota mem-cpu-quota --namespace=testing --output=yaml
A pod is a group of one or more containers. A container is an enclosed, self-contained execution process, much like a process in an operating system. Kubernetes uses pods to run your code and images in the cluster.
Kubernetes works with Pods, rather than containers, so that containers in the same pod can be guaranteed to run on the same machine. Containers in the same pod share their networking infrastructure, storage resources, and lifecycle.
Create a simple Pod inside testing, check it's logs and expose it for access via browser:
kubectl create -f https://raw.githubusercontent.com/dme86/kube-tutorial/main/pods/nginx-hello.yml --namespace=testing
kubectl logs nginx-hello --namespace=testing
127.0.1.1 babylon.loc babylon
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
kubectl get pods --namespace=testing -o wide
kubectl describe po nginx-hello --namespace=testing
kubectl expose pod nginx-hello --type=NodePort --port=8080 --target-port=80 --namespace=testing
minikube service nginx-hello --url --namespace=testing
kubectl get service -n testing
Try to create a second pod from the pods folder. The service will expose both pods, see:
kubectl describe service --namespace=testing
Accessing the pod via browser you'll see the Server name - either nginx-hello or nginx-hello-2. Delete the container wich is displayed on the website and reload: Kubernetes will automatically route the traffic to the other (healthy) container.
kubectl delete pod nginx-hello --namespace=testing
A deployment is an object in Kubernetes that lets you manage a set of identical pods.
Without a deployment, you’d need to create, update, and delete a bunch of pods manually.
With a deployment, you declare a single object in a YAML file. This object is responsible for creating the pods, making sure they stay up to date, and ensuring there are enough of them running
You can also easily autoscale your applications using a Kubernetes deployment.
This will create a simple deployment with the same container we used in the Pods section inside our development namespace:
kubectl apply -f https://raw.githubusercontent.com/dme86/kube-tutorial/main/deployments/nginx-deployment.yml --namespace=development
Check your deployment:
kubectl get deployments --namespace=development
Now you can effortlessly scale up or down the amount of containers in your deployment:
kubectl scale --replicas=6 deployment nginx-hello --namespace=development