gobomb / myDoc

The documents and notes
12 stars 3 forks source link

Cheatsheet #2

Open gobomb opened 6 years ago

gobomb commented 6 years ago

https://gobomb.github.io/post/cheatsheet/

Cheatsheet Go 可以使用 go tool vet -shadow you_file.go 检查幽灵变量 go get 只下载不安装: go get -d string 和 int 转换 //string到int int,err:=strconv.Atoi(string) //string到int64 int64, err := strconv.ParseInt(string, 10, 64) //i

gobomb commented 6 years ago

awk格式化输出:

docker images|awk '{print $1":"$2}'

如上述命令过滤并输出形如k8s.gcr.io/coredns:1.1.3([image name]:[tag])的结果

gobomb commented 6 years ago

一键持久化 k8s 相关镜像 bash 脚本:

#!/bin/bash

i=0
docker images|awk '{print  $1":"$2}'|grep k8s| while read line
do
        docker save $line >/root/saved/k"$i".tar
        echo k"$i".tar saved
        ((i++))
done
gobomb commented 6 years ago

Mac 下没有tac(逆序cat),可用tail -r代替

gobomb commented 6 years ago

统计重复行:

sort [file] |uniq -c

gobomb commented 6 years ago

set -e 子命令返回值非零,脚本立即退出 set +e 子命令返回值非零,脚本不立即退出,继续执行

gobomb commented 6 years ago

kubectl delete pods <pod> --grace-period=0 --force

强制删除 pod

gobomb commented 6 years ago

git diff > patch

git apply patch

保存 diff 和打补丁

gobomb commented 6 years ago

列出文件:ls -l |grep "^-"

列出目录:ls -l |grep "^d"

gobomb commented 6 years ago

/etc/docker/daemon.json中加入{"live-restore": true}systemctl restart docker使之生效

使得 docker daemon 重启的时候,容器不会退出

原理:docker daemon 关闭后,容器进程变孤儿进程,父进程号变为1;docker daemon 恢复时候,从文件列表获取容器信息,把容器信息重新加入到 Daemon 对象的 containers 字典里

gobomb commented 6 years ago
type T struct{}
var _ I = T{}

其中 I为interface

go 用来判断 type T 是否实现了 I,如果 T 没有实现接口 I,则编译报错

gobomb commented 6 years ago
echo 'HISTSIZE=10000' >> /etc/profile
. /etc/profile

使 bash history 记录的命令行数为10000

gobomb commented 6 years ago

转换变量全部大写或小写

echo $val | awk '{print tolower($1)}'
echo $val | awk '{print toupper($1)}' 
gobomb commented 6 years ago

截取变量里的文件名和目录

