Monday, September 12, 2022

linux monitoring using slack


SLACK_WEBHOOK="https://hooks.slack.com/services/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"
SLACK_CHANNEL=${SLACK_CHANNEL-"testing"}
SLACK_USERNAME=${SLACK_USERNAME-"sai"}

[ -z $SLACK_WEBHOOK ] && { echo "The SLACK_WEBHOOK is not set!"; exit 1; }

# server status data

HOSTNAME=`hostname`
HOSTIP=`hostname -I | sed -r 's/(\S+) (\S+)/\1, \2/g'`
ROUTES=`ip route list scope global`
UPTIME=`uptime`
DISK=`df -hT /`
MEMORY=`free -h`

# sending the message

statusMessage() {
echo -e ":black_small_square: *${HOSTNAME}* - ${HOSTIP}\n"
echo -e "_Uptime:_\n\`\`\`${UPTIME}\`\`\`"
echo -e "_Memory:_\n\`\`\`${MEMORY}\`\`\`"
echo -e "_Disk:_\n\`\`\`${DISK}\`\`\`"
echo -e "_Routes:_\n\`\`\`${ROUTES}\`\`\`"
}

PAYLOAD="{\"channel\": \"#${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"`statusMessage`\"}"
curl -X POST --data-urlencode "payload=$PAYLOAD" $SLACK_WEBHOOK

Thursday, September 8, 2022

Docker automation using Shell script


#!/bin/sh echo "upgrade total system!..." sudo apt upgrade -y echo "system update!..." sudo apt update -y echo "Installing Docker Engine on Ubuntu!..." echo "Uninstalling old versions!.." sudo apt-get remove docker docker-engine docker.io containerd runc echo "Install using the repository!..." echo "Set up the repository!..." echo "Your System is updating!...." sudo apt-get update -y echo "Install packages to allow apt to use a repository over HTTPS" sudo apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release echo "Add Docker's official GPG key!..." curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "To set up the stable repository" echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null echo "Installling Docker Engine" echo "Your System is updating!...." sudo apt-get update -y echo "Installing the latest version of Docker Engine and containerd!..." sudo apt-get install -y docker-ce docker-ce-cli containerd.io echo "To install a specific version of Docker Engine!..." echo "List the versions available in your repo!..." sudo apt-cache madison docker-ce echo "Install a specific version!..." sudo apt-get install -y docker-ce=5:20.10.6~3-0~ubuntu-focal docker-ce-cli=5:20.10.6~3-0~ubuntu-focal containerd.io echo "Check Docker Engine is installed correctly by running the hello-world image!.." sudo docker run docker/whalesay cowsay Docker! echo "Docker is Successfull Installed!..."

Monday, September 5, 2022

terraform basic commands


terraform init
terraform plan
terraform apply
terraform destroy
terraform refresh
terraform graph
apt install graphviz
terraform graph | dot -Tsvg > graph.svg

helm commands


helm -h --- help command
helm completion bash --- to display the output of
helm list ---- to display the version
helm create (name) ---- to create the helm chart
helm depandency build (folder name) ---- to build the
helm dep list (name) ---- to list the dependency
helm dep update (dir name) --- it will update
helm dep update (dir name) ---- it will update
helm env ----- to display the helm environment
helm get (all,hooks,values,notes,manifest) (name) ---- to display the information
helm install (name) (dir helm name) (opction --set (name=value))
helm history (name) ---- to display the history
helm del (kube-name) (helm name) ---- delete the chart
helm lint (chart name) ---- check the error to
helm ls ---- to list the helm pakages
helm pakage (name) ----- to convert into compressed file
helm diff upgrade (kub name) ( helm name) --
helm pull (chart name) ---- to pull the helm
helm repo add
helm repo update
helm repo remove
helm rollback (name) 1 ---- to create another one
helm search hub (name) --- to search the chart
helm search repo (name) --- to search the chart
helm status name
helm install --debug --dry-run (relese-name) (chart folder) ---- to
helm get manifest (relese-name) ---- to display the set
helm install --dry-run --debug --set costCode=SS45877
sai ./mychart/ --- to set from input using set

--set (name=value)
sprig library to use function on helm

{{ .Values.data}} ---- to pass the value from the

{{- if eq .Values.data "us-e" }}
style: true
{{- end }} ---- to use (-) in starting


{{- with .Values.tags }}
{{- toYaml . | nindent 2 }} --- to
machine: {{ .machine }}
{{- end }}
------ doesn't use the multiple line

notes.txt ---- is used to write a commands in
helm template sai ./mychart --- to display the template
helm get values name --- to get a used
helm history name --- to display error and log
--atomic ---- when chat goes error it forward to
--timeout 7m3s ---- to control the time out
--fource --- when upgrade happend it will stop all
--cleanup-on-failure --- when upgrade goes fail total fail log

chart.yaml {
apiVersion:
name:
description:
icon:
keywords:
home:
sources:
maintainers:
}
helm package name ----- to convert into zip file
helm lint name ---- to check the error log

functions{
- to avoid a blank spaces
include --- to use pipeline
qoutes
template
upper
default
nindent 4 --- new line with indent of 4
}

define variable
{{ $myValue := "sai" }} --- assign a value
{{ $myValue = "gok" }} --- reassign a value
{{-if myValue }}

for loop
{{- range .Values.mychart.contry }}
{{ . | upper | qoutes }}
{{- end }}

for loop disnory
{{ - range $key, $value := .Values.image }}
- {{ $key }}: {{ $value }}
{{- end }}

helm get manifest name ---- to see the detail
helm depandency update name ---- to update depandencny on

kubernets commands


kubectl run ngix --image=(img name) --- run the pods

kubectl get pods ---- to check the pod's details

kubectl describe pod (image name) --- details the pod

kubectl get nodes --- to display the nodes

kubectl get pods -o wide ---- to display the

kubectl create deployment (name) ---- create deployment

kubectl edit deployment (name) --- edit deployment

kubectl delete deployment (name) --- delete deployment

status of different k8s components
kubectl get nodes | pod | services | replicaset

kubectl delete pod (podname) --- to delete a single

kubectl logs (pod name) --- log to console

kubectl exec -it (pod name) --- bin/bash -- get

yml:
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app:
spec:
container:
- name: nginx-con
image: nginx

kubectl create -f pod-definition.yml ---- to create k8s yml

kubectl apply -f pod.yml ---- same as create command

kubectl run nginx --image=nginx --- to create an image

kubectl describe pod (pod name) ---- to check the

kubectl delete pod (pod name) --- to delete the

kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml

kubectl edit pod (name) --- to edit the configuration

kubectl get replicationcontroller ------ to see the replication on

kubectl replace -f replica.yml ------ to replace the file

kubectl scale --replicas=6 -f replica.yml ----- another way to

kubectl get rs ----- to display the replicasets

kubectl describe pods ----- to display the error on

kubectl create -f /root/replicaset-definition-1.yaml ---- to locate in root

kubectl scale rs new-replica-set --replicas=5 ---- to replica 5

kubectl get all --- to see all commands thats

kubectl get deployments ---- to get the deployments

kubectl set image deployment

kubectl rollout status deployment ----- to check the status

kubectl rollout history deployment ----- to check the history

kubectl rollout undo deployment ----- to undo the rollout

kubectl get svc ---- to list the service where

kubectl describe svc kubernetes ----- to display the port

master node
-api server ---- for all communication
-scheduler ----- schedules pods on nodes
-controller manager ---- runs controllers
-etcd --- open source , distributed key-value database from

worker node
-kubelet
-kube-proxy
-container runtime

use kind commends
kind create cluster --name (sai)
kind delete cluster --name (sai)

minikube service (name) --url ---- to print the ip

kubectl get pod name -o yaml | more -----

kubectl get pod name -o wide ----- to display

kubectl exec -it name -- /bin/sh ----- to run

kubectl delete pod name ---- to delete the pod

container communicate with another container using localhost:port using network

pod communicate with another pod using eathernet device using

node communicate with another node using route table with

nodeePort range --- 30000 to 32767

clusterip ---- used to communicate with application a and

application a call from node port application b send

ingress is used to connect with nodeports on mapping

