elastic search

安装

官网给了丰富的安装方式。

Centos 7

添加仓库

1
2
3
4
5
6
7
8
9
$ sudo vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md

安装

1
$ sudo yum install --enablerepo=elasticsearch elasticsearch

systemd 配置

1
2
$ sudo systemctl daemon-reload
$ sudo systemctl enable elasticsearch.service

启动

1
sudo systemctl start elasticsearch.service

配置

添加用户

文档参考这里

对于使用 yum 在 centos 7 安装的 es 来说,可执行程序在 /usr/share/elasticsearch/bin 下。

1
2
3
4
5
6
7
8
9
10
# 命令格式
$ bin/elasticsearch-users
([useradd <username>] [-p <password>] [-r <roles>]) |
([list] <username>) |
([passwd <username>] [-p <password>]) |
([roles <username>] [-a <roles>] [-r <roles>]) |
([userdel <username>])

# 创建管理员
$ bin/elasticsearch-users useradd wii -p <password> -r superuser

角色 内置角色在这里

  • superuser : 管理员,所有权限

安装 Kibana

yum 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# install public signing key
$ rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

# 添加源
$ vim /etc/yum.repo.d/kibana.repo
[kibana-7.x]
name=Kibana repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

# 安装
$ yum install kibana
$ chkconfig --add kibana

# systemd
$ sudo /bin/systemctl daemon-reload
$ sudo /bin/systemctl enable kibana.service

启动 Kibana

1
$ sudo systemctl start kibana.service

配置

1
2
3
# file: /etc/kibana/kibana.yml
# 添加如下内容
server.host: "0.0.0.0"

访问

http://host:5601

使用

数据

工具

在 Kibana 首页,打开 Dev Tools,或者直接访问 http://host:5601/app/dev_tools#/console

image-20210922115803345

左侧为请求,右侧为响应。

请求的第一行,为请求方法及 URL,下方为数据

写入数据

查询

1
2
3
4
5
6
GET index_article/_search
{
"query": {
"match_all": {}
}
}

curl

参数

1
2
-X	指定请求方法
-H 添加 Header,添加多个可通过重复指定

示例

1
2
3
4
5
6
7
8
# 格式
curl [options] [URL...]

# 指定方法
curl -X POST [URL...]

# 添加 Header
curl -H "Content-Type: application/json" [URL...]

nginx 日志分析

日志文件合并

1
2
# 对于 gz 格式日志文件,使用 gzcat
$ gzcat access-*.gz > access-all.log

分析

goaccess

安装

使用 brew 或下载

1
$ brew install goaccess

日志格式匹配

nginx 日志格式配置需要转换为 goaccess 可识别的日志格式,下面是示例。

nginx 配置 goaccess 格式配置
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$request_body" "$upstream_addr" $upstream_status $upstream_response_time $request_time log-format %h %^ [%d:%t %^] "%r" %s %b "%R" "%u" "%^" "%^" "%^" %^ %^ %T
date-format %d/%b/%Y
time-format %T
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_addr $upstream_response_time $pipe %h %^[%d:%t %^] "%r" %s %b "%R" "%u" %T %^

日志格式配置

分析前,需要配置日志格式。日志格式可以在 ~/.goaccessrc 文件配置,也可以在命令行中指定。

1
2
3
4
5
6
7
8
# ~/.goaccessrc
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u" "%^" "%^" "%^" %^ %^ %T
date-format %d/%b/%Y
time-format %T


# 使用命令行指定
goaccess access-all.log --log-format='%h %^[%d:%t %^] "%r" %s %b "%R" "%u" "%^" "%^" "%^" %^ %^ %T' --date-format='%d/%b/%Y' --time-format='%T'

离线分析

1
2
3
4
5
# 使用 ~/.goaccessrc 文件配置日志格式
$ LANG="en_US.UTF-8" goaccess access-all.log -o report.html

# 使用命令行指定日志格式
$ LANG="en_US.UTF-8" goaccess access-all.log --log-format='%h %^[%d:%t %^] "%r" %s %b "%R" "%u" "%^" "%^" "%^" %^ %^ %T' --date-format='%d/%b/%Y' --time-format='%T' -o report.html

如果出错,可以尝试去掉 LANG="en_US.UTF-8"

spring boot maven

单个可执行 jar 包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>pub.wii.cook.springboot.CookSpringBootApplication</mainClass>
</manifest>
</archive>
<descriptors> <!-- 会被忽略 -->
<descriptor>src/assemble/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

