Showing posts with label pod. Show all posts
Showing posts with label pod. Show all posts

Sunday, March 13, 2022

Kubernetes Cluster Part 2: Pods Command with Example


Pods

Pods are the smallest/basic deployable units of computing that you can create and manage in Kubernetes.

What is a Pod?

While Kubernetes supports more container runtimes other than just Docker, Docker is the most commonly known runtime, and it helps to describe Pods using some terminology from Docker.

The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation - the same things that isolate a Docker container. Within a Pod's context, the individual applications may have further sub-isolations applied.

In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes

Using Pods

Pods that run a single container. The "one-container-per-Pod" model is the most common Kubernetes use case; in this case, you can think of a Pod as a wrapper around a single container; Kubernetes manages Pods rather than managing the containers directly.

A Pod can run multiple containers that need to work together. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit of service—for example, one container serving data stored in a shared volume to the public, while a separate sidecar container refreshes or updates those files.

Pod networking

Each Pod is assigned a unique IP address for each address family. Every container in a Pod shares the network namespace, including the IP address and network ports. Inside a Pod (and only then), the containers that belong to the Pod can communicate with one another using localhost. When containers in a Pod communicate with entities outside the Pod, they must coordinate how they use the shared network resources (such as ports). Within a Pod, containers share an IP address and port space, and can find each other via localhost.

Pod Lifecycle

Pods follow a defined lifecycle:
  1. starting in the Pending Phase
  2. moving through Running Phase if at least one of its primary containers starts OK, 
  3. and then through either the Succeeded or Failed Phases depending on whether any container in the Pod terminated in failure.

Pod properties

To switch to the Pod

    kubectl exec -it pod1 -- bash

To access the application running in the pod ( consider 192.168.135.2 is IP of the pod)

     curl <Pod's IP address>

To find all the properties of the pod in yaml format (even if we define the properties, kubernetes, internally will auto create a looot of properties for the pod, check it out by typing the below command)

    kubectl get pods pod1 -o yaml

To find all the properties of the pod in json format

    kubectl get pods pod1 -o json


EXAMPLE:
To create a pod with docker image nginx

Type in this command:

    kubectl run nginx-pod1 --image=nginx

OR 

follow the first step:

1.# vi nginx-pod1.yaml

apiVersion: v1
kind: Pod
metadata:
        name: nginx-pod1
        labels:
                app: nginx
                tier: dev
spec:
        containers:
            - name: nginx-container
              image: nginx
                nodeName: kworker1

 
kubectl create -f nginx-pod1.yaml (Create new without update)
kubectl apply -f nginx-pod1.yaml (update / create new)


2.# Display Pods and it's properties

    kubectl get pod

    kubectl get pod -o wide

    kubectl get pod nginx-pod1 -o yaml

    kubectl get pod nginx-pod1 -o json

    kubectl describe pod nginx-pod1





3.# To get inside the pod

    kubectl exec -it nginx-pod1 -- bash



4.# Create test HTML page

cat <<EOF > /usr/share/nginx/html/test.html

<!DOCTYPE html>

<html>

<head>

<title>Testing..</title>

</head>

<body>

<h1 style="color:rgb(90,70,250);">Hello, DevopsWorld...!</h1>

<h2>Congratulations, you passed :-) </h2>

</body>

</html>

EOF


exit



5.# Expose PODS using NodePort service

    kubectl expose pod nginx-pod1 --type=NodePort --port=80


note: under the port tab: there is 80:30437
80 is when you use the cluster-IP to access the webpage, example (10.105.4.192:80)
30437 is when you use the localhost to access the webpage, example (localhost:30437)

6.# Display Service and find NodePort

    kubectl describe svc nginx-pod1

    kubectl get svc


7.# Open Web-browser and access webapge using

    http://nodeip:nodeport/test.html


8.# Delete pod & svc

    kubectl delete svc nginx-pod

    kubectl delete pod nginx-pod


Monday, March 7, 2022

ASSIGNMENT: Kubernetes Pod



Try This
Consideration:- You have 3 nodes ( 1 master and 2 worker nodes) Cluster.

  1. Create a Pod called nginx-pod1 with image nginx (it runs on Port 80)
    kubectl run nginx-pod1 --image=nginx 
    OR
    #nginx-pod1.yaml
    apiVersion: v1
    kind: Pod
    metadata:
        name: nginx-pod1
    spec:
        containers:
            - name: c1
             
    image: nginx

  2. Create another Pod called tomcat-pod1 with image tomcat (it runs on Port 8080)
    kubectl run tomcat-pod1 --image=tomcat
    OR
    #tomcat-pod1.yaml
    apiVersion: v1
    kind: Pod
    metadata:
        name: tomcat-pod1
    spec:
        containers:
            - name: c1
              image: tomcat

  3. Access these pods on worker nodes (use curl command ). Are you able to Access ? If not then checkout the reason.
    kubectl get pods -o wide
    curl <podIP>
    yes able to.
    for nginx is ok but for tomcat need to put port number (8080)

  4. Go inside tomcat pod ( use kubectl exec ....command) and try to access nginx service Pod by using nginx-pod1 IP address and pod name. Are you able to access the nginx website ? if no then figure out the reason.
    kubectl exec -it tomcat-pod1 -- bash
    curl <nginx-pod1 IP:port>
    can cause in same localhost (calico network layer)


  5. Go inside nginx pod (use kubectl exec ....command) and try to access tomcat service with IP address and pod name ot tomcat-pod1. Are you able to access the nginx website? if no then figure out the reason.
    kubectl exec -it nginx-pod1 -- bash
    curl <tomcat-pod1 IP:port>
    can cause in same localhost (calico network layer)

  6. Change docker image of nginx-pod1 from nginx to tomcat. Is it possible to do on a running pod?
    No.
    Error from server (AlreadyExists): pods "nginx-pod1" already exists
    kubectl edit pod nginx-pod1 -o yaml (then edit the image to change to tomcat).

  7. Check the restart value of nginx-pod1 (it must change to 1)
    kubectl get pod nginx-pod1 -o wide

  8. check all the events of tomcat-pod1 and also find out in which namespace tomcat-pod1 is created.
    kubectl describe pod tomcat-pod1
    Name:         tomcat-pod1
    Namespace:    default

  9. Display the tomcat-pod1 properties in json format.
    kubectl get pod tomcat-pod1 -o json

  10. Delete all the pods.
    kubectl delete pod --all


