linux awk sed grep的用法

正则表达式基础知识

正则表达式:

是一套规则与方法,以行为基础工作单位

BRE(基本正则表达式)集合

元字符:^[].*$

作用:匹配字符,匹配位置,锚定位置

^:用于模式的最左侧,^start,表示以start开头的行
$:用于模式的最右行,end$,表示以end结尾的行
^$:用于表示空行
.:匹配任意一个且只有一个字符
*:匹配前一个字符,包含零次,可以理解为匹配所有内容
.*:匹配所有内容,除了空行
\:转义符,特殊符号需要保持字符串格式,需要转义
^.*:匹配任何字符开头(包括数量任意,内容任意,非空行)
.*$:匹配以任何字符结尾(包括数量任意,内容任意,非空行)
[]:匹配方括号内的内容,如[abc]表示匹配a或者b或者c,等价表达式[a-c],注:字母表中连续的字母序列或者连续的数字
[^]:取反,[^abc]匹配除了a、b、c之外的字符

ERD(扩展正则表达式)集合
使用grep -E生效
+:表示匹配前一个字符一次或者多次
[]+:表示匹配中括号中的内容一次或者多次
?:匹配前一个字符0次或者1次
|:“或”,表示同时匹配管道符前后的内容
():分组过滤,表示将()中的内容作为一个整体
a{n,m}:表示匹配a最少n次,最大m次
a{n,}:表示匹配a最少n次
a{n}:表示匹配a正好n次
a{,m}:表示匹配a最大m次

简述三剑客的作用

awk擅长取列;

sed擅长取行和替换;

grep擅长查找功能,比如可以根据关键字查询对应的日志。

三剑客使用介绍

grep是从文本文件或者数据流匹配文本行或者数据

grep [选项] 查找内容 [源文件]
观察其组成结构,由四部分组成:指令名(grep)、选项、查找内容、源文件,其中需要注意的有两个位置,下面让我们徐徐道来。

源文件
源文件部分是可有可无的,若不指定任何文件名称或是所给予的文件名为-,则grep指令会从标准输入设备读取数据,其使用如下所示:

// 文件路径为/test

// 接收cat的输入
cat ./test |grep 'hello'

// 存在路径部分参数
grep 'hello' ./test
选项部分
选项部分比较多,可以通过grep --help指令来看一下有哪些选项:

Regexp selection and interpretation: // 正则表达式选择和解释
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline

Miscellaneous: // 各种各样的
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines // 搜索不匹配的行
-V, --version display version information and exit
--help display this help text and exit

Output control: // 输出控制
-m, --max-count=NUM stop after NUM matches
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name

Context control: // 上下文控制
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
看着选项内容真的很多,背起来着实不易,幸好文档中给我们做了分类,只需要记住这些分类是干什么的,然后在需要的时候从里面进行搜索即可快速搜寻到所需用法(感觉看其内容比看菜鸟教程上的内容容易很多)

(1)当需要通过正则的方式进行搜索内容时,去"Regexp selection and interpretation"区块找选项即可,常用的有:

-E:通过正则表达式进行搜索
(2)当需要对输出的内容进行控制时,去"Output control"区块找选项即可,常用的有如下几个:

-m 数量:表征匹配多少次就会停止
-n:显示匹配行及行号
-H:打印每一个匹配的文件名
-r:能够递归查询,即可以输入文件夹查询
-c:统计匹配到行的个数
(3)当需要获取输出内容的上下文进行操纵时,去"Context control"区块找选项即可,常用的有如下几个:

-B 数量、-A 数量、-C 数量:分别表征获取内容前、后、前后几行
--color:对输出的内容添加颜色
(4)除了一些划分比较理解的选项,还有一些选项我个人认为划分的并不是很合理,但是它们仍然很重要,让我们一起来看看有哪些:

-i:忽略字母大小写
-v:反向选择,也就是显示出没有搜索出字符串内容的那一行
二、经典用法
上面已经将其基本使用做了详细的阐述,俗话说的好:光说不练假把式,光练不说真把式,连说带练全把式。既然上面阐述了一通理论的东西,下面我们就来实战几个常用场景,将理论付诸于实践。在实战之前先创建一个文件,文件名是test,文件内容如下所示:

hello world!!!
dog
cat
pig
big pig
tiger
Elephant
从确定文件中过滤出包含pig的

$ grep 'pig' ./test
pig
big pig
从包含某一部分内容的文件中过滤包含pig的

$ grep 'pig' ./te*
pig
big pig
从某一文件夹下所有内容中过滤出包含pig的

$ grep -r 'pig' .
./test:pig
./test:big pig
从某一文件中过滤出不包含pig的

$ grep -v 'pig' ./test
hello world!!!
dog
cat
tiger
Elephant
在过滤文件时显示行数

$ grep -n 'pig' ./test
4:pig
5:big pig
匹配出以开头的内容(通过基本正则表达式匹配即可,基本正则表达式字符有^$.[]*)

$ grep ^p ./test
pig
匹配出包含pig或cat内容的行(用到了扩展正则表达式,其在基本正则表达式基础上增加了(){}?+|等)

$ grep -E 'pig|cat' ./test
cat
pig
big pig
匹配出包含hello和world内容的行

$ grep 'hello' ./test |grep 'world'
hello world!!!
获取到匹配内容‘big pig'的前一行内容

$ grep -B1 'big pig' ./test
pig
big pig
获取匹配到'pig'行的数量

$ grep -c 'pig' ./test
获取到的pig行的内容高亮显示

$ grep --color 'pig' ./test
pig
big pig

sed是一个流编辑器

一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等

sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

动作说明:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!

sed匹配范围:
空地址:全文匹配,即逐行操作
单地址:指定行匹配
/.../:被模式匹配的每一行
区间范围:10,20是第十行到第二十行;10,+5,表示第十行向下五行;/.../,/.../也可多个模式一起结合匹配。
步长:1~2,表示从1开始,步长为2

实现功能:
1.提取,打印:
sed -n '匹配模式p' 文件名,如sed -n '1,2p' passwd.txt,显示第一至第二行。
2.过滤
sed -n '匹配模式p' 文件名,如sed -n '/^system/p' passwd.txt,显示已system开头的行

awk是一个处理文本的编程语言工具

能用简短的程序处理标准输入、文件、数据排序、计算以及生成报表等。

也是按行处理,默认分隔符是“ ”

awk基本命令

 awk -h
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options: (standard)
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
Short options:          GNU long options: (extensions)
        -b                      --characters-as-bytes
        -c                      --traditional
        -C                      --copyright
        -d[file]                --dump-variables[=file]
        -D[file]                --debug[=file]
        -e 'program-text'       --source='program-text'
        -E file                 --exec=file
        -g                      --gen-pot
        -h                      --help
        -i includefile          --include=includefile
        -l library              --load=library
        -L[fatal|invalid]       --lint[=fatal|invalid]
        -M                      --bignum
        -N                      --use-lc-numeric
        -n                      --non-decimal-data
        -o[file]                --pretty-print[=file]
        -O                      --optimize
        -p[file]                --profile[=file]
        -P                      --posix
        -r                      --re-interval
        -S                      --sandbox
        -t                      --lint-old
        -V                      --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
        gawk '{ sum += $1 }; END { print sum }' file
        gawk -F: '{ print $1 }' /etc/passwd
$0 代表一整行数据
$1代表第一列
$NF代表最后一列,$(NF-1)倒数第二列

linux tee的用法

tee命令用于将标准输入复制到每个指定文件,并显示到标准输出。tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

  • 基本参数
tee --help

Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.

  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
  -p                        diagnose errors writing to non pipes
      --output-error[=MODE]   set behavior on write error.  See MODE below
      --help     display this help and exit
      --version  output version information and exit

MODE determines behavior with write errors on the outputs:
  'warn'         diagnose errors writing to any output
  'warn-nopipe'  diagnose errors writing to any output not a pipe
  'exit'         exit on error writing to any output
  'exit-nopipe'  exit on error writing to any output not a pipe
The default MODE for the -p option is 'warn-nopipe'.
The default operation when --output-error is not specified, is to
exit immediately on error writing to a pipe, and diagnose errors
writing to non pipe outputs.
  • 将屏幕输出同时输出到文件:
ping www.baidu.com | tee pingbaidu.log
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
......
cat pingbaidu.com
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
......
  • 追加到输出文件
ping www.sina.com | tee -a pingbaidu.log
PING ww1.sinaimg.cn.w.alikunlun.com (47.246.16.231) 56(84) bytes of data.
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=1 ttl=57 time=2.15 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=2 ttl=57 time=5.42 ms
......
cat pingbaidu.log  
PING www.wshifen.com (103.235.46.40) 56(84) bytes of data.
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=1 ttl=55 time=3.79 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=2 ttl=55 time=3.87 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=3 ttl=55 time=4.62 ms
64 bytes from 103.235.46.40 (103.235.46.40): icmp_seq=4 ttl=55 time=3.77 ms
PING ww1.sinaimg.cn.w.alikunlun.com (47.246.16.231) 56(84) bytes of data.
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=1 ttl=57 time=2.15 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=2 ttl=57 time=5.42 ms
64 bytes from 47.246.16.231 (47.246.16.231): icmp_seq=3 ttl=57 time=2.16 ms
......
  • 将内容输出到多个文件,我们直接在tee命令后面直接添加对应的文件
ping www.baidu.com | tee pingbaidu.log ping.log
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
......

cat pingbaidu.log 
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
......

cat ping.log 
ING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=21.2 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.4 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=20.5 ms
  • 忽略中断事件,这个时候我们可以使用-i参数
$ ping www.baidu.com | tee -i pingbaidu.log ping.log
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=20.4 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.6 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.9 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=19.3 ms
^C
--- www.wshifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 19.311/19.842/20.465/0.450 ms

......

cat pingbaidu.log 
PING www.wshifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172: icmp_seq=1 ttl=46 time=20.4 ms
64 bytes from 183.232.231.172: icmp_seq=2 ttl=46 time=19.6 ms
64 bytes from 183.232.231.172: icmp_seq=3 ttl=46 time=19.9 ms
64 bytes from 183.232.231.172: icmp_seq=4 ttl=46 time=19.3 ms

--- www.wshifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 19.311/19.842/20.465/0.450 ms

删除文件

删除文件命令:

find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \;

实例命令:

find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

说明:

将/opt/soft/log/目录下所有30天前带".log"的文件删除。具体参数说明如下:

find:linux的查找命令,用户查找指定条件的文件;

/opt/soft/log/:想要进行清理的任意目录;

-mtime:标准语句写法;

+30:查找30天前的文件,这里用数字代表天数;

"*.log":希望查找的数据类型,"*.jpg"表示查找扩展名为jpg的所有文件,"*"表示查找所有文件,这个可以灵活运用,举一反三;

-exec:固定写法;

rm -rf:强制删除文件,包括目录;

{} \; :固定写法,一对大括号+空格+\+; 

nginx反向代理kibana,使用二级目录

nginx的配置文件

    location ^~ /es/ {
#                auth_basic "Please input password";   #这里是验证时的提示信息
#                auth_basic_user_file password/passwd;
                proxy_redirect off;
                rewrite ^/es/(.*)$ /$1 break;
                proxy_http_version 1.1;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://kibana:5601/;
}

kibana.xml

server.name: kibana  
server.host: "0"  ## 任意主机可访问
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN" ## 中文显示
server.basePath: "/es"  ## 与nginx一致

TOMCAT的docker镜像封装支持中文

Dockerfile如下,fonts是windows下的字体文件

