K8s and Kind. Getting Started
August 16, 2023 | K8sI've been spending some time playing around with Kubernetes, and as I dive into it, I'll be sharing my experiences right here. I'm a bit new to blogging, so bear with me as I work to improve. The site theme is pretty basic, so before my next post I'll be working to improve text formatting.
For now, I've chosen to start with Kubernetes Kind. It's a great tool for local development and testing, but since my main role is DevOps, I'll eventually be crossing over into microk8s. This shift aligns perfectly with my current job's environments, where we're committed to running our containers on AWS EC2 instances. A full deployment of Kubernetes is more expensive and robust than our average client requires. So this will allow us a greater deal of flexibility during development, and we'll be able to deploy to our customers' lightweight instances with ease.
Microk8s looks to be a lightweight entry point into Kubernetes for us. It could even become the defacto way we implement k8s for our smaller clients. The journey has just begun, so stay tuned as I explore and document the path towards a robust and efficient Kubernetes environment.
I'm starting on Ubuntu 22.04. Step one is to install golang.
Then we need to install kind
To manage our cluster, we'll want kubeclt. The 'classic' parameter is required since kubectl will need more system control than snap usually allows.
With these installed, we can deploy our first test website. The following .yaml starts an nginx server with just the default welcome webpage on it. We'll explore putting more content into it in future posts. Name the file: website.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: website spec: replicas: 2 selector: matchLabels: app: website template: metadata: labels: app: website spec: containers: - name: nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: website spec: selector: app: website ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort
Now we can use the following bash script to deploy the yaml, and report the ip and port we can access it on. Be sure to `chmod +x` this file. Name the file: kind-my-k8s-website.sh
#!/bin/bash # Deploy the service kubectl apply -f website.yaml # Get IP and NodePort NODE_IP=$(kubectl get nodes -o=jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}') NODE_PORT=$(kubectl get svc website -o=jsonpath='{.spec.ports[0].nodePort}') # Report the information echo "Website is accessible at: http://$NODE_IP:$NODE_PORT"
Before we run this script, we need to create a cluster:
kind create cluster --name my-k8s-website
Now lets be sure our kubectl is using this cluster as it's context:
kubectl config use-context kind-my-k8s-website
Finally, lets start our service:
./kind-my-k8s-website.sh
After the service is started, this script will attempt to show the ip and port the service can be accessed on:
deployment.apps/website unchanged service/website unchanged Website is accessible at: http://172.18.0.2:30784
We can attempt to curl this address to check if we see the nginx welcome page.
curl -s --head http://172.18.0.2:30784 | head -n1
I simplified the output that curl would output, but this gave me an HTTP/1.1 200 OK response, so we know nginx is responding successfully.
Thanks for following along on this journey into Kubernetes! If you have any questions, comments, or insights, please feel free to reach out to me on Threads. Stay tuned for more posts as I continue to dive into new technologies and share my discoveries. Happy coding, and don't hesitate to join the conversation!