Configuration of K8s_Cluster over AWS by using Ansible

Configuration of K8s_Cluster over AWS by using Ansible

No alt text provided for this image

What is a Kubernetes cluster?

A Kubernetes cluster is a set of node machines for running containerized applications. If you’re running Kubernetes, you’re running a cluster.

At a minimum, a cluster contains a control plane and one or more compute machines, or nodes. The control plane is responsible for maintaining the desired state of the cluster, such as which applications are running and which container images they use. Nodes actually run the applications and workloads.

The cluster is the heart of Kubernetes’ key advantage: the ability to schedule and run containers across a group of machines, be they physical or virtual, on-premises or in the cloud. Kubernetes containers aren’t tied to individual machines. Rather, they’re abstracted across the cluster.

Kubernetes clusters are comprised of one master node and a number of worker nodes. These nodes can either be physical computers or virtual machines, depending on the cluster.

The master node controls the state of the cluster; for example, which applications are running and their corresponding container images. The master node is the origin for all task assignments. It coordinates processes such as:

  • Scheduling and scaling applications
  • Maintaining a cluster’s state
  • Implementing updates

The worker nodes are the components that run these applications. Worker nodes perform tasks assigned by the master node. They can either be virtual machines or physical computers, all operating as part of one system. There must be a minimum of one master node and one worker node for a Kubernetes cluster to be operational. For production and staging, the cluster is distributed across multiple worker nodes. For testing, the components can all run on the same physical or virtual node.

namespace is a way for a Kubernetes user to organize many different clusters within just one physical cluster. Namespaces enable users to divide cluster resources within the physical cluster among different teams via resource quotas. For this reason, they are ideal in situations involving complex projects or multiple teams. 

What makes up a Kubernetes cluster?

No alt text provided for this image

A Kubernetes cluster contains six main components:

  1. API server: Exposes a REST interface to all Kubernetes resources. Serves as the front end of the Kubernetes control plane.
  2. Scheduler: Places containers according to resource requirements and metrics. Makes note of Pods with no assigned node, and selects nodes for them to run on.
  3. Controller manager: Runs controller processes and reconciles the cluster’s actual state with its desired specifications. Manages controllers such as node controllers, endpoints controllers, and replication controllers.
  4. Kubelet: Ensures that containers are running in a Pod by interacting with the Docker engine, the default program for creating and managing containers. Takes a set of provided PodSpecs and ensures that their corresponding containers are fully operational.
  5. Kube-proxy: Manages network connectivity and maintains network rules across nodes. Implements the Kubernetes Service concept across every node in a given cluster.
  6. Etcd: Stores all cluster data. Consistent and highly available Kubernetes backing store. 

These six components can each run on Linux or as Docker containers. The master node runs the API server, scheduler, and controller manager, and the worker nodes run the kubelet and kube-proxy.

Step1: Ansible Configuration file “/etc/ansible/ansible.cfg”

No alt text provided for this image

Step2: Ansible Role to launch ec2 instances

No alt text provided for this image

After creating ansible roles we need to write the YAML code inside the respective files. we have vars folder to keep variables and tasks folder to write tasks.

No alt text provided for this image
No alt text provided for this image

Kubernetes Multi-Node Cluster

Step3: Ansible Roles to configure k8s Master Node

No alt text provided for this image

The Master Node task file will performances the following :

  • Installation and Enabling Docker Services
  • Configuration of Kubernetes Repository
  • Installation of 3 basic packages: kubeletkubeadm, and kubectl
  • Change driver of docker from cgroupfs to systemd
  • Set the "net.bridge.bridge-nf-call-iptables" to ‘1’ in your sysctl config file which ensures that packets are properly processed by IP tables during filtering and port forwarding.
  • SELinux needs to be set to permissive mode, which allows access to the host filesystem.
  • Disable SWAP to enable the kubelet to work properly
  • Set Up flannel pod network
  • Initialize a cluster by executing the following command:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Step4: Ansible Roles to configure k8s Slave Node

No alt text provided for this image

The Slave Node task file will performances the following :

  • Installation and Enabling Docker Services
  • Configuration of Kubernetes Repository
  • Installation of 3 basic packages: kubeletkubeadm, and kubectl
  • Set the "net.bridge.bridge-nf-call-iptables" to ‘1’ in your sysctl config file which ensures that packets are properly processed by IP tables during filtering and port forwarding.
  • Change driver of docker from cgroupfs to systemd
  • Pull required Images using kubeadm
  • SELinux needs to be set to permissive mode, which allows access to the host filesystem.
  • Disable SWAP to enable the kubelet to work properly
  • Set Up flannel pod network
  • Joining Slave Node to Master Node

Step5: Execute the playbook to Configure Master and Slave Node

No alt text provided for this image
ansible-playbook k8s.yml
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Step6: Check Status of Cluster

No alt text provided for this image

Thank you so much for Reading !!!!



👍🏻👍🏻👏🏻👏🏻

To view or add a comment, sign in

More articles by Divyansh Saxena

Insights from the community

Others also viewed

Explore topics