var=/path/to/file
# `file` 去掉变量var从左边算起的最后一个'/'字符及其左边的内容,返回从左边算起的最后一个'/'(不含该字符)的右边的内容:
 ${var##*/}
# `/path/to` 去掉变量var从右边算起的第一个'/'字符及其右边的内容,返回从右边算起的第一个'/'(不含该字符)的左边的内容:
 ${var%/*}
gobomb commented 6 years ago

UNIX

ctrl-c产生SIGINT信号 ctrl-\产生SIGQUIT信号 ctrl-d表示EOF字符 ctrl-z产生SIGTSTP信号 fg %[job_id]产生SIGCONT信号

gobomb commented 6 years ago

Python

Python print 的输出有缓冲,使用 nohup 后台运行需要加-u才能使得日志马上输出到文件

nohup python -u test.py > nohup.out 2>&1 &

gobomb commented 6 years ago

Intellj IDEA

批量修改变量名: shift+F6

gobomb commented 6 years ago

git

将某次 commit 以来的更新打成 patch:

git format-patch [commit]

打 patch:

git am /path/to/patch

gobomb commented 6 years ago

k8s

修改 yaml:

kubectl edit deployment kubernetes-dashboard -n kube-system

gobomb commented 6 years ago

kubectl 调试级别(glog) https://kubernetes.io/docs/reference/kubectl/cheatsheet/

--v=0   Generally useful for this to ALWAYS be visible to an operator.
--v=1   A reasonable default log level if you don’t want verbosity.
--v=2   Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems.
--v=3   Extended information about changes.
--v=4   Debug level verbosity.
--v=6   Display requested resources.
--v=7   Display HTTP request headers.
--v=8   Display HTTP request contents
gobomb commented 6 years ago

Mac homebrew

brew tap

查看第三方仓库

brew tap [x/y]

安装新的第三方仓库

gobomb commented 6 years ago

Linux

jobs列出后台进程

kill %[job_id]杀死进程

或者

jobs -l列出后台进程和对应的 PID

kill [PID]

gobomb commented 6 years ago

Linux

ls 不带颜色输出

ls -la --color=never

gobomb commented 6 years ago

ssh

同时使用密钥和密码登陆(常规的密钥登陆方法在这里就不重复了)

/etc/ssh/sshd_conf中加一句

AuthenticationMethods "publickey,password" "publickey,keyboard-interactive"

参考:

https://serverfault.com/questions/93807/how-do-i-setup-sshd-to-require-both-a-private-key-and-a-password/774817#774817

gobomb commented 6 years ago

ssh

有时启动 sshd (比如在 alpine 中)需要生成密钥对

ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
gobomb commented 6 years ago

查看系统/内核日志

tailf /var/log/syslog

tailf /var/log/kern.log

gobomb commented 5 years ago

按照 CPU 从大到小查看进程 ps aux|sort -k 3 -r -n|less

按照内存从大到小查看进程 ps aux|sort -k 4 -r -n|less

gobomb commented 5 years ago

su- 不仅切换用户,还会切换到家目录和切换环境变量

gobomb commented 5 years ago

docker

通过 pid 查到 container id:

docker inspect -f '{{.State.Pid}} {{.Id}}' $(docker ps -q) | grep <Pid>

gobomb commented 5 years ago

creates the configuration for the Service, but prints it to stdout as YAML instead of sending it to the Kubernetes API server:

kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run | kubectl set selector --local -f - 'environment=qa' -o yaml

the output from the stdout is like this:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: my-svc
  name: my-svc
spec:
  clusterIP: None
  selector:
    environment: qa
  type: ClusterIP
status:
  loadBalancer: {}
gobomb commented 5 years ago

删除最早的10个pvc:

kubectl delete -n some-namespaces pvc `kc get pvc -n archive-pvc --sort-by=".metadata.creationTimestamp" |head|awk '{print $1}'|grep -v NAME`
gobomb commented 5 years ago

截断文件

truncate --size=10M filename
gobomb commented 5 years ago

\cp src dst

在命令前加\可以取消 alias,cp经常会被 alias 成cp -i以防止覆盖已有文件

gobomb commented 5 years ago

k8s 给 node 添加污点

kubectl taint nodes node1 key1=value1:NoSchedule

kubectl taint nodes node1 key1:NoSchedule-

gobomb commented 5 years ago

ssh 本地端口转发

ssh -L 2181:bp-zookeeper:2181 root@0.0.0.0 &

gobomb commented 5 years ago

man 章节

1、Standard commands (标准命令)
2、System calls (系统调用)
3、Library functions (库函数)
4、Special devices (设备说明)
5、File formats (文件格式)
6、Games and toys (游戏和娱乐)
7、Miscellaneous (杂项)
8、Administrative Commands (管理员命令)
9 其他(Linux特定的), 用来存放内核例行程序的文档。

man 3 sleep 指定章节

gobomb commented 5 years ago
第1列分钟0~59
第2列小时0~23(0表示子夜)
第3列日1~31
第4列月1~12
第5列星期0~7(0和7表示星期天)
第6列要运行的命令

crontab -l 列出crontab文件

crontab -e 编辑crontab文件
# truncate the nfs log
# at 1:00 am every Sat.
* 1 * * 6 /usr/bin/truncate --size=0 /path/to/ganesha.log
gobomb commented 5 years ago

https://sittinginoblivion.com/wiki/cant-rmdir-mydatabase-errno-39

mysql : Can't rmdir './mydatabase', Errno: 39

gobomb commented 5 years ago

ctrl + b/f 向后/前移动一个字母 alt + b/f 向后/前移动一个单词

ctrl + w 往前删除一个单词 alt + d 往后删除一个单词

ctrl + h/d 往后/前删除一个字母

ctrl + u/k 往后/前删除到行首/尾

在 zsh 里,ctrl+u是删除整行,可以在 .zshrc 里加 bindkey \^U backward-kill-line

gobomb commented 5 years ago
#通过私钥推导出公钥:
ssh-keygen  -y -f id_rsa > id_rsa.pub.tobecompared

#然后看是不是和原来的公钥相同:
diff id_rsa.pub.tobecompared id_rsa.pub
gobomb commented 5 years ago

k8s 更新镜像:

kubectl set image -n [ns] deploy/[deployName] [containerName]=[imageName]

gobomb commented 5 years ago

k8s 查看对象更改版本

kubectl rollout history deploy/<deployname>

kubectl rollout history deploy/<deployname> --revision=12

  # Rollback to the previous deployment
  kubectl rollout undo deployment/abc

  # Rollback to daemonset revision 3
  kubectl rollout undo daemonset/abc --to-revision=3

  # Rollback to the previous deployment with dry-run
  kubectl rollout undo --dry-run=true deployment/abc
gobomb commented 5 years ago

通过ssh远程执行本地脚本

ssh root@10.10.12.21 'cat|bash' < rm-yarn.sh
gobomb commented 5 years ago

bash 单引号的嵌套

$ echo 'this is a single quote: '\'' '

this is a single quote: '
gobomb commented 5 years ago

修改yum源

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

yum makecache

yum -y update
gobomb commented 5 years ago

永久监听 TCP 连接:

nc -lk 8099
gobomb commented 5 years ago

回声服务器

server 端:

mkfifo fifo
cat fifo|nc -k -l 4458|cat>fifo

client 端:

echo "hi"|nc 10.10.12.28 4458
gobomb commented 5 years ago

alpine 容器跑 go 二进制

在 dockerfile 里添加一句:

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
gobomb commented 5 years ago

查看任意网卡发往5688的包

tcpdump -i any dst port 5688

查看所有容器的ip

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

gobomb commented 5 years ago

kubectl 是用 restful http 和 json 与 apiserver 通信的,docker 客户端是通过 tcp 或者 sock 文件和 docker daemon 通信的

gobomb commented 5 years ago

- 可指定 nameserver

nslookup baidu.com - 8.8.8.8

dig 加 @

dig @8.8.8.8 www.baidu.com A