# alias.txt # add this script to the end of .bashrc "export alias.txt" or ". alias.txt" # just do this: ". alias.txt" y=$(grep -c '/alias.txt' ~/.bashrc) [ $y -eq 0 ] && [ "$1" != "n" ] && [ "$1" != "no" ] && echo . ~/alias.txt >> ~/.bashrc alias_ver=ver_1.2 alias ls='ls --color=auto' alias ll='ls -alh --color=auto --time-style "+%Y-%m-%d %H:%M:%S"' alias la='ls -a --color=auto' alias l.='ls -d .* --color=auto' alias hisg='history | grep' alias grep='grep --color=auto' alias df='df -hT' alias du='du -ch' alias dua='du -cha' alias ifc='set -x; ifconfig; set +x' alias ipa='set -x; ip a; set +x' alias yi='yum install -y' alias yd='mkdir -p /tmp/yum_down; cd /tmp/yum_down; pwd; yum install -y --downloadonly --downloaddir=.' alias vi='func() { [ -f /usr/bin/vim ] && vim $1 || \vi $1;}; func' alias -- -='cd -' alias ..='cd ..; pwd' alias cd..='cd ..; pwd' alias cdansible='cd /etc/ansible; pwd' alias cdnet='cd /etc/sysconfig/network-scripts; pwd' alias cdrules.d='cd /etc/udev/rules.d; pwd' alias cdul='cd /usr/local/; pwd' alias cdwww='cd /var/www; pwd' alias cdhtm='cd /var/www/html; pwd' alias cdinit.d='cd /etc/rc.d/init.d; pwd' alias cdrepos.d='cd /etc/yum.repos.d; pwd' alias cdapt='cd /etc/apt; pwd' alias cdetc='cd /etc; pwd' alias cdvar='cd /var; pwd' alias cdopt='cd /opt; pwd' alias cdmedia='cd /media; pwd' alias cdhome='cd /home; pwd' alias cddata='cd /data; pwd' alias cdtmp='cd /tmp; pwd' #alias cdz='cd /usr/local/zabbix; pwd' alias cdza='cd /usr/local/zabbix_agents_linux; pwd' alias pg='ps -ef | grep -v "grep" | grep' alias pag='ps aux | grep -v "grep" | grep' alias ng='netstat -anp | grep' alias sg='ss -anp | grep' mcd() { mkdir -p "$1"; cd "$1"; pwd;} cdl() { cd "$1"; ll;} bak() { [ -f "$1".bak ] && echo -e "$1.bak already exists,\033[31;1m No backup.\033[0m" || { mv "$1"{,.bak}; ls -l "$1"*; } } bakcp() { [ -f "$1".bak ] && echo -e "$1.bak already exists,\033[31;1m No backup.\033[0m" || { mv "$1"{,.bak}; cp "$1".bak $1; ls -l "$1"*; } } unbak() { rename .bak '' $1; } srestart() { set -x; service $1 restart; set +x; } sstart() { set -x; service $1 start; set +x; } sstop() { set -x; service $1 stop; set +x; } sstatus() { set -x; service $1 status; set +x; } alias ntp='func() { set -x; date; ntpdate ntp.ntsc.ac.cn; date; set +x; }; func' my() { [ ! $1 ] && mysql -uroot -p10jqka || mysql -uroot -p$1;} #linux系统查看主机型号、序列号: alias dmi='dmidecode -t 1' #获取操作系统和版本号 function os_version() { os=unknown [ -f /etc/os-release ] || { echo "file /etc/os-release not exist."; exit 1; } os=$(grep -E '^NAME=' /etc/os-release | awk -F '"' '{print $2}') version=$(grep -E '^VERSION=' /etc/os-release | awk -F '"' '{print $2}') echo ${os}:${version} } alias osversion='os_version' function proc_cpuinfo() { # CPU 型号 model_name=$(grep 'model name' /proc/cpuinfo | uniq | awk -F": " '{print $2}') # 物理 CPU 数量 physical_cpu_count=$(grep "physical id" /proc/cpuinfo | sort -u | wc -l) # 每个物理 CPU 的核心数 cpu_cores=$(grep "cpu cores" /proc/cpuinfo | uniq | awk -F": " '{print $2}') # 每个核心的线程数 siblings=$(grep "siblings" /proc/cpuinfo | uniq | awk -F": " '{print $2}') # 总的逻辑 CPU 数量 total_processors=$(grep -c processor /proc/cpuinfo) echo "CPU 型号: $model_name" echo "物理 CPU 的数量: $physical_cpu_count" echo "每个物理 CPU 的核心数: $cpu_cores" echo "每个物理 CPU 的线程数: $siblings" echo "总的逻辑 CPU 数量: $total_processors" } alias cpuinfo='proc_cpuinfo' list_process_dir() { for pid in $(ps -ef | grep -v "grep" | grep ${1} | awk -F " " '{print $2}') do ls -al /proc/${pid}/exe done } alias lp='list_process_dir' #################################### ## docker alias d='docker' alias di='docker images' alias dia='docker images -a' alias dp='docker ps' alias dpa='docker ps -a' alias dh='docker history' alias dhq='docker history -q' alias dnls='docker network ls' alias dni='docker network inspect' alias dstop='docker stop' alias drm='docker rm' alias dcp='docker cp' dstoprm() { docker stop $1 docker rm $1 } dstopall() { docker stop $(docker ps -q) } drmall() { #docker rm $(docker ps -a -q) docker rm $(docker ps -a | grep Exited | awk '{print $1}') } enter() { PID=$(docker inspect --format {{.State.Pid}} $1) nsenter --target $PID --mount --uts --ipc --net --pid } dsaveprint() { docker images --format "{{.Repository}} {{.Tag}}" | grep $1 | awk '{print "docker save "$1":"$2" -o "$1$2".img; gzip -9 "$1$2".img"}' } dsave() { docker images --format "{{.Repository}} {{.Tag}}" | grep $1 | while read line do image=$(echo "$line" | awk '{print $1}') tag=$(echo "$line" | awk '{print $2}') set -x docker save "$image":"$tag" -o "$image$tag".img gzip -9 "$image$tag".img set +x done } alias dconfigd='sed -n '"'"'s/.*"auth": "\(.*\)"/\1/p'"'"' ~/.docker/config.json | base64 -d' #################################### # Kubectl ## Get、Describe alias kgn='kubectl get nodes' alias kgno='kubectl get nodes -o wide' alias kdn='kubectl describe nodes' alias kgn0='echo "NODE (The Infomation from pods mate info.)"; kubectl get pods -o=custom-columns="NODE:.spec.nodeName" | grep -v -E "NODE|none" | sort | uniq' alias kgn1='kubectl get nodes -o=custom-columns="Name:.metadata.name,CapacityCPU:.status.capacity.cpu,CapacityMEM:.status.capacity.memory,CapacityDisk:.status.capacity.ephemeral-storage,AllocatableCPU:.status.allocatable.cpu,AllocatableMEM:.status.allocatable.memory,AllocatableDisk:.status.allocatable.ephemeral-storage"' alias kgn2='kubectl describe nodes | grep -E '"'"'(^(Name|Allocated resources):)|(^ (memory|cpu) )'"'"'' alias kgn3='kubectl describe node | \grep -E '"'"'(^Name:)|.*\%.*\%.*\%.*\%'"'"'' alias kgns='kubectl get namespace' alias kds='kubectl describe namespace' alias kgd='kubectl get deployments.apps' alias kgdo='kubectl get deployments.apps -o wide' alias kgdoy='kubectl get deployments.apps -o yaml' # 带上具体的deployments alias kdd='kubectl describe deployments.apps' alias kgd0='kubectl get deployments.apps -o=custom-columns='"'"'name:metadata.name,replicas:spec.replicas,cpu.request:spec.template.spec.containers[0].resources.requests.cpu,memory.request:spec.template.spec.containers[0].resources.requests.memory,cpu.limits:spec.template.spec.containers[0].resources.limits.cpu,memory.limits:spec.template.spec.containers[0].resources.limits.memory'"'" alias kgds='kubectl get daemonsets.apps' alias kgdso='kubectl get daemonsets.apps -o wide' alias kgdsoy='kubectl get daemonsets.apps -o yaml' # 带上具体的daemonsets alias kdds='kubectl describe daemonsets.apps' alias kgss='kubectl get statefulsets.apps' alias kgsso='kubectl get statefulsets.apps -o wide' alias kgssoy='kubectl get statefulsets.apps -o yaml' alias kdss='kubectl describe statefulsets.apps' alias kgcj='kubectl get cronjobs.batch' alias kgcjo='kubectl get cronjobs.batch -o wide' alias kgcjoy='kubectl get cronjobs.batch -o yaml' alias kdcj='kubectl describe cronjobs.batch' alias kgj='kubectl get jobs.batch' alias kgjo='kubectl get jobs.batch -o wide' alias kgjoy='kubectl get jobs.batch -o yaml' alias kdj='kubectl describe jobs.batch' alias kgsec='kubectl get secrets' alias kgsecoy='kubectl get secrets -o yaml' alias kdsec='kubectl describe secrets' # 解析 kubernetes.io/dockerconfigjson 类型的 secret alias kgsecd='func() { [ "$1" == "" ] && { echo -e "secret_name is empty.\nexit 1"; return 1; }; secd=$(kubectl get secrets $1 --output="jsonpath={.data.\.dockerconfigjson}" | base64 -d); echo ${secd}; echo ${secd} | sed '"'"'s/.*auth":\s*"\(.*\)".*/\1/'"'"' | base64 -d; }; func' # 解析 kubernetes.io/tls 类型的 secret alias kgsecdtls='func() { [ "$1" == "" ] && { echo -e "secret_name is empty.\nexit 1"; return 1; }; kubectl get secrets $1 --output="jsonpath={.data.tls\.crt}" | base64 -d | openssl x509 -noout -dates; }; func' alias kgsvc='kubectl get services' alias kgsvcoy='kubectl get services -o yaml' alias kgsvcm='kubectl get servicemonitors.monitoring.coreos.com' alias kdsvc='kubectl describe services' alias kgep='kubectl get endpoints' alias kgepoy='kubectl get endpoints -o yaml' alias kdep='kubectl describe endpoints' alias kgsvcm='kubectl get servicemonitors.monitoring.coreos.com' alias kdsvcm='kubectl describe servicemonitors.monitoring.coreos.com' alias kgcm='kubectl get configmaps' alias kgcmoy='kubectl get configmaps -o yaml' alias kdcm='kubectl describe configmaps' alias kgi='kubectl get ingress' alias kgioy='kubectl get ingress -o yaml' # 带上具体的ingress alias kdi='kubectl describe ingress' alias kgsc='kubectl get storageclass' alias kgscoy='kubectl get storageclass -o yaml' alias kgpv='kubectl get pv' alias kgpvo='kubectl get pv -o wide' alias kgpvoy='kubectl get pv -o yaml' alias kgpvc='kubectl get pvc' alias kgpvco='kubectl get pvc -o wide' alias kgpvcoy='kubectl get pvc -o yaml' alias kgrs='kubectl get replicasets.apps' alias kgrso='kubectl get replicasets.apps -o wide' alias kgrsoy='kubectl get replicasets.apps -o yaml' alias ktp='kubectl top pod' alias ktn='kubectl top node' alias kgp='kubectl get pods' alias kgpo='kubectl get pods -o wide' alias kgpoy='kubectl get pods -o yaml' alias kgpoj='kubectl get pods -o json' # 从 pod_short_name 获得 pod_name alias kgpn='func() { kubectl get pods -o=custom-columns='"'"'name:metadata.name'"'"' | \grep $1 | head -n ${2-1}; }; func' #查看具体pod 详情 alias kdp='func() { [ "$1" == "" ] && { echo -e "pod_name is empty.\nexit 1"; return 1; }; pod_name=$(kgpn $1); set -x; kubectl describe pod ${pod_name}; set +x;}; func' alias kggrepo='kubectl get gitrepositories.source.toolkit.fluxcd.io' alias kghrepo='kubectl get helmrepositories.source.toolkit.fluxcd.io' alias kghc='kubectl get helmcharts.source.toolkit.fluxcd.io' alias kgks='kubectl get kustomizations.kustomize.toolkit.fluxcd.io' alias kghr='kubectl get helmreleases.helm.toolkit.fluxcd.io' alias kghroy='kubectl get helmreleases.helm.toolkit.fluxcd.io -o yaml' alias kdhc='kubectl describe helmcharts.source.toolkit.fluxcd.io' alias kdks='kubectl describe kustomizations.kustomize.toolkit.fluxcd.io' alias kdhr='kubectl describe helmreleases.helm.toolkit.fluxcd.io' alias kgevt='kubectl get events.events.k8s.io' # 发布 alias kaf='func() { set -x; kubectl apply -f $1; set +x;}; func' alias kbscd='func() { [ "$1" == "" ] && { echo -e "deployment_name is empty.\nexit 1"; return 1; }; set -x; kubectl scale deployment $1 --replicas=${2-1}; set +x;}; func' alias kbscss='func() { [ "$1" == "" ] && { echo -e "statefulset_name is empty.\nexit 1"; return 1; }; set -x; kubectl scale statefulset $1 --replicas=${2-1}; set +x;}; func' # 查看 Deployment 所有版本的镜像 alias krhdi='func() { [ "$1" == "" ] && { echo -e "deployment_name is empty.\nexit 1"; return 1; }; kubectl rollout history deployment $1 | awk '"'"'NR>2 {print $1}'"'"' | xargs -i -t -r kubectl rollout history deployment $1 --revision={} |grep Image;}; func' #编辑 alias ked='func() { [ "$1" == "" ] && { echo -e "deployment_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit deployment $1; set +x;}; func' alias keds='func() { [ "$1" == "" ] && { echo -e "daemonset_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit daemonset $1; set +x;}; func' alias kess='func() { [ "$1" == "" ] && { echo -e "statefulsets_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit statefulsets $1; set +x;}; func' alias kecj='func() { [ "$1" == "" ] && { echo -e "cronjobs_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit cronjobs $1; set +x;}; func' alias kej='func() { [ "$1" == "" ] && { echo -e "jobs_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit jobs $1; set +x;}; func' alias kesec='func() { [ "$1" == "" ] && { echo -e "secrets_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit secrets $1; set +x;}; func' alias kesvc='func() { [ "$1" == "" ] && { echo -e "services_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit services $1; set +x;}; func' alias kecm='func() { [ "$1" == "" ] && { echo -e "configmaps_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit configmaps $1; set +x;}; func' alias kei='func() { [ "$1" == "" ] && { echo -e "ingress_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit ingress $1; set +x;}; func' alias kehr='func() { [ "$1" == "" ] && { echo -e "hr_name is empty.\nexit 1"; return 1; }; set -x; kubectl edit hr $1; set +x;}; func' #删除 alias kdeld='func() { [ "$1" == "" ] && { echo -e "deployment_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete deployment $1; set +x;}; func' alias kdelds='func() { [ "$1" == "" ] && { echo -e "daemonset_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete daemonset $1; set +x;}; func' alias kdelss='func() { [ "$1" == "" ] && { echo -e "statefulsets_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete statefulsets $1; set +x;}; func' alias kdelcj='func() { [ "$1" == "" ] && { echo -e "cronjobs_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete cronjobs $1; set +x;}; func' alias kdelj='func() { [ "$1" == "" ] && { echo -e "jobs_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete jobs $1; set +x;}; func' alias kdeli='func() { [ "$1" == "" ] && { echo -e "ingress_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete ingress $1; set +x;}; func' alias kdelp='func() { [ "$1" == "" ] && { echo -e "pod_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete pod $1; set +x;}; func' alias kdelhr='func() { [ "$1" == "" ] && { echo -e "hr_name is empty.\nexit 1"; return 1; }; set -x; kubectl delete hr $1; set +x;}; func' alias kdelf='func() { set -x; kubectl delete -f $1; set +x;}; func' #删除所有Evicted状态的pod alias kdelpocom='kubectl get pods | grep Completed | awk '"'"'{print $1}'"'"' | xargs -t -r kubectl delete pod' alias kdelpopen='kubectl get pods | grep Pending | awk '"'"'{print $1}'"'"' | xargs -t -r kubectl delete pod' alias kdelpoima='kubectl get pods | grep ImagePullBackOff | awk '"'"'{print $1}'"'"' | xargs -t -r kubectl delete pod' alias kdelpoout='kubectl get pods | grep -E "OutOfmemory|OutOfcpu" | awk '"'"'{print $1}'"'"' | xargs -t -r kubectl delete pod' alias kdelpoerr='kubectl get pods | grep -E "Error|Evicted|CrashLoopBackOff|ContainerStatusUnknown" | awk '"'"'{print $1}'"'"' | xargs -t -r kubectl delete pod' #滚动更新 alias krrd='func() { [ "$1" == "" ] && { echo -e "deployment_name is empty.\nexit 1"; return 1; }; set -x; kubectl rollout restart deployment $1; set +x;}; func' alias krrds='func() { [ "$1" == "" ] && { echo -e "daemonset_name is empty.\nexit 1"; return 1; }; set -x; kubectl rollout restart daemonset $1; set +x;}; func' alias krrss='func() { [ "$1" == "" ] && { echo -e "statefulsets_name is empty.\nexit 1"; return 1; }; set -x; kubectl rollout restart statefulsets $1; set +x;}; func' # 快速切换namespace alias ksetns='func() { [ "$1" == "" ] && { echo -e "namespace is empty.\nexit 1"; return 1; }; set -x; kubectl config set-context --current --namespace=$1; set +x;}; func' alias kgetns='kubectl config view --minify --output "jsonpath={..namespace}"' alias kdefault='set -x; kubectl config set-context --current --namespace=default; set +x' alias kkube-system='set -x; kubectl config set-context --current --namespace=kube-system; set +x' alias kingress-nginx='set -x; kubectl config set-context --current --namespace=ingress-nginx; set +x' alias ktest='set -x; kubectl config set-context --current --namespace=test; set +x' alias kstage='set -x; kubectl config set-context --current --namespace=stage; set +x' alias kprod='set -x; kubectl config set-context --current --namespace=prod; set +x' alias kmoni='set -x; kubectl config set-context --current --namespace=monitoring; set +x' alias ksre='set -x; kubectl config set-context --current --namespace=sre; set +x' alias kcdp-application='set -x; kubectl config set-context --current --namespace=cdp-application; set +x' alias kcdp-datahub='set -x; kubectl config set-context --current --namespace=cdp-datahub; set +x' alias kcdp-foundation='set -x; kubectl config set-context --current --namespace=cdp-foundation; set +x' alias kcdp-platform='set -x; kubectl config set-context --current --namespace=cdp-platform; set +x' alias kcdp-system='set -x; kubectl config set-context --current --namespace=cdp-system; set +x' alias kcdp-xaas='set -x; kubectl config set-context --current --namespace=cdp-xaas; set +x' alias kflux-system='set -x; kubectl config set-context --current --namespace=flux-system; set +x' alias klocal-path-storage='set -x; kubectl config set-context --current --namespace=local-path-storage; set +x' alias kcdp-version='kubectl get hr application-service -n cdp-platform -o yaml | grep "platform.releaseVersion"' ### Exec alias kex='func() { pod_name=$(kgpn $1); set -x; kubectl exec -it ${pod_name} -- /bin/bash; set +x;}; func' alias kexsh='func() { pod_name=$(kgpn $1); set -x; kubectl exec -it ${pod_name} -- /bin/sh; set +x;}; func' # 依次进入 filebeat 容器,不能使用xargs的写法,for写法 alias kfex='func() { [ "$1" == "" ] && { echo -e "pod_name is empty.\nexit 1"; return 1; }; for i in $(kubectl get pods | grep $1 | awk '"'"'{print $1}'"'"'); do set -x; kubectl exec -i -t $i -- bash; set +x; wait; done;}; func' # 批量在pod中执行sh -c命令,可以用通配符,xargs写法 alias kxexc='kubectl get pods | grep filebeat | awk '"'"'{print $1}'"'"' | xargs -i -t kubectl exec -i {} -- sh -c' alias kxexcd='kubectl get pods | grep delete-logs | awk '"'"'{print $1}'"'"' | xargs -i -t kubectl exec -i {} -- sh -c' # for写法 alias kfexc='func() { [ "$1" == "" ] && { echo -e "pod_name is empty.\nexit 1"; return 1; }; for i in $(kubectl get pods | grep $1 | awk '"'"'{print $1}'"'"'); do set -x; kubectl exec -i $i -- sh -c "$2"; set +x; wait; done;}; func' # 批量在k8s节点执行sh -c命令 alias kxexns='kubectl get nodes -o=custom-columns="NAME:.metadata.name" | awk '"'"'NR>1'"'"' | xargs -i -t kubectl node-shell {} -- sh -c' # 删除 /opt/log/stash/ 目录下的tmp/gz/zip文件 alias kxexcrm='kubectl get pods | grep filebeat | awk '"'"'{print $1}'"'"' | xargs -i -t kubectl exec -i {} -- sh -c "rm -rf /opt/log/stash/*/*.tmp && rm -rf /opt/log/stash/*/*.gz && rm -rf /opt/log/stash/*/*.zip"' alias kxexcrmd='kubectl get pods | grep delete-logs | awk '"'"'{print $1}'"'"' | xargs -i -t kubectl exec -i {} -- sh -c "rm -rf /opt/log/stash/*/*.tmp && rm -rf /opt/log/stash/*/*.gz && rm -rf /opt/log/stash/*/*.zip"' #查看日志 #alias kl='func() { pod_name=$(kgpn $1); set -x; kubectl logs --tail=100 ${pod_name}; set +x;}; func' alias klc='func() { pod_name=$(kgpn $1); set -x; kubectl logs --tail=100 ${pod_name} -c $2; set +x;}; func' # 从 pod_short_name 执行 kubectl logs -f --tail 1 #alias klf='func() { pod_name=$(kgpn $1); kubectl logs --tail=10 -f ${pod_name};}; func' alias klfc='func() { pod_name=$(kgpn $1); kubectl logs --tail=10 -f ${pod_name} -c $2;}; func' alias kl='func() { pod_name=$(kgpn $1); ns=$(kubectl config view --minify --output "jsonpath={..namespace}"); set -x; grep "journald" /etc/docker/daemon.json && /usr/local/bin/kubectl logs -n ${ns} ${pod_name} || kubectl logs --tail=100 ${pod_name}; set +x;}; func' alias klf='func() { pod_name=$(kgpn $1); ns=$(kubectl config view --minify --output "jsonpath={..namespace}"); set -x; grep "journald" /etc/docker/daemon.json && /usr/local/bin/kubectl logs -n ${ns} -f ${pod_name} || kubectl logs --tail=100 -f ${pod_name}; set +x;}; func' kubectl completion bash >/dev/null 2>&1 && source <(kubectl completion bash) alias k='kubectl' complete -o default -F __start_kubectl k helm completion bash >/dev/null 2>&1 && source <(helm completion bash) crictl completion bash >/dev/null 2>&1 && source <(crictl completion bash) alias kns='kubectl node-shell'