Shashikant shah

Showing posts with label Part-04. Show all posts
Showing posts with label Part-04. Show all posts

Monday, 9 June 2025

What is a yaml file in Kubernetes?

 YAML Basics:-


•       YAML is not a Markup Language

•       YAML is used to store information about different things

•       We can use YAML  to define key, Value pairs like variables, lists and objects

•       YAML is very similar to JSON (Javascript Object Notation)

•       YAML primarily focuses on readability and user friendliness

•       YAML is designed to be clean and easy to read

•       We can define YAML files with two different extensions

        abc.yml

        abc.yaml


1.YAML Comments

2.YAML Key Value Pairs

3.YAML Dictionary or Map

4.YAML Array / Lists

5.YAML Spaces

6.YAML Document Separator


Step-01: Comments & Key Value Pairs

Space after the colon is mandatory to differentiate key and value

# Defining simple key-value pairs

Basic unit: key: value

name: motu

age: 23

city: noida

Use # to add comments

# This is a comment

name: John  # Inline comment


Step-02: Dictionary / Map

Set of properties grouped together after an item

Equal amount of blank space is required for all the items under a dictionary

person:  # Dictionary

  name: motu

  age: 23

  city: noida


Step-03: Array / Lists

Dash indicates an element of an array

Use - to denote each item in a list.

person: # Dictionary

  name: motu

  age: 23

  city: Hyderabad

  hobbies: # List 

    - cycling

    - cookines

# Or inline style:

 hobbies: [cycling, cooking]   # List with a different notation  


Step-04: Multiple Lists

Dash indicates an element of an array

person: # Dictionary

  name: motu

  age: 23

  city: Hyderabad

  hobbies: # List 

    - cycling

    - cooking

  hobbies: [cycling, cooking]   # List with a different notation 

  friends: # multiple lists

    - name: friend1

      age: 22

    - name: friend2

      age: 25           


Step-05: YAML Spaces (Indentation)

  • YAML is space-sensitive (use spaces, not tabs).
  • Indentation indicates nesting.
  • Standard: 2 spaces per level (not enforced, but common).

person:

  name: John

  address:

    city: Mumbai

    pin: 400001

Step-06: YAML Document Separator

  • Use --- to separate multiple documents in one file.
  • ... (optional) indicates the end of a document.

---

name: Alice

age: 25

---

name: Bob

age: 28

...

Step-07: Kubernetes YAML Top-level Objects

# Types of Kubernetes Objects

# Pod, ReplicaSet, Deployment, Service and many more

Pod API Objects Reference

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#pod-v1-core

apiVersion:

kind:

metadata:

spec: 

# apiVersion: version of k8s objects

# kind: k8s objects

# metadata: define name and labels for k8s objects

# spec: specification or real definition for k8s objects


Step-08: Sample Pod Template for Reference

apiVersion: v1 # String

kind: Pod  # String

metadata: # Dictionary

  name: myapp-pod

  labels: # Dictionary

    app: myapp        

spec:

  containers: # List

    - name: myapp

      image: stacksimplify/kubenginx:1.0.0

      ports:  # List

        - containerPort: 80


Dry Run (syntax + context check without applying):

# kubectl apply -f yourfile.yaml --dry-run=client


Check specific resources:

# kubectl explain deployment

# kubectl explain deployment.spec


# kubectl apply -f 01-pod-definition.yml

# kubectl get pods







Thursday, 25 January 2024

Kubernetes cluster installation.

########### Kubernetes Cluster Setup ######


############ for Master ##############
Step 1: Prepare Hostname, Firewall and SELinux
On your master node, set the hostname and if you don’t have a DNS server, then also update your /etc/hosts file.
# vim /etc/hosts
192.168.56.101        master.example.com master
192.168.56.102        worker01
192.168.56.103        worker02
 
# Install Kubernetes
# sudo modprobe overlay
# sudo modprobe br_netfilter
# cat  <<EOF | sudo tee /etc/sysctl.d/kube.conf
net/bridge/bridge-nf-call-ip6tables = 1
net/bridge/bridge-nf-call-iptables = 1
net/bridge/bridge-nf-call-arptables = 1
EOF
 

# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

# echo "1"  > /proc/sys/net/ipv4/ip_forward

disable SElinux and update your firewall rules.
#sudo setenforce 0
#sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
 
# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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
EOF
 
# sudo sysctl --system
# sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# sudo swapoff -a
# yum install -y yum-utils
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin  kubelet kubectl kubeadm kubernetes-cni
# systemctl enable kubelet
# systemctl enable docker
# service firewalld stop
# systemctl disable firewalld
# rm -f /etc/containerd/config.toml
# systemctl restart containerd
# systemctl enable containerd
# echo -e "192.168.50.10 master\n192.168.50.11 worker1\n192.168.50.12 worker2" >> /etc/hosts
# kubeadm config images pull
# kubeadm init --apiserver-advertise-address=192.168.50.10 --pod-network-cidr=10.244.0.0/16
# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# sudo chown $(id -u):$(id -g) $HOME/.kube/config
# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# systemctl status containerd
# systemctl status kubelet
 

############# for worker01 ####################

Step 1: Prepare Hostname, Firewall and SELinux
 
On your master node, set the hostname and if you don’t have a DNS server, then also update your /etc/hosts file.
# vim /etc/hosts
192.168.56.101        master.example.com master
192.168.56.102        worker01
192.168.56.103        worker02

# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

# echo "1"  > /proc/sys/net/ipv4/ip_forward
 
sudo modprobe overlay
sudo modprobe br_netfilter
cat  <<EOF | sudo tee /etc/sysctl.d/kube.conf
net/bridge/bridge-nf-call-ip6tables = 1
net/bridge/bridge-nf-call-iptables = 1
net/bridge/bridge-nf-call-arptables = 1
EOF
 
 
# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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
EOF
 # sudo sysctl --system

 # sudo setenforce 0
 # sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
 # sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
 # sudo swapoff -a
 # yum install -y yum-utils
 # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
 # yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin  kubelet kubectl kubeadm kubernetes-cni
# systemctl enable kubelet
# systemctl enable docker
# service firewalld stop
# systemctl disable firewalld
# rm -f /etc/containerd/config.toml
# systemctl restart containerd
# systemctl status kubelet
# echo -e "192.168.56.101 master\n192.168.56.102 worker01\n192.168.56.103 worker02" >> /etc/hosts

# systemctl status kubelet --> nahi chalega..
# kubeadm join 192.168.56.101:6443 --token al34ik.aocuycxej7cu9e09\

      --discovery-token-ca-cert-hash sha256:79c40e306623488a85aac952930cad89d825069d0801c0c9b9dcca9897bd5a22 --v=5
# systemctl status kubelet --> worker join ke badh chal jayega..
# systemctl status containerd
# systemctl status kubelet
on Master  node command :- 
# sudo kubectl label node <worker01> node-role.kubernetes.io/worker=worker
[root@master ~]# kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
master.example.com   Ready    control-plane   11h   v1.28.2
worker01             Ready    worker          10h   v1.28.2
worker02             Ready    worker          10h   v1.28.2
 
############# Reset  k8s cluster ############

# kubeadm reset

######### check certificate and tokan #########

How to create a token.

# kubeadm token create

# kubeadm token list



# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt \ | openssl rsa -pubin -outform der 2>/dev/null \ | openssl dgst -sha256 -hex \ | awk '{print $2}'






############# Renew Expired Certificates #############

# kubeadm certs check-expiration

# kubeadm certs renew all

#####################################