networking{
cluster-ip ----- is a virtual ip
node port

docker commands


con = container
Docker run con name ---- run the container
docker create con name --- create a con
docker start -a con name ---- to start a
docker system prune ---- to show the stop, build
docker ps --- current running container
docker ps -a --- all docker container
docker stop con name --- stop the container
docker kill con name --- kill the container
docker rm con name --- remove the container
docker images -- display all images
docker rmi con name ---remove the docker images
docker pull image name --- pull the docker image
docker exec con name --- execute the docker container
docker run con name --name (defult) --- to mentain
docker run -d con name --- run the con
docker attach name --- attach the docker id uniq
docker run -d con name sleep (sec) --- to
docker version --- to identify the version of the
docker run -p 8080:3456 con name -- port mapping
docker inspect con name --- display the details in
docker logs con name --- display the logs of
docker run img name: version --- to run the
docker build. --- build the docker images
docker build. -t image name --- build and mention
docker history con name ---- to see the history
cat /root/{app-name}/Dockerfile --- to see the image of the
docker run redis --- to run the server
docker exec -it con id redis-cli ---- to run
docker exec -it con name sh, bash, Powershell, zsh
docker commit -c 'CMD ["comment"]' con id ---- to
docker run -t -d -v /home/Krrish/Desktop/sampledir/:/var/www/html --name good day
WORKDIR /usr/app --- work dir app
docker-compose up -d ---- to start a multiple containers
docker-compose down ---- to stop a multiple containers
docker-compose up --build ---- to rebuild the container
restart type = no, always, on-failure, unless-stoped
docker build -f Dockerfile.dev . --- to build any
docker run -p 3000:3000 -v $(pwd):/app con name ----
docker run -p 3000:3000 -v /app/node_modeules -v $(pwd):/app con


network{
default bridge ---- default 172.17.0.1
custom bridge ---- 172.18.0.1 isolated
host ----- connect with host ip without define ports
macvlan ---- it also asign like a vm and
ipvlan --- connect with host and the macvlan
}

Shell Scripting


cat /etc/shells ---- to display total shells in our
sleep (sec) ----- to sleep and run
$? --- when code is succesfull it return 0
$1 ---- to get a input in same line
echo ${#variable} ---- to display the string count
set -x ---- to print with an command
set -n ---- to debug the code while running
set -e ---- to stop the while code in

grep{
grep -v "#" ---- to grep the details except
grep -i string ---- to remove the case sencitive
grep -w ---- match exact word
grep -v --- to print execpt that line
grep -o --- to print exact word only
grep -n --- to match the line number
grep -c ---- to match the line count
grep -A "string" 1 --- the string is match
grep -B "string" 2 --- the string is match
grep -C 1 "string" --- before and after print
grep -r "string" * --- to search n a
grep -l "string" --- to print only file names
grep -h "sting" --- to hide the file name
grep -f file filename --- to search from a
grep -E "string|srting|string" --- check with a multiple string
grep "^string" --- to print the starting line with
grep "string$" ---- to printthe match word with the
grep "^$" filename --- to match with an empty
grep "\^" --- print the exact word on it
grep "t...e" --- to match with first and last
grep "\bline\b" --- to print the word and end
grep "yf?" --- the f in the string is
grep "yf*" ---- have f or mor time
grep "[yft]" ---- split with an multiple char
grep "yf+" --- the f in the string is
grep "[a-f]" --- to search with a char to
grep "[a-dp-r]" --- to print with a secquance word
grep "^[xy]" --- to print the line it starting
grep "x{4}" --- check with 4 times
grep "gf{3,4}\b" --- 3 , 4 time only
grep "[[:digit:]] filename --- to print the number where
grep "[[:upper:]] filename --- to print the upper case
grep "[[:lower:]] filename --- to print the lower case
}

jq{
cat filename | jq '.' --- display in a
cat filename | jq -c '.[]' --- display output
}

cut{
cut -f 2 filename ---- to seprate with the
cut -d ':' -f 2,4 filename ---- to filter
cut -d ':' -1,4 filename --output-delimiter=" " ---- to
cut -sf 1 filename ---- to execute with wich

}
awk{
awk ' /version/ { print $4 } ---- to
awk ' NR==1 { print $4 } ---- to
awk -F '[ /]' '/version { print $4 }
awk 'BEGIN { OFS="_" } { print $1,$3 }
awk ' { print NR,$4 } ---- to print
awk ' { print NR,$4,NF } ---- to print
awk ' { print $NF } ---- to print
awk '{ print substr( $1, 3, 1 ) }'
}

tr{
tr '[:upper:]' '[:lower:]' < filename ---- convert upper to
tr 'i' 'I' < filename ---- change the letter
tr -d ',' ---- to delete a command
tr -d '[()]' ---- to delete a multiple command
}

sed {
sed ' ' filename ---- to work like a
sed -n '3p' filename ------- to print exact line
sed -n '3,10p' filename ---- to print in a
sed -n '3,+7p' filename ---- to print a count
sed -n '3~7p' filename ---- to print 3,7,10,13 count
sed '4d' filename ----- to delete 4 line doesn't
sed -i '4,6d' filename ---- to remove the value

}
if [ $num1 -gt $num2 -o $num3 -lt $num4
if [[ $num1 -gt $num2 || $num3 -lt $num4
if (( $num1 > $num2 )) --- use simples

$(whoami) ----- to use the defalt command method
echo $SHELL --- to display the current shell is
ps $$ --- to display the current shell is
ctrl + u ---- to erase the complete line
ctrl + w --- to remove the particular word
type -a (command name) --- to identify the command
#!/bin/bash --- to mention the execute path
chmod +x filename ---- to change the permission of
./filename ---- to execute the file from the terminal
#!/usr/bin/env bash --- to create a env on our
# ---- single line command
<<(mention any word ending with the same name) -----
example
< dkgjkndklgj
dogged
SAMPLE ---- to end it
variables two type
-system
upper case
env
- user-defined
lowercase
var="sai"
echo $var --- to print the output
don't use space to declare the variable
case sensitive
${var}var ---- use { } to decular the variable
echo "hello world $(whoami)" --- to execute the shell
echo 'hello world $HOME' --- doesn't execute the script
\ --- to work like a single qoutes
ls \ && pwd --- to use a multi
unset SAMPLE --- to un set delete variable
read -p "ENTER USER NAME:" (variable) --- to input
(-p) -- prompt
(-t) --- time out input
(-s) --- password input doesn't input
echo $variable ---- to print the user name
echo $(( 1+1 )) --- to use operation +,-
declare -i int=100 ---- to decule a integer data
readony variable ----- used to doesn't change the value
${newvariable?errormessage} ---- to identiye a decular variable
alias --- to set our own command
PS1="TERMINAL" ---- to change our own terminal command
PS1="\u@defined name" ---- to custom our terminal
echo $? --- to check the previes command state
test 10 -gt 5 && echo "10 is greater"
[ 10 -gt 15 ] ---- same like a
((10<15)) --- same like that

if and elif condition
if [ 10 -gt 5 ]
then
echo "10 is greater"
elif [ 10 -gt 20 ]
then
echo "equal"
else
echo "not"
fi

number comparisons
gt --- gather than <
lt---- less than >
ge --- greater or equal to <=
le --- less than or equal to >=
eq ---- equal to =
ne ---- not equal to !=
-a --- and &
-o --- or |

array
x=(1 3 5 "sai")
echo "${x[*]}" ---- print all number

string comparisons
=
!=
-z ---- not null value check

file comparisons
-b
-c
-d ---- to check a directory
-f ---- to check a regular file

cli position
echo $0 --- correct script variable
echo $IFS ---- tab char
cat -stv <<< $IFS --- to display non printable

case statements switch case

case $var in
"sai") echo "in side " ;;
"nmap") "scan tool" ;;
*) echo "no value" ll
esac

loops

for
in
(())
for x in 1 2 3 4 5 6
do
echo $x
done

for (( i=1; i < 10 ; i++ ))
do
for(( j=1 ; j < 10 ; j++ ))
do
echo ${i}${j} || printf "\n"
done
done
-----------
while
while [ expression ]
do
....
done

while :
do
.....
done ----- infinity loop syntex
-------------
--function--

function sai(){
echo good boy;
}
sai;
sai;
------------


my=0
while :
do
read -p "value : " my
if [[ $my -eq 0 ]]
then
echo "running"
else

-----
while get input from another file
while IFS="," read f1 f2 f3 f4
do
echo "$f2"
done < filename
----
input two value and print it
read x;
read y;
echo $((x+y));
echo $((x-y));
echo $((x*y));
echo $((x/y));

Argocd with rollouts examples

examples in My github https://github.com/krishnansai/argocd-rollouts.git ArogoCd with ArgoRollouts Rollouts.yml # This example demonst...