Friday, March 4, 2022

Kubernetes Cluster Part 1: Creating a single Control-Plane cluster with kubeadm & creating a Calico Pod Network




PART 1: Creating a single Control-Plane cluster with kubeadm and creating a Calico Pod Network


kubeadm Tool:

The kubeadm tool helps you bootstrap a minimum viable Kubernetes cluster that conforms to best practices.

The kubeadm tool is good if you need:
  • A simple way for you to try out Kubernetes, possibly for the first time.
  • A way for existing users to automate setting up a cluster and test their application.
  • A building block in other ecosystem and/or installer tools with a larger scope.

Before you begin, to follow this guide, you need:
  1. One or more machines running a deb/rpm-compatible Linux OS; for example: Ubuntu or CentOS.
  2. 2 GB or more of RAM per machine--any less leaves little room for your apps.
  3. At least 2 CPUs on the machine that you use as a control-plane node.
  4. Full network connectivity among all machines in the cluster. You can use either a public or a private network.
  5. Check required ports. (if internal network, do not need to configure ports)
  6. Control-plane node(s).

Installing runtime
By default, Kubernetes uses the Container Runtime Interface (CRI) to interface with your chosen container runtime.
If you don't specify a runtime, kubeadm automatically tries to detect an installed container runtime by scanning through a list of well known Unix domain sockets.

Runtime 

 Path to Unix domain socket

Docker   

/var/run/docker.sock

Containerd 

/run/containerd/containerd.sock

CRI-O 

/var/run/crio/crio.sock

If both Docker and containerd are detected, Docker takes precedence. This is needed because Docker 18.09 ships with containerd and both are detectable even if you only installed Docker. If any other two or more runtimes are detected, kubeadm exits with an error.


Installing kubeadm, kubelet and kubectl
· kubeadm: 
the command to bootstrap the cluster.

· kubelet: 
the component that runs on all of the machines in your cluster and does things like starting pods and containers.

· kubectl: 
the command line utility to talk to your cluster.


Infrastructure
Example: 3 Virtual Machines (1 Master Node and 2 Worker Nodes). There must be network connectivity among these VMs.


Installation of kubeadm on Ubuntu (Both on Master and Worker Nodes)

sudo apt-get update && sudo apt-get install -y apt-transport-https curl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update

apt install -qq -y kubeadm=1.21.0-00 kubelet=1.21.0-00 kubectl=1.21.0-00

sudo apt-mark hold kubelet kubeadm kubectl




OPTIONAL

So in particular case docker was using the groupfs which I changed to systemd

Create the file as:

vim /etc/docker/daemon.json

     "exec-opts": ["native.cgroupdriver=systemd"] 
}

systemctl restart docker 
systemctl status docker



Installation of kubeadm on RHEL/CentOS (Both on Master and Worker Nodes)

In case if you are using CentOS/RHEL

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg \
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

# Set SELinux in permissive mode (effectively disabling it)

sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo systemctl enable --now kubelet



Create The Master Server/Control-Plane & Calico Pod Network
On master machine run the below command

1. initialize kubeadm and dont forget to change the master server IP! with a CIDR range which you can define yourself.

     kubeadm init --apiserver-advertise-address=<<Master ServerIP>> --pod-network-cidr=192.168.0.0/16
        
2. mkdir -p $HOME/.kube

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

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

5. kubeadm token create --print-join-command (Copy the token from master to agent/worker nodes (remember to swapoff -a)) 

5.2 Alternatively if you want to auto-off swapoff, you can type this:

    sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

6. Install Calico
6.1 kubectl create -f https://docs.projectcalico.org/v3.18/manifests/calico.yaml
6.2 kubectl get nodes (Wait for above command and run again it may take a minute or so to get all the nodes in ready state.)


OPTIONAL (Do not run below commands if Calico is configured properly by following the above steps)

Install the Tigera Calico operator and custom resource definitions.
kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml

Install Calico by creating the necessary custom resource. For more information on configuration options available in this manifest, see the installation reference.
kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml

Note: Before creating this manifest, read its contents and make sure its settings are correct for your environment. For example, you may need to change the default IP pool CIDR to match your pod network CIDR.

Confirm that all of the pods are running with the following command.
watch kubectl get pods -n calico-system

Wait until each pod has the STATUS of Running.

Note: The Tigera operator installs resources in the calico-system namespace. Other install methods may use the kube-system namespace instead.

Remove the taints on the master so that you can schedule pods on it.
kubectl taint nodes --all node-role.kubernetes.io/master-

It should return the following.
node/<your-hostname> untainted

Confirm that you now have a node in your cluster with the following command.
kubectl get nodes -o wide

It should return something like the following.
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME <your-hostname> Ready master 52m v1.12.2 10.128.0.28 <none> Ubuntu 18.

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...