lsof -i:[port]
netstat -anp |grep [port]
netstat -anp | grep tcp |wc -l
ls /proc/[PID]/task | wc -l
pmap
du -sh
df -h
df -h |grep /dev/
blkid
dumpe2fs [-bh] 设备文件名
iftop <-i [interface]>
tail -f /var/log/messages
ps -aux|wc -l
docker stats $(docker ps -a --format="{{.Names}}")
grep "model name" /proc/cpuinfo | wc -l
ifconfig eth0 | grep 'inet'
ifconfig eth0 | grep 'inet' | sed 's/.inet//g' | sed 's/netmask.$//g'
echo "999999" | passwd --stdin "justmine"
/etc/init.d/network restart
netstat -tulnp
备注:这个命令很有用,可以一目了然地看出主机开放了哪些端口,以及端口是否对整个Internete开放,占用的应用程序。
netstat –nat | grep 8083
- #!/bin/bash
-
- IFS=$'\n\n'
-
- declare namespace="namespace"
- for row in $(kubectl -n ${namespace} get rs)
- do
- # echo ${row}
- declare name=$(echo "${row}"|awk '{print $1}')
- declare desired=$(echo "${row}"|awk '{print $2}')
- # echo "replica set: ${name}, desired: ${desired}"
- if [ $((${desired})) == 0 ]; then
- # echo ${name}
- kubectl -n ${namespace} delete rs ${name}
- fi
- done
备注:替换成自己的namespace即可。
以deployment为单位,准备就绪的条件为实例的当前数量、期望数量、最新数量相等,间隔10秒watch一次。可以将该脚本加入CI/D管道中。
- #!/bin/bash
-
- IFS=$'\n\n'
-
- #1: ready, 0: not ready.
- declare ready=0
-
- while [ $((${ready})) == 0 ]
- do
- sleep 10s
- echo ""
- declare AllIsReady=1
- for row in $(kubectl -n [namespace] get deployment)
- do
- echo ""
- declare name=$(echo "${row}"|awk '{print $1}')
- declare desired=$(echo "${row}"|awk '{print $2}')
- declare current=$(echo "${row}"|awk '{print $3}')
- declare uptodate=$(echo "${row}"|awk '{print $4}')
- declare available=$(echo "${row}"|awk '{print $5}')
- echo "deployment: ${name}, desired: ${desired}, current: ${current}, uptodate: ${uptodate}, available: ${available}"
- if [ $((${desired})) == $((${current})) -a $((${current})) == $((${uptodate})) -a $((${uptodate})) == $((${available})) ]; then
- echo "${name} has been ready.";
- else
- echo "${name} has been not ready.";
- AllIsReady=0
- fi
- done
-
- if [ $((${AllIsReady})) == 1 ]; then
- ready=1
- fi
- done
备注:替换成自己的namespace即可。其他的资源类型同理,大家可以举一反三。
cat /proc/sys/kernel/random/uuid
nohup mysql -S /home/mysql/port-3306/3306_mysql.sock -h[domain name] -P3306 -u[user name] -p[password] [database name] < [databaseBackupedsql file full path] >/dev/null 2>&1 &
docker system df
kubectl top node
kubectl top pod --all-namespaces
备注:此命令需要先安装metrics_server
echo "" > /etc/ld.so.preload
top
备注:排查隐藏的挖矿程序。
客户机:192.168.1.113,服务器:192.168.13.110,用户:root。
让客户机可以免密登录服务器。
1. 在客户机(192.168.1.113)上生成一对SSH密钥、公钥。
ssh-keygen -t rsa 或 ssh-keygen -b 4096 -t rsa
备注:-t表示密钥类型,-b表示密钥长度,默认1028。可以一路【回车】到底,也可以输入SSH密钥的加密密码,即pub_key。
2. 将SSH公钥上传到服务器(192.168.13.110)
ssh-copy-id root@192.168.13.110
备注:这一步需要输入服务器账户root的密码。
2.1. 异常处理
如果发生ERROR: @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!等错误,执行命令:
ssh-keygen -R 192.168.13.110
然后再次执行命令ssh-copy-id root@192.168.13.110。
注意:如果在步骤1输入了公钥Key,那么第一次SSH登录时,需要输入SSH密钥的加密密码。
scp root@192.168.1.110:/home/mysql.tar.gz /home/mysql
备注:输入服务器root用户密码开始下载。
scp -r /root/mysql.tar.gz root@192.168.1.110:/home/mysql
备注:需要先配置SSH互信,参照上面【Linux配置SSH互信】配置即可。
- #!/bin/bash
-
- IFS=$'\n\n'
-
- function check_port() {
- netstat -tlpn | grep "\b$1\b"
- }
-
- function getAvailablePorts()
- {
- echo "port range[$1 $2], the size of available ports: $3"
- declare ports=$(seq $3)
- declare index=0
- for((i=$1;i<$2;i++));
- do
- if check_port $i; then
- echo "$i port has been occupied"
- else
- ports[$index]=$i
- let index++
- if [ $(($index)) == $(($3)) ]; then
- break
- fi
- fi
- done
-
- echo ${ports[*]}
- }
-
- declare ports=$(getAvailablePorts 30000 32767 10)
- echo "the available ports of kubernetes is: ${ports[*]}"
测试用例:按长度获取kubernetes noteport端口范围内的可用端口数。
首先引入其他shell脚本文件,比如:. ./getAvailablePorts.sh。语法:. [filepath]。然后就可以像本地一样调用函数,比如:declare ports=$(getAvailablePorts 30000 32767 10)。
- #!/bin/bash
- IFS=$'\n\n'
-
- declare routes=$(curl -X GET http://127.0.0.1:81/services/light/routes)
- echo $routes
- declare JQ_EXEC=`which jq`
- declare routeid=$(echo $routes | ${JQ_EXEC} .data[0].id | sed 's/\"//g')
- echo ""
- echo $routeid
.[对象名],例如:.data。
.[对象名][下标].属性名,例如:.data[0].id。
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: nodejs-hello-world
- namespace: tools
- spec:
- selector:
- matchLabels:
- app: nodejs-hello-world
- replicas: 1
- template:
- metadata:
- labels:
- app: nodejs-hello-world
- spec:
- containers:
- - name: nodejs-hello-world
- image: taskrabbit/elasticsearch-dump
- imagePullPolicy: Always
- ports:
- - containerPort: 3000
- args:
- - "--input=http://192.168.1.123:9200"
- - "--output=http://192.168.1.123:9200"
- - "--type=data"
备注:input表示旧地址,output表示新地址;创建Deployment,将自动执行,全程无需参与,也不用安装nodejs的环境npm。
sh -x [shell文件路径]
- # 允许整个操作花费的最大秒数。
- declare maxTime=10
- # 最大连接时间
- declare maxConnectTime=2
- # 重试次数
- declare retryCount=5
-
- # 示例
- curl --request GET --connect-timeout $maxConnectTime --max-time $maxTime --retry $retryCount --url [API地址]
- yum install ntp -y;
- ntpdate cn.pool.ntp.org;
- yum install jq -y;
- cat [json file full name] | jq tostring
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime