Skip to content

Commit

Permalink
Update Kubectl Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed ElBakry committed Aug 13, 2019
1 parent d0c8fce commit 3762fe9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
64 changes: 32 additions & 32 deletions cluster-health.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ namespace="$1"

cluster_objects() {
echo -e "\e[44mCollecting Information from the Cluster:\e[21m"
deployments=$(zkubectl get deployment --all-namespaces | grep -v NAMESPACE | wc -l)
pods=$(zkubectl get po --all-namespaces | grep -v NAMESPACE | wc -l)
services=$(zkubectl get svc --all-namespaces | grep -v NAMESPACE | wc -l)
ingresses=$(zkubectl get ing --all-namespaces | grep -v NAMESPACE | wc -l)
statefulset=$(zkubectl get statefulset --all-namespaces | grep -v NAMESPACE | wc -l)
postgresql=$(zkubectl get postgresql --all-namespaces | grep -v NAMESPACE | wc -l)
daemonset=$(zkubectl get daemonset --all-namespaces | grep -v NAMESPACE | wc -l)
replicaset=$(zkubectl get rs --all-namespaces | grep -v NAMESPACE | wc -l)
serviceaccount=$(zkubectl get sa --all-namespaces | grep -v NAMESPACE | wc -l)
storageclass=$(zkubectl get sc --all-namespaces | grep -v NAMESPACE | wc -l)
PodDistrubtion=$(zkubectl get pdb --all-namespaces | grep -v NAMESPACE | wc -l)
CustomResources=$(zkubectl get crd --all-namespaces | grep -v NAMESPACE | wc -l)
cronjobs=$(zkubectl get cronjobs --all-namespaces | grep -v NAMESPACE | wc -l)
persistancevolumes=$(zkubectl get pv --all-namespaces | grep -v NAMESPACE | wc -l)
volumeclaims=$(zkubectl get pvc --all-namespaces | grep -v NAMESPACE | wc -l)
hpa=$(zkubectl get hpa --all-namespaces | grep -v NAMESPACE | wc -l)
deployments=$(kubectl get deployment --all-namespaces | grep -v NAMESPACE | wc -l)
pods=$(kubectl get po --all-namespaces | grep -v NAMESPACE | wc -l)
services=$(kubectl get svc --all-namespaces | grep -v NAMESPACE | wc -l)
ingresses=$(kubectl get ing --all-namespaces | grep -v NAMESPACE | wc -l)
statefulset=$(kubectl get statefulset --all-namespaces | grep -v NAMESPACE | wc -l)
postgresql=$(kubectl get postgresql --all-namespaces | grep -v NAMESPACE | wc -l)
daemonset=$(kubectl get daemonset --all-namespaces | grep -v NAMESPACE | wc -l)
replicaset=$(kubectl get rs --all-namespaces | grep -v NAMESPACE | wc -l)
serviceaccount=$(kubectl get sa --all-namespaces | grep -v NAMESPACE | wc -l)
storageclass=$(kubectl get sc --all-namespaces | grep -v NAMESPACE | wc -l)
PodDistrubtion=$(kubectl get pdb --all-namespaces | grep -v NAMESPACE | wc -l)
CustomResources=$(kubectl get crd --all-namespaces | grep -v NAMESPACE | wc -l)
cronjobs=$(kubectl get cronjobs --all-namespaces | grep -v NAMESPACE | wc -l)
persistancevolumes=$(kubectl get pv --all-namespaces | grep -v NAMESPACE | wc -l)
volumeclaims=$(kubectl get pvc --all-namespaces | grep -v NAMESPACE | wc -l)
hpa=$(kubectl get hpa --all-namespaces | grep -v NAMESPACE | wc -l)
echo -e "\e[1m\e[39mCluster Resources:\e[21m"
echo -e "${BLUE}"Deployments" :${GREEN}$deployments"
echo -e "${BLUE}"Services" :${GREEN}$services"
Expand All @@ -53,52 +53,52 @@ cluster_objects() {
}

cluster_nodes() {
nodes=$(zkubectl get nodes | grep -v NAME | wc -l)
worker=$(zkubectl get nodes | grep -v NAME | grep worker | wc -l)
master=$(zkubectl get nodes | grep -v NAME | grep master | wc -l)
node_status=$(for i in $(zkubectl get node | grep -v NAME | awk {'print $2'} | sort -u); do echo "$i";done)
nodes=$(kubectl get nodes | grep -v NAME | wc -l)
worker=$(kubectl get nodes | grep -v NAME | grep worker | wc -l)
master=$(kubectl get nodes | grep -v NAME | grep master | wc -l)
node_status=$(for i in $(kubectl get node | grep -v NAME | awk {'print $2'} | sort -u); do echo "$i";done)
echo -e "\e[1m\e[39mCluster Node Status:\e[21m"
echo -e "${BLUE}"ALL Nodes" :${GREEN}$nodes"
echo -e "${BLUE}"Worker Nodes" :${GREEN}$worker"
echo -e "${BLUE}"Master Nodes" :${GREEN}$master"
echo -e "${BLUE}"Nodes Status" :${GREEN}$node_status"
echo -e "\e[1m\e[39mNodes Conditions:\e[21m"
echo -e "${BLUE}$(zkubectl describe node | grep kubelet | awk {'print $15'} | sort -u)"
echo -e "${BLUE}$(kubectl describe node | grep kubelet | awk {'print $15'} | sort -u)"
echo -e "\e[1m\e[39mPods Per Node:\e[21m"
for node in $(zkubectl get node | grep -v NAME | awk {'print $1'})
do pod_per_node=$(zkubectl get pods --all-namespaces --field-selector spec.nodeName=$node -o wide | wc -l)
for node in $(kubectl get node | grep -v NAME | awk {'print $1'})
do pod_per_node=$(kubectl get pods --all-namespaces --field-selector spec.nodeName=$node -o wide | wc -l)
echo -e "${BLUE}"$node" \t :${GREEN}$pod_per_node"
done
# Nodes Per AZ
a=$(zkubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1a | grep -v NAME | grep -v master | wc -l)
b=$(zkubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1b | grep -v NAME | grep -v master | wc -l)
c=$(zkubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1c | grep -v NAME | grep -v master | wc -l)
a=$(kubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1a | grep -v NAME | grep -v master | wc -l)
b=$(kubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1b | grep -v NAME | grep -v master | wc -l)
c=$(kubectl get node -l failure-domain.beta.kubernetes.io/zone=eu-central-1c | grep -v NAME | grep -v master | wc -l)
echo -e "\e[1m\e[39mWorker Nodes per AZ:\e[21m"
echo -e "${BLUE}"eu-central-1a" \t :${GREEN}$a"
echo -e "${BLUE}"eu-central-1b" \t :${GREEN}$b"
echo -e "${BLUE}"eu-central-1c" \t :${GREEN}$c"
#Node Types
types=$(zkubectl describe node | grep beta.kubernetes.io/instance-type | cut -d"=" -f2 | sort | uniq -c | awk -F$'\t' {'print $2 $1'})
types=$(kubectl describe node | grep beta.kubernetes.io/instance-type | cut -d"=" -f2 | sort | uniq -c | awk -F$'\t' {'print $2 $1'})
echo -e "\e[1m\e[39mCluster Node Types:\e[21m"
echo -e "\e[34m$types"
}

pod_with_issues() {
echo -e "\e[1m\e[39mPods not in Running or Completed State:\e[21m"
zkubectl get pods --all-namespaces --field-selector=status.phase!=Running | grep -v Completed
kubectl get pods --all-namespaces --field-selector=status.phase!=Running | grep -v Completed
}

top_mem_pods() {
echo -e "\e[1m\e[39mTop Pods According to Memory Limits:\e[21m"
for node in $(zkubectl get node | awk {'print $1'} | grep -v NAME)
do zkubectl describe node $node | sed -n "/Non-terminated Pods/,/Allocated resources/p"| grep -P -v "terminated|Allocated|Namespace"
for node in $(kubectl get node | awk {'print $1'} | grep -v NAME)
do kubectl describe node $node | sed -n "/Non-terminated Pods/,/Allocated resources/p"| grep -P -v "terminated|Allocated|Namespace"
done | grep '[0-9]G' | awk -v OFS=' \t' '{if ($9 >= '2Gi') print "\033[0;36m"$2," ", "\033[0;31m"$9}' | sort -k2 -r | column -t

}
top_cpu_pods() {
echo -e "\e[1m\e[39mTop Pods According to CPU Limits:\e[21m"
for node in $(zkubectl get node | awk {'print $1'} | grep -v NAME)
do zkubectl describe node $node | sed -n "/Non-terminated Pods/,/Allocated resources/p" | grep -P -v "terminated|Allocated|Namespace"
for node in $(kubectl get node | awk {'print $1'} | grep -v NAME)
do kubectl describe node $node | sed -n "/Non-terminated Pods/,/Allocated resources/p" | grep -P -v "terminated|Allocated|Namespace"
done | awk -v OFS=' \t' '{if ($5 ~/^[2-9]+$/) print "\033[0;36m"$2, "\033[0;31m"$5}' | sort -k2 -r | column -t
}

Expand Down
6 changes: 3 additions & 3 deletions deploy-without-application.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

echo -e "\e[1m\e[39mThese Deployments do not have Application in the Labels\e[21m"
namespaces=$(zkubectl get ns | awk {'print $1'} | grep -v NAME)
namespaces=$(kubectl get ns | awk {'print $1'} | grep -v NAME)
for namespace in $namespaces
do
echo -e "\e[1m\e[39mNamespace: $namespace\e[21m"
deployments=$(zkubectl get deploy -n $namespace | awk {'print $1'} | grep -v NAME)
deployments=$(kubectl get deploy -n $namespace | awk {'print $1'} | grep -v NAME)
for deploy in $deployments
do

var=$(zkubectl get deployment -n ${namespace} --output=json ${deploy} | \
var=$(kubectl get deployment -n ${namespace} --output=json ${deploy} | \
jq -j '.metadata.labels | to_entries | .[] | "\(.key)=\(.value),"')
labels=${var%?}

Expand Down
10 changes: 5 additions & 5 deletions deploy-without-limits.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/bin/bash

echo -e "\e[1m\e[39mThese Deployments that do not have Limits configured\e[21m"
namespaces=$(zkubectl get ns | awk {'print $1'} | grep -v NAME)
namespaces=$(kubectl get ns | awk {'print $1'} | grep -v NAME)
for namespace in $namespaces
do
echo -e "\e[1m\e[39mNamespace: $namespace\e[21m"
deployments=$(zkubectl get deploy -n $namespace | awk {'print $1'} | grep -v NAME)
deployments=$(kubectl get deploy -n $namespace | awk {'print $1'} | grep -v NAME)
for deploy in $deployments
do

var=$(zkubectl get deployment -n ${namespace} --output=json ${deploy} | \
var=$(kubectl get deployment -n ${namespace} --output=json ${deploy} | \
jq -j '.spec.selector.matchLabels | to_entries | .[] | "\(.key)=\(.value),"')
selector=${var%?}

if [ $((zkubectl get po -n ${namespace} -l ${selector} | grep -v NAME | wc -l) 2> /dev/null ) -eq 0 ]
if [ $((kubectl get po -n ${namespace} -l ${selector} | grep -v NAME | wc -l) 2> /dev/null ) -eq 0 ]
then
continue
fi
cpulimit=$(zkubectl describe node | grep $(zkubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
cpulimit=$(kubectl describe node | grep $(kubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
awk {'print $1'} | head -n1) | awk {'print $5'} | grep -Ev "^$" | sort -u | \
awk '{ if ($0 ~ /[0-9]*m/) print $0; else print $0*1000;}' | sed 's/[^0-9]*//g')
if [ $cpulimit -eq 0 ]
Expand Down
32 changes: 16 additions & 16 deletions deployment-health.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ if [ $# -ne 2 ]; then
exit 1
fi

var=$(zkubectl get deployment -n ${namespace} --output=json ${deploy} | \
var=$(kubectl get deployment -n ${namespace} --output=json ${deploy} | \
jq -j '.spec.selector.matchLabels | to_entries | .[] | "\(.key)=\(.value),"')
selector=${var%?}

pod_status() {
no_of_pods=$(zkubectl get po -n $namespace -l $selector | grep -v NAME | wc -l)
no_of_pods=$(kubectl get po -n $namespace -l $selector | grep -v NAME | wc -l)
if [[ $no_of_pods -eq 0 ]]
then
echo "Deployment $deploy has 0 replicas"
exit 0
fi
pods_status=$(for i in $(zkubectl get po -n $namespace -l $selector | grep -v NAME | awk {'print $3'} | sort -u); do echo "$i";done)
restart_count=$(zkubectl get po -n $namespace -l $selector | grep -v NAME | awk {'print $4'} | grep -v RESTARTS | sort -ur | awk 'FNR <= 1')
pods_status=$(for i in $(kubectl get po -n $namespace -l $selector | grep -v NAME | awk {'print $3'} | sort -u); do echo "$i";done)
restart_count=$(kubectl get po -n $namespace -l $selector | grep -v NAME | awk {'print $4'} | grep -v RESTARTS | sort -ur | awk 'FNR <= 1')
echo -e "${BLUE}"Number of Pods" :${GREEN}$no_of_pods"
echo -e "${BLUE}"Pods Status" :${GREEN}$pods_status"
echo -e "${BLUE}"MAX Pod Restart Count" :${GREEN}$restart_count"
readiness() {
r=$(zkubectl get po -n $namespace | grep $deploy | grep -vE '1/1|2/2|3/3|4/4|5/5|6/6|7/7' &> /dev/null )
r=$(kubectl get po -n $namespace | grep $deploy | grep -vE '1/1|2/2|3/3|4/4|5/5|6/6|7/7' &> /dev/null )
if [[ $? -ne 0 ]]
then
echo -e "${BLUE}"Readiness" :${GREEN}"ALL Pods are Ready""
Expand All @@ -51,13 +51,13 @@ pod_status() {
}
pod_distribution() {
echo -e "\e[1m\e[39mPod Distribution per Node\e[21m"
for nodes in $(zkubectl get po -n $namespace -l $selector -o wide | grep $deploy | awk {'print $7'} | sort -u)
for nodes in $(kubectl get po -n $namespace -l $selector -o wide | grep $deploy | awk {'print $7'} | sort -u)
do
echo -e "${BLUE}$nodes \t \t :${GREEN}$(zkubectl describe node $nodes | grep $deploy | wc -l)"
echo -e "${BLUE}$nodes \t \t :${GREEN}$(kubectl describe node $nodes | grep $deploy | wc -l)"
done
echo -e "\e[1m\e[39mNode Distribution per Availability Zone\e[21m"
node_dist=$(for node in $(zkubectl get po -n $namespace -l $selector -o wide | grep $deploy | awk {'print $7'} | sort -u)
do zkubectl get node --show-labels $node
node_dist=$(for node in $(kubectl get po -n $namespace -l $selector -o wide | grep $deploy | awk {'print $7'} | sort -u)
do kubectl get node --show-labels $node
done | awk {'print $6'} | grep -v LABELS)
a=$(echo $node_dist | grep -o eu-central-1a | wc -l)
b=$(echo $node_dist | grep -o eu-central-1b | wc -l)
Expand All @@ -70,16 +70,16 @@ pod_distribution() {

pod_utilization() {

cpulimit=$(zkubectl describe node | grep $(zkubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
cpulimit=$(kubectl describe node | grep $(kubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
awk {'print $1'} | head -n1) | awk {'print $5'} | grep -Ev "^$" | sort -u | \
awk '{ if ($0 ~ /[0-9]*m/) print $0; else print $0*1000;}' | sed 's/[^0-9]*//g')

memlimit=$(zkubectl describe node | grep $(zkubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
memlimit=$(kubectl describe node | grep $(kubectl get po -n ${namespace} -l ${selector} | grep -v NAME | \
awk {'print $1'} | head -n1) | awk {'print $9'} | grep -Ev "^$" | sort -u | \
awk '{ if ($0 ~ /[0-9]*Gi/) print $0*1024; else if ($0 ~ /[0-9]*G/) print $0*1000; \
else if ($0 ~ /[0-9]*M/ || $0 ~ /[0-9]*Mi/) print $0 ; else print $0}' | sed 's/[^0-9]*//g')
dcores=$(zkubectl top pods -n $namespace | grep $deploy | awk {'print $2'} | sed 's/[^0-9]*//g' | awk '{n += $1}; END{print n}')
dmem=$(zkubectl top pods -n $namespace | grep $deploy | awk {'print $3'} | sed 's/[^0-9]*//g' | awk '{n += $1}; END{print n}')
dcores=$(kubectl top pods -n $namespace | grep $deploy | awk {'print $2'} | sed 's/[^0-9]*//g' | awk '{n += $1}; END{print n}')
dmem=$(kubectl top pods -n $namespace | grep $deploy | awk {'print $3'} | sed 's/[^0-9]*//g' | awk '{n += $1}; END{print n}')


if [ $cpulimit -eq 0 ]
Expand All @@ -95,7 +95,7 @@ pod_utilization() {
echo -e "${BLUE}"Memory Utilization" :${GREEN}$deploymentmem%"
fi
echo -e "\e[1m\e[39mTop Pods CPU Utilization\e[21m"
zkubectl top pods -n $namespace -l $selector | grep -v NAME| \
kubectl top pods -n $namespace -l $selector | grep -v NAME| \
awk 'FNR <= 5' | awk {'print $1,$2'}| awk '$2=($2/'$cpulimit')*100"%"' | \
awk '{printf $1 " " "%0.2f\n",$2}' | sort -k2 -r | \
awk -v OFS='\t' '{if ($2 >= 80) print "\033[0;36m"$1," ", "\033[0;31m"":"$2"%"; else print "\033[0;36m"$1," ","\033[0;32m"":"$2"%";}'
Expand All @@ -105,15 +105,15 @@ pod_utilization() {
echo -e "\e[1m\e[33mWARN: Pods do not have Memory Limits\e[21m"
else
echo -e "\e[1m\e[39mTop Pods Memory Utilization\e[21m"
zkubectl top pods -n $namespace -l $selector | grep -v NAME | \
kubectl top pods -n $namespace -l $selector | grep -v NAME | \
awk 'FNR <= 5' | awk {'print $1,$3'} | awk '$2=($2/'$memlimit')*100"%"' | \
awk '{printf $1 " " "%0.2f\n",$2}' | sort -k2 -r | \
awk -v OFS=' \t' '{if ($2 >= 80) print "\033[0;36m"$1," ", "\033[0;31m"":"$2"%"; else print "\033[0;36m"$1," ","\033[0;32m"":"$2"%";}'
fi
}

clear
zkubectl get deploy $deploy -n $namespace &> /dev/null
kubectl get deploy $deploy -n $namespace &> /dev/null
status=$?
if [ $status -ne 0 ]; then
echo -e "Deployment $deploy not exist. \nPlease make sure you provide the correct deployment name and the correct namespace"
Expand Down

0 comments on commit 3762fe9

Please sign in to comment.