assemble.xml

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
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0
http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>

<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>metaInf-spring</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>plexus</handlerName>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>

openstack network

网络

网络类型设置主要参考两篇文章,设置网络硬件接口 provider设置自服务虚拟网络

类型

  • local
  • flat
  • vlan
  • vxlan
  • gre
  • geneve

连接外网 & 分配外网IP

openstack 配置的是 self-service 网络模式,外网是指虚拟网络外的网络,这里是路由器的网络 192.168.6.1/24

配置

物理机网络接口设置

这里以 ubuntu 20.04 为例,物理机有两个网口(enp4s0、enp5s0)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# /etc/netplan/99-config.xml
network:
version: 2
renderer: networkd
ethernets:
enp5s0:
addresses:
- 192.168.6.33/24
gateway4: 192.168.6.1

# /etc/netplan/00-installer-config.yaml
network:
ethernets:
enp4s0:
dhcp4: false
version: 2

这里是把 enp5s0 网口设置为固定 ip,关闭 enp4s0 的 dhcp 且不分配 ip。

neutron 的配置要关注 /etc/neutron/plugins/ml2 的内容。

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
# cat ml2_conf.ini | grep -v '#' | grep '\S'
[DEFAULT]
[ml2]
type_drivers = flat,vlan,vxlan
tenant_network_types = vxlan
mechanism_drivers = linuxbridge,l2population
extension_drivers = port_security
[ml2_type_flat]
flat_networks = provider
[ml2_type_geneve]
[ml2_type_gre]
[ml2_type_vlan]
[ml2_type_vxlan]
vni_ranges = 1:1000
[ovs_driver]
[securitygroup]
enable_ipset = true
[sriov_driver]

# cat linuxbridge_agent.ini | grep -v '#' | grep '\S'
[DEFAULT]
[agent]
[linux_bridge]
physical_interface_mappings = provider:enp4s0
[network_log]
[securitygroup]
enable_security_group = true
firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver
enable_ipset = true
[vxlan]
enable_vxlan = true
local_ip = 192.168.6.33
l2_population = true

linuxbridge_agent.ini 配置的 physical_interface_mappings = provider:enp4s0 中的 provider:enp4s0 ,冒号前面是新增的接口名称,enp4s0 为系统显示的硬件网络接口。

ml2_conf.ini 中配置 flat_networks = provider ,指定 flat 类型接口使用的网络接口。

设置多值

physical_interface_mappings = provider:enp4s0,providervpn:enp5s0

flat_networks = provider,providervpn

sysctl.conf

编辑 /etc/sysctl.conf,设置 net.ipv4.ip_forward=1

网络拓扑

image-20210904120142325

image-20210904165426999

注意

  • 创建外部网络用 FLAT 类型,物理网络填 provider(和 linuxbridge_agent.ini 中配置的一致)
  • 创建内部网络用 VXLAN 类型

连接外网

首先,要在管理员下创建外部网络。

image-20210904120508173

连接外网有两种方式,一种是实例直连新建的外部网络,一种是创建内部网络,内部网络通过路由连接外部网络。

网络拓扑图中,test 主机是直连外部网络,可以分配 192.168.6.* 地址,以及访问外网,data 和 work 连接内部网络(10.1.0.0/24),然后内部网络通过路由连接外部网络。

分配外部网络 ip

分配外部网络地址有两种方式,直连外部网络和 floating ip。

直连外部网络是给实例添加接口,并指定网络为外部网络,这样在实例里面查看网络信息可以查看到分配的外网 ip。使用 floating ip 的话,将 floating ip 实例分配的内网 ip 绑定,实例内并不能看到 floating ip,外部可以通过 floating ip 访问实例。

最后一步

openstack 默认的安全组禁止所有请求访问,需要添加入口权限。

image-20210904171252080

distro mirrors

ubuntu

22.04

清华源

1
2
3
4
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

20.04

清华源

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

18.04

清华源

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

安装 Arch Linux

创建并挂载分区

  1. 查看硬盘
1
$ fdisk -l
  1. 使用 fdisk 创建分区表
1
2
3
$ fdisk /dev/sda
(fdisk) p # 打印分区信息
(fdisk) g # 创建 GPT 分区表

2.1 创建分区

创建 /boot 分区

