top of page

Kubernetes-Day-1

### Project: Installing and Setting Up a Kubernetes Cluster with `kubeadm` on Amazon Linux 2


#### Scenario:

You are setting up a Kubernetes cluster on Amazon Linux 2 with one master node and one worker node using `kubeadm`. The objective is to have a fully functional Kubernetes cluster where you can deploy and manage containerized applications.


### Step 1: Prepare the Environment


#### On Both Master and Worker Nodes


1. Update the system:

- Ensures all packages are up-to-date.

sudo yum update -y

2. Update hostname & /etc/hosts file

On Master and on slave :

hostnamectl set-hostname master-node (on master)


hostnamectl set-hostname worker-node-1 (on slave)

On Master and on slave :

vim /etc/hosts

<master-node private-ip> master-node

      <worker-node private-ip> worker-node-1


Example:

      172.31.40.232 master-node

    172.31.35.133 worker-node-1


3. Disable SELinux:

- Disables Security-Enhanced Linux to avoid conflicts with Kubernetes.

On Master and on slave :

sudo setenforce 0

sudo reboot


4. Configure Kubernetes repository:

- Adds the Kubernetes package repository for installation.

On Master and on slave :

cat <<EOF >> /etc/yum.repos.d/kubernetes.repo
kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF


5. Install Docker:

- Installs Docker as the container runtime for Kubernetes.

On Master and on slave :

yum install -y docker kubelet-1.21.3 kubeadm-1.21.3 kubectl-1.21.3

systemctl start kubelet docker

systemctl enable docker kubelet


### Step 2: Initialize the Master Node


#### On the Master Node


1. Initialize the Kubernetes master node:

- Sets up the master node with the specified Pod network.

sudo kubeadm init --apiserver-advertise-address=<replace master node private IP> --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem --pod-network-cidr=192.168.0.0/16



2. Set up `kubectl` for the ec2-user ( exit as root user):

- Allows using `kubectl` commands to interact with the cluster.

   mkdir -p $HOME/.kube

   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

   sudo chown $(id -u):$(id -g) $HOME/.kube/config


3. Install a Pod network add-on (Calico):

- Deploys a Pod network for inter-Pod communication.

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml


4. Validate the master node status:

- Ensures the master node is ready.

kubectl get nodes

### Step 3: Join the Worker Node to the Cluster

#### On the Master Node


kubeadm token create --print-join-command


Copy the output and paste on worker Node to join the cluster

sample output : kubeadm join 172.31.45.223:6443 --token gb4wz5.pvwd61g6ex281a6k --discovery-token-ca-cert-hash sha256:8f1a31c5fc23612a021dbe7c75566c736d53dfb5cf1e18083b3631cfc53fae29


#### On the Worker Node


1. Join the worker node to the cluster:

- Uses the token from the master node to join the cluster.

sudo kubeadm join 172.31.45.223:6443 --token gb4wz5.pvwd61g6ex281a6k --discovery-token-ca-cert-hash sha256:8f1a31c5fc23612a021dbe7c75566c736d53dfb5cf1e18083b3631cfc53fae29


### Step 4: Validate the Cluster Setup


#### On the Master Node


1. Check the status of all nodes:

- Verifies that both the master and worker nodes are ready.

kubectl get nodes


2. Deploy a test Nginx application:

- Confirms that the cluster is functional.

kubectl create deployment nginx --image=nginx


3. Expose the Nginx deployment as a service:

- Makes the Nginx application accessible.

kubectl expose deployment nginx --port=80 --type=NodePort


4. Get the service details to access the application:

- Retrieves the NodePort for accessing Nginx.

kubectl get svc


5. Access the application via browser:

- Use the worker node's IP and NodePort to verify Nginx is running.

http://<worker-node-ip>:<node-port>


### Conclusion:

You have successfully set up a basic Kubernetes cluster with one master and one worker node on Amazon Linux 2 using `kubeadm`. You can now deploy and manage applications within your Kubernetes environment.


 

### Project: Managing an Nginx Pod in Kubernetes


#### Scenario:

You are tasked with managing a simple Nginx Pod in Kubernetes. The goal is to create a Pod using the Nginx image, retrieve information about it, view detailed information, and finally delete the Pod. Each step is validated using appropriate `kubectl` commands.


### 1. Run a Pod with the Nginx Image


- Explanation: Create a Pod running the Nginx web server.

Command:

kubectl run nginx-pod --image=nginx


- Validation:

Verify that the Pod has been created:

kubectl get pods



### 2. Get Information about All Pods


- Explanation: Retrieve information about all the Pods in the default namespace.


Command:

kubectl get pods


- Validation:

This will show basic details such as Pod name, status, restarts, and age.



