Kubernetes for the Absolute Beginners

Vishal Patel

6/4/202414 min read

Audience

This blog is ideal for beginners in the IT and software development fields interested in DevOps practices, cloud computing, and microservices architecture. A basic understanding of software development and familiarity with command-line interfaces are recommended prerequisites.

Introduction

Introduction:

  • Kubernetes is a popular platform for hosting production-grade applications.

  • Demand for engineers with Kubernetes knowledge is increasing.

  • This course is designed for absolute beginners and starts with the basics.

Course Content:

  • Illustrations and analogies to simplify complex concepts.

  • Demos to walk through basic concepts and show how to do it yourself.

Containers Overview

Kubernetes Overview

  • Kubernetes (K8s) is an open-source container orchestration technology developed by Google.

  • It is used to manage and automate the deployment, scaling, and operation of containerized applications.

Understanding Containers and Orchestration

  • Containers are isolated environments that can run their own processes, networking interfaces, and mounts.

  • Orchestration refers to the management and coordination of multiple containers.

Docker Overview

  • Docker is a popular container technology that provides a high-level tool for managing containers.

  • It allows users to easily create, run, and manage containers without the need for low-level container setup.

Key Concepts

  • Operating System Kernel: The core software responsible for interacting with the underlying hardware.

  • Containers Share the Kernel: Docker containers share the underlying kernel of the Docker host, allowing them to run different operating systems as long as they are based on the same kernel (e.g., Linux).

  • Virtual Machines vs. Containers:

    • Virtual machines have their own operating system, while containers share the kernel of the Docker host.

    • Containers are lightweight and boot up faster compared to virtual machines.

    • Containers have less isolation but utilize resources more efficiently.

Using Docker

  • Many containerized versions of applications are readily available in public registries like Docker Hub.

  • To use Docker, install it on your host and run the "docker run" command with the name of the image to bring up an application stack.

Images vs. Containers

  • An image is a package or template used to create one or more containers.

  • Containers are running instances of images that have their own environments and processes.

Dockerizing Applications

  • Developers can create Docker images for their applications and push them to public repositories like Docker Hub.

  • This simplifies the deployment process for operations teams, as they can use the images to deploy applications without the need for complex setup instructions.

Additional Resources

  • Docker for the Absolute Beginners course

  • Docker Swarm course

Container Orchestration

Container Orchestration Overview

  • Container orchestration is the process of automating the deployment, management, and scaling of containers.

  • Kubernetes is a popular container orchestration technology.

Advantages of Container Orchestration

  • High availability: Multiple instances of the application running on different nodes ensure that hardware failures do not bring down the application.

  • Load balancing: User traffic is load balanced across containers to handle increased demand.

  • Seamless scaling: More instances of the application can be deployed quickly and easily to meet increased demand.

  • Resource optimization: The number of underlying nodes can be scaled up or down based on resource availability.

  • Declarative configuration: Configuration files allow for easy management of container deployments.

Kubernetes

  • Kubernetes is a leading container orchestration technology.

  • It is supported by major public cloud providers like GCP, Azure, and AWS.

  • Kubernetes is known for its flexibility and customization options.

Kubernetes Architecture

Kubernetes Concepts

  • Node: A physical or virtual machine where Kubernetes is installed and containers are launched.

  • Cluster: A group of nodes that work together to provide high availability and load balancing.

  • Master: A node that manages the cluster and orchestrates containers on worker nodes.

  • Components of Kubernetes:

    • API server: Frontend for Kubernetes, used by users and management devices to interact with the cluster.

    • ETCD: Distributed key-value store used to store cluster data.

    • Scheduler: Distributes work or containers across multiple nodes.

    • Controllers: Monitor and respond to changes in the cluster, such as node failures or container crashes.

    • Container runtime: Software used to run containers (e.g., Docker).

    • QLE: Agent that runs on each node to ensure containers are running as expected.

Master and Worker Nodes

  • Master nodes:

    • Run the Kubernetes API server.

    • Store cluster information in ETCD.

    • Have the control manager and scheduler.

  • Worker nodes:

    • Host Docker containers.

    • Run the QLE agent to interact with the master.

