Bash scripts Examples

```html Simple Bash Shell Script Examples

Simple Bash Shell Script Examples

Here are some simple Bash shell script examples to help you get started with your new skills. Each script includes a brief explanation of what it does.

1. Hello World

A basic script to print "Hello, World!" to the terminal.

#!/bin/bash # Print "Hello, World!" to the terminal echo "Hello, World!"

2. Greeting Script

A script that greets the user by name.

#!/bin/bash # Ask for the user's name echo "What is your name?" read name # Greet the user echo "Hello, $name!"

3. Basic Arithmetic

A script to perform basic arithmetic operations.

#!/bin/bash # Read two numbers from the user echo "Enter first number:" read num1 echo "Enter second number:" read num2 # Perform arithmetic operations sum=$((num1 + num2)) diff=$((num1 - num2)) prod=$((num1 * num2)) quot=$((num1 / num2)) # Display the results echo "Sum: $sum" echo "Difference: $diff" echo "Product: $prod" echo "Quotient: $quot"

4. File Existence Check

A script to check if a file exists and display a message.

#!/bin/bash # File to check file="example.txt" # Check if file exists if [ -e "$file" ]; then echo "File $file exists." else echo "File $file does not exist." fi

5. Loop Through Numbers

A script that loops through numbers 1 to 5 and prints each number.

#!/bin/bash # Loop through numbers 1 to 5 for i in {1..5} do echo "Number: $i" done

6. Countdown Timer

A script to create a simple countdown timer.

#!/bin/bash # Countdown from 10 to 1 for i in {10..1} do echo "$i" sleep 1 done echo "Time's up!"

7. Directory Listing

A script to list all files in a directory.

#!/bin/bash # Directory to list dir="." # List all files in the directory echo "Files in $dir:" ls "$dir"

8. Backup Script

A script to back up a directory.

#!/bin/bash # Source and destination directories src="/path/to/source" dest="/path/to/destination" # Create a backup cp -r "$src" "$dest" echo "Backup of $src completed to $dest"

9. Check Disk Space

A script to check the disk space usage.

#!/bin/bash # Check disk space usage echo "Disk space usage:" df -h

10. Simple To-Do List

A script to manage a simple to-do list.

#!/bin/bash # To-do list file todo_file="todo.txt" # Display menu echo "1. View to-do list" echo "2. Add a new item" echo "3. Exit" echo "Choose an option:" read option case $option in 1) if [ -e "$todo_file" ]; then cat "$todo_file" else echo "To-do list is empty." fi ;; 2) echo "Enter a new to-do item:" read item echo "$item" >> "$todo_file" echo "Item added." ;; 3) echo "Goodbye!" exit 0 ;; *) echo "Invalid option." ;; esac
```

create a 3+3 node k8s cluster using ubuntu

 A detailed guide on creating a 3-master, 3-worker Kubernetes cluster using Ubuntu 22.04 and Docker as the container runtime. This setup utilizes the kubeadm tool for cluster initialization and assumes a static IP address range of 192.168.0.165 - 180 for all nodes.

