DEV Community

Cover image for Set Sail with Kubernetes: The Definitive Guide for New Captains
7h3-3mp7y-m4n
7h3-3mp7y-m4n

Posted on

Set Sail with Kubernetes: The Definitive Guide for New Captains

So, I’ve read a bunch of beginner guides on Kubernetes, and honestly? Most of them either lack depth or are just step-by-step walkthroughs that can leave you stuck. But a few were truly great, and they inspired me to create the best Kubernetes guide ever one that’ll have you running your cluster and creating objects with ease, no matter your background. I hope I succeed on this journey.

What is Kubernetes?

Yep, I know, this is how every other blog starts. But hey, it’s always good to know more about our beloved Kubernetes, right?
Kubernetes ("K8s" if you’re in a hurry) is an open-source system that automates container deployment tasks. Originally developed by Google, it’s now maintained by the awesome folks at the Cloud Native Computing Foundation (CNCF).

Fun Fact: The Kubernetes logo represents a ship’s wheel, symbolizing that you’re the captain of your cluster, steering those containers with the help of an orchestrator.

Kubernetes Features: Why It's the Best Thing Since Sliced Bread

Just like I said, Kubernetes has a fantastic community contributing to its continuous improvement. Here are some of the cool features that make Kubernetes the go-to platform for container management:

  • Automated Rollouts, Scaling, and Rollbacks: Imagine having a personal assistant who always ensures you have the right number of containers running, even when one goes down. That’s Kubernetes for you!

  • Service Discovery, Load Balancing, and Network Ingress: Worried about network issues? Kubernetes has you covered. It’s like having a GPS for your containers.

  • Stateless and Stateful Applications: Whether your app needs to remember everything or nothing at all, Kubernetes has built-in objects to manage both.

  • Storage Management: Persistent storage? No problem. Kubernetes abstracts it all for you, no matter where it’s stored.

  • Declarative State: Kubernetes lets you describe the desired state of your cluster, and it automatically makes it so-like magic 🪄 but better.

  • Works Across Environments: Whether you’re in the cloud, on the edge, or just tinkering on your laptop, Kubernetes is there for you.

  • Highly Extensible: Think of Kubernetes as a LEGO set; you can build anything you want with custom object types, controllers, and operators. (My favorite is Knative🤫)

Getting Started: Let’s Roll To Our Ship 🚢 ☸️

Kubernetes can run in various environments, thanks to the range of distributions it offers. Creating a cluster using the official distribution can be complex, so most people start with a packaged solution like Minikube (my personal favorite ❤️), MicroK8s, K3s, or Kind.

When I first started learning Kubernetes, I used Docker Desktop’s Kubernetes extension—it was the easy way out 🤫. But, if you want to dive deep, installing Kubernetes via Minikube is great practice. You can also try Kubeadm, cause it’s often asked in CKA if you're preparing for it.

Pro Tip: If you’re lazy like me, Homebrew is your best friend.

brew install minikube
Enter fullscreen mode Exit fullscreen mode

after the Minikube does its thing 🍺 type your welcoming command.

minikube start
Enter fullscreen mode Exit fullscreen mode

And just like that, you’ve got a working Kubernetes cluster! if you got stuck you can also refer to their docs here.

The welcome screen would be similar to this.

MiniKube start
Yay! My fellow captains, we just started our ship(Kubernetes cluster).

Basic Kubernetes Terms: Let's Get Familiar With Tools

Now that we’ve got Kubernetes running, let’s talk about some basic terms. What if you don't know them? If you’re eager to learn more about its architecture, I’ve got a great article for you, check it outto deepen your understanding. I hope you've read the article, but even if not, I assume you have enough knowledge to follow this walkthrough.

Nodes

diagram of Node
Nodes are the physical machines that make up your Kubernetes cluster. They run the containers you create. Let’s see our node in action

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

You’ll see something like this✨
preview of node
For more details, we can use

Kubectl get node -o wide
Enter fullscreen mode Exit fullscreen mode

in kubectl the get is a command which is used to retrieve and display information about Kubernetes resources. When you use kubectl get, you are asking Kubernetes to fetch and show you details about specific resources.

Namespace

In Kubernetes, we have a helpful concept called namespace. Think of it as a folder for your Kubernetes objects, providing a layer of isolation to keep everything organized and prevent mixing things up like a salad. 🌱

Namespaces are especially useful in environments with many users or teams, allowing each to have their own isolated resources without interfering with others.