FROM tomcat:8.5.54-jdk8
COPY ./fonts /usr/share/fonts/Fonts
COPY ./sources.list /etc/apt/sources.list
RUN apt update && apt install locales-all fontconfig ttf-dejavu  ttf-mscorefonts-installer -y  && rm -rf /var/cache/apk/* && chmod 777 -R /usr/share/fonts/Fonts && cd /usr/share/fonts && mkfontscale && mkfontdir && fc-cache -fv
ENV LANG zh_CN.utf8
#ENV LANG C.UTF-8
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas

sources.list如下

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

server.xml修改

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" URIEncoding="UTF-8"/> ## 增加UTF-8部分

catalina.sh修改

JAVA_OPTS="$JAVA_OPTS -Xms16384m -Xmx16384m -Djava.protocol.handler.pkgs=org.apache.catalina.webresources  -Dfile.encoding=UTF8 -Djava.awt.headless=true"  

## headless=true,解决验证码不显示
## -Dfile.encoding=UTF8,解决字符集问题
## -Duser.timezone=GMT+08,解决时区问题

LINUX shell 获取日期并格式化

#!/bin/sh

# upload logs to hdfs

source /etc/profile

#获取昨天日期 yyyyMMdd 格式

yesterday=`date --date='1 days ago' +%Y%m%d`

#获取昨天日期 yyyy-MM-dd 格式

yesterday2=`date --date='1 days ago' +%Y-%m-%d`

#获取今天日期 yyyyMMdd 格式

today=`date --date='0 days ago' +%Y%m%d`

#获取今天日期 yyyy-MM-dd 格式

today2=`date --date='0 days ago' +%Y-%m-%d`

#获取明天日期 yyyyMMdd 格式

today=`date --date='1 days' +%Y%m%d`

#获取明天日期 yyyy-MM-dd 格式

today2=`date --date='1 days' +%Y-%m-%d`

#获取当前时间 yyyy-MM-dd HH:mm:ss 格式

nowtime=`date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"`

#获取当前日间 HH:mm:ss 格式

nowtime2=`date --date='0 days ago' +%H:%M:%S`

#获取1小时前时间 yyyy-MM-dd HH:mm:ss 格式

onehourage=`date --date='1 hours ago' "+%Y-%m-%d %H:%M:%S"`

#获取1小时后时间 yyyy-MM-dd HH:mm:ss 格式

onehourage2=`date --date='1 hours' "+%Y-%m-%d %H:%M:%S"`

#获取2小时前时间 yyyy-MM-dd HH:mm:ss 格式

twohourage=`date --date='2 hours ago' "+%Y-%m-%d %H:%M:%S"`

#获取2小时后时间 yyyy-MM-dd HH:mm:ss 格式

twohourage2=`date --date='2 hours' "+%Y-%m-%d %H:%M:%S"`

#获取1个月前时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month ago' "+%Y-%m-%d %H:%M:%S"

#获取1个月加1天前时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month ago + 1 day ago' "+%Y-%m-%d %H:%M:%S"

#获取1个月减1天前时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month ago - 1 day ago' "+%Y-%m-%d %H:%M:%S"

#获取1个月加1天加1小时加1分钟加1秒钟前时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month ago + 1 day ago + 1 hour ago + 1 min ago + 1 sec ago' "+%Y-%m-%d %H:%M:%S"

#获取1个月加1天减1小时减1分钟减1秒钟前时间 yyyy-MM-dd HH:mm:ss 格式  可以 + - 滥用

date --date='1 month ago + 1 day ago - 1 hour ago - 1 min ago - 1 sec ago' "+%Y-%m-%d %H:%M:%S"

#获取1个月加1天加1小时加1分钟加1秒钟后时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month + 1 day  + 1 hour  + 1 min  + 1 sec ' "+%Y-%m-%d %H:%M:%S"

#获取1个月加1天减1小时减1分钟减1秒钟后时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 month + 1 day  - 1 hour  - 1 min  - 1 sec ' "+%Y-%m-%d %H:%M:%S"

#获取1年后1个月加1天减1小时减1分钟减1秒钟后时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 year 1 month + 1 day  - 1 hour  - 1 min  - 1 sec ' "+%Y-%m-%d %H:%M:%S"

#获取1年前1个月加1天减1小时减1分钟减1秒钟后时间 yyyy-MM-dd HH:mm:ss 格式

date --date='1 year 1 month + 1 day  - 1 hour  - 1 min  - 1 sec ' "+%Y-%m-%d %H:%M:%S"

CENTOS7升级内核

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available #查看可升级内核版本
yum --enablerepo=elrepo-kernel install kernel-ml -y #mainline较为激进
#指定版本
yum --disablerepo='*' --enablerepo='kernel-ml-x86_64                                                                                                5.17.1-1.el7.elrepo.*' upgrade
sed -i 's/saved/0/g' /etc/default/grub #自己注意修改
grub2-mkconfig -o /boot/grub2/grub.cfg && reboot

ubuntu安装kubernetes

基础环境

四台主机,一主三从,ubuntu20.04

重点:主机名必须不同!必须不同!必须不同!

修改系统参数

  • 禁用swap
vim /etc/fstab
注释掉swap行

  • 确保时区和时间正确
timedatectl set-timezone Asia/Shanghai
  • master安装ntp服务
apt install ntp -y
  • node节点变更ntp服务器
vim /etc/ntp.conf
清空,只写如下行
server master节点ip或者内部域名
  • net.bridge.bridge-nf-call-iptables
vim /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
  • rp_filter
vim /etc/sysctl.d/10-network-security.conf

#将下面两个参数的值从2修改为1
net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.all.rp_filter=1

部署docker

  • installdocker.sh
#!/bin/env bash
apt update
apt upgrade -y
apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - 
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt update
apt install docker-ce docker-ce-cli containerd.io -y
  • daemon.json

最后一行是重点

 {
	    "registry-mirrors": ["https://0xj5rnq5.mirror.aliyuncs.com"],
	    "log-driver":"json-file",
	    "log-opts": {"max-size":"500m", "max-file":"3"},
	    "exec-opts": ["native.cgroupdriver=systemd"]
 }

部署kubernetes master

  • installk8s.sh
#!/bin/env bash
k8sversion=1.22.7-00
apt update && apt install -y ca-certificates curl software-properties-common apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
tee /etc/apt/sources.list.d/kubernetes.list <<EOF 
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt update
apt install -y kubelet=${k8sversion} kubeadm=${k8sversion} kubectl=${k8sversion}
apt-mark hold kubelet kubeadm kubectl
  • 初始化master

kubeadm init --kubernetes-version=1.22.7 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/16 --apiserver-advertise-address=0.0.0.0 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

  • 非root用户要做的
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
  • root用户要做的
/etc/profile
export KUBECONFIG=/etc/kubernetes/admin.conf
  • 安装calico网络插件(可选)
第一种:
wget -c https://docs.projectcalico.org/v3.21/manifests/calico.yaml
修改CALICO_IPV4POOL_CIDR,不能与宿主机冲突
kubectl apply -f calico.yaml

第二种:(安装正确,但是安装istio时候,ca过不去,不知道具体原因,暂时不适用这个方式)
wget https://docs.projectcalico.org/manifests/tigera-operator.yaml
wget https://docs.projectcalico.org/manifests/custom-resources.yaml
修改custom-resources.yaml,ippool修改的于pod-network一致
kubectl create -f tigera-operator.yaml
kubectl create -f custom-resources.yaml

给master打上污点
kubectl taint nodes --all node-role.kubernetes.io/master-
  • 安装flannel网络插件(可选)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  • 初始化后的输出
Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.31.0.99:6443 --token 08gygc.dgz7t1gf1plglllc \
	--discovery-token-ca-cert-hash sha256:f4fbe6d79db14d9a038a01ba34e5fd460a3807b967c664e8658f0e4948e3beba 

安装K8s worker节点

  • 执行 installk8s.sh
  • 加入节点

kubeadm join 172.31.0.99:6443 --token 08gygc.dgz7t1gf1plglllc \
	--discovery-token-ca-cert-hash sha256:f4fbe6d79db14d9a038a01ba34e5fd460a3807b967c664e8658f0e4948e3beba

删除节点

  • 在master执行
kubectl drain tqu4jnp1sot5qxtx-0003  --delete-emptydir-data --force --ignore-daemonsets
kuberctl delete nods tqu4jnp1sot5qxtx-0003
  • 在client执行
kuberctl reset
  • token失效后的处理
kubeadm token create --print-join-command

安装helm

installhelm.sh

#!/bin/env bash
curl https://baltocdn.com/helm/signing.asc | apt-key add -
apt install apt-transport-https --yes
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | tee /etc/apt/sources.list.d/helm-stable-debian.list
apt update
apt install helm

istio

  • 安装istio
wget -c https://github.com/istio/istio/releases/download/1.13.1/istio-1.13.1-linux-amd64.tar.gz
tar -zxvf istio-1.13.1-linux-amd64.tar.gz
mv istio-1.13.1 istio
export $PWD/bin:$PATH
cd istio
istioctl install --set profile=demo -y
kubectl label namespace default istio-injection=enabled
  • 删除istio
istioctl experimental uninstall --purge 
kubectl delete namespace istio-system

更新证书到100年

下载kubernetes源码

wget -c https://github.com/kubernetes/kubernetes/archive/refs/tags/v1.22.7.tar.gz
tar -zxvf v1.22.7.tar.gz
mv ./kubernetes-1.22.7 /tmp/kubernetes
修改./staging/src/k8s.io/client-go/util/cert/cert.go
    // NotAfter: now.Add(duration365d * 10).UTC(),
    NotAfter:              now.Add(duration365d * 100).UTC(),
修改:./cmd/kubeadm/app/constants/constants.go
    // CertificateValidity = time.Hour * 24 * 365
    CertificateValidity = time.Hour * 24 * 365 * 100

docker run --rm -it -v /tmp/kubernetes:/go/src/k8s.io/kubernetes larryq/kube-cross:v-go1.17.7
#cd /go/src/k8s.io/kubernetes
make all WHAT=cmd/kubeadm GOFLAGS=-v
以下两个可以不编译
make all WHAT=cmd/kubectl GOFLAGS=-v
make all WHAT=cmd/kubelet GOFLAGS=-v

输出文件路径:/tmp/kubernetes/_output/local/bin/linux/amd64
将原kubeadm备份
mv /usr/bin/kubeadm /usr/bin/kubeadm-bak
cp /tmp/kubernetes/_output/local/bin/linux/amd64/kubeadm /usr/bin/
chmod a+x /usr/bin/kubeadm

如果在kubeadm init之前就处理了kubeadm,直接init即可,如果是之后,则需进行更新操作
检查有效期kubeadm certs check-expiration
更新证书:kubeadm certs renew all

kubernetes初始化错误

kunernetes初始化命令:

kubeadm init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/16 --apiserver-advertise-address=0.0.0.0 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

报错:
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused.

解决:
修改配置文件/etc/docker/daemon.json,添加以下行:

{"exec-opts": ["native.cgroupdriver=systemd"]}

原因:

这是cgroup驱动问题。