Preparation:

  1. Choose Your Nodes: Select six Ubuntu 22.04 machines (three for masters and three for workers). Ensure they have SSH access enabled and meet the minimum system requirements for running Kubernetes (https://kubernetes.io/docs/home/).
  2. Disable Swap: On all machines, disable swap to avoid potential issues with Kubernetes. You can check if swap is enabled with swapon -s and disable it permanently with edits to /etc/fstab. Refer to Ubuntu documentation for specific instructions (https://pimylifeup.com/ubuntu-turn-off-swap/).
  3. Update and Upgrade: Update the package lists and upgrade all packages on each node using:
Bash
sudo apt update
sudo apt upgrade -y
  1. Install Docker: Install Docker following the official Ubuntu documentation (https://docs.docker.com/engine/install/ubuntu/). Verify Docker installation with docker version.

Master Node Configuration (Perform on all three master nodes):

  1. Install kubeadm: Install the kubeadm package used for initializing the Kubernetes cluster:
Bash
sudo apt install -y kubeadm kubelet
  1. Initialize the Cluster (One Master Only):

    Important Note: Perform this step only on one of the master nodes. This node will become the cluster control plane.

    Bash
    # Replace "<master_ip>" with the IP address of the current master node (e.g., 192.168.0.165) 
    sudo kubeadm init --apiserver-advertise-address <master_ip> --pod-network-cidr 10.244.0.0/16
    

    This command will generate various outputs, including a kubeadm join configuration token. Keep this information readily available for joining worker nodes later.

  2. Configure Flannel Pod Network (Optional):

    While kubeadm init creates a basic pod network, Flannel is a popular pod networking solution for Kubernetes clusters. You can follow the official Flannel documentation for installation on all nodes (https://github.com/flannel-io/flannel).

  3. Configure kubelet for non-root users (Optional):

    By default, kubelet runs with root privileges. To improve security, consider configuring it to run as a non-root user. Refer to the official Kubernetes documentation for detailed instructions (https://kubernetes.io/docs/tasks/administer-cluster/kubelet-in-userns/).

Worker Node Configuration (Perform on all three worker nodes):

  1. Join the Cluster:

    Use the kubeadm join configuration token obtained from the master node initialization to join each worker node to the cluster:

    Bash
    # Replace "<master_ip>" with the IP address of any master node and "<token>" with the actual join token
    sudo kubeadm join <master_ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash_value>
    

    Replace <hash_value> with the actual hash value provided during the master node initialization output.

Verification:

  1. Verify Cluster Status:

    On any master node, use the kubectl get nodes command to check if all nodes (masters and workers) are registered and in a "Ready" state.

  2. Test Deployment (Optional):

    Once the cluster is up and running, you can deploy a simple pod to verify functionality. Here's an example of deploying an Nginx pod:

    Bash
    kubectl create deployment my-nginx --image=nginx
    kubectl expose deployment my-nginx --type=LoadBalancer --port=80
    

    This will create an Nginx deployment and expose it as a service on port 80. You can then access the Nginx service from any machine on your network by finding the assigned external IP address using kubectl get service.

Additional Notes:

  • This guide provides a basic setup for a Kubernetes cluster. For a production environment, further configuration and security best practices should be implemented.
  • Consider using tools like Kubectl RBAC for role-based access control within the cluster.
  • Explore Kubernetes documentation for managing deployments, pods, services, and other cluster resources (https://kubernetes.io/docs/home/).

IP Allocation:

Assigning static IPs from the 192.168.0.165 - 180 range to each node:

Static IP Configuration:

  1. Configure Network Interfaces:

Before proceeding, ensure you know the network interface names on each node (e.g., eth0, ens33). You can identify them using the ip addr command.

On each node, edit the network configuration file (/etc/netplan/00-installer-config.yaml) using a text editor like nano.

Here's an example configuration for the first master node (192.168.0.165):

YAML
network:
  version: 2
  renderer: networkd
  ethernets:
    <interface_name>:  # Replace "<interface_name>" with your actual interface name (e.g., eth0)
      addresses:
      - 192.168.0.165/24
      gateway: 192.168.0.1
      nameservers:
        addresses:
        - 8.8.8.8
        - 1.1.1.1

Replace <interface_name> with the actual network interface name on your system. Adapt the IP address, subnet mask, gateway, and nameserver details according to your network configuration.

Repeat this configuration for all remaining nodes, assigning static IPs within the 192.168.0.166 - 180 range and adjusting the interface name if necessary.

  1. Apply Network Changes:

After editing the configuration file, apply the changes using:

Bash
sudo netplan apply
  1. Verify Network Connectivity:

Verify if the static IP addresses are applied correctly by running ip addr and checking if the assigned IP addresses are listed for the network interface.

Additionally, ping other nodes using their static IPs to confirm network connectivity between them.

Important Note:

  • Ensure your network configuration (gateway, subnet mask, DNS servers) aligns with your actual network setup.
  • Assigning static IPs might require additional configuration on your network router or DHCP server to avoid IP conflicts.

With these steps completed, you should have a 3-master, 3-worker Kubernetes cluster set up with static IPs using kubeadm and Docker on Ubuntu 22.04.

Remember, this is a basic configuration. Consider exploring further security best practices and advanced cluster management techniques for production environments.

-------

Ansible playbook with the naming convention:

Inventory File (inventory.yaml):

YAML
[masters]
master01.cloud.local ansible_host=192.168.0.165
master02.cloud.local ansible_host=192.168.0.166
master03.cloud.local ansible_host=192.168.0.167

[workers]
worker01.cloud.local ansible_host=192.168.0.168
worker02.cloud.local ansible_host=192.168.0.169
worker03.cloud.local ansible_host=192.168.0.170

[k8s_all:children]
masters
workers

[k8s_all:vars]
k8s_version: "1.24.0"  # Update with desired Kubernetes version

Playbook (k8s-cluster.yml):

YAML
---
- hosts: k8s_all
  become: true
  tasks:
    - name: Update package lists
      apt: update_cache=yes

    - name: Upgrade all packages
      apt: upgrade=dist

    - name: Install Docker pre-requisites
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
        state: present

    - name: Add Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker repository
      apt_repository:
        repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
        state: present

    - name: Install Docker Engine
      apt:
        name: docker-ce
        state: present

    - name: Create Docker group
      group:
        name: docker
        state: present

    - name: Add current user to Docker group
      user:
        name: "{{ ansible_user }}"
        groups: docker
        append: yes
        state: present

    - name: Enable Docker service
      service:
        name: docker
        enabled: yes

    - name: Restart Docker service
      service:
        name: docker
        state: restarted

    - name: Install kubeadm, kubelet, and kubectl
      apt:
        name:
          - kubeadm
          - kubelet
          - kubectl
        state: present

- hosts: masters
  become: true
  tasks:
    - name: Initialize Kubernetes cluster (on one master only)
      command: kubeadm init --apiserver-advertise-address {{ ansible_host }} --pod-network-cidr 10.244.0.0/16
      when: inventory_hostname == "master01.cloud.local"  # Execute only on the first master

    - name: Configure Flannel Pod Network (Optional)
      # Include tasks for Flannel installation here

    - name: Get join token (on any master)
      shell: kubeadm token list
      register: join_token_output
      when: inventory_hostname != "master01.cloud.local"

    - name: Set fact for join token
      set_fact:
        join_token: "{{ join_token_output.stdout | regex_replace('^(.*)token ([^ ]+).*$', '\\2') }}"
      when: inventory_hostname != "master01.cloud.local"

- hosts: workers
  become: true
  tasks:
    - name: Join the Kubernetes cluster
      command: kubeadm join {{ master01.cloud.local }}:6443 --token {{ join_token }} --discovery-token-ca-cert-hash sha256:<hash_value>
      when: true
      register: join_output

    - debug:
        msg: "Node {{ inventory_hostname }} joined the cluster with status {{ join_output.stdout }}"

The changes are reflected in the inventory file and playbook:

  • Hostnames in the masters and workers groups now use the `master01.cloud.


VCF 9 Home Lab | Embedded vIDM (viDB) --- AD Integration, Users, Groups & NSX SSO

VCF 9 Home Lab | Embedded vIDM (viDB) — AD Integration, Users, Groups & NSX SSO 📅 May 2026  |  🏷️ VCF 9 Home Lab Series  ...