### 3. Show Detailed Information of the Pod


- Explanation: Get detailed information, including events, conditions, and resource details for the specific Pod.

Command:

kubectl describe pod nginx-pod


- Validation:

This provides an in-depth report, including the state of the container, events, and network settings.



### 4. Get the Pod Information with Wide Output


- Explanation: Show more details about the Pod, including the node it is running on and the Pod’s IP.

Command:

kubectl get pods -o wide


- Validation:

The output includes additional columns such as Node, IP, and Container Image.



### 5. Delete the Pod


- Explanation: Remove the Nginx Pod from the cluster.

Command:

kubectl delete pod nginx-pod


- Validation:

Ensure the Pod has been deleted by checking the status:

kubectl get pods


### Conclusion:

You have successfully managed a Pod lifecycle in Kubernetes by running an Nginx Pod, retrieving its information, viewing detailed output, and deleting it, all with proper validations.


 

### Project: Creating a Pod Using a YAML File in Kubernetes


#### Scenario:

You need to create a Pod using a YAML configuration file in Kubernetes. The Pod will be named `sample`, and it will contain a container named `samp-cont` that uses the `nginx` image.


### 1. Create the YAML File for the Pod


- Explanation: Write a YAML file that defines the Pod `sample` with the container `samp-cont` using the `nginx` image.


YAML File Content (`sample-pod.yaml`):


apiVersion: v1

kind: Pod

metadata:

  name: sample

spec:

  containers:

  - name: samp-cont

    image: nginx


Let's break down the YAML file line by line for the Kubernetes Pod creation (`sample-pod.yaml`)

### 1. `apiVersion: v1`


- Explanation: This defines the version of the Kubernetes API that you're using for the resource you're defining. In this case, `v1` refers to the stable version of the Kubernetes API for basic objects like Pods.

- `v1` is the most common version for core Kubernetes objects like Pods, Services, and ConfigMaps.


### 2. `kind: Pod`


- Explanation: This specifies the type of Kubernetes object you're creating. In this case, it indicates that you're defining a Pod, which is the smallest deployable unit in Kubernetes. A Pod can contain one or more containers that run together.

- `Pod` means you're creating a single Pod.


### 3. `metadata:`


- Explanation: The `metadata` section contains information about the Pod, such as its name, labels, annotations, etc. This section is used to uniquely identify and organize Kubernetes objects.

- Here, you're only providing a `name` for the Pod.


### 4. `name: sample`


- Explanation: This gives a name to the Pod, which must be unique within the namespace. In this example, the Pod is named `sample`. You will refer to this name when interacting with the Pod (e.g., `kubectl get pod sample`).

- `sample` is the name of the Pod.


### 5. `spec:`


- Explanation: The `spec` (short for "specification") field is where you define the desired state of the Pod. This section tells Kubernetes what the Pod should look like and how it should behave. The spec includes container definitions, volumes, networking, etc.

- Here, you're specifying the containers that should run inside the Pod.


### 6. `containers:`


- Explanation: The `containers` section is a list of containers that will run inside the Pod. Each Pod can run one or more containers, but in this case, you're defining just one container inside the Pod.

- `containers:` indicates that the following items define container configurations.


### 7. `- name: samp-cont`


- Explanation: This specifies the name of the container within the Pod. The name must be unique within the Pod, and it's used to reference this specific container in the logs, monitoring tools, etc. In this example, the container is named `samp-cont`.

- `samp-cont` is the name of the container inside the Pod.


### 8. `image: nginx`


- Explanation: The `image` field specifies the container image to be used for this container. In this case, you're using the official Nginx image from Docker Hub (`nginx`). This image contains the Nginx web server software. When the Pod is created, Kubernetes will pull this image from a container registry (like Docker Hub) if it’s not already present on the node.

- `nginx` is the name of the image from Docker Hub that contains the Nginx web server.


### 2. Create the Pod Using the YAML File


- Explanation: Apply the YAML configuration to create the Pod in the Kubernetes cluster.


Command:


kubectl apply -f sample-pod.yaml



### 3. Validate the Pod Creation


- Explanation: Check that the Pod `sample` has been created and is running.


Command:


kubectl get pods



### 4. View Detailed Information about the Pod


- Explanation: Retrieve detailed information about the `sample` Pod, including its status, container, and events.


Command:


kubectl describe pod sample



### 5. Clean Up: Delete the Pod


- Explanation: Delete the `sample` Pod to clean up resources in your Kubernetes cluster.


Command:


kubectl delete pod sample


### Conclusion:

In this project, you have created a Pod using a YAML configuration file in Kubernetes, validated the Pod’s creation, viewed detailed information about it, and then deleted the Pod to clean up your environment.


 

bottom of page