Kubectl Command-Line Tool

  • Used to deploy and manage applications on a Kubernetes cluster.

  • Common commands:

    • kubectl run

    • : Deploy an application on the cluster.

    • kubectl cluster-info

    • : View information about the cluster.

    • kubectl get nodes

    • : List all nodes in the cluster.

Pods

Kubernetes Pods

  • Pods are the smallest objects in Kubernetes that encapsulate containers.

  • Pods have a 1:1 relationship with containers (except for helper containers).

  • Scaling up involves creating new pods, while scaling down involves deleting existing pods.

  • Pods can have multiple containers, but usually not multiple containers of the same kind.

  • Helper containers can be part of the same pod as the application container for direct communication and shared resources.

  • Pods provide automatic management of network connectivity, storage sharing, and container lifecycles.

  • The

  • kubectl run

  • command deploys a Docker container by creating a pod.

  • The

  • kubectl get pods

  • command lists the pods in the cluster.

  • Pods are initially in a "ContainerCreating" state and transition to "Running" when the container is running.

  • External user access to the pod's services will be covered in a later lecture on networking and services.

Introduction to YAML

YAML Files

  • YAML (YAML Ain't Markup Language) is used to represent data, especially configuration data.

  • Comparison with XML and JSON formats.

  • Key-value pairs:

  • key: value

  • Arrays:

    • Use dashes (-) to indicate array elements.

    • Example:

fruits:

- Apple

- Banana

  • Dictionaries:

    • Use indentation to group properties.

    • Example:

banana:

calories: 105

fat: 0.4

carbs: 22.8

  • Lists containing dictionaries containing lists.

  • When to use a dictionary or a list:

    • Dictionary: Stores properties of a single object.

    • List: Stores multiple items of the same type of object.

  • Key notes:

    • Dictionaries are unordered collections (order of properties doesn't matter).

    • Lists are ordered collections (order of items matters).

    • Lines beginning with a hash (#) are comments.

Pods with YAML

Creating a Pod using YAML

  • Kubernetes uses YAML files as inputs for creating objects (pods, replicas, deployments, services, etc.).

  • Top-level fields in a Kubernetes definition file:

    • API version: Version of the API used to create the object.

    • Kind: Type of object being created (e.g., pod, replica set, deployment, service).

    • Metadata: Data about the object (name, labels, etc.).

    • Spec: Additional information pertaining to the object.

  • Creating a pod with a single container:

    • Create a YAML file with the following structure:

apiVersion: v1

kind: Pod

metadata:

name: my-app-pod

labels:

app: my-app

spec:

containers:

- name: nginx

image: nginx

  • Run the command

  • kubectl create -f pod-definition.yaml

  • to create the pod.

  • Use the command

  • kubectl get pods

  • to see a list of pods.

  • Use the command

  • kubectl describe pod my-app-pod

  • to see detailed information about the pod.

Replication Controllers and ReplicaSets

Kubernetes Controllers

  • Controllers are processes that monitor Kubernetes objects and respond accordingly.

  • Replication controller:

    • Ensures that a specified number of pods are running at all times.

    • Helps balance load across multiple pods on different nodes.

  • Replica set:

    • Newer technology that replaces replication controller.

    • Similar to replication controller but has some differences.

  • Creating a replication controller:

    • Create a replication controller definition file (.yaml).

    • Include the following sections:

      • API version (v1)

      • Kind (ReplicationController)

      • Metadata (name, labels)

      • Spec (template, replicas)

    • Use the

    • kubectl create -f

    • command to create the replication controller.

  • Creating a replica set:

    • Create a replica set definition file (.yaml).

    • Include the following sections:

      • API version (apps/v1)

      • Kind (ReplicaSet)

      • Metadata (name, labels)

      • Spec (template, replicas, selector)

    • Use the

    • kubectl create -f

    • command to create the replica set.

  • Labels and selectors:

    • Pods and objects can be labeled to identify them.

    • Replica sets use selectors to identify which pods to monitor.

    • Match labels selector matches labels specified under it to labels on the pod.

  • Scaling a replica set:

    • Update the number of replicas in the definition file and use

    • kubectl replace -f

    • to update the replica set.

    • Use the

    • kubectl scale --replicas

    • command to scale the replica set without modifying the file.

  • Commands:

    • kubectl create -f

    • : Create a Kubernetes object from a definition file.

    • kubectl get

    • : List Kubernetes objects.

    • kubectl delete replicaset

    • : Delete a replica set.

    • kubectl replace -f

    • : Replace or update a Kubernetes object.

    • kubectl scale --replicas

    • : Scale a replica set.

Deployments

Kubernetes Deployments

  • Deployments provide capabilities for:

    • Rolling updates: Upgrading Docker instances seamlessly.

    • Rollbacks: Undoing recent changes.

    • Pausing and resuming changes: Applying changes in a controlled manner.

  • Deployment definition file:

    • Similar to replica set definition file.

    • Kind: Deployment.

  • Creating a deployment:

    • Create a deployment definition file (.yaml).

    • Use the

    • kubectl create -f

    • command to create the deployment.

  • Viewing created objects:

    • kubectl get deployments

    • : List deployments.

    • kubectl get replicaset

    • : List replica sets.

    • kubectl get pods

    • : List pods.

    • kubectl get all

    • : List all Kubernetes objects.

Deployments - Update and Rollback

Kubernetes Deployments: Updates and Rollbacks

  • Rollouts and versioning:

    • Each deployment triggers a rollout and creates a new revision.

    • Revisions help track changes and enable rollbacks.

  • Deployment strategies:

    • Recreate strategy: Destroys all old replicas and creates new ones.

    • Rolling update strategy (default): Upgrades replicas one by one, ensuring continuous availability.

  • Updating deployments:

    • Modify the deployment definition file and run

    • kubectl apply

    • Use

    • kubectl set image

    • to update the container image.

  • Viewing deployment details:

    • kubectl describe deployment

    • : See detailed information about a deployment.

  • Rolling update process:

    • Creates a new replica set and deploys new pods.

    • Scales down the old replica set while scaling up the new replica set.

  • Rolling back deployments:

    • Run

    • kubectl rollout undo

    • to roll back to a previous revision.

Commands:

  • kubectl create

  • : Create a deployment.

  • kubectl get deployments

  • : List deployments.

  • kubectl apply

  • : Update a deployment.

  • kubectl set image

  • : Update the container image in a deployment.

  • kubectl rollout status

  • : Check the status of a rollout.

  • kubectl rollout history

  • : View the history of rollouts.

  • kubectl rollout undo

  • : Roll back a deployment.

Basics of Networking in Kubernetes

Kubernetes Networking

  • Single-node cluster:

    • Each pod gets an internal IP address from the 10.244.0.0/16 network.

    • Pods can communicate with each other using their internal IP addresses.

  • Multi-node cluster:

    • Each node has its own internal network with the same address (e.g., 10.244.0.0/16).

    • Pods on different nodes have the same IP addresses, leading to conflicts.

  • Networking requirements:

    • All pods must be able to communicate with each other without NAT.

    • All nodes must be able to communicate with containers.

    • All containers must be able to communicate with nodes.

  • Prebuilt networking solutions:

    • Cisco CNI Networks

    • Weave Net (used in Play with Kubernetes labs)

    • Calico

    • Flannel

    • VMware NSX-T

  • Custom networking setup:

    • Assigns a different network address for each network in the node.

    • Creates a virtual network of all pods and nodes with unique IP addresses.

    • Enables communication between pods and nodes using routing techniques.

Services – NodePort

Kubernetes Services

  • Services enable communication between components within and outside the application.

  • Types of services:

    • NodePort: Exposes a port on the node that forwards requests to a port on the pod.

    • ClusterIP: Creates a virtual IP inside the cluster for communication between services.

    • LoadBalancer: Provisions a load balancer for the application in supported cloud providers.

NodePort Service

  • Maps a port on the node to a port on the pod.

  • Service definition file:

    • API version: v1

    • Kind: Service

    • Metadata:

      • Name:

    • Spec:

      • Type: NodePort

      • Ports:

        • TargetPort: Port: NodePort:

      • Selector: :

  • Creating a service:

    • Create a service definition file (.yaml).

    • Use the

    • kubectl create -f

    • command to create the service.

  • Accessing the service:

    • Use the node port to access the service from outside the cluster.

    • Example:

    • curl http://<node-IP>:<node-port>

  • Load balancing:

    • Services automatically load balance requests across multiple pods.

    • Random algorithm is used for load balancing.

  • Spanning across multiple nodes:

    • Services automatically span across all nodes in the cluster.

    • Same port number is used on all nodes for the service.

Services – ClusterIP

Kubernetes Service Cluster IP

  • A full-stack web application typically has different pods hosting different parts of the application.

  • Pods have dynamic IP addresses and can go down anytime, making it challenging to establish reliable communication between them.

  • A service in Kubernetes helps group pods together and provides a single interface to access them.

  • ClusterIP service:

    • Groups pods together and provides a single interface for other pods to access them.

    • Requests are forwarded to one of the pods under the service randomly.

    • Each service gets an IP and name assigned to it inside the cluster.

    • Other pods can access the service using the cluster IP or the service name.

  • Creating a ClusterIP service:

    • Use a service definition file with the following structure:

      • API version: v1

      • Kind: Service

      • Metadata:

        • Name:

      • Spec:

        • Type: ClusterIP (default)

        • Ports:

          • TargetPort: Port:

        • Selector: :

    • Create the service using the

    • kubectl create -f

    • command.

    • Check the service status using the

    • kubectl get services

    • command.

Services - Load Balancer

Kubernetes Service Load Balancer

  • NodePort service exposes applications on a port on the worker nodes.

  • End users need a single URL to access the application (e.g., example.voting.app.com).

  • Setting up an external load balancer can be tedious.

  • Kubernetes supports integrating with native load balancers of certain cloud providers.

  • To use a load balancer:

    • Set the service type to

    • LoadBalancer

    • for the frontend services.

    • This only works with supported cloud platforms (e.g., GCP, AWS, Azure).

    • In unsupported environments, it will behave like a NodePort service.

Microservices Application

Microservices Architecture and Docker

  • Sample voting application:

    • Voting app (Python): User interface for voting.

    • Worker (.NET): Processes votes and updates database.

    • Result app (Node.js): Displays vote results.

    • Redis: In-memory database for votes.

    • PostgreSQL: Persistent database for votes.

  • Setting up the application stack on a single Docker engine:

    • Run Docker commands to start containers for Redis, PostgreSQL, and the application services.

    • Use the

    • --name

    • option to name the containers.

    • Use the

    • --link

    • option to link containers together (deprecated).

      • Example:

      • --link redis:redis

      • links the voting app to the Redis container.

      • Creates an entry in the

      • /etc/hosts

      • file on the voting app container with the Redis container's IP.

  • Limitations of using links:

    • Deprecated and may be removed in the future.

    • Advanced concepts in Docker Swarm and networking provide better alternatives.

Microservices Application on Kubernetes

Deploying a Microservices Application on Kubernetes

  • Planning the deployment:

    • Deploy applications as pods (later convert to deployments for simplicity).

    • Enable connectivity between pods.

    • Enable external access for external-facing applications.

  • Connectivity requirements:

    • Redis: Accessed by voting app and worker app.

    • PostgreSQL: Accessed by worker app and result app.

    • Voting app: Accessed by external users.

    • Result app: Accessed by external users.

    • Worker app: Not accessed by any other component or external users.

  • Creating services:

    • Redis service (type: ClusterIP, name: Redis).

    • PostgreSQL service (type: ClusterIP, name: DB).

    • Voting app service (type: NodePort, high port > 30,000).

    • Result app service (type: NodePort, high port >30,000).

  • No service for worker app:

    • Worker app does not run any service that needs to be accessed by others.

    • A service is only required if the application has a process/service that needs to be exposed.

  • Docker images:

    • codecloud/example-voting-app:v1

    • codecloud/example-worker:v1

    • codecloud/example-result:v1

    • redis (official image)

    • postgres (official image)

Kubernetes on Cloud – Introduction

Deploying Kubernetes Clusters on Cloud Platforms

  • Options for deploying Kubernetes clusters on the cloud:

    • Self-hosted or turnkey solutions:

      • Provision VMs and use tools/scripts to configure Kubernetes.

      • Responsible for maintaining VMs, patching, and upgrading.

      • Examples: Using tools likekops on AWS.

    • Hosted or managed solutions:

      • Kubernetes cluster and VMs are deployed and managed by the provider.

      • No access to master nodes or VMs for configuration changes.

      • Examples: Google Kubernetes Engine (GKE), Azure Kubernetes Service, AWS Elastic Kubernetes Service (EKS).

  • This section will cover deploying the example voting application on:

    • Google Kubernetes Engine (GKE)

    • Azure Kubernetes Service

    • AWS Elastic Kubernetes Service (EKS)

  • Note:

    • This is not a demo of deploying a production-grade Kubernetes cluster.

    • Concepts learned in this course are applicable to any Kubernetes environment.

    • Focus is on provisioning and managing Kubernetes clusters, reusing deployment and service definition files.

Kubernetes on GCP (GKE)

Deploying a Microservices Application on Google Kubernetes Engine (GKE)

  • Prerequisites:

    • Google Cloud account (free trial available).

    • Basic understanding of Google Cloud and shell.

  • Steps:

    • Create a Kubernetes cluster on GKE:

      • Go to the Kubernetes Engine page in the Google Cloud console.

      • Click on "Create cluster".

      • Give the cluster a name (e.g., "example-voting-app").

      • Choose a location and zone.

      • Leave the master version as default or select a release channel.

      • Leave the worker node configuration as default.

      • Click on "Create" to start the cluster creation process.

    • Connect to the cluster:

      • Once the cluster is created, click on the "Connect" button.

      • Copy the command provided and run it in Cloud Shell.

    • Clone the GitHub repository:

      • Open the GitHub repository in a browser.

      • Clone the repository to Cloud Shell using

      • git clone <repository-URL>

    • Deploy the application:

      • Change the service type to "LoadBalancer" in the voting app and result app service definition files.

      • Create the deployments and services using

      • kubectl create -f <file-name>

    • Verify the deployment:

      • Run

      • kubectl get deployments, services

      • to check the status of deployments and services.

      • Wait for the load balancers to get an external IP.

    • Test the application:

      • Copy the external IP of the voting app load balancer and open it in a browser.

      • Cast a vote and observe the result app updating accordingly.

Kubernetes on AWS (EKS)

Deploying a Microservices Application on Amazon EKS

  • Prerequisites:

    • AWS account.

    • Basic AWS knowledge (configuring cluster role, IAM role, VPC, EC2 key pair).

    • AWS CLI installed and configured.

    • QLE utility installed.

  • Steps:

    • Create an EKS cluster:

      • Go to the Amazon EKS console.

      • Click on "Create cluster".

      • Give the cluster a name (e.g., "example-voting-app").

      • Select the region (e.g., "US West (Oregon)").

      • Choose the Kubernetes version (e.g., "1.16").

      • Select the cluster service role created earlier.

      • Click on "Next".

    • Add a node group:

      • Click on "Add node group".

      • Give the node group a name (e.g., "demo-workers").

      • Select the EKS node role created earlier.

      • Choose the subnets (default VPC).

      • Select the SSH key pair (optional).

      • Click on "Next".

    • Configure compute settings:

      • Leave the default values for instance type, disk size, etc.

      • Click on "Next".

    • Configure auto scaling:

      • Leave the default values for minimum nodes, maximum nodes, and desired size.

      • Click on "Next".

    • Review and create:

      • Review the configuration and click on "Create".

    • Wait for the cluster and node group to be created.

  • Configure kubectl:

    • Run

    • aws eks update-kubeconfig --name <cluster-name>

    • to configure kubectl for the EKS cluster.

    • Verify the connection by running

    • kubectl get nodes

  • Deploy the application:

    • Clone the GitHub repository.

    • Change the service type to "LoadBalancer" in the voting app and result app service definition files.

    • Create the deployments and services using

    • kubectl create -f <file-name>

  • Verify the deployment:

    • Run

    • kubectl get deployments, services

    • to check the status of deployments and services.

    • Wait for the load balancers to get a public IP.

  • Test the application:

    • Copy the external IP of the voting app load balancer and open it in a browser.

    • Cast a vote and observe the result app updating accordingly.

Kubernetes on Azure (AKS)

Deploying a Microservices Application on Azure Kubernetes Service (AKS)

  • Prerequisites:

    • Azure account (free access available).

    • Basic Azure knowledge (creating resource groups, subscriptions).

  • Steps:

    • Create an AKS cluster:

      • Go to the Azure portal.

      • Search for "AKS".

      • Click on "Create".

      • Select your subscription.

      • Create a new resource group (e.g., "voting-app-rg").

      • Give the cluster a name (e.g., "example-voting-app").

      • Choose the Kubernetes version (e.g., "1.16").

      • Select the node size (e.g., "Standard_B2s").

      • Set authentication to "System assigned".

      • Click on "Review + create".

      • Click on "Create".

    • Connect to the cluster:

      • Once the cluster is created, click on "Connect".

      • Launch the Azure Cloud Shell.

      • Run

      • az aks get-credentials --resource-group <resource-group-name> --name <cluster-name>

      • to configure kubectl.

    • Clone the GitHub repository:

      • Open the GitHub repository in a browser.

      • Clone the repository to Cloud Shell using

      • git clone <repository-URL>

    • Deploy the application:

      • Change the service type to "LoadBalancer" in the voting app and result app service definition files.

      • Create the deployments and services using

      • kubectl create -f <file-name>

    • Verify the deployment:

      • Run

      • kubectl get deployments, services

      • to check the status of deployments and services.

      • Wait for the load balancers to get an external IP.

    • Test the application:

      • Copy the external IP of the voting app load balancer and open it in a browser.

      • Cast a vote and observe the result app updating accordingly.

Conclusion

  • Summary of topics covered in the course:

    • Containers and Docker.

    • Container orchestration.

    • Kubernetes concepts (pods, replica sets, deployments, services).

    • Kubernetes networking basics.

    • Working with kubectl commands and Kubernetes definition files.

    • Deploying a sample microservices application on Kubernetes on Google Cloud Platform.

  • Encouragement and good luck for the learner's Kubernetes journey.

What's Next in Your Kubernetes Journey?

Once you have a solid understanding of the basics of Kubernetes, it's time to take your knowledge to the next level and start using it in more practical ways. Here are a few recommendations to help you continue your Kubernetes learning journey:

1. Pursue the Certified Kubernetes Administrator (CKA) Certification:

The CKA certification is a highly regarded credential that demonstrates your proficiency in Kubernetes administration. It's a practical exam that tests your ability to manage and troubleshoot Kubernetes clusters in real-world scenarios. To prepare for the CKA exam, I highly recommend the training provided by KodeKloud. Their CKA learning path is designed to give you the knowledge and hands-on experience you need to pass the exam and become a certified Kubernetes administrator.


2. Gain Practical Experience:

If you're already working in the cloud space, try to find opportunities to work on projects that involve Kubernetes or one of the cloud Kubernetes services. Practical experience is the best way to solidify your understanding of Kubernetes and learn how to apply it effectively in real-world environments.

3. Take a Beginner's Course:

If you still have questions or feel like you need a more comprehensive introduction to Kubernetes, I recommend taking a beginner's course. KodeKloud offers a beginner's course that covers everything you need to know to get started with Kubernetes, including hands-on labs and exercises.


4. Stay Up-to-Date:

Kubernetes is constantly evolving, so it's important to stay up-to-date with the latest changes and developments. Follow Kubernetes pages on LinkedIn and other social media platforms to stay informed about new releases, features, and best practices.

By following these recommendations, you can continue to build your Kubernetes skills and become a valuable asset to any organisation that uses Kubernetes. Good luck on your Kubernetes journey!