Call us
General

Kubernetes for Beginners: A Step-by-Step Guide to Setting Up Your First Cluster

Discover Kubernetes with Cpluz's beginner-friendly guide. Learn to set up your first cluster with our step-by-step tutorial, mastering container orchestration and deployment.


4 min readCpluz

Kubernetes for Beginners: A Step-by-Step Guide to Setting Up Your First Cluster

Kubernetes has revolutionized the way organizations manage and deploy containerized applications. As a container orchestration system, it automates many of the manual processes involved in deploying, scaling, and managing containers. If you're new to Kubernetes, understanding its core concepts and setting up your first cluster can seem daunting. In this article, we'll provide a comprehensive step-by-step guide to help you get started with Kubernetes.

What is Kubernetes?

Kubernetes, also known as K8s, is an open-source container orchestration system for automating the deployment, scaling, and management of containerized applications. It was originally designed by Google, and is now maintained by the Cloud Native Computing Foundation (CNCF). Kubernetes allows you to declare your desired state for your application, and it will automatically manage the underlying infrastructure to achieve that state.

Key Components of Kubernetes

To understand how Kubernetes works, it's essential to familiarize yourself with its core components:

  • Pods: The basic execution unit in Kubernetes, a pod represents a logical host for one or more containers.
  • ReplicaSets: Ensures a specified number of replicas (identical pods) are running at any given time.
  • Deployments: Manages the rollout of new versions of an application, providing a way to update and roll back deployments.
  • Services: Provides a network identity and load balancing for accessing a pod or a group of pods.
  • Persistent Volumes (PVs) and Claims: Persistent storage for data that needs to survive even after a pod is deleted.

Setting Up Your First Kubernetes Cluster

Setting up a Kubernetes cluster can be done in various ways, including using cloud providers like Google Kubernetes Engine (GKE), Amazon Elastic Container Service for Kubernetes (EKS), or Azure Kubernetes Service (AKS), or on-premises using tools like Minikube or kubeadm. For this guide, we'll use Minikube, a popular tool for running Kubernetes locally on your machine.

Installing Minikube

First, download and install Minikube on your machine. You can find installation instructions for your operating system on the official Minikube website. Once installed, you can verify that Minikube is working by running the following command:

kubectl get nodes

This command should display your local machine as a node in your Kubernetes cluster.

Creating a Kubernetes Cluster with Minikube

To create a Kubernetes cluster with Minikube, run the following command:

minikube start

This command will start a single-node Kubernetes cluster on your local machine. You can verify that the cluster is up and running by running the following command:

kubectl get nodes

This command should display your local machine as a node in your Kubernetes cluster.

Deploying a Sample Application

Now that your Kubernetes cluster is up and running, let's deploy a sample application to test it out. We'll use the popular NGINX web server as an example. First, create a YAML file called nginx-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

This YAML file defines a deployment called nginx-deployment that runs two replicas of the NGINX container. To deploy the application, run the following command:

kubectl apply -f nginx-deployment.yaml

This command will create the deployment and start running the NGINX containers. You can verify that the deployment is successful by running the following command:

kubectl get deployments

This command should display the nginx-deployment deployment with a READY status of 2/2.

Accessing the Application

To access the NGINX application, you'll need to expose the deployment as a service. Create a YAML file called nginx-service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer

This YAML file defines a service called nginx-service that exposes the NGINX deployment on port 80. To create the service, run the following command:

kubectl apply -f nginx-service.yaml

This command will create the service and expose the NGINX application. You can verify that the service is successful by running the following command:

kubectl get svc

This command should display the nginx-service service with an EXTERNAL-IP address. You can access the NGINX application by visiting the EXTERNAL-IP address in your web browser.

Conclusion

Setting up your first Kubernetes cluster can seem daunting, but with this step-by-step guide, you should now have a basic understanding of Kubernetes and how to set up a cluster using Minikube. Remember, practice makes perfect, so be sure to experiment with different configurations and applications to get the most out of your Kubernetes cluster. If you have any questions or need further assistance, feel free to reach out to us at info@cpluz.com or visit cpluz.com for professional design and hosting solutions.