Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheatsheet #2

Open
gobomb opened this issue Sep 14, 2018 · 52 comments
Open

Cheatsheet #2

gobomb opened this issue Sep 14, 2018 · 52 comments

Comments

@gobomb
Copy link
Owner

gobomb commented Sep 14, 2018

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
Copy link
Owner Author

gobomb commented Sep 17, 2018

awk格式化输出:

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

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

@gobomb
Copy link
Owner Author

gobomb commented Sep 17, 2018

一键持久化 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
Copy link
Owner Author

gobomb commented Sep 19, 2018

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

@gobomb
Copy link
Owner Author

gobomb commented Sep 19, 2018

统计重复行:

sort [file] |uniq -c

@gobomb
Copy link
Owner Author

gobomb commented Sep 26, 2018

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

@gobomb
Copy link
Owner Author

gobomb commented Sep 26, 2018

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

强制删除 pod

@gobomb
Copy link
Owner Author

gobomb commented Sep 26, 2018

git diff > patch

git apply patch

保存 diff 和打补丁

@gobomb
Copy link
Owner Author

gobomb commented Sep 29, 2018

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

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

@gobomb
Copy link
Owner Author

gobomb commented Sep 30, 2018

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

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

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 11, 2018

type T struct{}
var _ I = T{}

其中 I为interface

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 11, 2018

echo 'HISTSIZE=10000' >> /etc/profile
. /etc/profile

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 16, 2018

转换变量全部大写或小写

echo $val | awk '{print tolower($1)}'
echo $val | awk '{print toupper($1)}' 

@gobomb
Copy link
Owner Author

gobomb commented Oct 16, 2018

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

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 20, 2018

UNIX

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 21, 2018

Python

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

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

@gobomb
Copy link
Owner Author

gobomb commented Oct 24, 2018

Intellj IDEA

批量修改变量名:
shift+F6

@gobomb
Copy link
Owner Author

gobomb commented Oct 26, 2018

git

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

git format-patch [commit]

打 patch:

git am /path/to/patch

@gobomb
Copy link
Owner Author

gobomb commented Oct 31, 2018

k8s

修改 yaml:

kubectl edit deployment kubernetes-dashboard -n kube-system

@gobomb
Copy link
Owner Author

gobomb commented Nov 2, 2018

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
Copy link
Owner Author

gobomb commented Nov 6, 2018

Mac homebrew

brew tap

查看第三方仓库

brew tap [x/y]

安装新的第三方仓库

@gobomb
Copy link
Owner Author

gobomb commented Nov 12, 2018

Linux

jobs列出后台进程

kill %[job_id]杀死进程

或者

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

kill [PID]

@gobomb
Copy link
Owner Author

gobomb commented Nov 12, 2018

Linux

ls 不带颜色输出

ls -la --color=never

@gobomb
Copy link
Owner Author

gobomb commented Nov 12, 2018

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
Copy link
Owner Author

gobomb commented Nov 12, 2018

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
Copy link
Owner Author

gobomb commented Nov 15, 2018

查看系统/内核日志

tailf /var/log/syslog

tailf /var/log/kern.log

@gobomb
Copy link
Owner Author

gobomb commented Nov 26, 2018

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

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

@gobomb
Copy link
Owner Author

gobomb commented Nov 27, 2018

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

@gobomb
Copy link
Owner Author

gobomb commented Dec 10, 2018

docker

通过 pid 查到 container id:

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

@gobomb
Copy link
Owner Author

gobomb commented Dec 17, 2018

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
Copy link
Owner Author

gobomb commented Dec 29, 2018

删除最早的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
Copy link
Owner Author

gobomb commented Dec 29, 2018

截断文件

truncate --size=10M filename

@gobomb
Copy link
Owner Author

gobomb commented Dec 29, 2018

\cp src dst

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

@gobomb
Copy link
Owner Author

gobomb commented Jan 8, 2019

k8s 给 node 添加污点

kubectl taint nodes node1 key1=value1:NoSchedule

kubectl taint nodes node1 key1:NoSchedule-

@gobomb
Copy link
Owner Author

gobomb commented Jan 10, 2019

ssh 本地端口转发

ssh -L 2181:bp-zookeeper:2181 [email protected] &

@gobomb
Copy link
Owner Author

gobomb commented Jan 14, 2019

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
Copy link
Owner Author

gobomb commented Jan 17, 2019

第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
Copy link
Owner Author

gobomb commented Feb 14, 2019

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

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

@gobomb
Copy link
Owner Author

gobomb commented Feb 27, 2019

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
Copy link
Owner Author

gobomb commented Mar 1, 2019

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

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

@gobomb
Copy link
Owner Author

gobomb commented Mar 4, 2019

k8s 更新镜像:

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

@gobomb
Copy link
Owner Author

gobomb commented Mar 4, 2019

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
Copy link
Owner Author

gobomb commented Mar 6, 2019

通过ssh远程执行本地脚本

ssh [email protected] 'cat|bash' < rm-yarn.sh

@gobomb
Copy link
Owner Author

gobomb commented Mar 7, 2019

bash 单引号的嵌套

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

this is a single quote: '

@gobomb
Copy link
Owner Author

gobomb commented Mar 8, 2019

修改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
Copy link
Owner Author

gobomb commented Mar 11, 2019

永久监听 TCP 连接:

nc -lk 8099

@gobomb
Copy link
Owner Author

gobomb commented Mar 11, 2019

回声服务器

server 端:

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

client 端:

echo "hi"|nc 10.10.12.28 4458

@gobomb
Copy link
Owner Author

gobomb commented Mar 12, 2019

alpine 容器跑 go 二进制

在 dockerfile 里添加一句:

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

@gobomb
Copy link
Owner Author

gobomb commented Mar 14, 2019

查看任意网卡发往5688的包

tcpdump -i any dst port 5688

查看所有容器的ip

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

@gobomb
Copy link
Owner Author

gobomb commented May 2, 2019

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

@gobomb
Copy link
Owner Author

gobomb commented May 3, 2019

- 可指定 nameserver

nslookup baidu.com - 8.8.8.8

dig 加 @

dig @8.8.8.8 www.baidu.com A

@gobomb
Copy link
Owner Author

gobomb commented May 4, 2019

journalctl -u docker.service --no-pager

@gobomb
Copy link
Owner Author

gobomb commented May 5, 2019

centos

修改nameserver,只修改 /etc/resolv.conf 重启会不生效

需要修改

vim /etc/sysconfig/network-scripts/ifcfg-ens160(网卡配置)

DNS1=10.10.11.22

@gobomb gobomb transferred this issue from gobomb/gobomb.github.io Aug 20, 2019
@gobomb gobomb mentioned this issue Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant