01-top命令基本使用

1: Top 命令按键

options按键说明示例其他
-u显示特定 user 的进程top -u lucas
-p显示特定 PID 的进程top -p 10001 10002
-n10 次输出后退出程top -n 10
1按数字 1 可以显示每个 CPU 核心使用情况
M根据驻留内存RES大小进行排序,由大到小(注意大小写)
RRES反序排序
P根据 CPU 使用百分比大小进行
T根据时间/累计时间进行排序
c切换显示命令名称和完整命令行
i忽略闲置和僵死进程。这是一个开关式命令。
f更改显示内容通过 f 键可以选择显示的内容。按 f 键之后会显示列的列表,空格确定显示或者取消显示 a 是切换
d更改刷新时间
o进入 top 后,按下 o 后,会让你输入过滤条件,输入 COMMAND=nginx,现在就只剩下模糊匹配 nginx 关键字的进程了
Shift+P对进程 CPU 利用率进行排序
Shift+O对进程 Memory 利用率进行排序

2 Top 参数说明

2.1 CPU 状态信息

0.0%us 【user space】— 用户空间占用 CPU 的百分比。

01-zshrc 笔记

zshrc笔记

1:zshrc 文件

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/home/lucas/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

# 修改默认主题
ZSH_THEME="ys"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    git
    zsh-syntax-highlighting
    zsh-autosuggestions
    sudo
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

source $ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source $ZSH_CUSTOM/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh

# 修改默认编辑器为vim
export EDITOR=/usr/bin/vim

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$GOPATH:$GOBIN:$GOROOT/bin:$PATH

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH="$PATH:/mnt/c/Program\ Files/Docker/Docker/resources/bin"
export PATH="$PATH:/usr/local/lib/node-v16.4.2-linux-x64/bin"

# export DOCKER_HOST="tcp://0.0.0.0:2375"

# alias
# alias docker=docker.exe
# alias docker-compose=docker-compose.exe
# alias go colorgo

# history 命令显示时间
export HIST_STAMPS="yyyy-mm-dd"

# ----------------proxy----------------------
#
WindowsIP=$(cat /etc/resolv.conf | grep -oP '(?<=nameserver\ ).*')
PROXY_PORT=7078
PROXY_HTTP="http://${WindowsIP}:${PROXY_PORT}"
# Git & SSH for Git proxy
proxy_git() {
    git config --global http.https://github.com.proxy ${PROXY_HTTP}
    if ! grep -qF "Host github.com" ~/.ssh/config; then
        echo "Host github.com" >>~/.ssh/config
        echo "  User git" >>~/.ssh/config
        echo "  ProxyCommand nc -X 5 -x ${PROXY_SOCKS5} %h %p" >>~/.ssh/config
    else
        lino=$(($(awk '/Host github.com/{print NR}' ~/.ssh/config) + 2))
        sed -i "${lino}c\  ProxyCommand nc -X 5 -x ${PROXY_SOCKS5} %h %p" ~/.ssh/config
    fi
}
# Set proxy
set_proxy() {
    export http_proxy="${PROXY_HTTP}"
    export https_proxy="${PROXY_HTTP}"
    proxy_git
}
# Unset proxy
unset_proxy() {
    unset http_proxy
    unset https_proxy
    git config --global --unset http.https://github.com.proxy
}

# Set alias
alias proxy=set_proxy
alias deproxy=unset_proxy

alias px='proxychains4' # 此后 px curl google.com

# wsl sync time
# alias synctime='sudo ntpdate asia.pool.ntp.org;sudo ntpdate time.nist.gov;'
alias synctime1='sudo ntpdate asia.pool.ntp.org;'
alias synctime2='sudo ntpdate time.nist.gov;'

# 手动释放内存
alias freemem='~/myshell/free-mem.sh'

# alias godoc='GO111MODULE=off godoc'
alias fy='unproxy dict'

2:其他说明

01-Golang Files and I/O

Golang Files and I/O

Table of Contents

一:读文件

1.1 读一个 whole 文件

1
2
3
4
5
d, err := ioutil.ReadFile("foo.txt")
if err != nil {
    log.Fatalf("ioutil.ReadFile failed with '%s'\n", err)
}
fmt.Printf("Size of 'foo.txt': %d bytes\n", len(d))

1.2 读一个文件,close

1
2
3
4
5
f, err := os.Open("foo.txt")
if err != nil {
    log.Fatalf("os.Open failed with '%s'\n", err)
}
defer f.Close()

1.3 read file by line

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ReadLines reads all lines from a file
func ReadLines(filePath string) ([]string, error) {
	file, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	res := make([]string, 0)
	for scanner.Scan() {
		line := scanner.Text()
		res = append(res, line)
	}
	if err = scanner.Err(); err != nil {
		return nil, err
	}
	return res, nil
}

func main() {
	path := "main.go"
	lines, err := ReadLines(path)
	if err != nil {
		log.Fatalf("ReadLines failed with '%s'\n", err)
	}
	fmt.Printf("File %s has %d lines\n", path, len(lines))
}

二:写文件

2.1 Write the whole file

1
2
3
4
5
d := []byte("content of the file")
err := ioutil.WriteFile("foo.txt", d, 0644)
if err != nil {
    log.Fatalf("ioutil.WriteFile failed with '%s'\n", err)
}

2.2 Open file for writing

1
2
3
4
f, err := os.Create("foo.txt")
if err != nil {
    log.Fatalf("os.Open failed with '%s'\n", err)
}

2.3 Open file for appending

1
2
3
4
f, err := os.OpenFile(filePath, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0666)
if err != nil {
    log.Fatalf("os.Open failed with '%s'\n", err)
}

2.4 Write to a file

1
2
d := []byte("data to write")
nWritten, err := f.Write(d)

2.5 File permissions when creating files

使用 os.Create 或创建新文件时 os.OpenFile,需要为新文件提供文件权限。

01-微服务限流简单实现

微服务限流简单实现

Table of Contents

一:限流的目的

保障服务稳定的三大利器:熔断、降级、服务限流。今天和大家谈谈限流算法的几种实现方式,本文所说的限流并非是 Nginx、网关层面的限流,而是业务代码中的逻辑限流。

01-docker registry http api v2

docker registry http api v2

Table of Contents

1: Overview

methodspathEntityDescription
GET/v2/Base检查 the endpoint 是否实现了 Docker Registry API V2.
GET/v2/{name}/tags/listTags通过 name 获取 repository 下的 tags
GET/v2/{name}/manifests/{reference}Manifest获取由 name 和 references 标识的 manifest,其中 referencec 可以是 tag 或 digest。还可以向该 endpoint 发出 HEAD 请求以获取资源信息,而不接收所有数据。
PUT/v2/{name}/manifests/{reference}ManifestPut 由 name 和 reference 标识的 manifest
DELETE/v2/{name}/manifests/{reference}Manifest删除由 digest 标识的 manifest
GET/v2/{name}/blobs/{digest}Blob从 registry retrieve 由 digest 标识的 blobs
DELETE/v2/{name}/blobs/{digest}BlobDelete the blob identified by name and digest
POST/v2/{name}/blobs/uploads/Initiate(启动) Blob upload 服务启动可恢复的 blob upload 服务。如果成功,将提供上载位置以完成上载。或者,如果存在 digest 参数,则请求正文将用于在单个请求中完成上载。
GET/v2/{name}/blobs/uploads/{uuid}Blob Upload检索由 uuid 标识的上传状态。此 endpoint 的主要目的是 resolve 可恢复 upload 的当前状态。
PATCH/v2/{name}/blobs/uploads/{uuid}Blob Upload上传 指定 upload 的 chunk 数据块
PUT/v2/{name}/blobs/uploads/{uuid}Blob Upload完成 uuid 指定的上传,可以选择附加 body 作为最后一个块
DELETE/v2/{name}/blobs/uploads/{uuid}Blob UploadCancel outstanding upload processes, releasing associated resources. If this is not called, the unfinished uploads will eventually timeout.
GET/v2/_catalogCatalog在 registry 中检索一个有序的,可用的 repositry 的 json 列表

2: 专有名词解释

  • repositry name(存储库名称):存储库指在库中存储的镜像
  • digest(摘要):是 image 各个层的唯一标识。虽然算法允许使用任意算法,但是为了兼容性应该使用 sha256。 例:sha256:6c3c624b58dbbcd3c0dd82b4c53f04194d1247c6eebdaab7c610cf7d66709b3b

3: Push image 过程

先推各个 Layer 层到 registry 仓库,然后上传清单 Manifest

01-WSL2中配置docker远程tcp

WSL2中配置docker远程tcp

1: 修改 /lib/systemd/system/docker.service

1
2
3
4
5
# sudo vim /lib/systemd/system/docker.service

[Service]
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// --containerd=/run/containerd/containerd.sock
# 上面这一行,主要是增加了`-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock`

2: 设置 DOCKER_HOST 环境变量

1
2
3
4
# sudo vim ~/.zshrc
export DOCKER_HOST=tcp://0.0.0.0:2375

source ~/.zshrc

3: 修改 docker 启动配置文件/etc/default/docker

1
2
3
# 开启远程访问 -H tcp://0.0.0.0:2375
# 开启本地套接字访问 -H unix:///var/run/docker.sock
DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock"

4: 重启 docker

1
sudo service docker restart

01-mysql数据库优化最完整指南

阶段一: 数据库表设计

  • 表结构设计

阶段二: 数据库部署

  • 主从复制
  • 集群
  • 主从复制+Keepalived 实现双机热备份

主流的 HA 软件有: Keepalived,Heartbeat

01-Convert json to csv

Table of Contents

1: Convert json to csv

1.1 json 文件

  • company.json
1
2
3
4
5
6
7
[
  { "App": "Instagram", "Company": "Facebook", "Category": "Social Media" },
  { "App": "WeChat", "Company": "Tencent", "Category": "Social Media" },
  { "App": "Hotstar", "Company": "Disney", "Category": "Entertainment" },
  { "App": "CNBC", "Company": "Comcast", "Category": "News" },
  { "App": "SnapChat", "Company": "Snap", "Category": "Social Media" }
]

1.2 convert

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

// Application struct
type Application struct {
    App      string
    Company  string
    Category string
}

func main() {
    // read data from file
    jsonDataFromFile, err := ioutil.ReadFile("./company.json")

    if err != nil {
        fmt.Println(err)
    }

    // Unmarshal JSON data
    var jsonData []Application
    err = json.Unmarshal([]byte(jsonDataFromFile), &jsonData)

    if err != nil {
        fmt.Println(err)
    }

    csvFile, err := os.Create("./data.csv")

    if err != nil {
        fmt.Println(err)
    }
    defer csvFile.Close()

    writer := csv.NewWriter(csvFile)

    for _, usance := range jsonData {
        var row []string
        row = append(row, usance.App)
        row = append(row, usance.Company)
        row = append(row, usance.Category)
        writer.Write(row)
    }

    // remember to flush!
    writer.Flush()
}

01-MInikube搭建

[toc]

1: 安装 Docker 环境

1.1: 安装 docker

1.2: 配置 docker

docker 加速 /etc/docker/daemon.json 重启 docker,使配置生效

1
2
3
sudo systemctl daemon-reload

sudo systemctl restart docker

2: 安装 Minikube

https://cloud.tencent.com/developer/article/1817826

2.1 使用普通的 minikube

2.1.1 更新软件源

2.1.2 更新软件包

2.1.3 安装 docker

2.1.4 安装 k8s

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 执行以下命令安装 https 工具以及 k8s
apt-get update && apt-get install -y apt-transport-https curl
apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated

# 执行下面命令测试是否正常
kubeadm init

# 如果安装时,出现下面情况,说明系统得镜像源中,找不到 k8s 的软件包
No apt package "kubeadm", but there is a snap with that name.
Try "snap install kubeadm"

No apt package "kubectl", but there is a snap with that name.
Try "snap install kubectl"

# 可以打开 /etc/apt/sources.list  文件,添加一行
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

# 再次执行安装k8s

# 如果出现下面
The following signatures couldn't be verified because the public key is not available

# 则执行下面命令,添加key
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add

上面命令,安装了 kubeletkubeadmkubectl