1
2
3
4
(fdisk) n  # 创建分区
(fdisk) Partition number...: # 回车
(fdisk) First section...: # 回车
(fdisk) Last section...: +1G

创建 / 分区

1
2
3
4
(fdisk) n  # 创建分区
(fdisk) Partition number...: # 回车, 默认值
(fdisk) First section...: # 回车
(fdisk) Last section...: # 回车, 使用最大 section number

确认

1
(fdisk) p   # 重新打印分区,有新创建的两个分区

保存

1
(fdisk) w
  1. 格式化分区
1
2
$ mkfs.fat -F32 /dev/sda1   # 格式化 /boot 分区(假设为 /dev/sda1)
$ mkfs.ext4 /dev/sda2 # 格式化 / 分区(假设为 /dev/sda2)
  1. 挂载
1
2
3
$ mount /dev/sda2 /mnt       # 挂载根分区
$ mkdir /mnt/boot # 创建 boot 挂载点
$ mount /dev/sda1 /mnt/boot # 挂载 /boot 分区

安装

1
$ pacstrap -K /mnt base linux linux-firmware

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# fstab
$ genfstab -U /mnt >> /mnt/etc/fstab

# Chroot
$ arch-chroot /mnt

# Time
$ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
$ hwclock --systohc

# root passwd
$ passwd

# 安装 vim
$ pacman -S vim

# Localization
$ vim /etc/locale.conf # 写入如下内容
LANG=en_US.UTF-8

# hostname
$ vim /etc/hostname

Boot loader

1
2
3
4
5
6
7
8
9
10
11
# GRUB
$ pacman -S grub efibootmgr
$ grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=ArchLinux

## 如何需要添加其他系统引导,默认不检测其他系统
$ vim /etc/default/grub
# 确保如下配置
GRUB_DISABLE_OS_PROBER=false

## 配置 GRUB
$ grub-mkconfig -o /boot/grub/grub.cfg

重启

1
$ exit

Arch Linux 初始化

中文

1
2
3
4
5
6
7
8
# 修改文件 /etc/locale.gen
en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
zh_CN.GBK GBK
zh_CN GB2312

# locale-gen
$ sudo locale-gen

zsh

1
2
3
# 文件 ~/.zshrc
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

网络设置

使用 systemd-networkd 管理网络

新增文件 /etc/systemd/network/20-wired.network,下面是静态 IP 配置示例。

1
2
3
4
5
6
7
[Match]
Name=enp1s0

[Network]
Address=192.168.6.2/24
Gateway=192.168.6.1
DNS=192.168.6.1

启动 systemd-networkd

1
2
3
4
$ systemctl enable systemd-networkd
$ systemctl start systemd-networkd
$ systemctl enable systemd-resolved
$ systemctl start systemd-resolved

检查 DNS

1
$ resolvectl status

配置 SSH 远程登录

1
2
3
4
$ pacman -S openssh
$ vim /etc/ssh/sshd_config # 添加如下内容
PasswordAuthentication yes
PermitRootLogin yes # 允许 Root 用户远程登录(密钥、密码均可)

启动 sshd

1
2
systemctl enable sshd.service
systemctl start sshd.service

创建用户及用户组

1
2
3
$ groupadd -g 100 wii
$ pacman -S zsh
$ useradd -m -d /home/wii -s /bin/zsh -g wii wii

普通用户获取超级管理员权限

1
2
3
4
$ pacman -S sudo
$ chmod 0600 /etc/sudoers
$ vim /etc/sudoers # 添加如下内容, 最好在文件最后
wii ALL=(ALL:ALL) NOPASSWD: ALL # wii 用户使用 sudo 获取所有权限,且不需要密码

必备

1
2
3
$ pacman -S sudo git curl wget zip unzip base-devel
# C++ 开发
$ pacman -S gdb cmake ninja texinfo

Wake On Lan

1
2
3
4
5
6
7
8
9
10
11
12
sudo pacman -S ethtool
sudo ethtool -s eth0 wol g

# 持久化配置
# /etc/systemd/network/50-wired.link
[Match]
MACAddress=aa:bb:cc:dd:ee:ff

[Link]
NamePolicy=kernel database onboard slot path
MACAddressPolicy=persistent
WakeOnLan=magic

或使用 wol service。

1
2
3
4
# 安装
yay -Sy wol-systemd
# enable
sudo systemctl enable wol@eno1.service # eno1 为网络接口