You might already know this, but if not, here's a quick tip: there are already existing namespaces in your cluster. You can list them with the following command:

kubectl get ns

Enter fullscreen mode Exit fullscreen mode

Here, ns is just a shorthand for namespace---a little shortcut to save you some typing! and now we can see all the existing namespaces.

Fetching Namespace

Bunch of namespaces we can see, Let’s create our own namespace for this guide

namespace check
The create command in kubectl is used to create various objects within your Kubernetes cluster.

Cooking with Kubernetes:

Now, all our work will be neatly tucked away in our namespace called mycookbook. Neat, right?

Pods: The Building Blocks

Pods are the smallest and most basic objects in Kubernetes. Instead of running containers directly on nodes, Kubernetes runs them inside pods. Think of a pod as a tortilla wrapping around your containers, sounds delicious, doesn’t it?

Let’s create our first tortilla I mean pod haha 😂

kubectl -n mycookbook run mypod --image=nginx --port=8080

so we are making our first pod with nginx which listens at port:8080, We can also see it while it's getting created if we are fast enough.

Kubectl -n my cookbook get pods

Creation of our pod
what about we see its whole journal? we can use the describe command which is very helpful on days when you are debubbing. So lets type

kubectl -n mycookbook describe pods/mypod

Mypod inside
Let's verify our setup by hopping into our browser and visiting the given port. But wait, why do we see nothing when we visit our desired port? 🤔

Don’t worry, this is expected! By default, a Pod in Kubernetes is only accessible within the Kubernetes cluster itself. This means that external access is not available out of the box.

So, does this mean we can never access our first Pod?

Absolutely not! You can still access your Pod externally, but you’ll need to use a Kubernetes Service to expose it to the outside world. Kubernetes Services acts as an entry point, allowing external traffic to reach your Pods. So, while you may not

Making Pizza Kubernetes: Pods, Deployments, and Services

Oh God, I'm hungry now, I'm craving something like pizza 🍕, how about we make a pizza on Kubernetes instead of in our kitchen?😋

I’m making pizza out of niginx and my recipe will have 5 ngnix pod, and they listen to the port 80 and this time we will eat it together 🤗

so lets make our dough(deployment) for our pizzas.

Deployments: Dough ?
The Deployment is your recipe. It tells Kubernetes what kind of pizza (Pods) you want, how many you want to bake, and what ingredients (containers) to use. For example, you might want 5 Margherita pizzas (Pods) with the same ingredients (containers). Kubernetes will ensure that you have exactly 5 of these pizzas ready, all made from the same recipe. So let's deploy 5 Nginx pods and call it pizza (for being simple).🍕

kubectl -n mycookbook create deployment pizza --image=nginx --replicas=3 --dry-run=client -o yaml > pizza.yml

Enter fullscreen mode Exit fullscreen mode

yes we are making pizza from scratch and we should use yaml instead for those direct commands, Now when we cat pizza.yml we could see a our pizza dough like this..

Deploying our pizza
Oops, we only got 3 replicas, but we wanted 5! No worries, we can scale it up ⬆️. But before that, we should apply it by using apply command.

kubectl apply -f pizza.yml

Enter fullscreen mode Exit fullscreen mode

so we could see our deployment is being created, we can verify it by the kubectl -n mycookbook get deploy and see it rolling 😊. But we got the dough for only 3 pizzas what should we do? compromise? Nah, we can we can scale it up ⬆️ by this.

kubectl -n mycookbook scale deploy/pizza --replicas=5

Enter fullscreen mode Exit fullscreen mode

Clarification: The scale command allows you to adjust the number of replicas (instances) of a deployment. In this case, it increases the number of Nginx pods from 3 to 5.

Or, you can use the edit command to update the number of replicas directly.

What? What is an edit? can we also be creative like we get while making pizza? And the answer is Yes! 🙌

The edit command in Kubernetes is indeed a powerful tool! It allows you to manually modify the configuration of your objects, like deployments. Here's how it works:

  • Power and Caution: Just like wielding a sharp knife in the kitchen, edit gives you great control but requires careful handling. If not used properly, changes might cause unexpected issues with your deployment.

  • How It Works: When you run the edit command, it opens the configuration of your deployment in your default text editor (like Vim or Nano). From there, you can make changes directly, such as updating the number of replicas in the replicas field.

  • Safe Editing: Although powerful, it's wise to proceed with caution. If you make a mistake, it could affect how your deployment behaves. A safer approach is to use the edit command to open the configuration, then save and apply changes carefully.

We'll dive into using the edit command later, and I promise you'll get the hang of it! 🤗

Services: Plating and Serving 🍽️

Our pods(pizzas) are running, but how do we access them? That's where Kubernetes Services comes in. They expose your pods to the network, letting you access them from outside the cluster.

One of the best resources for learning more about Kubernetes is its comprehensive documentation. Contributed to by experts from around the world, it simplifies complex concepts and enhances your understanding. So let's plate our pizzas ;) With the help of Kubernetes documentation(Kubernetes presentation book) by clicking here.

By reading through docs we can do this by adding this yaml to our pizza.

---
apiVersion: v1
kind: Service
metadata:
  name: pizza
  namespace: mycookbook
  labels:
    app: pizza
spec:
  selector:
    app: pizza
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30080  # Optional: Specify a NodePort or let Kubernetes assign one

Enter fullscreen mode Exit fullscreen mode

You can copy this and there is also an easy way to do this but we are head chef right now so let's do everything from scratch 🤗, let's apply this, I have saved this file as pizzasvc.yml, you can name it anything or you can also add it to our previous deployment file and apply it.

Service check
Let's also verify it by kubectl -n mycookbook get svc/pizza.We can see that it's ready to be served 😋.

But wait did we edit command as I promised? no, right ? So let's use it cause I forgot to name our delicious pizzas as Margherita. So let us use it

kubectl -n mycookbook edit deploy/pizza

Enter fullscreen mode Exit fullscreen mode

woah what just opened? the heart of our pizza❤️. let us find the place where we can name our pod Margherita.

Changing name of container

kubectl -n mycookbook get deployment pizza -o jsonpath='{.spec.template.spec.containers[*].name}'

Enter fullscreen mode Exit fullscreen mode

and now we have each container named a Margherita, Yummy 😋.

Reaching the endpoint: Time to eat 🤤

Pizza on ship with k8s
Now, we can check out your nginx pizza by visiting the assigned port. We can follow the pattern as http://: . For me the NodePort is 30080 as I have defined you can check it by the kubectl -n mycookbook get svc and for the node, we can use kubectl get node -o wide.

we can use curl but for Minikube it is useful to use minikube -n mycookbook svc pizza

There was another easy way to skip the service yaml part by using the kubectl -n mycookbook expose deploy/pizza --type=NodePort --port=80" which can do the same But I wanted you guys to feel like a chef to learn it with heart ❤️.

So there you have it, folks! 🍕
If you followed along, you could have your very own pizza or whatever you were making cooked up and ready to serve. We all have a little chef inside us, and instead of a kitchen, we're using Kubernetes to cook our creations! 😉

But after cooking, what comes next? Dishes, right?

In the Kubernetes world, "doing the dishes" means cleaning up our resources to keep things tidy and efficient. Let's go ahead and do that!

Cleaning Up: Doing the Dishes

To clean up everything we created:

  1. Delete the Deployment:

     kubectl -n mycookbook delete deployment pizza
    
    
  2. Delete the Service:

     kubectl -n mycookbook delete service pizza
    
    

Wrapping up: Farwell 😔

Yes, I know there are more advanced topics like RBAC, Ingress, Volumes, and many more. But those are for another time, as they dive deeper into the wonderful world of Kubernetes.

For now, I hope you've gotten a good overview of Kubernetes. I truly hope you learned something valuable today and enjoyed our little cooking adventure in the world of K8s.

As a parting gift, I want to share something cool with you. Did you know that Kubernetes has a beautiful dashboard? It's like a control panel where you can visualize everything that's happening in your cluster.

Wanna check it out? Just type

minikube dashboard

Enter fullscreen mode Exit fullscreen mode

Isn't that pretty? 😉 Enjoy exploring, and see you next time when we dive into the more advanced topics! ☸️

Top comments (2)

Collapse
 
codycodes profile image
Cody Antonio Gagnon

Fantastic overview! Loving the foodie symbolism 🍕🍞🌯

Next time I use k8s I might get a little hungry 🤤

Collapse
 
7h33mp7ym4n profile image
7h3-3mp7y-m4n

Thanks😄 Glad you enjoyed the foodie vibes! 🍕 Kubernetes and cooking really do go hand in hand, next time you're on K8s, keep a snack close by 😉