Skip to content


k8s_安装4_私仓harbor

四、harbor仓库

k8s经常使用的镜像地址

从 Kubernetes 1.25 开始,我们的容器镜像注册中心已经从 k8s.gcr.io 更改为 registry.k8s.io .
registry.aliyuncs.com/google_containers是定时同步kubernetes的镜像到阿里镜像仓库服务的,但只是K8S组件的镜像,阿里云镜像仓库有谷歌和RedHat的镜像,但是不全。
当我们下载k8s.gcr.io,gcr.io镜像和quay.io镜像,可以把k8s.gcr.io,gcr.io, quay.io镜像换成阿里云或其它国内镜像加速地址下,如下所示:

k8s中相关镜像

#k8s.io  不能访问
registry.k8s.io/kube-apiserver:v1.28.2
registry.k8s.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2

docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.28.2
docker pull registry.k8s.io/dns/k8s-dns-node-cache:1.22.28
docker pull k8s.mirror.nju.edu.cn/dns/k8s-dns-node-cache:1.22.28

# gcr.io
gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.0
docker pull m.daocloud.io/gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.0
docker pull m.daocloud.io/gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.2

# k8s.gcr.io
k8s.gcr.io/pause:3.2
registry.aliyuncs.com/google_containers/pause:3.2

#docker.io
docker.io/flannel/flannel-cni-plugin:v1.2.0
https://registry.cn-hangzhou.aliyuncs.com

#quay.io 可以下载
quay.io/external_storage/nfs-client-provisioner:latest
docker pull quay.nju.edu.cn/jetstack/cert-manager-webhook:v1.13.1

#docker.elastic.co 可以下载,有时慢
docker.elastic.co/beats/filebeat:8.11.0

#没有登录,需先登录
Error response from daemon: pull access denied for registry.aliyuncs.com/google_containers/cert-manager-webhook, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
docker login registry.cn-hangzhou.aliyuncs.com
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/cert-manager-webhook:v1.13.2

私仓

Docker比较流行使用的三种私有仓库
Docker Registry,Nexus,Harbor

Docker Registry

通过使用 Docker Compose,我们可以轻松地在本地部署 Docker Registry.
Harbor完全是在Registry上的封装,目前比Registry功能主要的强化在于:

  • 提供UI界面
  • 提供基于角色管理的用户权限
  • 提供用户操作记录审计确认
  • 提供镜像
  • 提供对Helm Chart等的支持

Nexus

Java 开发中的那个 Maven 私服,对对对,就是这个 Nexus。Nexus它也可以应用于 docker 仓库。

优势

docker就可以安装,可以代理其它仓库并缓存到本地。
nexus3覆盖更全面,啥都可以做,是一个混合仓库maven、yum、npm的私服

repositories说明:(参考maven已有repositories介绍,因为刚开始进来maven相关的库是已经存在的)

  • maven-public:它的类型是group(分组)。即:仓库所有的访问读取入口,都是从 public 开始,读取会分别从 snapshots、releases、central 中都去找,只要其中一个找到就读取回来),就是本地仓库找不到,就去配置的网络仓库中去找【网络仓库地址,会在docker-public中配置阿里云加速镜像地址】
  • maven-releases 发布后的jar包,放到release中
  • maven-snapshots 测试的jar包,放到snapshots中
  • maven-central 它的类型是proxy(代理)。代理不存数据,是只读的。这个类似配置maven仓库时我们配置的一个aliyun仓库,帮我们去代理到aliyun仓库

nexus中有个默认自带的仓库列表,里面包含了各种各样的仓库。
这些仓库主要分为三类,代理仓库、宿主仓库和仓库组。

  • 代理仓库:代理仓库主要是让使用者通过代理仓库来间接访问外部的第三方远程仓库的。代理仓库会从被代理的仓库中下载构件,缓存在代理仓库中以供maven用户使用。

  • 宿主仓库: 宿主仓库主要是给我们自己用的,主要有2点作用:
        将私有的一些构件通过nexus中网页的方式上传到宿主仓库中给其他同事使用
        将自己开发好的一些构件发布到nexus的宿主仓库中以供其他同事使用。

  • 仓库组:(默认maven-public)仓库组中可以有多个代理仓库和宿主仓库,而maven用户只用访问一个仓库组就可以间接地访问这个组内所有的仓库,仓库组中多个仓库是有顺序的,当maven用户从仓库组下载构件时,仓库组会按顺序依次在组内的仓库中查找组件,查找到了立即返回给本地仓库,所以一般情况我们会将速度快的放在前面。仓库组内部实际上是没有构件内容的,他只是起到一个请求转发的作用,将maven用户下载构件的请求转发给组内的其他仓库处理。

Harbor 私仓

它是Docker Registry的更高级封装,它除了提供友好的Web UI界面,角色和用户权限管理,用户操作审计等功能外,它还整合了K8s的插件(Add-ons)仓库,即Helm通过chart方式下载,管理,安装K8s插件,而chartmuseum可以提供存储chart数据的仓库【注:helm就相当于k8s的yum】。另外它还整合了两个开源的安全组件,一个是Notary,另一个是Clair,Notary类似于私有CA中心,而Clair则是容器安全扫描工具,它通过各大厂商提供的CVE漏洞库来获取最新漏洞信息,并扫描用户上传的容器是否存在已知的漏洞信息,这两个安全功能对于企业级私有仓库来说是非常具有意义的。
harbor 的优势很明显,特别是可以自建文件夹进行分组这点就非常好。其实说实话,作为一个私有的镜像仓库,harbor 已经做得很好的了,唯一的缺点是它无法帮你下载镜像。
相比Nexus要费资源。使用 Harbor 必须要先安装 docker 以及 docker-compose。

安装参考:
https://agou-ops.cn/post/containerdharbor%E7%A7%81%E6%9C%89%E4%BB%93https/

Harbor相关地址

官网:​ ​https://goharbor.io/​​

Github地址:​ ​https://github.com/goharbor/harbor​​

操作文档:​ ​https://goharbor.io/docs/

Harbor安装有多种方式:

在线安装:从Docker Hub下载Harbor相关镜像,因此安装软件包非常小
离线安装:安装包包含部署的相关镜像,因此安装包比较大

只在仓库机器上执行
Harbor安装前提条件
Harbor被部署为几个Docker容器。因此,您可以在任何支持Docker的Linux发行版上部署它。目标主机需要安装Docker,并安装Docker Compose。
安装Harbor前,需要安装Docker engine,Docker Compose和Openssl。

私有库默认是不支持删除镜像的,需要修改config.yml配置文件,在storage节点下加入 delete: enabled: true
删tag不会回收空间

Harbor 高可用方式

http://t.csdnimg.cn/k43Ao

  1. 安装两台 Harbor 仓库,他们共同使用一个存储(一般常见的便是 NFS 共享存储)需要额外配置 Redis 和 PostgreSQL 以及 NFS 服务
  2. 安装两台 Harbor 仓库,并互相配置同步关系。

1.docker环境的安装

docker-ce安装

首先先把服务停止了,不要直接卸载

systemctl stop docker

重命名数据目录

把默认的docker目录改一下名称。

mv /var/lib/docker /var/lib/docker-bak

卸载旧版本

Docker 的旧版本被称为 docker 或 docker-engine。如果这些已安装,请卸载它们以及关联 的依赖关系。

yum remove docker docker-common docker-selinux docker-engine -y

安装docker-ce

## 阿里云源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce

docker version
Client: Docker Engine – Community
Version: 26.1.0
API version: 1.45
Go version: go1.21.9
Git commit: 9714adc
Built: Mon Apr 22 17:09:57 2024
OS/Arch: linux/amd64
Context: default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://docker.nju.edu.cn/"],
"insecure-registries":["repo.k8s.local"],
"exec-opts":["native.cgroupdriver=systemd"],
"data-root": "/var/lib/docker",
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 5,
"log-driver":"json-file",
"log-opts": {
"max-size": "300m",
"max-file": "2"
},
"experimental": true,
"live-restore": true
}
EOF

报错
Failed to chown socket at step GROUP: No such process
可能是文件被锁定,用户组没添加成功

lsattr /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/services
----i----------- /etc/passwd
----i----------- /etc/shadow
----i----------- /etc/group
----i----------- /etc/gshadow
----i----------- /etc/services
chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/services
groupadd docker

恢复数据目录

安装完docker-ce后,系统会创建新的docker目录,删除新的,如何把备份的改回docker名称。
切记:不要启动docker!!

rm -rf /var/lib/docker
mv /var/lib/docker-bak /var/lib/docker

启动docker服务

systemctl enable containerd

docker -v
Docker version 26.1.0, build 9714adc

ctr version
Client:
  Version:  1.6.31
  Revision: e377cd56a71523140ca6ae87e30244719194a521
  Go version: go1.21.9

Server:
  Version:  1.6.31
  Revision: e377cd56a71523140ca6ae87e30244719194a521
  UUID: 07d08019-3523-4f01-90db-367b21874598

cat /etc/containerd/config.toml

报错1
Error response from daemon: Unknown runtime specified docker-runc
需要针对容器里面的docker-runc改一下名称,用runc替换docker-runc。
grep -rl ‘docker-runc’ /var/lib/docker/containers/ | xargs sed -i ‘s/docker-runc/runc/g’
systemctl restart docker

报错1
level=error msg="failed to initialize a tracing processor \"otlp\"" error="no OpenTelemetry endpoint: skip plugin"
使用
systemctl start containerd
不要用
systemctl start docker

2.安装docker-compose:

https://github.com/docker/compose/releases
安装方式

  • 二进制安装docker-compose
  • yum方式安装docker-compose
  • python3+pip 安装

二进制方式安装docker-compose:

简单快速

wget https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64
wget https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-linux-x86_64
sudo cp -arf docker-compose-linux-x86_64 /usr/bin/docker-compose
sudo chmod +x /usr/bin/docker-compose
ln -s /usr/bin/docker-compose /usr/local/bin/docker-compose

卸载
sudo rm /usr/bin/docker-compose

yum方式安装docker-compose:

安装复杂

相关包安装

yum -y install libjpeg zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install -y libffi-devel zlib1g-dev

openssl升级

centos7默认的openssl版本和python3.10以上的版本不兼容
openssl version

OpenSSL 1.0.2k-fips  26 Jan 2017
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz
tar -zxvf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/openssl 
make -j && make install

ln -s /usr/local/openssl/lib/libcrypto.so.1.1  /usr/lib64/libcrypto.so.1.1
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
/usr/local/openssl/bin/openssl version
OpenSSL 1.1.1q  5 Jul 2022

备份原来的openssl文件,可通过whereis openssl查询位置

mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old

用新的文件替换旧的文件,执行命令如下:

ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl

openssl version
OpenSSL 1.1.1q  5 Jul 2022
python3+pip 安装
yum install epel-release 
yum install -y python3 python3-pip python3-devel

pip3 –version

pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
pip 国内镜像加速
mkdir ~/.pip/
vi ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com         
disable-pip-version-check = true        
timeout = 120
pip升级
pip3 install --upgrade pip --trusted-host mirrors.aliyun.com

pip3 –version
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly.
pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

docker-compose 安装
pip3 install docker-compose --trusted-host mirrors.aliyun.com

ln -s /usr/local/python3/bin/docker-compose /usr/bin/docker-compose 

docker-compose version
/usr/local/lib/python3.6/site-packages/paramiko/transport.py:32: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.hazmat.backends import default_backend
docker-compose version 1.29.2, build unknown
docker-py version: 5.0.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017
docker-compose 卸载

pip uninstall docker-compose

安装错误

setuptools-rust

File "/tmp/pip-build-6plwm63o/bcrypt/setup.py", line 11, in <module>
        from setuptools_rust import RustExtension
    ModuleNotFoundError: No module named 'setuptools_rust'
pip3 install setuptools-rust
The wheel package is not available.
pip3 install wheel

cffi

Could not find a version that satisfies the requirement cffi>=1.4.1 (from versions: )
pip3 install cffi
Successfully installed cffi-1.15.1 pycparser-2.21

Rust

his package requires Rust >=1.48.0.
pip3 install Rust

zlib,bzip

configure: error: zlib development files not found
yum install zlib zlib-devel
yum install bzip2 bzip2-devel bzip2-libs

libjpeg

The headers or library files could not be found for jpeg,
    a required dependency when compiling Pillow from source.

yum install libjpeg zlib libtiff

3.harbor 安装

准备文件

wget https://github.com/goharbor/harbor/releases/download/v2.8.4/harbor-offline-installer-v2.8.4.tgz
tar xf harbor-offline-installer-v2.8.4.tgz
mv harbor /usr/local/
cd /usr/local/
cd harbor
cp harbor.yml.tmpl harbor.yml

设定访问域名,端口,admin密码,db密码,存储目录

vi harbor.yml
hostname: repo.k8s.local
http:
    port:5100
#https:
  # https port for harbor, default is 443
  #port: 443
  # The path of cert and key files for nginx
  #certificate: /your/certificate/path
  #private_key: /your/private/key/path

harbor_admin_password: Harbor12345

database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123

# The default data volume
data_volume: /data_harbor

初始化

其实这个prepare的作用是用来初始化将harbor.yml转化为/usr/local/harbor/docker-compose.yml文件

./prepare

prepare base dir is set to /usr/local/harbor
Unable to find image 'goharbor/prepare:v2.8.4' locally
v2.8.4: Pulling from goharbor/prepare
b73ab88bdeef: Pull complete 
0a4647ff4f26: Pull complete 
ac87c0a6beec: Pull complete 
58290933e402: Pull complete 
2dd75ae2b8d6: Pull complete 
0432d14b35c2: Pull complete 
1d94d426c05b: Pull complete 
9242105872e7: Pull complete 
7079d6fb028f: Pull complete 
e3e737964616: Pull complete 
Digest: sha256:80837b160b3a876748c4abb68b35389485b4ddcd5de39bb82a7541d3f3051cae
Status: Downloaded newer image for goharbor/prepare:v2.8.4
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir

开始安装

./install.sh
[Step 0]: checking if docker is installed ...

Note: docker version: 24.0.6

[Step 1]: checking docker-compose is installed ...

Note: Docker Compose version v2.21.0

[Step 2]: loading Harbor images ...
a074a02dfff1: Loading layer [==================================================>]  37.79MB/37.79MB
a1845a3d89a2: Loading layer [==================================================>]  9.188MB/9.188MB
3f06bc32288c: Loading layer [==================================================>]  3.584kB/3.584kB
245244bd15d4: Loading layer [==================================================>]   2.56kB/2.56kB
42ca8ea5af72: Loading layer [==================================================>]  47.58MB/47.58MB
8d1a6771e613: Loading layer [==================================================>]  48.37MB/48.37MB
Loaded image: goharbor/harbor-jobservice:v2.8.4
9e404a035c29: Loading layer [==================================================>]  84.62MB/84.62MB
8a45a3e2d467: Loading layer [==================================================>]  3.072kB/3.072kB
50103680c597: Loading layer [==================================================>]   59.9kB/59.9kB
7da34aa8a12d: Loading layer [==================================================>]  61.95kB/61.95kB
Loaded image: goharbor/redis-photon:v2.8.4
5d6d0147b133: Loading layer [==================================================>]  89.19MB/89.19MB
f7f30f0432f2: Loading layer [==================================================>]  3.584kB/3.584kB
b895ffa154de: Loading layer [==================================================>]  3.072kB/3.072kB
9fb8c7a01498: Loading layer [==================================================>]   2.56kB/2.56kB
8a232dc48045: Loading layer [==================================================>]  3.072kB/3.072kB
839e0de14204: Loading layer [==================================================>]  3.584kB/3.584kB
3f683bb644b2: Loading layer [==================================================>]  20.48kB/20.48kB
Loaded image: goharbor/harbor-log:v2.8.4
627fc8f29b12: Loading layer [==================================================>]  115.9MB/115.9MB
b4faf8a74f36: Loading layer [==================================================>]   25.2MB/25.2MB
22c2b4c49c70: Loading layer [==================================================>]   5.12kB/5.12kB
98c144348806: Loading layer [==================================================>]  6.144kB/6.144kB
6f34146f1977: Loading layer [==================================================>]  3.072kB/3.072kB
8dd9b9af7425: Loading layer [==================================================>]  2.048kB/2.048kB
04498149158d: Loading layer [==================================================>]   2.56kB/2.56kB
7600d3f327f6: Loading layer [==================================================>]   2.56kB/2.56kB
e30935897ec8: Loading layer [==================================================>]   2.56kB/2.56kB
b91c1501abe9: Loading layer [==================================================>]  9.728kB/9.728kB
Loaded image: goharbor/harbor-db:v2.8.4
736147cbb70a: Loading layer [==================================================>]  81.13MB/81.13MB
Loaded image: goharbor/nginx-photon:v2.8.4
3ee113d617fa: Loading layer [==================================================>]  72.75MB/72.75MB
8f8c635f3d64: Loading layer [==================================================>]  38.64MB/38.64MB
50ede47ef7b6: Loading layer [==================================================>]  19.94MB/19.94MB
bbe4550fbed9: Loading layer [==================================================>]  65.54kB/65.54kB
6a6c08954476: Loading layer [==================================================>]   2.56kB/2.56kB
4fcee09b3045: Loading layer [==================================================>]  1.536kB/1.536kB
cd9e13a0fadf: Loading layer [==================================================>]  12.29kB/12.29kB
5c4cf244ed4a: Loading layer [==================================================>]  2.123MB/2.123MB
2f207d2f7a63: Loading layer [==================================================>]  419.8kB/419.8kB
Loaded image: goharbor/prepare:v2.8.4
e4e75b52265a: Loading layer [==================================================>]  9.188MB/9.188MB
6ca0b8687881: Loading layer [==================================================>]  3.584kB/3.584kB
2efe438491fa: Loading layer [==================================================>]   2.56kB/2.56kB
6c8c2dc9cf24: Loading layer [==================================================>]  59.31MB/59.31MB
70aa7368b062: Loading layer [==================================================>]  5.632kB/5.632kB
1ad1e6d7b7f2: Loading layer [==================================================>]  116.7kB/116.7kB
fdf3c64c43d4: Loading layer [==================================================>]  44.03kB/44.03kB
af312371ff9e: Loading layer [==================================================>]  60.26MB/60.26MB
2ef0db7a0b49: Loading layer [==================================================>]   2.56kB/2.56kB
Loaded image: goharbor/harbor-core:v2.8.4
0cbd65e4d842: Loading layer [==================================================>]  6.699MB/6.699MB
dfd5a1cf5002: Loading layer [==================================================>]  4.096kB/4.096kB
793940424064: Loading layer [==================================================>]  3.072kB/3.072kB
44888bf86da0: Loading layer [==================================================>]    196MB/196MB
561960448b05: Loading layer [==================================================>]   14.1MB/14.1MB
deb1d83b4cbd: Loading layer [==================================================>]  210.9MB/210.9MB
Loaded image: goharbor/trivy-adapter-photon:v2.8.4
dd52e9dde638: Loading layer [==================================================>]  81.13MB/81.13MB
8cfe6bb78139: Loading layer [==================================================>]    6.1MB/6.1MB
8aebde8774f2: Loading layer [==================================================>]  1.233MB/1.233MB
Loaded image: goharbor/harbor-portal:v2.8.4
e2bb7b6f858e: Loading layer [==================================================>]  6.172MB/6.172MB
b58529f5727f: Loading layer [==================================================>]  4.096kB/4.096kB
15b87640160a: Loading layer [==================================================>]  3.072kB/3.072kB
f8cc13293a41: Loading layer [==================================================>]  17.57MB/17.57MB
51175195e0e4: Loading layer [==================================================>]  18.36MB/18.36MB
Loaded image: goharbor/registry-photon:v2.8.4
8c3c80de8e46: Loading layer [==================================================>]  6.167MB/6.167MB
ba247990a26d: Loading layer [==================================================>]  9.143MB/9.143MB
78c730633955: Loading layer [==================================================>]  15.88MB/15.88MB
901f70ff7f25: Loading layer [==================================================>]  29.29MB/29.29MB
e91438791db8: Loading layer [==================================================>]  22.02kB/22.02kB
eb4f8ee41ee3: Loading layer [==================================================>]  15.88MB/15.88MB
Loaded image: goharbor/notary-server-photon:v2.8.4
d3bc6746e3a0: Loading layer [==================================================>]  6.167MB/6.167MB
e9dc9957d190: Loading layer [==================================================>]  9.143MB/9.143MB
6548f7c0890e: Loading layer [==================================================>]  14.47MB/14.47MB
8ab2eab06c9c: Loading layer [==================================================>]  29.29MB/29.29MB
475002e6da05: Loading layer [==================================================>]  22.02kB/22.02kB
70a417415ad1: Loading layer [==================================================>]  14.47MB/14.47MB
Loaded image: goharbor/notary-signer-photon:v2.8.4
bfe8ceaf89b9: Loading layer [==================================================>]  6.172MB/6.172MB
cf503352618a: Loading layer [==================================================>]  4.096kB/4.096kB
21e09698bb69: Loading layer [==================================================>]  17.57MB/17.57MB
7afc834ab33e: Loading layer [==================================================>]  3.072kB/3.072kB
14752e2b2fbf: Loading layer [==================================================>]  31.13MB/31.13MB
8daa88c089ea: Loading layer [==================================================>]  49.49MB/49.49MB
Loaded image: goharbor/harbor-registryctl:v2.8.4
4b4afa104b42: Loading layer [==================================================>]  9.188MB/9.188MB
4ef2e0c082a7: Loading layer [==================================================>]  26.03MB/26.03MB
8eb9f5ee0436: Loading layer [==================================================>]  4.608kB/4.608kB
d449c6ac0cd4: Loading layer [==================================================>]  26.82MB/26.82MB
Loaded image: goharbor/harbor-exporter:v2.8.4

[Step 3]: preparing environment ...

[Step 4]: preparing harbor configs ...
prepare base dir is set to /root/src/harbor
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir

Note: stopping existing Harbor instance ...

[Step 5]: starting Harbor ...
[+] Running 1/1

--Harbor has been installed and started successfully.----

测试

在外部机器,本机,k8s节点中修改host文件,增加指向
192.168.244.6 repo.k8s.local

vi /etc/hosts

192.168.244.6 repo.k8s.local

在浏览器访问 http://repo.k8s.local:5100/

输入初始密码登录
admin/Harbor12345

忘记密码

docker ps

f11056420a99   goharbor/harbor-core:v2.8.4          "/harbor/entrypoint.…"   3 weeks ago    Up About an hour (healthy)                                                                                        harbor-core  
f69e921a4464   goharbor/harbor-db:v2.8.4            "/docker-entrypoint.…"   3 weeks ago    Up About an hour (healthy)                                                                                        harbor-db                                                                                   

方式一 查看yaml密码

cat /usr/local/harbor/harbor.yml|grep password

harbor_admin_password: Harbor12345
  # The password for the root user of Harbor DB. Change this before any production use.
  password: local_ROOT_!!88

方式二 容器内查看

第一步、 进入容器
docker exec -it "" bash
docker exec -it "f11056420a99" bash

第二步、查看密码
env | grep HARBOR_ADMIN_PASSWORD

HARBOR_ADMIN_PASSWORD=Harbor12345

重置 admin 密码
docker exec -it "" bash
docker exec -it "f69e921a4464" bash

psql -U postgres -d registry
> select * from harbor_user;
> update harbor_user set salt='', password='' where user_id = "<admin user_id>";  
exit

重新启动Harbor私有镜像仓库后,密码就会自动重置为之前安装时配置的Harbor12345

重新启动Harbor私有镜像仓库

# docker-compose down
#./prepare 
# docker-compose up -d

docker-compose ps
no configuration file provided: not found
最常见的原因是没有在docker-compose.yml文件的路径下执行该命令。

cd /usr/local/harbor/
docker-compose ps

web登录时会一直提示用户名密码错误

如果之前清理过镜像可能把镜像删除了。

docker-compose down
./prepare 
prepare base dir is set to /usr/local/harbor
Unable to find image 'goharbor/prepare:v2.8.4' locally
docker-compose up -d

项目用户角色说明

受限访客:受限访客没有项目的完全读取权限。他们可以拉取镜像但不能推送,而且他们看不到日志或项目的其他成员。例如,你可以为来自不同组织的共享项目访问权限的用户创建受限访客。
访客:访客对指定项目具有只读权限。他们可以拉取和重新标记镜像,但不能推送。
开发者:开发者拥有项目的读写权限。
维护者:维护者拥有超越“开发者”的权限,包括扫描镜像、查看复制作业以及删除镜像和helm charts的能力。
项目管理员:创建新项目时,你将被分配给项目的“ProjectAdmin”角色。“ProjectAdmin”除了读写权限外,还有一些管理权限,如添加和删除成员、启动漏洞扫描等。

新建用户

用户管理/创建用户

k8s_user1
k8s_Uus1

k8s_pull
k8s_Pul1

项目

将项目设为公开,并将用户加入项目
library 公开
项目/成员/+用户
k8s_user1
维护者
k8s_pull
访客

项目/新建
k8s 公开
项目/成员/+用户
k8s_user1
维护者
k8s_pull
访客

项目命名规则
方式一 全地址做对应,前面增加私仓地址
docker.io/library/nginx:1.21.4
docker tag docker.io/library/nginx:1.21.4 repo.k8s.local/docker.io/library/nginx:1.21.4

方式二 忽略域名,目录对应
docker.io/library/nginx:1.21.4
docker tag docker.io/library/nginx:1.21.4 repo.k8s.local/library/nginx:1.21.4

方式三 忽略域名和目录,镜像对应
按属性放入目录,如基础服务放入google_containers,组件放library ,应用放app
repo.k8s.local/library/nginx:1.21.4
docker tag docker.io/library/nginx:1.21.4 repo.k8s.local/library/nginx:1.21.4

关闭:

cd /usr/local/harbor/

docker-compose down -v   
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml

cd /usr/local/harbor
docker-compose down -v   

开启:

cd /usr/local/harbor/
docker-compose up -d

Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-portal ... done
Creating registry      ... done
Creating registryctl   ... done
Creating harbor-db     ... done
Creating redis         ... done
Creating harbor-core   ... done
Creating nginx             ... done
Creating harbor-jobservice ... done
docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED         STATUS                   PORTS                                       NAMES
7ee76b324f96   goharbor/harbor-jobservice:v2.8.4    "/harbor/entrypoint.…"   3 minutes ago   Up 3 minutes (healthy)                                               harbor-jobservice
e1f9af0dfec1   goharbor/nginx-photon:v2.8.4         "nginx -g 'daemon of…"   3 minutes ago   Up 3 minutes (healthy)   0.0.0.0:5100->8080/tcp, :::5100->8080/tcp   nginx
55212a4181c5   goharbor/harbor-core:v2.8.4          "/harbor/entrypoint.…"   3 minutes ago   Up 3 minutes (healthy)                                               harbor-core
bfd166244ad3   goharbor/redis-photon:v2.8.4         "redis-server /etc/r…"   3 minutes ago   Up 3 minutes (healthy)                                               redis
beb5c0c77832   goharbor/harbor-registryctl:v2.8.4   "/home/harbor/start.…"   3 minutes ago   Up 3 minutes (healthy)                                               registryctl
17bd8d7f8a02   goharbor/harbor-db:v2.8.4            "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes (healthy)                                               harbor-db
c7c665923196   goharbor/registry-photon:v2.8.4      "/home/harbor/entryp…"   3 minutes ago   Up 3 minutes (healthy)                                               registry
a08329e11be2   goharbor/harbor-portal:v2.8.4        "nginx -g 'daemon of…"   3 minutes ago   Up 3 minutes (healthy)                                               harbor-portal
d8716d3159a0   goharbor/harbor-log:v2.8.4           "/bin/sh -c /usr/loc…"   3 minutes ago   Up 3 minutes (healthy)   127.0.0.1:1514->10514/tcp                   harbor-log

注册成服务

cat > /lib/systemd/system/harbor.service <<EOF
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor

[Service]
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/docker-compose -f /usr/local/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /usr/local/harbor/docker-compose.yml down

[Install]
WantedBy=multi-user.target
EOF

开机启动

systemctl daemon-reload 
systemctl status harbor
systemctl stop harbor
systemctl start harbor
systemctl enable harbor

防火墙

iptables -A INPUT -p tcp -m tcp --dport 5100 -j ACCEPT
iptabes-save

配置对Harbor的HTTPS访问

注意一旦启用https 那么http会强跳到https,http不再可用
要配置HTTPS,必须创建SSL证书。您可以使用由受信任的第三方CA签名的证书,也可以使用openssl进行自签名证书。
本节介绍如何使用 ​ ​OpenSSL​​​创建CA,以及如何使用CA签署服务器证书和客户端证书。您可以使用其他CA工具进行自签名
在生产环境中,一般是应该从CA获得证书,例如:在阿里云购买域名之后就可以下载相关域名的CA证书了。但是在测试或开发环境中,对于这种自己定义的内网域名,就可以自己生成自己的CA证书。

1. 生成CA证书私钥 ca.key。Generate a CA certificate private key.

openssl genrsa -out ca.key 4096

2. 根据上面生成的CA证书私钥,再来生成CA证书 ca.crt。 Generate the CA certificate.

设置 ​​-subj​​ 选项中的值来反映的组织,例如:省份、地市、域名等等信息。如果使用FQDN 【 (Fully Qualified Domain Name)全限定域名:同时带有主机名和域名的名称。】连接Harbor主机,则必须将其指定为通用名称(​​CN​​​)属性,可以看到示例写的是​​yourdomain.com​​。

openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=repo.k8s.local" \
 -key ca.key \
 -out ca.crt

 # 参数说明:
-new 指生成证书请求
-x509 表示直接输出证书
-key 指定私钥文件
-days 指定证书过期时间为3650天
-out 导出结束后证书文件
-subj 输入证书拥有者信息
生成服务器证书 Generate a Server Certificate

证书通常包含一个​​.crt​​​文件和一个​​.key​​​文件,例如​​yourdomain.com.crt​​​和​​yourdomain.com.key​​。

在这里,因为我上面设置的服务器域名为repo.k8s.local​​​,所以将要生成的证书为repo.k8s.local.crt​​​ 和 repo.k8s.local.key​​。

1.生成CA证书私钥
openssl genrsa -out harbor.key 4096
2.生成证书签名请求(CSR)

注意下使用repo.k8s.local.key 和 repo.k8s.local.csr

openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=repo.k8s.local" \
    -key harbor.key \
    -out harbor.csr
3.生成一个x509 v3扩展文件。Generate an x509 v3 extension file.
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=repo.k8s.local
DNS.2=repo.k8s
EOF
4. 使用该​​v3.ext​​​文件为您的Harbor主机生成证书 yourdomain.com.crt。
openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in harbor.csr \
    -out repo.k8s.local.crt

将服务器证书​​yourdomain.com.crt​​​和密钥​​yourdomain.com.key​​复制到Harbor主机上的certficates文件夹中。

mkdir /usr/local/harbor/certificate
cp repo.k8s.local.crt /usr/local/harbor/certificate/
cp harbor.key /usr/local/harbor/certificate/

双机时也复制到从机

scp  /usr/local/harbor/certificate/* [email protected]:.

编辑配置,打开https支持

vi  /usr/local/harbor/harbor.yml
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /usr/local/harbor/certificate/repo.k8s.local.crt 
  private_key: /usr/local/harbor/certificate/harbor.key
cd /usr/local/harbor/
./prepare 
#重新生成/usr/local/harbor/docker-compose.yml

systemctl daemon-reload 
systemctl stop harbor
systemctl start harbor
systemctl status harbor

netstat -lntp
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      31217/docker-proxy  
tcp        0      0 127.0.0.1:1514          0.0.0.0:*               LISTEN      30700/docker-proxy  
tcp        0      0 0.0.0.0:5100            0.0.0.0:*               LISTEN      31235/docker-proxy  

防火墙

iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptabes-save

http://repo.k8s.local:5100

https://repo.k8s.local

在浏览器访问,使用nat 5443指到192.168.244.6:443
https://repo.k8s.local:5443/

docker
将服务器证书​​yourdomain.com.crt​​​的编码格式转换为​​yourdomain.com.cert​​​,提供Docker使用

openssl x509 -inform PEM -in repo.k8s.local.crt -out repo.k8s.local.cert

mkdir -p /etc/docker/certs.d/repo.k8s.local/
cp repo.k8s.local.cert /etc/docker/certs.d/repo.k8s.local/
cp harbor.key /etc/docker/certs.d/repo.k8s.local/repo.k8s.local.key
cp ca.crt /etc/docker/certs.d/repo.k8s.local/

双机时也复制到从机

scp  /etc/docker/certs.d/repo.k8s.local/* [email protected]:.

测试docker登录

docker login repo.k8s.local
Username: k8s_user1
Password: k8s_Uus1

修改http为https
vi /etc/docker/daemon.json

#"insecure-registries":["repo.k8s.local:5100"],
"insecure-registries":["https://repo.k8s.local"],

错误
ctr: failed to resolve reference "xxx.local/library/docker/getting-started
需要拷贝一份上面harbor的ca到系统ca目录并更新
cp ca.crt /usr/local/share/ca-certificates/
/usr/sbin/update-ca-certificates

错误
Error response from daemon: missing client certificate harbor.cert for key harbor.key
注意 harbor.key key文件名和 cert一致

错误

Error response from daemon: Get "https://repo.k8s.local/v2/": Get "https://repo.k8s.local:5433/service/token?account=k8s_user1&client_id=docker&offline_token=true&service=harbor-registry": dial tcp 192.168.244.6:5433: connect: connection refused

harbor配制文件/usr/local/harbor/harbor.yml
修正注释掉 external_url: https://repo.k8s.local:5433

错误
Error response from daemon: Get "https://repo.k8s.local/v2/": tls: failed to verify certificate: x509: certificate signed by unknown authority

错误
Job harbor.service/start failed with result ‘dependency’.

systemctl staart harbor
systemctl status harbor

ctr测试推送

ctr -n k8s.io images ls  |grep busybox
repo.k8s.local:5100/google_containers/busybox:9.9                                                                                       application/vnd.docker.distribution.manifest.v2+json      sha256:023917ec6a886d0e8e15f28fb543515a5fcd8d938edb091e8147db4efed388ee 2.1 MiB   linux/amd64   

ctr -n k8s.io i tag  repo.k8s.local:5100/google_containers/busybox:9.9 repo.k8s.local/google_containers/busybox:9.9

ctr -n k8s.io i push --user k8s_user1:k8s_Uus1 repo.k8s.local/google_containers/busybox:9.9 
ctr: failed to do request: Head "https://repo.k8s.local/v2/google_containers/busybox/blobs/sha256:a416a98b71e224a31ee99cff8e16063554498227d2b696152a9c3e0aa65e5824": tls: failed to verify certificate: x509: certificate signed by unknown authority

#解决办法1.指定 -k 参数跳过证书校验。
ctr -n k8s.io i push --user k8s_user1:k8s_Uus1 -k repo.k8s.local/google_containers/busybox:9.9 

# 解决办法2.指定CA证书、Harbor 相关证书文件路径。
#将harbor的ca.crt 复制到当前节点
scp /root/src/harbor/ca.crt [email protected]:.
ctr -n k8s.io i push --user k8s_user1:k8s_Uus1 --tlscacert ca.crt repo.k8s.local/google_containers/busybox:9.9 

准备测试创建pod文件
vi test-harbor.yaml

# test-harbor.yaml
apiVersion: v1
kind: Pod
metadata:
  name: harbor-registry-test
spec:
  containers:
  - name: test
    image: repo.k8s.local/google_containers/busybox:9.9 
    args:
    - sleep
    - "3600"
  imagePullSecrets:
  - name: harbor-auth

创建pod
kubectl apply -f test-harbor.yaml

pod/harbor-registry-test created

查看
kubectl describe pod harbor-registry-test

Name:             harbor-registry-test
Namespace:        default
Priority:         0
Service Account:  default
Node:             node01.k8s.local/192.168.244.5
Start Time:       Mon, 16 Oct 2023 17:32:15 +0800
Labels:           
Annotations:      
Status:           Running
IP:               10.244.1.4
IPs:
  IP:  10.244.1.4
Containers:
  test:
    Container ID:  containerd://5fa473b370c56d71f0466e14960a974ead4559106495a79eb9b2a21a7ecee52f
    Image:         repo.k8s.local/google_containers/busybox:9.9
    Image ID:      repo.k8s.local/google_containers/busybox@sha256:023917ec6a886d0e8e15f28fb543515a5fcd8d938edb091e8147db4efed388ee
    Port:          

kubectl create secret docker-registry harbor-auth –docker-server=https://repo.k8s.local –docker-username=k8s_pull –docker-password=k8s_Pul1 –[email protected] -n default

k8s节点配置

containerd 配置私有仓库
Containerd 目前没有直接配置镜像加速的功能,但 containerd 中可以修改 docker.io 对应的 endpoint,所以可以通过修改 endpoint 来实现镜像加速下载。因为 endpoint 是轮询访问,所以可以给 docker.io 配置多个仓库地址来实现 加速地址+默认仓库地址

注意:这个配置文件是给crictl和kubelet使用,ctr是不可以用这个配置文件的,ctr 不使用 CRI,因此它不读取 plugins."io.containerd.grpc.v1.cri"配置。
直接修改:/etc/containerd/config.toml配置文件,这种方式在较新版本的contaienrd中已经被废弃,将来肯定会被移除,
两者取一
https://github.com/containerd/cri/blob/master/docs/registry.md
[plugins."io.containerd.grpc.v1.cri".registry.configs."repo.k8s.local".tls]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
和 config_path 会冲突

方式一 registry.mirrors
vi /etc/containerd/config.toml

[plugins."io.containerd.grpc.v1.cri".registry.configs]
      [plugins."io.containerd.grpc.v1.cri".registry.configs."repo.k8s.local".tls]
        insecure_skip_verify = true
      [plugins."io.containerd.grpc.v1.cri".registry.configs."repo.k8s.local".auth]
        username = "k8s_pull"
        password = "k8s_Pul1"

[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors."repo.k8s.local"]
        endpoint = ["http://repo.k8s.local:5100"]
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
        endpoint = ["https://registry.cn-hangzhou.aliyuncs.com"]

保存并关闭配置文件。

方式二 config_path
vi /etc/containerd/config.toml

[plugins."io.containerd.grpc.v1.cri".registry]
   config_path = "/etc/containerd/certs.d"
      [plugins."io.containerd.grpc.v1.cri".registry.configs."repo.k8s.local".auth]
        username = "k8s_pull"
        password = "k8s_Pul1"
#注意创建的目录即为configs后面填写的内容
mkdir -p /etc/containerd/certs.d/docker.io/

cat > /etc/containerd/certs.d/docker.io/hosts.toml <
http/https两者取一配制

mkdir -p /etc/containerd/certs.d/repo.k8s.local

#harbor私仓使用http方式
cat > /etc/containerd/certs.d/repo.k8s.local/hosts.toml <
#harbor私仓使用http 非常规端口方式
cat > /etc/containerd/certs.d/repo.k8s.local:5100/hosts.toml <

从harbor复制证书到节点
scp [email protected]:/etc/docker/certs.d/repo.k8s.local/* /etc/containerd/certs.d/repo.k8s.local/

#harbor私仓使用https方式 带证书
cat > /etc/containerd/certs.d/repo.k8s.local/hosts.toml <
#harbor私仓使用https方式
cat > /etc/containerd/certs.d/repo.k8s.local/hosts.toml <

重新启动containerd服务以使更改生效。可以使用以下命令之一:
使用systemd:

systemctl daemon-reload
systemctl restart containerd
systemctl status containerd

使用Docker Compose(如果您是使用Docker Compose运行的):
docker-compose restart

现在,containerd将使用您配置的私有Harbor源作为容器镜像的默认来源。您可以验证配置是否生效,通过拉取和运行一个位于Harbor Registry上的镜像来测试。

在harbor推送证书到节点
从harbor复制证书到节点
scp [email protected]:/etc/docker/certs.d/repo.k8s.local/* /etc/containerd/certs.d/repo.k8s.local/

节点上测试
ctr -n k8s.io i pull --user k8s_user1:k8s_Uus1 --tlscacert /etc/containerd/certs.d/repo.k8s.local/ca.crt repo.k8s.local/google_containers/busybox:9.9
ctr -n k8s.io images ls

ctr -n k8s.io i pull --user k8s_user1:k8s_Uus1 -k repo.k8s.local/google_containers/busybox:9.9

https 私库

ctr -n k8s.io i push --user k8s_user1:k8s_Uus1 -k repo.k8s.local/google_containers/busybox:9.9

推送到http

ctr -n k8s.io i push --user k8s_user1:k8s_Uus1 --plain-http repo.k8s.local/google_containers/busybox:9.9

ctr image pull --platform linux/amd64 docker.io/library/nginx:1.18.0

jq安装

jq安装
https://pkgs.org/centos-7/epel-x86_64/jq-1.5-1.el7.x86_64.rpm.html

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum install jq
  Installing : oniguruma-6.8.2-2.el7.x86_64                                                                                    1/2 
  Installing : jq-1.6-2.el7.x86_64                                                                                             2/2

卸载

卸载harbor步骤:(针对脚本一键安装)

docker-compose down   #停止 docker-compose 
rm -rf /data_harbor *    #删除挂载的harbor持久化数据的目录内容
rm -rf /opt/harbor   #删除harbor目录
docker-compose up -d   #重启

配置Harbor的高可用(双组复制)

新建用户
admin_sync/sync_9OK

新建目标(仓库管理-新建):
目标名:s2
url:https://10.100.5.6
用户名:admin
密码:Harbor12345
远程验证:x
连接测试:√

新建规则:
登陆S1的Harbor管理页面,进入kubernetes项目
点击“复制”,新建规则
名称:cp_to_s2
目标:https://10.100.5.6
触发模式:即刻

在S2的Harbor做相同配置

harbor-连接其他仓库报错
pkg/reg/adapter/native/adapter.go:126]: failed to ping registry

docker exec -it -u root docker ps |awk '/core/{print $1}' /bin/bash
echo "192.168.244.6    repo.k8s.local">>/etc/hosts
curl http://repo.k8s.local:5100

cat > /etc/hosts <

两个harbor配制相同主机会同步失败,先将备机配成ip
vi /usr/local/harbor/harbor.yml

hostname: 10.100.5.6

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 5100
systemctl stop harbor
cd /usr/local/harbor/
./prepare 
systemctl start harbor

harbor从机需要重新分配用户及项目的权限

各项目添加 k8s_pull 为访客
各项目添加 k8s_user1 为开发者

Posted in 安装k8s/kubernetes.

Tagged with , , .


k8s_安装3_容器

三、容器环境操作

容器选择
安装Docker或Containered

Kubernetes 默认容器运行时(CRI)为 Docker,所以需要先在各个节点中安装 Docker。另可选安装containerd
从kubernetes 1.24开始,dockershim已经从kubelet中移除,但因为历史问题docker却不支持kubernetes主推的CRI(容器运行时接口)标准,所以docker不能再作为kubernetes的容器运行时了,即从kubernetesv1.24开始不再使用docker了。

但是如果想继续使用docker的话,可以在kubelet和docker之间加上一个中间层cri-docker。cri-docker是一个支持CRI标准的shim(垫片)。一头通过CRI跟kubelet交互,另一头跟docker api交互,从而间接的实现了kubernetes以docker作为容器运行时。但是这种架构缺点也很明显,调用链更长,效率更低。
在安装Docker前需要确保操作系统内核版本为 3.10以上,因此需要CentOS7 ,CentOS7内核版本为3.10.

推荐使用containerd作为kubernetes的容器运行。

containerd配置起来比较麻烦
拉取镜像时需ctr ctrctl或安装nerdctl,推送镜像不方便
下载镜像的时候增加–all-platforms参数,譬如:ctr i pull –all-platforms,否则推送时出错,而加上–all-platforms太费带宽和空间,本来几秒的事,搞了几分钟,600多M
推送镜像的时候增加 用户和密码和–plain-http ctr i push –plain-http=true -u admin:xxxxxx ,不想-u user:password 每次必须使用 ctr pull/ctr push, 可以使用nerdctl
nerdctl 构建的机制和 docker 是完全不同的。
docker 首先会检查本地是否有 Dockerfile 中 FROM 的镜像。如果有,直接使用。没有则通过网络下载镜像;
nerdctl 会根据 Dockerfile FROM参数指定镜像的域名去网上找这个镜像,找到后确认和本地同名镜像校验无误之后,才会使用本地的镜像构建新镜像。
harbor而且要求一定要https,http不行.

方式一 安装docker

docker不支持centos6
推荐在harbor安装docker,k8s安装containered
如果之前安装过低版本docker,卸载

yum remove -y docker \
docker-client \
docker-client-latest \
docker-ce-cli \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

在master和node上操作

1 切换镜像源

方式一

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

方式二
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
   --add-repo \
   https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
yum makecache fast

2 查看当前镜像源中支持的docker版本

yum list docker-ce --showduplicates
yum list docker-ce-cli --showduplicates

3 安装特定版本的docker-ce

kubernetes各版本对应支持的docker版本列表
https://github.com/kubernetes/kubernetes/releases
kubernetes v1.28.2 支持 docker 24.0.5 ,安装docker-ce-24.0.6-1.el7

必须指定–setopt=obsoletes=0,否则yum会自动安装更高版本

yum install –setopt=obsoletes=0 docker-ce-24.0.6-1.el7 -y

Installing:
 docker-ce                                                            x86_64                                            3:24.0.6-1.el7                                                        docker-ce-stable                                             24 M
Installing for dependencies:
 audit-libs-python                                                    x86_64                                            2.8.5-4.el7                                                           base                                                         76 k
 checkpolicy                                                          x86_64                                            2.5-8.el7                                                             base                                                        295 k
 container-selinux                                                    noarch                                            2:2.119.2-1.911c772.el7_8                                             extras                                                       40 k
 containerd.io                                                        x86_64                                            1.6.24-3.1.el7                                                        docker-ce-stable                                             34 M
 docker-buildx-plugin                                                 x86_64                                            0.11.2-1.el7                                                          docker-ce-stable                                             13 M
 docker-ce-cli                                                        x86_64                                            1:24.0.6-1.el7                                                        docker-ce-stable                                             13 M
 docker-ce-rootless-extras                                            x86_64                                            24.0.6-1.el7                                                          docker-ce-stable                                            9.1 M
 docker-compose-plugin                                                x86_64                                            2.21.0-1.el7                                                          docker-ce-stable                                             13 M
 fuse-overlayfs                                                       x86_64                                            0.7.2-6.el7_8                                                         extras                                                       54 k
 fuse3-libs                                                           x86_64                                            3.6.1-4.el7                                                           extras                                                       82 k
 libcgroup                                                            x86_64                                            0.41-21.el7                                                           base                                                         66 k
 libsemanage-python                                                   x86_64                                            2.5-14.el7                                                            base                                                        113 k
 policycoreutils-python                                               x86_64                                            2.5-34.el7                                                            base                                                        457 k
 python-IPy                                                           noarch                                            0.75-6.el7                                                            base                                                         32 k
 setools-libs                                                         x86_64                                            3.3.8-4.el7                                                           base                                                        620 k
 slirp4netns                                                          x86_64                                            0.4.3-4.el7_8                                                         extras                                                       81 k

Transaction Summary
================================================================================================================================================================================================================================================================

4 添加一个配置文件

Docker在默认情况下使用的Cgroup Driver为cgroupfs,而kubernetes推荐使用systemd来代替cgroupfs;
第二行配置第三方docker仓库

mkdir - p /etc/docker 
cat > /etc/docker/daemon.json <<EOF 
{
    "registry-mirrors":["http://hub-mirror.c.163.com"],
    "exec-opts":["native.cgroupdriver=systemd"],
    "data-root": "/var/lib/docker",
    "max-concurrent-downloads": 10,
    "max-concurrent-uploads": 5,
    "log-driver":"json-file",
    "log-opts": {
        "max-size": "300m",
        "max-file": "2"
    },
    "live-restore": true
}
EOF

5 启动docker,并设置为开机自启

systemctl daemon-reload
systemctl restart docker && systemctl enable docker

6 检查docker状态和版本

docker version
Client: Docker Engine - Community
 Version:           24.0.6
 API version:       1.43
 Go version:        go1.20.7
 Git commit:        ed223bc
 Built:             Mon Sep  4 12:35:25 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.6
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.7
  Git commit:       1a79695
  Built:            Mon Sep  4 12:34:28 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.24
  GitCommit:        61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
 runc:
  Version:          1.1.9
  GitCommit:        v1.1.9-0-gccaecfc
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

systemctl start docker  # 启动docker服务
systemctl stop docker  # 停止docker服务
systemctl restart docker  # 重启docker服务

#测试
sudo docker run hello-world

在所有节点安装cri-docker

yum install -y libcgroup 

wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.4/cri-dockerd-0.3.4-3.el8.x86_64.rpm

rpm -ivh cri-dockerd-0.3.4-3.el8.x86_64.rpm

vim /usr/lib/systemd/system/cri-docker.service
----
#修改第10行内容
ExecStart=/usr/bin/cri-dockerd --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9 --container-runtime-endpoint fd://
----

systemctl start cri-docker
systemctl enable cri-docker

方式二 安装containerd

containerd 安装

wget https://github.com/containerd/containerd/releases/download/v1.7.11/cri-containerd-cni-1.7.11-linux-amd64.tar.gz
wget https://github.com/containerd/containerd/releases/download/v1.7.1/containerd-1.7.1-linux-amd64.tar.gz
tar xvf containerd-1.7.1-linux-amd64.tar.gz
mv bin/* /usr/local/bin/

tar xzf cri-containerd-cni-1.7.11-linux-amd64.tar.gz

#生成containerd 配置文件
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml

配置 containerd cgroup 驱动程序 systemd(所有节点)
当 systemd 是选定的初始化系统时,要将 systemd 设置为 cgroup 驱动
kubernets 自v1.24.0 后,就不再使用 docker.shim,替换采用 containerd 作为容器运行时端点

vi /etc/containerd/config.toml
#SystemdCgroup的值改为true
SystemdCgroup = true
#由于国内下载不到registry.k8s.io的镜像,修改sandbox_image的值为:
#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"

sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml

grep sandbox_image  /etc/containerd/config.toml
sed -i "s#registry.k8s.io/pause:3.8#registry.aliyuncs.com/google_containers/pause:3.9#g"       /etc/containerd/config.toml
grep sandbox_image  /etc/containerd/config.toml

vi /etc/containerd/config.toml

  address = "/run/containerd/containerd.sock"
    socket_path = "/var/run/nri/nri.sock"

启动containerd服务

mkdir -p /usr/local/lib/systemd/system
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
mv containerd.service /usr/lib/systemd/system/

cat /usr/lib/systemd/system/containerd.service 

cat > /usr/lib/systemd/system/containerd.service << EOF
# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
#uncomment to fallback to legacy CRI plugin implementation with podsandbox support.
#Environment="DISABLE_CRI_SANDBOXES=1"
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target
EOF

modprobe overlay

modprobe: FATAL: Module overlay not found.

lsmod | grep overlay
重新编译内核支持overlay

systemctl daemon-reload
systemctl enable --now containerd
systemctl status containerd

验证安装

ctr version
Client:
  Version:  v1.7.1
  Revision: 1677a17964311325ed1c31e2c0a3589ce6d5c30d
  Go version: go1.20.4

Server:
  Version:  v1.7.1
  Revision: 1677a17964311325ed1c31e2c0a3589ce6d5c30d
  UUID: 0a65fe08-25a6-4bda-a66f-c6f52e334e70

安装runc

安装runc

以下步骤所有节点都执行。

准备文件

wget https://github.com//opencontainers/runc/releases/download/v1.1.7/runc.amd64
chmod +x runc.amd64

查找containerd安装时已安装的runc所在的位置,如果不存在runc文件,则直接进行下一步
which runc
/usr/bin/runc
替换上一步的结果文件

cp  runc.amd64 /usr/bin/runc

验证runc安装

runc -v
runc version 1.1.7
commit: v1.1.7-0-g860f061b
spec: 1.0.2-dev
go: go1.20.3
libseccomp: 2.5.4

安装CNI插件

安装CNI插件

下载地址:https://github.com/containernetworking/plugins/releases

wget https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.3.0.tgz

安装crictl

安装 kubernetes 社区提供的 containerd 客户端工具 crictl
根据 https://www.downloadkubernetes.com/ 确定即将安装的Kubernetes版本, 本次即将安装 Kubernetes v1.28.0。 客户端工具 crictl 的版本号需和即将安装的 Kubernetes 版本号一致。
crictl 命令基本和docker一样的用法
下载地址:https://github.com/kubernetes-sigs/cri-tools/blob/master/docs/crictl.md

#wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.27.1/crictl-v1.27.1-linux-amd64.tar.gz
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.28.0/crictl-v1.28.0-linux-amd64.tar.gz
#tar -xf crictl-v1.27.1-linux-amd64.tar.gz -C /usr/local/bin
tar -xf crictl-v1.28.0-linux-amd64.tar.gz -C /usr/local/bin

# 编辑配置文件
cat >  /etc/crictl.yaml << EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 30
debug: false
pull-image-on-create: false
EOF

systemctl daemon-reload && systemctl restart containerd && systemctl status containerd

crictl --version
crictl version v1.28.0

配置Containerd运行时镜像加速器

Containerd通过在启动时指定一个配置文件夹,使后续所有镜像仓库相关的配置都可以在里面热加载,无需重启Containerd。
在/etc/containerd/config.toml配置文件中插入如下config_path:

config_path = "/etc/containerd/certs.d"

说明
/etc/containerd/config.toml非默认路径,您可以根据实际使用情况进行调整。

若已有plugins."io.containerd.grpc.v1.cri".registry,则在下面添加一行,注意要有Indent。若没有,则可以在任意地方写入。

[plugins."io.containerd.grpc.v1.cri".registry]
  config_path = "/etc/containerd/certs.d"

之后需要检查配置文件中是否有原有mirror相关的配置,如下:

[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
    endpoint = ["https://registry-1.docker.io"]

若有原有mirror相关的配置,则需要清理。
执行systemctl restart containerd重启Containerd。
若启动失败,执行journalctl -u containerd检查为何失败,通常是配置文件仍有冲突导致,您可以依据报错做相应调整。
在步骤一中指定的config_path路径中创建docker.io/hosts.toml文件。
在文件中写入如下配置。

mkdir /etc/containerd/certs.d/docker.io -pv
cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://xxx.mirror.aliyuncs.com"]
  capabilities = ["pull", "resolve"]
EOF

systemctl restart containerd

ctr和crictl

ctr是由containerd提供的一个客户端工具。
crictl是CRI兼容的容器运行时命令接口,和containerd无关,由kubernetes提供,可以使用它来检查和调试k8s节点上的容器运行时和应用程序。

crictl pull docker.io/library/nginx
crictl images
crictl rmi docker.io/library/nginx

ctr image pull docker.io/library/nginx:alpine
ctr image ls
ctr image check
ctr image tag docker.io/library/nginx:alpine harbor.k8s.local/course/nginx:alpine
ctr container create docker.io/library/nginx:alpine nginx
ctr container ls
ctr container info nginx
ctr container rm nginx

Posted in 安装k8s/kubernetes.

Tagged with , , , , , .


k8s_安装2_基础准备

二、k8s 集群搭建基础准备

k8s安装方式

Kubeadm,二进制包,minikube,k3s,k3d,kind,MicroK8s等等

minikube

master和worker在一起,测试用
采用生成虚拟机的方法,该虚拟机本质上是一个单节点 K8s 集群

k3s

适合单机测试脚本安装
curl -sfL https://get.k3s.io | sh –

k3d

顾名思义就是 docker 上的 k3s,轻松地在 docker 容器中运行高度可用的轻量级 k3s 集群

MicroK8s

适合离线开发、原型开发和测试,适合开发 IoT 应用,通过 MicroK8s 部署应用到小型Linux设备上。

kind

kind 即 Kubernetes In Docker,顾名思义,就是将 k8s 所需要的所有组件,全部部署在一个docker容器中,是一套开箱即用的 k8s 环境搭建方案。使用 kind 搭建的集群无法在生产中使用,但是如果你只是想在本地简单的玩玩 k8s,不想占用太多的资源,那么使用 kind 是你不错的选择。

Kubeadm 方式

Kubeadm 是 k8s 的部署工具,它提供了 kubeadm init 和 kubeadm join,专用于快速部署 k8s 集群,它能通过两条指令完成一个 Kubenetes 集群的搭建。Kubeadm 部署方式的优点是降低了部署门槛,部署方式快捷且简单;但缺点是屏蔽了诸多细节,遇到问题难以排查是哪里出现了问题。

二进制包安装

过于繁琐
后面会单独配制

k8s测试环境规划

192.168.244.6 harbor.k8s.local #harbor 访问地址
192.168.244.6 repo.k8s.local #harbor
192.168.244.5 node01.k8s.local
192.168.244.7 node02.k8s.local
192.168.244.4 master01.k8s.local

确保每个节点上 MAC 地址和 product_uuid 的唯一性
你可以使用命令 ip link 或 ifconfig -a 来获取网络接口的 MAC 地址
可以使用 sudo cat /sys/class/dmi/id/product_uuid 命令对 product_uuid 校验
生成uuid可以使用uuidgen命令,或者cat一下这个节点获取:/proc/sys/kernel/random/uuid

一、基础环境配置(所有主机均要配置)

第一步:关闭防火墙

#临时关闭
systemctl stop firewalld
systemctl stop iptables
/etc/init.d/iptables stop

#永久关闭
systemctl disable firewalld
systemctl disable iptables
chkconfig iptables off

machine-id修改

在master节点和node节点都需要执行,主要是为了清除克隆机器machine-id值一样的问题
rm -f /etc/machine-id && systemd-machine-id-setup

第二步:关闭 selinux

#永久关闭
setenforce 0 && sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
#临时关闭
setenforce 0

第三步:关闭 swap

交换分区的配置。kubelet 的默认行为是在节点上检测到交换内存时无法启动。 kubelet 自 v1.22 起已开始支持交换分区。自 v1.28 起,仅针对 cgroup v2 支持交换分区; kubelet 的 NodeSwap 特性门控处于 Beta 阶段,但默认被禁用。
如果 kubelet 未被正确配置使用交换分区,则你必须禁用交换分区。 例如,sudo swapoff -a 将暂时禁用交换分区。要使此更改在重启后保持不变,请确保在如 /etc/fstab、systemd.swap 等配置文件中禁用交换分区,具体取决于你的系统如何配置。

#临时关闭
swapoff -a
#永久关闭
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab

第四步:配置 ssh 互信

# 直接一直回车就行
dnf install ssh-keygen ssh-copy-id

## 使用rsa加密,兼容低版本
ssh-keygen -t rsa -b 4096 -C "[email protected]"
## 使用ed25519+sha256加密,向后兼容更佳
ssh-keygen -t ed25519 -C 'support OpenSSH 6.5~'
## 默认方式,看系统
ssh-keygen

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_ed25519 [email protected]

#for i in 192.168.244.4 192.168.244.7 ; do ssh-copy-id $i; done

第五步:hosts文件和yum源

设置主机名称,使用命令 hostnamectl set-hostname 主机名

如果不支持DNS解析,在各个节点中添加 hosts,即节点 IP地址+节点名称;

cat >> /etc/hosts << EOF
192.168.244.6 repo.k8s.local
192.168.244.5 node01.k8s.local
192.168.244.7 node02.k8s.local
192.168.244.4 master01.k8s.local
EOF

cat >> /etc/hosts << EOF
192.168.68.1 yjs-prd-master01.k8s.sys.ba.sh
192.168.68.2 yjs-prd-node01.k8s.sys.ba.sh
192.168.68.3 yjs-prd-node02.k8s.sys.ba.sh
EOF

配置完后可以在各台主机上通过ping master/ping node1/ping node2 来测试是否可以ping通

批量配置集群内各主机的hosts

for i in {192.168.244.4,192.168.244.7};do scp /etc/hosts root@$i:/etc/;done

批量配置集群内各主机的静态主机名

#跳过前2行localhost
tail -n +3 /etc/hosts | while read hang;do server=`echo ${hang} | awk -F " " '{print $1}'` && name=`echo ${hang} | awk -F " " '{print $2}'` && echo  ${name} ;done
tail -n +3 /etc/hosts | while read hang;do server=`echo ${hang} | awk -F " " '{print $1}'` && name=`echo ${hang} | awk -F " " '{print $2}'` && ssh -n root@${server} "hostnamectl set-hostname ${name}";done

原文链接:https://blog.csdn.net/UsamaBinLaden6976498/article/details/132897456

酌情关闭集群内主机的 SElinux、 swap、 OS 防火墙,并检查其状态

for i in {192.168.244.5,192.168.244.6}; do ssh root@$i "echo -e ' ';echo -e 'ip: '; \
ip a | grep -E -w 'inet.*ens33';echo -e ' ';echo -e 'SELinux:';sestatus;echo -e ' \
';echo -e 'firewall:'systemctl status firewalld;echo -e ' ';echo -e \
'SWAP: ';swapon --show;echo -e ' '" ; done

替换源

#阿里云
cp -r /etc/yum.repos.d/ /etc/yum.repos.d_bak​
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/rocky-*.repo

#上海交通大学
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.sjtug.sjtu.edu.cn/rocky|g' \
    -i.bak \
    /etc/yum.repos.d/[Rr]ocky*.repo

#epel源
dnf config-manager --set-enabled crb
dnf install epel-release -y
# 安装 EPEL Repo
#dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
# 安装 EPEL Next Repo
#dnf install -y  https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm
sed -e 's|^metalink=|#metalink=|g' \
    -e 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|g' \
    -i.bak \
    /etc/yum.repos.d/epel*.repo
# 注意:
# Rocky Linux 中 #baseurl=https://download.example/pub
# 与 CentOS 相同,而 Alma Linux #baseurl=https://download.fedoraproject.org/pub

dnf makecache

第六步:时间同步,让各个节点(虚拟机)中的时间与本机时间保持一致。

默认集群可接受节点时间差为10s以内

yum install -y chrony
systemctl start chronyd
systemctl enable chronyd
date

### 配制内部ntp服务器
cat > /etc/chrony.conf << EOF 
pool ntp.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
logdir /var/log/chrony
EOF
systemctl restart chronyd

#使用客户端进行验证
chronyc sources -v

第七步:内核升级(可选)

当前版本centos7.9
uname -a

Linux node01.k8s.local 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

CentOS 7 上安装最新版本 kernel-ml 内核的过程:
不指定版本默认就是当前最新版本
关于内核种类:
kernel-ml——kernel-ml 中的ml是英文【 mainline stable 】的缩写,elrepo-kernel中罗列出来的是最新的稳定主线版本
kernel-lt——kernel-lt 中的lt是英文【 long term support 】的缩写,elrepo-kernel中罗列出来的长期支持版本。ML 与 LT 两种内核类型版本可以共存,但每种类型内核只能存在一个版本。

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#导入 elrepo 源的 GPG 公钥,用于验证软件包的签名。
yum install wget

#rhel7
wget http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
#rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

#rhel8
wget https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm

#rhel9
wget https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm

#安装 elrepo 源的 rpm 包,添加 elrepo 源。
rpm -Uvh elrepo-release-7.0-2.el7.elrepo.noarch.rpm

rpm -Uvh elrepo-release-8.el8.elrepo.noarch.rpm

rpm -Uvh elrepo-release-9.el9.elrepo.noarch.rpm

#查看现在elrepo里有什么版本的新内核可以用,运行
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

# 安装 最新版ML 版本
yum --enablerepo=elrepo-kernel install kernel-ml-devel kernel-ml -y
# 安装 最新版LT 版本
yum --enablerepo=elrepo-kernel install kernel-lt-devel kernel-lt -y

#如果要在本机做开发编译,还要安装内核header
yum --enablerepo=elrepo-kernel install kernel-ml-headers -y

#如果提示kernel-ml-headers与旧版本的kernel-header冲突,先把旧版本header删除
yum remove kernel-headers

卸载旧header如提示有依赖旧header的软件包,拷贝列表,先确认关联删除,在安装kernel-ml-headers后再重装这些包

#确认已安装内核版本
rpm -qa | grep kernel

设置开机从新内核启动

CentOS 6.x,比较简单:
修改grub配置文件/etc/grub.conf,改
default=1

default=0

sed -i s/saved/0/g /etc/default/grub

CentOS 7.x,复杂些:
先查看机器上安装的内核启动顺序
awk -F\’ ‘$1=="menuentry " {print i++ " : " $2}’ /etc/grub2.cfg
再把新内核的顺序号(一般是0号)设为默认启动
grub2-set-default 0

Rocky9
设置开机从新内核启动

grub2-editenv list
grub2-set-default 0
grub2-editenv list

最后使grub的新配置生效
cp /boot/grub2/grub.cfg /boot/grub2/grub.bak.cfg

grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.5.6-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-6.5.6-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.71.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.71.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-342c8e733f0b488ca5712e22fa426d55
Found initrd image: /boot/initramfs-0-rescue-342c8e733f0b488ca5712e22fa426d55.img
done

更新 grub 配置。

reboot
重启系统,以使用新的内核启动。

总结:
该命令的作用是添加 elrepo 源、安装最新的 kernel-ml 内核包,并更新 grub 引导来使用该内核,从而升级系统的 Linux 内核。

uname -a

Linux master01.k8s.local 6.5.6-1.el7.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Oct  6 16:06:35 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux

3.10版本内核 在打开net.ipv4.tcp_tw_recycle 参数时会偶有丢包,4.12 内核已经废弃此选项。

echo 0 > /proc/sys/net/ipv4/tcp_tw_recycle

vim /etc/sysctl.conf

net.ipv4.tcp_tw_recycle = 0

https://blog.csdn.net/a8138/article/details/128939738

第八步:将桥接的 IPv4 流量传递到 iptables 的链(所有节点都设置);优化内核

优化参考:https://blog.csdn.net/ver_mouth__/article/details/126120802
https://help.aliyun.com/zh/ecs/support/common-kernel-network-parameters-of-ecs-linux-instances-and-faq
后继设置的需重启一下系统,否则已启动的进程还是老的参数

#设置

cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_keepalive_intvl=30
net.ipv4.tcp_keepalive_probes=10
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0 
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
net.ipv4.ip_local_port_range= 32768 60999
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
net.ipv4.tcp_tw_reuse=0
net.ipv4.tcp_max_tw_buckets=6000
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_synack_retries=2
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.netfilter.nf_conntrack_max=2310720
#net.netfilter.nf_conntrack_tcp_timeout_established=300
#net.netfilter.nf_conntrack_buckets=655360
net.ipv4.neigh.default.gc_thresh1=1024
net.ipv4.neigh.default.gc_thresh2=4096
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv6.neigh.default.gc_thresh1=8192
net.ipv6.neigh.default.gc_thresh2=32768
net.ipv6.neigh.default.gc_thresh3=65536
net.core.netdev_max_backlog=16384

net.core.rmem_max = 16777216 
net.core.wmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 8096 
net.core.somaxconn = 32768 
fs.inotify.max_user_instances=8192 
fs.inotify.max_user_watches=524288 
fs.file-max=52706963
fs.nr_open=52706963
kernel.pid_max = 4194303
net.bridge.bridge-nf-call-arptables=1
vm.swappiness=0 
vm.overcommit_memory=1 
vm.panic_on_oom=0 
vm.max_map_count = 262144
EOF

说明

net.ipv4.tcp_tw_recycle =0 #3.10版本内核 在打开net.ipv4.tcp_tw_recycle 参数时会偶有丢包,4.12 内核已经废弃此选项。
net.ipv4.tcp_tw_reuse =0 #导致Pod健康检查异常

net.ipv4.tcp_keepalive_time=600 #此参数表示TCP发送keepalive探测消息的间隔时间(秒)
net.ipv4.tcp_keepalive_intvl=30 #tcp检查间隔时间(keepalive探测包的发送间隔)
net.ipv4.tcp_keepalive_probes=10  #tcp检查次数(如果对方不予应答,探测包的发送次数)
net.ipv6.conf.all.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv6.conf.default.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv6.conf.lo.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv4.neigh.default.gc_stale_time=120 #ARP缓存条目超时
net.ipv4.conf.all.rp_filter=0  #默认为1,系统会严格校验数据包的反向路径,可能导致丢包
net.ipv4.conf.default.rp_filter=0 #不开启源地址校验
net.ipv4.conf.default.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.conf.lo.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.conf.all.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.ip_local_port_range= 45001 65000 # 定义网络连接可用作其源(本地)端口的最小和最大端口的限制,同时适用于TCP和UDP连接。阿里用 32768 60999
net.ipv4.ip_forward=1 # 其值为0,说明禁止进行IP转发;如果是1,则说明IP转发功能已经打开。
net.ipv4.tcp_max_tw_buckets=6000 #配置服务器 TIME_WAIT 数量
net.ipv4.tcp_syncookies=1 #此参数应该设置为1,防止SYN Flood
net.ipv4.tcp_synack_retries=2 #表示回应第二个握手包(SYN+ACK包)给客户端IP后,如果收不到第三次握手包(ACK包),进行重试的次数(默认为5)
net.bridge.bridge-nf-call-ip6tables=1 # 是否在ip6tables链中过滤IPv6包
net.bridge.bridge-nf-call-iptables=1 # 二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题
net.netfilter.nf_conntrack_max=2310720 #连接跟踪表的大小,建议根据内存计算该值CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (x / 32),并满足nf_conntrack_max=4*nf_conntrack_buckets,默认262144
#net.netfilter.nf_conntrack_tcp_timeout_established=300
#net.netfilter.nf_conntrack_buckets=655360 # 哈希表大小(只读)(64位系统、8G内存默认 65536,16G翻倍,如此类推)

net.ipv6.neigh.default.gc_thresh1=8192
net.ipv6.neigh.default.gc_thresh2=32768
net.ipv6.neigh.default.gc_thresh3=65536

#gc_thresh3 是ARP回收表大小的绝对限制
#gc_thresh2 设置为等于系统的最大预期邻居条目数的值
#在这种情况下,gc_thresh3 应该设置为一个比 gc_thresh2 值高的值,例如,比 gc_thresh2 高 25%-50%,将其视为浪涌容量。
#gc_thresh1 提高到较大的值;此设置的作用是,如果表包含的条目少于 gc_thresh1,内核将永远不会删除(超时)过时的条目。

net.core.netdev_max_backlog=16384 # 每CPU网络设备积压队列长度
net.core.rmem_max = 16777216 # 所有协议类型读写的缓存区大小
net.core.wmem_max = 16777216 # 最大的TCP数据发送窗口大小
net.ipv4.tcp_max_syn_backlog = 8096 # 第一个积压队列长度
net.core.somaxconn = 32768 # 第二个积压队列长度
fs.inotify.max_user_instances=8192 # 表示每一个real user ID可创建的inotify instatnces的数量上限,默认128.
fs.inotify.max_user_watches=524288 # 同一用户同时可以添加的watch数目,默认8192。
fs.file-max=52706963 # 文件描述符的最大值
fs.nr_open=52706963 #设置最大微博号打开数
kernel.pid_max = 4194303 #最大进程数
net.bridge.bridge-nf-call-arptables=1 #是否在arptables的FORWARD中过滤网桥的ARP包
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
vm.max_map_count = 262144
#使其生效
sysctl -p
sysctl --system
sysctl -a|grep vm.max_map_count

#确保每台机器的uuid不一致,如果是克隆机器,修改网卡配置文件删除uuid那一行
cat /sys/class/dmi/id/product_uuid

#加载网桥过滤模块,没有会影响node中pod和pod通信
modprobe br_netfilter

#centos6下报错
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
modprobe bridge

# 查看网桥过滤模块是否加载成功
lsmod | grep br_netfilter

#启动加载
cat > /etc/sysconfig/modules/br_netfilter.modules << EOF
#centos6 modprobe bridge
modprobe bridge
modprobe br_netfilter
EOF
chmod 755 /etc/sysconfig/modules/br_netfilter.modules

rocky9

echo 'br_netfilter' >> /etc/modules-load.d/br_netfilter.conf
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

参考
https://imroc.cc/kubernetes/trick/deploy/set-sysctl/
https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/

# 宿主ulimit
cat /etc/security/limits.conf

cat > /etc/security/limits.d/20-nofile.conf <<EOF
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF

cat > /etc/security/limits.d/20-nproc.conf <<EOF
*    -     nproc   65535
root soft  nproc  unlimited
root hard  nproc  unlimited
EOF

echo "ulimit -HSn 65535" >> /etc/rc.local

ulimit -a 
sysctl -p

systemctl show sshd

# containerd.service
安装containerd后替换
sed -i 's/LimitNOFILE=infinity/LimitNOFILE=65535/' /usr/lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl restart containerd

# centos7 system.conf
## 替换
#sed -n 's/#DefaultLimitNOFILE=/DefaultLimitNOFILE=65335/p' /etc/systemd/system.conf
#sed -i 's/^#DefaultLimitNOFILE=/DefaultLimitNOFILE=65335/' /etc/systemd/system.conf
#rocky9
sed -n 's/#DefaultLimitNOFILE=1024/DefaultLimitNOFILE=65335/p' /etc/systemd/system.conf
sed -i 's/^#DefaultLimitNOFILE=1024/DefaultLimitNOFILE=65335/' /etc/systemd/system.conf

##手动编辑
vi /etc/systemd/system.conf
DefaultLimitNOFILE=65335

cat /etc/systemd/system.conf|grep DefaultLimitNOFILE

systemctl daemon-reexec

openssl升级

建议openssl升级到1.1.1或更高版本
centos6.10
openssl version

OpenSSL 1.0.1e-fips 11 Feb 2013

centos7
openssl version

OpenSSL 1.0.2k-fips  26 Jan 2017
2023-Sep-11 14:46:17    openssl-1.1.1w.tar.gz 
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config --prefix=/usr/local/openssl --shared
make -j 8 && make install

echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v
ldd /usr/local/openssl/bin/openssl 

mv /usr/bin/openssl /usr/bin/openssl.old
ln -sv /usr/local/openssl/bin/openssl /usr/bin/openssl
openssl version
OpenSSL 1.1.1w  11 Sep 2023

第八步:配置ipvs功能(可选)

在kubernetes中service有两种代理模型,一种是基于iptables的,一种是基于ipvs的,两者比较的话,ipvs的性能明显要高一些,但是如果要使用它,需要手动载入ipvs模块。
所有节点配置ipvs模块,在内核4.19+版本nf_conntrack_ipv4已经改为nf_conntrack, 4.18以下使用nf_conntrack_ipv4即可
在 3.10 内核版本中从iptables 改用ipvs,kube-proxy 会报如下错误

E0415 09:46:55.397466       1 proxier.go:1192] Failed to sync endpoint for service: 172.16.0.82:30080/TCP, err: parseIP Error ip=[172 16 100 7 0 0 0 0 0 0 0 0 0 0 0 0]
E0415 09:46:55.398639       1 proxier.go:1950] Failed to list IPVS destinations, error: parseIP Error ip=[172 16 100 7 0 0 0 0 0 0 0 0 0 0 0 0]

可能会出现以下问题:
ipvs 指向的后端节点不对,如果重新 svc 删除重建又是对的
当副本数量变动时,ipvs 不能立马感知到
如果直接用域名访问 svc 时,会出现解析不了的情况

如使用ipvs建议先升级系统内核

Calico 在做网络策略限制的时候要求 kubec-proxy 组件不能采用 –masquerade-all 启动

所有工作节点上均执行

# 安装ipset和ipvsadm
yum install ipset ipvsadmin ipvsadm sysstat conntrack libseccomp -y
#  添加需要加载的模块写入脚本文件 
cat > /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash 
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
#modprobe -- nf_conntrack_ipv4
EOF
# 为脚本文件添加执行权限
chmod +x /etc/sysconfig/modules/ipvs.modules
# 执行脚本文件
sh /etc/sysconfig/modules/ipvs.modules

# rocky9 安装ipset和ipvsadm
dnf install ipset ipvsadm

modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

cat >> /etc/modules-load.d/ipvs.conf <<EOF 
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
EOF

systemctl restart systemd-modules-load.service

# 查看对应的模块是否加载成功
lsmod | grep -e ip_vs -e nf_conntrack
lsmod | grep -e bridge

第九步: 添加指定id的用户和用户组

cat /etc/passwd
cat /etc/group

/usr/sbin/groupadd k8s -g 1000
/usr/sbin/useradd -g k8s k8s -s /sbin/nologin -u 1000
id k8s

/usr/sbin/groupadd website -g 500

/usr/sbin/useradd -g website www -s /sbin/nologin -u 500
#/usr/sbin/useradd -g website www -d /dev/null -s /sbin/nologin -u 500

mkdir /home/www
chown www:website -R /home/www
chmod 700 /home/www
usermod -d /home/www www

第十步:工具安装

修改各节点默认挂载为Soft方式,防止nfs异常后卡死
手动编辑
vi /etc/nfsmount.conf

Soft=True

替换

echo 'Soft=True' >> /etc/nfsmount.conf

yum install sysstat -y

安装常用工具

yum -y install wget vim net-tools nfs-utils telnet yum-utils device-mapper-persistent-data lvm2 tar curl lrzsz rsync psmisc sysstat lsof

创建宿主共享目录,filebeat,ingress,nginx,php

mkdir -p /localdata/{es,filebeat,log}
mkdir -p /localdata/es/data
mkdir -p /localdata/filebeat/{data,socket}
mkdir -p /localdata/log/{ext-ingress,int-ingress,nginx,php}
chmod -R 777 /localdata/

第十一步:重启服务器

查看以上所有配置是否生效
三台主机上均执行

systemctl status firewalld # 查看防火墙
getenforce   # 查看selinux
lsmod | grep br_netfilter    # 查看网桥过滤模块
lsmod | grep -e ip_vs -e nf_conntrack  # 查看 ipvs 模块
sysctl net.bridge.bridge-nf-call-iptables

如果都正确,表示三台主机环境初始化均成功

最后重启一下
reboot

Posted in 安装k8s/kubernetes.

Tagged with , .


k8s_安装1_规划

参考:
https://zhuanlan.zhihu.com/p/646927705
https://blog.csdn.net/qq_37419449/article/details/122157277
https://blog.csdn.net/Gong_yz/article/details/12948302

centos7.9内核选择

可以选择长期支持版kernel-lt或最新版内核kernel-ml
推荐升级到最新内核

这里升级3.10 -> 6.5.6-1.el7.elrepo.x86_64

4.0以下内核存在cgroup泄漏问题,长时间运行后无法新建pod.
5.9以下内核使用ipvs会有1S延迟.

内核是3.10.x的docker默认使用文件系统是overlay1,overlay1最多使用64层,overlay2可以使用128层,升级至4.x之后的内核就默认使用overlay2,同时也修复了cgroup的问题。

要使用完整的 Cilium 功能, 需要非常新版本的 Linux 内核. 目前官方推荐的 Linux Kernel 是 ≥ 5.10

cgroups v2自Linux内核4.5版本加入支持,但到5.8才设置为默认.RHEL/Rocky 9 默认内核为5.14,默认为v2版本。

  • Linux各发行版将cgroups v2作为默认的情况如下:
  • Container-Optimized OS(从 M97 开始)
  • Ubuntu(从 21.10 开始,推荐 22.04+)
  • Debian GNU/Linux(从 Debian 11 Bullseye 开始)
  • Fedora(从 31 开始)
  • Arch Linux(从 2021 年 4 月开始)
  • RHEL 和类似 RHEL 的发行版(从 9 开始)

查看cgroups版本及默认版本

cgroup v2可以支持容器级别的(但还没有实现)OOM而非某个进程。一个容器或Pod可以运行多个进程,以前OOM killer不考虑它们的整体性,只杀死其中的一些进程,这种方式可能导致Pod进入不一致的状态。cgroup v2接口允许我们判断特定cgroup中的进程是否相互依赖,从而确定是否应该同时关闭。

另一个使用场景是可以加强集群安全性。管理没有root权限的容器的技术称为rootless容器,其允许由受限用户运行Kubernetes节点组件(如kubelet),提高安全性,并允许非管理用户在共享机器上创建Kubernetes集群等。

最后,eBPF需要cgroup v2来启用它的所有功能,当前Cilium是一个依托eBPF技术实现cni插件的开源项目,它的一些功能需要使用cgroup v2,当启用cgroup v2时,可以替换kube-proxy。

cgroups v1

grep cgroup /proc/filesystems
nodev   cgroup
stat -fc %T /sys/fs/cgroup/
tmpfs       # tmpfs,说明默认为 cgroups v1

支持cgroup2,但默认为cgroupsv1

grep cgroup /proc/filesystems
nodev   cgroup
nodev   cgroup2
stat -fc %T /sys/fs/cgroup/
tmpfs       # tmpfs,说明默认为 cgroups v1

默认为cgroupsv2

stat -fc %T /sys/fs/cgroup/
cgroup2fs      # cgroup2fs,说明默认为 cgroups v2

Kubernetes版本选择

Kubernetes每年出一个大版本,只维护最近的三个大版本,升级不能跨多版本,需一级一级升。
官方建议每年升一次级
这里使用1.28.6

Kubernetes 安装方式

推荐使用kubeadm
单独安装需要操作二十几个证书文件,比较麻烦.
kubeadm默认根证书有效期10年,认证通信证书为1年,需重新编译kubeadm延长时间。
cluster_name不要使用域名

Kubernetes TLS 认证通信

etcd是否独立

节点大于 25 时单独布署
线上最低可以融合部署3个

k8s代理模式选择

推荐ipvs
ipvs/iptables 在集群中不超过1000个服务的时候,iptables 和 ipvs 并无太大的差别,在有5千个服务(4万条规则)时,添加一条规则耗时11分钟
https://cloud.tencent.com/developer/article/1470033?from=15425

CNI接口选择

flannel/calico/cilium

小型集群推荐flannel -> vxlan+Directrouting //直接路由,跨网段使用vxlan,同网段使用host-gw模式。
线上推荐Calico

flannel,不支持pod之间的网络隔离.更像是经典的桥接模式的扩展,容器与容器之间,主机与容器之间都能互相通信
vxlan/hostgw

calico 适用场景:k8s环境中的pod之间需要隔离,Calico 不使用隧道或 NAT 来实现转发,通过 host 上路由配置完成跨 Host 转发

Cilium 性能出色,但是比较复杂

主机名命名规划

c1g-test-master01.k8s.sys.sh

ip分配

node节点:192.168.68.0/24
pod网段:10.128.0.0/16 默认:10.244.0.0/16
service网段(集群网段):10.96.0.0/16 默认:10.96.0.0/12
service集群网段:10.96.0.0/10

阿里云Terway
vpc 192.168.128.0/20
Service CIDR:172.16.0.0/20

Terway Pod独占模式专有网络网段 虚拟交换机网段 Pod虚拟交换机网段 Service CIDR网段 最大可分配Pod地址数
192.168.0.0/16 192.168.0.0/19 192.168.32.0/19 172.21.0.0/20 8192

容器选择

docker不再支持centos6
推荐Containerd
Docker/Containerd
Podman/CRI-O
从 Kubernetes 1.20 版本之后,默认推荐使用 Containerd 作为容器运行时,而不再依赖于 Docker。
从 Kubernetes 1.24 开始,删除了对 Docker(Dockershim)的容器运行时接口(CRI)的支持

使用containerd时
管理工具ctr+crictl
nerdctl+buildkit

harbor

建议单独机器,使用docker操作镜像
需要考虑点:

  • Trivy扫描镜像?
  • 高可用?主从复制或共享存储
  • 是否修改端口
  • 是否开启https
  • 仓库命名,使用统一名称/参照官方
  • 项目命名规则,用域名做项目名
  • 用户管理ldap?

存储

不要和harbor放一起,推送大镜像时整个集群会有io压力
测试可用nfs,线上推荐cephfs

ingress选择

通常使用Kubernetes Ingress
更多功能可以推荐APISIX

ingress 运行方式

现用 DaemonSet+HostNetwork+NodePort+nodeSelector

  • Deployment or DaemonSet?
  • HostNetwork 是否开启?
  • NodePort 是否开启?
  • nodeSelector or nodeAffinity?

damonset运行应用

  • ingress
  • NodeLocalDNS
  • filebeat
  • Prometheus

dns

CoreDNS从v1.13 开始替代 kube-dns,Kubernetes 1.21 版本中,kubeadm 移除了对将 kube-dns 作为 DNS 应用的支持,v1.24只支持CoreDNS.
NodeLocal->coredns(集群内域名,自定义指向)->自有dns(业务域名指向)->公网dns.
没有NodeLocal情况下,重启coredns pod导致集群业务解析域名失败5分钟。

Metrics Server

dashboard 需要
弹性伸缩 需要

Helm是否配置

Argocd灰度发布搭配

Ingress Nginx

  • Ingress Nginx
  • Istio
  • Traefik

StatefulSet是否使用

db等需要持久化数据

service 命名

$service_name.$namespace_name.svc.$cluster_name
如有内部调用,确定后不要修改

标签命名

显示默认标签
kubectl get nodes –show-labels

kubectl label node master01.k8s.local role=master
kubectl label node node01.k8s.local role=node

pod运行用户及用户组

www:website
指定uid:gid
500:500

k8s:k8s
1000:1000


KubeSphere

容器平台中维护和管理独立部署的 Kubernetes 集群。
联邦:多个 Kubernetes 集群可以聚合在一起作为 Kubernetes 资源池。当用户部署应用程序时,副本可以部署在资源池中的不同 Kubernetes 集群上。由此,跨区域和多集群实现了高可用性。

Submariner

Submariner 是一个完全开源的项目,可以帮助我们在不同的 Kubernetes 集群之间(无论是在本地还是云端)实现网络通信

Kubernetes的网络通信问题:

  •   1. 容器间通信: 即同一个Pod内多个容器间通信,通常使用loopback来实现。
  •   2. Pod间通信: K8s要求,Pod和Pod之间通信必须使用Pod-IP 直接访问另一个Pod-IP
  •   3. Pod与Service通信: 即PodIP去访问ClusterIP,当然,clusterIP实际上是IPVS 或 iptables规则的+ 虚拟IP,是没有TCP/IP协议栈支持的。但不影响Pod访问它.
  •   4. Service与集群外部Client的通信,即K8s中Pod提供的服务必须能被互联网上的用户所访问到。

Pod 初始化核心流程如下:

  • kube-apiserver 收到客户端请求(Controller 或 kubectl 客户端)后,创建对应的 Pod;
  • kube-scheduler 按照配置的调度策略进行 Pod 调度,选择最为合适的 Node 作为目标节点;
  • kubelet(运行于每个 Node 上的 K8s agent)Watch 监听到调度到所在节点的 Pod(s),开始真正创建 Pod;
  • 由 CRI 首先创建出 PodSandbox,初始化对应的网络 net namespace,调用 CNI 获取 Pod IP;
  • 接着 CRI 开始创建 Pod 中第一个 pause container,绑定到上一步创建的 net namespace 和 Pod IP;
  • 接着由 CRI 依次创建和启动 Pod 中声明的 initContainers 和 containers 容器;
  • 当所有的 containers 运行起来后,探针探测容器运行符合预期后,Pod 状态最终更新为 Running;

关于K8S中端口的概念区分

  • 1、Port:
    是service端口,即k8s中服务之间的访问端口 ,clusterIP:port 是提供给集群内部客户访问service的入口

  • 2、NodePort:
    容器所在node节点的端口,通过nodeport类型的service暴露给集群节点,外部可以访问的端口

  • 3、TargetPort
    是pod的端口 ,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

  • 4、ContainerPort
    是pod内部容器的端口,targetPort映射到containerPort

4种端口作用不一样,port和nodePort都是service的端口。
port暴露给集群内客户访问服务,nodePort暴露给集群外客户访问服务。
这两个端口到来的数据都需要经过反向代理kube-proxy流入后端pod的targetPod,从而到达pod中的容器。

nodeport端口范围

默认情况下,k8s 集群 nodePort 分配的端口范围为:30000-32767,如果我们需要更多的端口,或者重新规划使用端口,需要进行调整.
找到kube-apiserver.yaml文件的绝对路径,其路径为"/etc/kubernetes/manifests/kube-apiserver.yaml",并添加参数"- –service-node-port-range=10000-65535"
systemctl daemon-reload
systemctl restart kubelet

K8s 中多种 IP

Pod IP CIDR

在 K8s 中最常见的 IP 类型就是 Pod IP,在初始化 K8s 集群的时候,通过–cluster-cidr参数控制 Pod IP CIDR 网段,所有 Pod 动态分配的 IP 都会落在此 CIDR 网段内。
具体参数控制如下:通过 kube-controller-manager 组件的–cluster-cidr参数进行配置,根据集群规模一般会选择 16 位的网段来配置集群支持的 Pod IP CIDR 网段,如 10.0.0.0/16,理论上最大支持 2 ^ (32 – 16) = 65536 个 Pod IP 的分配。
【集群规模】可按需配置 Pod IP CIDR,K8s 官方支持的一个大集群(large cluster),最大支持约 5k Nodes、15w Pods。

Node CIDR

在通过 kube-controller-manager 组件的–cluster-cidr控制了 Pod IP 的 CIDR 网段后,首先会在集群中每个 Node 分配一个 subnet CIDR,他们都属于–cluster-cidr网段。
具体参数控制如下:通过 kube-controller-manager 组件的–allocate-node-cidrs=true、–node-cidr-mask-size=24参数控制每个 Node 节点的 subnet CIDR 子网段,这样落在每个 Node 上的 Pod 最大的可分配 IP 数量为 2 ^ (32 – 24) = 256 个,各云厂商会根据自己的网络策略,一般会预留一部分,最终可分配的 IP 一般为最大个数的一半 (128 个)。
【双协议栈】若开启了 dual-stack IP,则可通过–node-cidr-mask-size-ipv4=24、–node-cidr-mask-size-ipv6=64分别控制 IPv4 和 IPv6 的 Node CIDR 子网大小。
在 K8s 标准集群中,通过 kubelet 组件的–max-pods=110控制了默认一个 Node 最大的 Pod 数量为 110 个。

Service IP CIDR

除了上面提到的 Pod IP CIDR 和 Node CIDR 外,K8s 中还有一类 Service IP CIDR,控制 Service 资源的ClusterIP网段范围。
具体参数控制如下:通过 kube-apiserver 和 kube-controller-manager 组件的–service-cluster-ip-range=10.96.0.0/12控制 ServiceClusterIP的网段范围。
根据 Service Type 不同,除了Headless Service显式将.spec.clusterIP=None设置后,生成的 Service 将不会分配 ClusterIP,其他类型的 Service 则都会动态分配 ClusterIP
需要注意的是,Service 动态分配的 ClusterIP 仅在 K8s 集群内部可访问,通过 K8s 内置的 DNS 进行 Service 域名解析到此 ClusterIP,转发到后端真正的 Pod(s) 时进行流量的负载均衡。
【外部访问】若需要从集群外部访问集群内服务,可通过NodePort 或 EXTERNAL-IP 进行访问,还可通过 Ingress 进行更灵活的 L7 (http/https) 流量访问控制。
k8s集群初始化时的service网段,pod网段,网络插件的网段,以及真实服务器的网段,都不能相同,如果相同就会出各种各样奇怪的问题,而且这些问题在集群做好之后是不方便改的,改会导致更多的问题,所以,就在搭建前将其规划好。

Pod IP 分配流程

CNI-IPAM 分配 IP

host-local:是最常见的使用方式,通过在宿主机 Node 上以文件读写的方式,进行 IP 分配与释放。其中 IP 分可配的 range 范围由上面 2.2 所述,通过 Controller 为每一个 Node 分配对应的 CIDR,host-local 插件将从这个 Node CIDR 中动态分配 IP 给 Pod 使用。
其它有dhcp 配置复杂度高,实际使用较少,static 是通过直接指定 IP 地址,为 Pod 分配指定的 IP,支持 IPv4/IPv6,主要用于 debug 或指定 IP 分配的场景,一般较少使用。

hostNetwork 使用 Node IP
Pod 在创建的时候,可以设置 pod.spec.hostNetwork = true/false,表示是否宿主机 Node 的网络,若 hostNetwork = true,则 podIP 就直接设为宿主机 hostIP。

Pod IP 双协议栈
K8s 官方在 v1.20 (v1.23 发布 stable) 版本支持了 IPv4/IPv6 dual-stack 双协议栈

Pod IP 固定与回收
K8s 中通过 StatefulSet 实现有状态服务的管理,其生成的 Pod 编号 (如 mysql-0, mysql-1, mysql-2) 是唯一且重建后不变。某些业务场景下,业务上层需要对应的 Pod IP 保持不变,这样在 Pod 异常后快速拉起,上层业务不需要变更 IP 就可以自动恢复。因此这种场景就需要 Pod IP 在重建前后保持一致。
在 Pod 被删除后,还可以指定 Pod IP 的保留时长,防止短时间新创建的 Pod 复用了最近被删除 Pod 的 IP,很容易给上层业务造成困扰与异常,造成业务的不稳定。因此用户或各大云厂商可通过设计与实现 Pod 的固定与定时回收机制满足有状态服务的固定 IP 需求。

【思路之一】
可设计一个 ReservedIP CRD + 对应的 CNI plugin,在创建 Pod 时记录某个 StatefulSet 下对应 namespace/podName 与 ReservedIP 的映射关系,当此 namespace/podName 删除时,仅仅记录 ReservedIP 的 TTL (如 24h)。
在 TTL 过期之前,再次创建同名的 namespace/podName,则直接用之前保留的 Pod IP。若 TTL 已过期,则由对应的 GC 逻辑去真正调用 CNI IPAM plugin,删除对应的 Pod IP。

规划一个小规模k8s集群

规划200个25节点的k8s集群

每个node 一个c类,254个ip

--node-cidr-mask-size=24
10.128.0.0/24

以Nodes数量在25以内,每个集群64个B类,62个可用,共16002 个ip
默认参数10.244.0.0/16

--cluster-cidr=18
10.128.0.0/18
10.128.64.0/18
10.128.128.0/18
10.128.192.0/18

一个集群,每个node 20个pod算,62*20=1240个pod。
简单点直接使用 –cluster-cidr=16,分配整个B类
10.128.0.0/16

整个k8s,64个C类,估算62*4=248个集群,共4194302个ip
10.128.0.0/10

ip地址网段规划:

node节点:192.168.68.0/24
pod网段:10.128.0.0/16 默认:10.244.0.0/16
service网段(集群网段):10.96.0.0/16 默认:10.96.0.0/12
service集群网段:10.96.0.0/10 
service-cidr 只在Kubernetes集群内使用,不能和PodCIDR及本机网络有重叠或者冲突

节点命名应包含集群名称,节点角色,节点名称,此为固定格式,节点名称仅支持小写,集群名称部署完成后不能修改
c1g-test-master01.k8s.sys.sh
c1g-test-master02.k8s.sys.sh

node02.k8s.local

vip 192.168.68.160
c1g-prd-master01.k8s.sys.sh 192.168.68.1    
c1g-prd-master02.k8s.sys.sh 192.168.68.2    
c1g-prd-master03.k8s.sys.sh 192.168.68.3    
c1g-prd-node01.k8s.sys.sh   192.168.68.4    
c1g-prd-node02.k8s.sys.sh   192.168.68.5    
c1g-prd-node03.k8s.sys.sh   192.168.68.6    

k8s通过CNI接口接入其他插件来实现网络通讯。目前比较流行的插件有flannel,calico等。
CNI插件存放位置:# cat /etc/cni/net.d/10-flannel.conflist

插件使用的解决方案如下:
虚拟网桥,虚拟网卡,多个容器共用一个虚拟网卡进行通信。
多路复用:MacVLAN,多个容器共用一个物理网卡进行通信。
硬件交换:SR-LOV,一个物理网卡可以虚拟出多个接口,这个性能最好。

容器间通信:同一个pod内的多个容器间的通信,通过lo即可实现;

pod之间的通信:
同一节点的pod之间通过cni网桥转发数据包。(brctl show可以查看)
不同节点的pod之间的通信需要网络插件支持。

pod和service通信: 通过iptables或ipvs实现通信,ipvs取代不了iptables,因为ipvs只能做负载均衡,而做不了nat转换。

pod和外网通信:iptables的MASQUERADE。

Service与集群外部客户端的通信;(ingress、nodeport、loadbalancer)

============
https://blog.51cto.com/u_14035463/5585073

一、k8s 集群平台规划

操作系统

CentOS
最低配置 CentOS 7.6 kernel 4.4.x 推荐版本CentOS 7.9

Ubuntu
最低配置 Ubuntu 16.04.5 4.4.0-131 推荐版本Ubuntu 18.04.5 4.15.0

用virtbox 安装centos7.9
操作系统为最小化安装,需yum源和apt源.兼容包,开发工具,管理工具。
2c4g,30G,将显示设置中的“显卡控制器”设置为vboxvga,nat网络

yum install wget  lrzsz net-tools nfs-utils curl curl-devel unzip sudo telnet rsync
yum install chrony
systemctl status chronyd
systemctl restart chronyd
systemctl enable chronyd
/bin/timedatectl set-timezone Asia/Shanghai
/bin/timedatectl set-local-rtc 1

cat >/etc/locale.conf<<EOF
LANG="en_US.UTF-8"
SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
SYSFONT="latarcyrheb-sun16"
EOF

集群架构规划

k8s 集群可以有两种规划方式,单master集群 和 多master集群,也就是一主多从和多主多从

宿主机

系统Centos7.9

单机测试环境

虚拟机和主机之间网络连接方式改为桥接模式,没有IP条件用NAT
一主二从

master:cpu:2 mem:2G hd:30G
node:cpu:2 mem:4G hd:30G及以上

线下测试环境

一主二从

master:cpu:2 mem:4G hd:30G
node:cpu:4 mem:8G hd:40G及以上

线上环境

使用物理机规格推荐
管理节点
CPU Xeon 4214 10 core 2.2GHz(以上) 最低2  推荐2
Memory 16GB ECC DDR4  RDIMM(以上)最低32G  推荐 64G+
Raid 缓存2GB NV 带电容保护,无特别要求、支持raid1  1
HD 600GB SAS 10K企业级硬盘,如etcd 融合部署,可考虑SSD 盘作为数据盘 最低1.2T 推荐2.4T以上
工作节点配置建议
CPU Xeon 4214 10 core 2.2GHz(以上) 最低2  推荐2
Memory 16GB  ECC DDR4 ?RDIMM(以上)最低64G  推荐 128G+
Raid 缓存2GB NV 带电容保护,无特别要求、支持raid1  1
HD 600GB SAS 10K企业级硬盘,如etcd 融合部署,可考虑SSD 盘作为数据盘 最低1.2T 推荐2.4T以上
使用虚拟机规格推荐
管理节点
最小
master:cpu:8 mem:16G hd:300G
推荐
master:cpu:16+ mem:64G+ hd:600G+
工作节点配置建议
node:cpu:8 mem:32G hd:600G
node:cpu:16+ mem:64G+ hd:1.2T+

集群节点命名规划

节点命名应包含集群名称,节点角色,节点名称,此为固定格式,节点名称仅支持小写,集群名称部署完成后不能修改

格式:集群名称 + 节点角色 + 节点名称
示例:
prd-blog-master01.k8s.local

存储

nfs、ceph、gluster
不推荐采用本地存储方式

k8s代理模式

ipvs/iptables
Iptables:

  • 灵活,功能强大
  • 规则遍历匹配和更新,呈线性时延

IPVS:

  • 工作在内核态,有更好的性能
  • 调度算法丰富:rr,wrr,lc,wlc,ip hash…

ipvs依赖iptables进行包过滤、SSNAT、masquared(伪装),如果没有加载并启用ipvs模块,或者没有配置ipvs相关配置,则会被降级成iptables模式。

如何查看k8s中kube-proxy的模式是ipvs还是iptables

kubectl get configmap kube-proxy -n kube-system -o yaml | grep mode
cat /var/log/kube-proxy.log | grep "Using iptables Proxier"

修改模式
kubectl edit configmap kube-proxy -n kube-system
删除节点原kube-proxy的pod,让k8s自己重建

DNS 模式

kubedns/coredns

CoreDNS:是一个DNS服务器,Kubernetes默认采用,以Pod部署在集群中, CoreDNS服务监视Kubernetes API,为每一个Service创建DNS记录用于域名解析。

NodeLocal DNSCache
解决CoreDNS 性能问题有查询产生5s的延时.

网络 CNI 选型

同一个pod内的多个容器可以互相通信,共用网络,通过lo即可实现。
同一节点的pod之间通过cni网桥转发数据包。
不同节点的pod之间的通信需要网络插件支持。
pod和service通信: 通过iptables或ipvs实现通信,ipvs取代不了iptables,因为ipvs只能做负载均衡,而做不了nat转换。
pod和外网通信使用iptables的M地址伪装功能。
Service与集群外部客户端的通信;(ingress、nodeport、loadbalancer)

目前Kubernetes支持多种CNI 网络方案,Flannel、Calico、canel、 kube-router、 Weave、Cilium。CNI 一旦选型后,后期不支持修改。
目前比较常用的时flannel和calico,flannel的功能比较简单,不具备复杂网络的配置能力,calico是比较出色的网络管理插件。
kubelet来调CNI插件时,会到 /etc/cni/net.d/目录下去找插件的配置文件,并读取它,来加载该插件,并让该网络插件来为Pod提供网络服务
默认安装Flannel
flannel vxlan和calico ipip模式都是隧道方案,但是calico的封装协议IPIP的header更小,所以性能比flannel vxlan要好一点点
flannel的host-gw模式,以及calico的bgp模式为路由方案,网络性能好。

Flannel网络插件要怎么部署

flannel适用于小型集群,每个node写静态路由
Flannel 使用虚拟网络来实现网络功能
凡是部署kubelet的节点,都需要部署flannel
1.它支持直接运行为宿主机上的一个守护进程。
3.它也支持运行为一个Pod

flannel的工作方式有3种:
默认使用vxlan
1) VxLAN:
VxLAN有两种工作方式:
a. VxLAN: 这是原生的VxLAN,即直接封装VxLAN首部,UDP首部,IP,MAC首部这种的。
b. DirectRouting: 这种是混合自适应的方式,Host-GW 和 VxLAN 自动切换

2) host-GW:
由宿主机通过查询本地路由表,来做路由转发,实现跨主机的Pod通信,这种模式带来的问题时,当k8s集群非常大时,会导致宿主机上的路由表变得非常巨大

3) UDP: 这是早期使用方式,性能最差的

在flannel的vxlan模式下,ping不在同一节点的pod,流量路径为pod(容器)->cni0->flannel.1-> 本host IP -对端host IP ->flannel.1-> cni0 ->对应的pod(容器)
flannel的host-gw模式下的不同节点上pod访问流量路径如下:
pod(容器)->cni0-> 本host IP -对应host IP -> cni0 ->对应的pod(容器)**而不会经过flannel.1网卡(正常情况应该没有这个网卡)

比较小而简单的集群使用flannel,通常使用flannel+vxlan ,开启DirectRouting
在配置flannel时,一定要注意,不要在半道上,去修改。不足是不能做pod之间的隔离
相同 Node 上的 pod 相互访问是不需要经过 Flannel 的

Calico

官方网址:https://docs.tigera.io/
Calico是一种非常复杂的网络组件,大于50节点时需要自己的etcd数据库集群,可以控制pod之间的通信,像iptables,适用于pod之间有隔离需求
Calico 使用基于路由的方法实现网络功能。每个容器都有一个唯一的 IP 地址,这些 IP 地址由网络拓扑自动分配。每个节点上都有一个 agent,它负责将路由规则下发到节点的内核。这些规则将每个容器的 IP 地址与对应的容器所在节点的 MAC 地址绑定在一起。这样,当容器之间通信时,流量将被路由到相应的节点,然后被路由到目标容器。Calico 还支持多种路由协议,例如 BGP、OSPF 和 Bird。
Calico的开发测试周期包括定期测试数千个节点集群
监听ECTD中心的存储获取事件

Calico的几种模式
  • IPIP模式:适用于互相访问的pod不在同一个网段中,跨网段访问的场景。把 IP 层封装到IP 层的一个 tunnel。作用其实基本上就相当于一个基于IP层的网桥!一般来说,普通的网桥是基于mac层的,根本不需 IP,而这个ipip 则是通过两端的路由做一个 tunnel,把两个本来不通的网络通过点对点连接起来。
  • BGP工作模式:适用于互相访问的pod在同一个网段,适用于大型网络。BGP 边界网关协议(Border Gateway Protocol, BGP):是互联网上一个核心的去中心化自治路由协议。BGP不使用传统的内部网关协议(IGP)的指标
  • Route Reflector 模式(RR)(路由反射):Calico维护的网络在默认是(Node-to-Node Mesh)全互联模式,Calico集群中的节点之间都会相互建立连接,用于路由交换。但是随着集群规模的扩大,mesh模式将形成一个巨大服务网格,连接数成倍增加。这时就需要使用 Route Reflector(路由器反射)模式解决这个问题。

Calico本身支持多种网络模式,从overlay和underlay上区分。Calico overlay 模式,一般也称Calico IPIP或VXLAN模式,不同Node间Pod使用IPIP或VXLAN隧道进行通信。Calico underlay 模式,一般也称calico BGP模式,不同Node Pod使用直接路由进行通信。在overlay和underlay都有nodetonode mesh(全网互联)和Route Reflector(路由反射器)。如果有安全组策略需要开放IPIP协议;要求Node允许BGP协议,如果有安全组策略需要开放TCP 179端口;官方推荐使用在Node小于100的集群,我们在使用的过程中已经通过IPIP模式支撑了100-200规模的集群稳定运行。

当集群规模大于50节点时,应该使用calico-typha 。用于Calico通过Typha直接与Etcd通信,以降低kube-apiserver的负载,可以和kube-apiserver使用一个etcd.
每个Pod/calico-typha可承载100~200个Calico节点的连接请求,最多不要超过200个.

在calico中,全网路由的数目和endpoints的数目一致,通过为node分配网段,可以减少路由数目,但不会改变数量级。
如果有1万个endpoints,那么就至少要有一台能够处理1万条路由的设备。
无论用哪种方式部署始终会有一台设备上存放着calico全网的路由。
当要部署calico网络的时候,第一步就是要确认,网络中处理能力最强的设备最多能设置多少条路由。

通常使用 Calico overlay 模式+IP-in-IP+CrossSubnet
在跨网段的k8s节点的pod间通信时才使用vxlan或ipip进行封包, 同一网段的8s节点的pod间通信则直接使用bgp模式。使用同网段内的k8s节点作为BGP Speaker全互联交换各自的pod子网路由实现互通, 避免封包.

50节点以下
Calico overlay 模式+IP-in-IP+CrossSubnet
50~200节点
Calico overlay 模式+IP-in-IP+CrossSubnet+typha(etcd)

Calico的BGP(RR模式)比cilium的BGP路径更短.

calico优缺点
  • calico的好处是endpoints组成的网络是单纯的三层网络,报文的流向完全通过路由规则控制,没有overlay等额外开销。

  • calico的endpoint可以漂移,并且实现了acl。

  • calico的缺点是路由的数目与容器数目相同,非常容易超过路由器、三层交换、甚至node的处理能力,从而限制了整个网络的扩张。

  • calico的每个node上会设置大量(海量)的iptables规则、路由,运维、排障难度大。

  • calico的原理决定了它不可能支持VPC,容器只能从calico设置的网段中获取ip。

  • calico目前的实现没有流量控制的功能,会出现少数容器抢占node多数带宽的情况。

  • calico的网络规模受到BGP网络规模的限制。

  • 在calico中,全网路由的数目和endpoints的数目一致,通过为node分配网段,可以减少路由数目,但不会改变数量级。

  • 如+ 果有1万个endpoints,那么就至少要有一台能够处理1万条路由的设备。

  • 无论用哪种方式部署始终会有一台设备上存放着calico全网的路由。

  • 当要部署calico网络的时候,第一步就是要确认,网络中处理能力最强的设备最多能设置多少条路由。

Canal

Canal是Flannel和Calico的组合

Cilium

Cilium 是一种基于 eBPF (Extended Berkeley Packet Filter) 技术的网络插件,它使用 Linux 内核的动态插件来提供网络功能,如路由、负载均衡、安全性和网络策略等。
要使用完整的 Cilium 功能, 需要非常新版本的 Linux 内核. 目前官方推荐的 Linux Kernel 是 ≥ 5.10.
Cilium 的网络隔离策略遵循白名单机制,在不创建网络策略的情况下,对于网络不作任何限制,在为指定类型的 pod 集合创建网络策略后,除策略中允许的访问地址外,其它请求都会被拒绝。
Cilium 推荐大于 5.2 的内核版本,从而充分利用 eBPF 的能力。Kubernetes worker 需要打开 TCP 端口 8472(VXLAN)和 TCP 端口 4240(健康检查)。此外,还必须为健康检查启用 ICMP 8/0
默认情况下,Cilium 不允许 Pod 与其他节点上的 Pod 通信。要解决此问题,请启用 Ingress Controller 以使用 “CiliumNetworkPolicy” 进行跨节点路由请求

其不但支持最基本的 pod 之间通信,甚至还支持 L4/L7 层等负载均衡,甚至把 kube-proxy 给干掉了,同时其还自带 service mesh、DNS Proxy、网络追踪等各种乱七八糟你想得到想不到的功能它都有,着实是生猛!
另外由于其是基于 ebpf 的,所以在功能上能实现跳过一些协议栈以及一些过滤阶段等,因此性能上来讲会比其他一些通俗的网络插件要好。
传统防火墙在第 3 层和第 4 层运行,而 Cilium 还能确保 REST/HTTP、gRPC 和 Kafka 等现代第 7 层应用协议的安全(除了在第 3 层和第 4 层执行外)。它能根据应用协议请求条件执行网络策略,例如
• 允许方法为 GET、路径为 /public/.* 的所有 HTTP 请求。拒绝所有其他请求。
• 要求所有 REST 调用都包含 HTTP 标头 X-Token:[0-9]+。

不过他也不是完全没缺点,其主要缺点就在于对内核的要求非常之高,因为其会用到很多 linux 内核高版本才有的功能,同时安装也挺费劲的。它的官网上提供了数十种安装方式,我反正折腾了好久好久才部署好。另外就是 cilium 由于用了很多 ebpf 的方法,导致其非常复杂,不管是代码实现还是流量走向的观察都比较复杂,不像是 flannel,看看主机路由表啥的就能把流量走向看个大概的。

如果您需要更高的性能和更细粒度的安全控制,可以选择Cilium;如果您需要更简单的网络策略和更容易上手的解决方案,则可以选择Calico

带宽: calico+ipip > flannel+vxlan > cilium+vxlan
速度: calico+ipip > flannel+vxlan > cilium+vxlan
稳定性:cilium+vxlan > calico+ipip > flannel+vxlan

如果k8s节点同在同一网段下,没有跨网段节点
50节点以下
flannel+host-gw
Calico+IPIP+crossSubnet

如果k8s节点同在同一网段下,但有少量跨网段需求
50节点以下
flannel+VxLAN+DirectRouting
Calico+IPIP(VXLAN)+crosssubnet+nodetonode mesh
50~100节点
Calico+IPIP(VXLAN)+crosssubnet+etcd+nodetonode mesh
cilium+vlan
100~200节点
Calico+IPIP(VXLAN)+crosssubnet+etcd+Route Reflector
cilium+vlan

如果有高内核,外部BGP条件,跨网段大集群需求
cilium+BGP

有网络隔离需求
Calico/cilium

3/4 层(IP 地址和端口)和第 7 层(API 协议)的隔离需求和网络eBPF流量可见性,pod固定IP需求
cilium

Ingress 选型

https://www.upyun.com/tech/article/524/1.html?utm_source=zhihu&utm_medium=wenz&utm_campaign=wf65g&utm_term=345147856&utm_content=002
Kubernetes Ingress k8s社区的ingres-nginx

K8S 默认的 Nginx ingress
  • https://github.com/kubernetes/ingress-nginx
  • Ingress-NGINX v1.9.3 k8s supported version 1.28, 1.27,1.26, 1.25 Nginx Version 1.21.6
  • 路由匹配:host,path
  • 部署策略:ab部置,金丝雀
  • upstream探测:重试,超时
  • 防护能力:limit-conn,limit-count,limit-req,ip-writelist
  • 使用Nginx config reload,reload 会很严重的影响业务
  • Nginx ingress 的插件开发非常困难
Nginx Ingress
  • ginx公司的nginx-ingress
  • ttps://github.com/nginxinc/kubernetes-ingress
  • 路由匹配:host,path
  • 部署策略:
  • pstream探测:重试,超时,心跳探测
  • 防护能力:
APISIX
  • 路由匹配:path,method,host,header,nginx变量,args变量,自定义函数
  • 部署策略:ab部置,灰度发布,金丝雀
  • upstream探测:重试,超时,心跳探测,熔断
  • 防护能力:limit-conn,limit-count,limit-req,ip-writelist
  • 优势热更新,不需reload
kong ingress
  • 路由匹配:path,method,host,header
  • 部署策略:蓝绿发布,金丝雀
  • upstream探测:心跳探测,熔断
  • 防护能力:limit-conn,limit-count,limit-req,ip-writelist,response limit
Traefik
  • 路由匹配:path,method,host,header,query,path prefix

  • 部署策略:蓝绿部置,灰度发布,金丝雀

  • upstream探测:重试,超时,心跳探测,熔断

  • 防护能力:limit-conn,limit-req,ip-writelist

  • 优势:
    Traefik 是云原生模式,配置文件极其简单,采用分布式配置,同时支持多种自动配置发现。不仅支持 K8s、etcd,Golang 的生态语言支持比较好,且开发成本较低,迭代和测试能力较好

  • 不足:
    扩展能力弱
    不支持 Reload
    性能和功能上不如 Nginx(虽然消耗也较低)

haproxy
Istio Ingress

envoy 平台

Ambassador

envoy 平台

防火墙

集群内部建议不设置防火墙。如设置防火墙可参考下表
多集群联邦模式部署,需要考虑hosts?集群可以访问member?集群的6443 端口。

服务 协议 操作 起始端口 结束端口 备注
ssh TCP allow 22
etcd TCP allow 2379 2380
apiserver TCP allow 6443
calico TCP allow 9099 9100
bgp TCP allow 179
nodeport TCP allow 30000 32767
master TCP allow 10250 10258
dns TCP allow 53
dns UDP allow 53
local-registry TCP allow 5000 离线环境安装
local-apt TCP allow 5080 离线环境安装
rpcbind TCP allow 111 使用 NFS 作为持久化存储
ipip IPIP allow Calico 需要允许 IPIP 协议
服务角色规划
  • Master 节点:Master是Kubernetes 的主节点。Master节点上面主要由四个模块组成:etcd、api server、+ controller manager、scheduler,高可用架构一般建议在3个节点,可以和etcd 融合部署。Master节点上不运+ 行任何用户容器。
  • Etcd节点:etcd 节点在集群规模大于 25 个节点的情况下,可以采用单独节点,推介采用SSD存储作为Etcd 存+ 储数据。节点规模小可以采用和Master 节点融合部署
  • Node 节点:Node在每个节点上运行,为用户业务 Pod 提供 Kubernetes 运行环境。Node 节点一般 + kube-proxy 、kubelet 两个组件。
  • 镜像仓库节点: 私有harbor仓库。
  • 集群内部负载节点:只作为集群内部管理, 主要用途为 K8S API 、KubeSphere API和对外暴露KubeSpher + Web 界面,现有环境中有可用的负载均衡服务,比如硬件的 F5,或者某个公有云、私有云的 slb,也可自已装+ haproxy + keepalive 节点配置软LB。
Master 节点包含服务:
服务 最小规模 用途
Master 节点 3 节点
kube-apiserver 3 副本 Kubernetes REST API
kube-controller-manager 3 副本 Kubernetes 管理控制中心
kube-scheduler 3 副本 Kubernetes 调度器,负责将 Pods 指派到节点上。
etcd 3 副本 键值型数据库 主要用来记录K8s上所有资源的状态。
Node 节点包含服务
服务 最小规模 用途
Node 节点 3 节点
coredns 2 副本 高性能、易扩展的DNS服务端,用于集群内部service?域名访问等()
nodelocaldns 节点数(daemonset 方式运行) 在集群的上运行一个dnsCache daemonset来提高clusterDNS性能和可靠性
kube-proxy 节点数(daemonset?方式运行) Kubernetes Service的通信与负载均衡机制的重要组件
kubelet 节点(节点服务) 每个节点上的主要的“节点代理”,每个节点都会启动 kubelet进程,用来处理 Master 节点下发到本节点的任务。
docker 节点(节点服务) 容器引擎
calico-kube-controller 1 Calico 控制节点
Calico-cni 节点数(daemonset方式运行) CNI插件,用于容器之间网络通信等。
镜像仓库节点
服务 最小规模 用途
Harbor 2 镜像仓库主备架构,主对外提供服务。
集群配比参考
Nodes 数量 Master 数量 Master 配置 数据盘挂载大小
25 3 8C16G 500G以上
100 5 16C32G 800G 以上
250 5-7 16C64G 1T 以上
Etcd 节点配置推荐:
在25节点以内,可以将etcd 节点和Master 融合部署: Nodes 数量 Etcd 数量 Etcd 配置 数据盘挂载SSD 大小
25 3 4核8 G 200G
100 3-5 8 核 16G 500G
250 5-7 8 核 32G 1T
网络CIDR 配置推荐(以Calico 为例):
Nodes 数量 KubePodsCIDR KubeServiceCIDR 备注
25 10.233.0.0/18 10.233.0.0/18 可提供16K IPs
100 10.233.0.0/16 10.233.0.0/16 可提供64K IPs
250 10.0.0.0/14 10.128.0.0/14 可提供256K IPs
日志组件
Nodes 数量 是否外置 推荐 备注
5 内置 Es master 1 副本 es data 2副本
10 内置 Es master 3 副本 es data 3副本 具体可参考日志量和集群存储IO和网络能力,决定是否需要外置。
10节点以上 外置 eck

集群规模参考示例

场景一:小规模

节点数量:3台Master + 3 Node
配置考虑:3Master 8核16G, Node 16核32G 。

场景二:次小规模

节点数量:3 台Master节点 + 25台Nodes以内
配置考虑:3 台Master 8核16G ,25台Nodes 16核32G

需要外置组件:日志

场景三:中型规模

节点数量:5台Master 节点 + 3台Etcd 节点 + 100台Node 节点
配置考虑:5台Master 节点考虑16核32G。
3 台Etcd 节点8核16G 数据盘挂载SSD。
需要外置组件: 日志

场景四:大型规模

节点数量:5台Master 节点 + 5台Etcd节点 + 250台Node节点
配置考虑:5台Master 节点考虑16核32G。
5台Etcd 节点8核16G 数据盘挂载SSD。


所有 pod 都应该设置 limit 和request 数值

所有 pod 都应该设置 limit 和request 数值,否则资源利用率统计很不准。资源不够的时候,没有设置 request 和 limit 的 pod 最先被驱逐,最后是 request=limit 的 pod 被驱逐。集群在使用中,有的资源允许压缩使用,有的不成。CPU、网络I/O可以;内存和磁盘空间不建议。但很多时候 pod 并不是同时使用,因此稍有有一些超额分配是可以的,可以在 limitrange 中的 maxLimitRequestRatio 设置百分比。当然,这个百分比要通过测试实现。也就是说,当资源不够的时候,pod 会被驱逐,根据你能接受的驱 逐pod的 频率,设置评定当时的超分率。当然,超额使用的设置还和每个 namespace 的 qouta 的设置有关。从集群角度看,K8s 整体的 qouta 可以大于整体的可提资源
K8s 节点会预留资源,主要分为两部分 Kube-reserved, system-reserved。当资源利用率超过的可分配资源时(开始侵占预留资源),K8s 开始驱逐内存。K8s 驱逐 pod 的阈值可以设置,也就是说,当节点的资源少于多少的时候,开始驱逐 pod。

容量规划主要是对比 request 资源和实际使用率。根据实际使用率的上浮下浮 20%,我们设定资源的上限、下限预估值。
低估造成资源不够,高估造成资源浪费。如果出现高估,可以减少 request,limit 不变,让 request< limit。如果出现了低估,那降低 limit,让 request=limit。

磁盘和内存驱逐上限

例如在kubelet的启动参数中添加–eviction-hard=memory.available<500Mi,nodefs.available<10%,nodefs.inodesFree<5%,其中nodefs.available参数用于设置根目录使用的阈值。这里的10%表示当根目录使用超过10%时,节点将开始启动驱逐过程。需要注意的是,驱逐过程可能会导致节点上的Pod被删除,因此在调整阈值时需要谨慎。建议在根目录使用接近阈值时就开始进行清理或扩容操作,以避免出现驱逐的情况。

kubelet 默认的关于节点存储的驱逐触发条件:
   1.nodefs.available<10%(容器 volume 使用的文件系统的可用空间,包括文件系统剩余大小和 inode 数量)
   2.imagefs.available<15%(容器镜像使用的文件系统的可用空间,包括文件系统剩余大小和 inode 数量)
nodefs是kubelet目录所在分区,默认在/var/lib/kubelet/目录;imagefs是docker安装目录所在的分区,默认在/var/lib/docker,自定义如/data/docker目录。

kubelet 默认的关于节点内存资源的驱逐触发条件:

    memory.available<100Mi
查看节点的可分配资源:

kubectl describe node node01.k8s.local | grep Allocatable -B 4 -A 3

驱逐配置
--eviction-hard=memory.available<5%,nodefs.available<10%,imagefs.available<10%

--eviction-soft=memory.available<10%,nodefs.available<15%,imagefs.available<15%

--eviction-soft-grace-period=memory.available=2m,nodefs.available=2m,imagefs.available=2m

--eviction-max-pod-grace-period=30

--eviction-minimum-reclaim=memory.available=0Mi,nodefs.available=500Mi,imagefs.available=500Mi
扩容依据

当节点资源使用率超过70% ,建议增加集群节点或进行节点扩容。


目前 Kubernetes 中的业务主要可以分为
  • 业务 API对象
  • 长期伺服型(long-running) Deployment
  • 批处理型(batch) Job
  • 节点后台支撑型(node-daemon) DaemonSet
  • 有状态应用型(stateful application) StatefulSet
  • 资源对象
  • 资源对象 Pod、ReplicaSet、ReplicationController、Deployment、StatefulSet、DaemonSet、Job、+ CronJob、HorizontalPodAutoscaling、Node、Namespace、Service、Ingress、Label、+ CustomResourceDefinition
  • 存储对象 Volume、PersistentVolume、Secret、ConfigMap
  • 策略对象 SecurityContext、ResourceQuota、LimitRange
  • 身份对象 ServiceAccount、Role、ClusterRole
安装顺序参考
  • 基础
  • 容器
  • 私仓
  • 主体
  • ipvs
  • 存储
  • UI
  • ingress

避坑点

参考:https://mp.weixin.qq.com/s/GH-iX0W_d435p4YDgxjHXA

4.14以下linux内核搭配Kubernetes在1.9.x~1.21以下版本 存在cgroup泄漏问题,长时间运行后无法新建pod,升级内核

参考:https://tencentcloudcontainerteam.github.io/2018/12/29/cgroup-leaking/
pod 的 "cannot allocated memory"报错,node 内核日志的“SLUB: Unable to allocate memory on node -1”

原因
创建销毁容器/云主机、登录宿主机、cron 定时任务等会触发创建临时的 memory cgroup,但产生的 cache 内存并不会被主动回收.最多65535个memory cgroup共存

临时解决定时执行
echo 3 > /proc/sys/vm/drop_caches

其化解决方法
内核配置 nokmem 禁用 kmem accounting 功能,修改虚机启动的引导项 grub 中的cgroup.memory=nokmem,让机器启动时直接禁用 cgroup的 kmem 属性
K8s(Kubernetes) 禁用 KernelMemoryAccounting 功能,重新编译kubelet和runc
docker/runc 禁用 KernelMemoryAccounting 功能,重新编译kubelet和runc

内核 4.12 以下tcp_tw_recycle

k8s向外连接时,通过 NAT 方式联网并与服务端交互时,服务端看到的是同一个 IP。也就是说对服务端而言这些客户端实际上等同于一个,可惜由于这些客户端的时间戳可能存在差异。于是乎从服务端的视角看,便可能出现时间戳错乱的现象,进而直接导致时间戳小的数据包被丢弃,具体的表现通常是是客户端明明发送的 SYN,但服务端就是不响应 ACK。
外部机器关闭ip复用。

echo 0 > /proc/sys/net/ipv4/tcp_tw_recycle

由于 tcp_tw_recycle 坑太多,在内核 4.12 之后已移除

5.9以下linux内核,IPVS 模式会启用net.ipv4.vs.conntrack=1,复用TCP连接问题,存在大量TCP短连接场景,不建议使用IPVS,建议替换为iptables模式,会有部分连接多出了1s的延迟的问题。

net.ipv4.vs.conn_reuse_mode=0会出现部分流量被转发到销毁Pod的问题。
net.ipv4.vs.conn_reuse_mode=1会有部分连接多出了1s的延迟的问题

NodePort netfilter 的 SNAT 源端口冲突

大流量下负载到NodePort时,会偶尔有SYN 收到但没响应
conntrack -S 看到 insert_failed 数量在不断增加

解决
不使用源端口选举,在 iptables 的 MASQUERADE 规则如果加 –random-fully 这个 flag 可以让端口选举完全随机,基本上能避免绝大多数的冲突,但也无法完全杜绝。最终决定开发 LB 直接绑 Pod IP,不基于 NodePort,从而避免 netfilter 的 SNAT 源端口冲突问题。

k8s长连接问题

1.长连接服务扩容失效
原因是客户端长连接一直保留在老的Pod容器中,新扩容的Pod没有新的连接过来,导致K8S按照步长扩容第一批Pod之后就停止了扩容操作,而且新扩容的Pod没能承载请求,进而出现服务过载的情况,自动扩容失去了意义.
参照下面减少keepalive_time时间

2.conntrack 表项超时导致tcp长连接中断
采取ipvs代理 部署后在调用gRPC服务时小概率出现Connection reset by peer的错误。

  • nf_conntrack_max:连接跟踪表的大小,建议根据内存计算该值CONNTRACK_MAX = RAMSIZE (in bytes) / + 16384 / (x / 32),并满足nf_conntrack_max=4*nf_conntrack_buckets,默认262144
  • nf_conntrack_buckets:哈希表的大小,(nf_conntrack_max/nf_conntrack_buckets就是每条哈希记录链表+ 的长度),默认65536
  • nf_conntrack_tcp_timeout_established:tcp会话的超时时间,默认是432000 (5天)
sysctl -n net.netfilter.nf_conntrack_max
2310720
sysctl -n net.netfilter.nf_conntrack_tcp_timeout_established
432000
sysctl -n net.netfilter.nf_conntrack_buckets
262144

解决
增加nf_conntrack_max,nf_conntrack_buckets,减少nf_conntrack_tcp_timeout_established ,减少keepalive
64G内存的机器,推荐配置

net.netfilter.nf_conntrack_max=4194304
net.netfilter.nf_conntrack_tcp_timeout_established=300
net.netfilter.nf_conntrack_buckets=1048576

2.Ipvs的连接优化
如果出现连接主动关闭的问题,因为从 client 到 server 经历了一层 ipvs,所以最大的可能就是 ipvs出将连接主动断开,而 client 端还不知情.

原始

net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

pod中特权模式优化,减少时间

echo 'net.ipv4.tcp_keepalive_time=600'  >> /etc/sysctl.conf
echo 'net.ipv4.tcp_keepalive_probes=10' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_keepalive_intvl=30'  >> /etc/sysctl.conf
sysctl -p

max_syn_backlog 过小 Pod 偶尔存活检查失败

net.ipv4.tcp_max_syn_backlog 默认是 1024,如果短时间内并发新建 TCP 连接太多,SYN 队列就可能溢出,导致部分新连接无法建立。
改成 8096

Kubernete证书使用超长有效期10年,避免证书过期。

验证这些证书的到期时间:

openssl x509 -noout -dates -in $(kubectl config view --minify --output 'jsonpath={.clusters[0].cluster.certificate-authority}')

ipvs/iptables 在集群中不超过1000个服务的时候,iptables 和 ipvs 并无太大的差别,在有5千个服务(4万条规则)时,添加一条规则耗时11分钟.避免使用iptables

磁盘和内存驱逐上限

没有NodeLocal情况下,重启coredns pod导致集群业务解析域名失败5分钟,安装NodeLocal

DNS 解析偶尔 5S 延时

conntrack 表DNAT插入时冲突
解决方案一: 使用 TCP 发送 DNS 请求
resolv.conf 可以加 options use-vc

解决方案二: 避免相同五元组 DNS 请求的并发

resolv.conf 加上 options参数

spec:
      dnsConfig:
        options:
          - name: single-request-reopen

解决方案三: 使用本地 DNS 缓存
每个容器自带一个 DNS 缓存服务。每个节点运行一个 DNS 缓存服务,所有容器都把本节点的 DNS 缓存作为自己的 nameserver。

时间

各node时间必须一致

Secret 和 ConfigMap

Secret 和 ConfigMap使用变量时不支持热更新
ConfigMap 使用subpath时不支持热更新

k8s升级

由于版本的兼容性,只能从一个次要版本升级到另外一个次要版本, 不能跳跃升级。1.19.x → 1.20.y 是可以的.
升级后,因为容器规约的哈希值已更改,所有容器都会被重新启动。

Posted in 安装k8s/kubernetes.

Tagged with , , .


Stable Diffusion 以Docker部署CPU运行在Linux系统

基于文本生成图像的强大模型

Stable Diffusion 的代码开源在 GitHub 上,地址如下:
https://github.com/CompVis/stable-diffusion

SDXL是 v1.5 模型的官方升级版,SDXL 的总参数数量为 66 亿,而 v1.5 为 9.8 亿
SDXL 的默认图像大小是 1024×1024,这比 v1.5 模型的 512×512 大 4 倍
SDXL 1.0 应该在具有 8GB VRAM 的 GPU 上有效工作。但sdwebui现在做不到(需要10G),能做到的只有COMFY UI

Stable Diffusion web UI
https://github.com/AUTOMATIC1111/stable-diffusion-webui
1.9.3 2024/4/23
1.6版本性能强劲,对小显存用户比较友好,不再轻易出现跑SDXL大模型爆显存的情况

cpu 版使用这个
https://github.com/openvinotoolkit/stable-diffusion-webui
This repo is a fork of AUTOMATIC1111/stable-diffusion-webui which includes OpenVINO support through a custom script to run it on Intel CPUs and Intel GPUs.
1.6版本

AMD显卡满血Stable Diffusion(SD+Fooocus+ComfyUI)无脑部署笔记(Linux+ROCm6.1.1)
https://zhuanlan.zhihu.com/p/656480759

Stable-Diffusion-WebUI使用指南
https://blog.csdn.net/qq_39108291/article/details/131025589

stablediffusion与midjouney的区别

1. midjouney的特点:

midjouney是一个商业化产品,用户需要付费才能使用,而且只能通过其官方Discord上的Discord机器人使用。midjouney没有公布其技术细节,但是其生成的图像效果非常惊艳,普通人几乎已经很难分辨出它产生的作品,竟然是AI绘画生成的。
midjouney善于适应实际的艺术风格,创造出用户想要的任何效果组合的图像。它擅长环境效果,特别是幻想和科幻场景,看起来就像游戏的艺术效果。midjouney的提示词门槛低,不需要特别精细的描述也可以出不错的图像。但是缺点是画面不太受控,而且被BAN的敏感词非常多,像bare,nude(裸体)这类词就用不了。

2. stablediffusion的特点:

stablediffusion是一个开源的模型,任何人都可以免费使用(但是需要有GPU来跑),也可以部署到GoogleColab和Drive去薅Tesla T4。stablediffusion是基于latent diffusion model(LDM)的条件扩散模型,采用CLIP text encoder提取的text embeddings作为condition。stablediffusion对当代艺术图像有比较好的理解,可以产生充满细节的艺术作品。除了文生图功能外,还支持图生图、图像重绘、个性化模型训练、可控生成等多种扩展应用。stablediffusion比较适合生成复杂的、有创意的插图。但是缺点是提示词门槛高,手部问题,Lora的兼容性等等。

3.midjouney和stablediffusion的优劣

midjouney和stablediffusion各有优劣之处,具体如下:

① midjouney的优点是:出图质量高、出图稳定、提示词简单、艺术风格丰富、环境效果出色。

②midjouney的缺点是:收费昂贵、只能通过Discord使用、技术细节不透明、画面不太受控、敏感词过多。

③stablediffusion的优点是:开源免费、可本地部署或云端使用、技术原理清晰、扩展应用多样、当代艺术理解好。

④stablediffusion的缺点是:需要GPU资源、提示词门槛高、手部问题突出、Lora兼容性差。

       那么对于普通人来说,选择stablediffusion可能比选择midjouney更合适,原因如下:

①stablediffusion是免费的,而midjouney是收费的,对于预算有限的普通人来说,stablediffusion更划算。

②stablediffusion是开源的,而midjouney是闭源的,对于想要了解AI绘图技术原理和细节的普通人来说,stablediffusion更透明。

③stablediffusion是灵活的,而midjouney是固定的,对于想要尝试不同功能和插件的普通人来说,stablediffusion更多样。

④stablediffusion是创新的,而midjouney是成熟的,对于想要挑战自己和发挥想象力的普通人来说,stablediffusion更有趣。

总的来说,如果您会使用midjouney,就相当于您学会了买车票搭车去某个地方,你只能选择路线,而您会使用stablediffusion,就相当于您自己买了一辆车,考了一个驾照,想去哪里就去哪里,想怎么开车就怎么开车(划重点)

安装要求

最核心的关键点:看显卡、看内存、看硬盘、看CPU。其中最重要的是看显卡,显卡N卡(英伟达Nvida显卡),最低10系起步,显存最低4G,6G及格;内存最低8G,16G及格;硬盘可用空间最好有个500G朝上,固态最佳,机械硬盘也没多大问题。CPU其实没太大要求,有好显卡的,CPU一般不会很差。

可能的误区

  • 1.误区1:必须用linux或者windows
    mac os也可以,但是同样在于其它的坑多,不建议在mac os上使用,当然我也没试过。
    windows和linux这个图形界面会占用显存,特别是显存小的时候更明显(windows只有server版可以只装命令行用)

  • 2.误区2:必须用N卡
    N卡坑少,出问题容易找到答案,并不是只能N卡。A卡、cpu也可以、i卡理论上可以,使用难度较大。Nvidia显卡(Cuda)或AMD显卡(ROCm)

  • 3.误区3:必须用conda
    用conda的原因在于很多开发者通常需要多个python环境,个人部署直接装在系统就行。

请放弃尝试在Win下通过虚拟机、WSL安装ROCm;

windows安装

推荐哔哩哔哩站秋叶做的整合包A绘世启动器.支持cpu/N卡/A卡
由于网络下载问题,这里使用的模型直接使用秋叶整合包中下载的。

最新一次更新整合包版本v4.8,24 年 4 月更新。
网盘:https://pan.quark.cn/s/2c832199b09b
解压密码:bilibili@秋葉aaaki
https://www.bilibili.com/video/BV1iM4y1y7oA/?spm_id_from=333.976.0.0

模型文件

Stable-diffusion支持五种模型文件,Checkpoint、Embedding、Hypernetwork、Lora和VAE。

  • 1.Checkpoint
    Checkpoint模型就是我们在webui的Stable Diffusion checkpoint中选择的模型。Checkpoint模型我把它理解为AI的记忆,它控制着AI能画出什么,画出的东西长什么样。
  • 2.Lora
    Lora模型是一种生成对抗网络模型,主要用对Checkpoint模型进行定向的微调。个人理解是用来做Checkpoint模型提示词重映射的。比如,在一个Checkpoint模型中,提示词“猫”对应形象是“加菲猫”,那么Lora就可以将“猫”这个提示词重映射,使其对应的形象变成HelloKitty,那么在加持Lora模型的Checkpoint模型上输入“猫”这个提示词后AI将不再生成加菲猫,而全部生成HelloKitty。
  • 3.Embedding
    在Stable diffuion中,Embedding模型提供了一种向已有模型嵌入新内容的方式,Embedding模型可以使用很少的图片数据,来生成一个具有新的风格或者人物形象的模型,并且可以使用特定的提示词来映射这些特征。
  • 4.Hypernetworks
    Hypernetworks又叫超网络,Hypernetworks模型是一种风格化文件,可以为AI生成的图像应用指定画风。如,同样是画一个HelloKitty,在没有应用Hypernetworks模型的情况,画出来的HelloKitty就是一只正常的HelloKitty,如果给AI应用一个金享泰画风的Hypernetworks模型,那么AI画出来的HelloKitty就变成一只油腻的HelloKitty。
  • 5.VAE
    VAE的作用就是在第一章中讲到的将图片从潜空间的压缩数据解码变成一张正常的图片,不同的VAE会影响AI出图的色调,如果当我们不使用VAE时,AI生成的图片均会有一层灰蒙蒙的感觉,使用VAE会使图片的饱和度有所区别。
  • 6.AestheticGradients
    AestheticGradients(美学渐变)是以插件的形式存在一种模型修改技术,AestheticGradients模型需要依赖AestheticGradients插件才能使用,效果和Hypernetworks差不多,但是基于AestheticGradients插件提供了更多的可调节参数,而Hypernetworks的参数是已经定死了不可更改的。

相关网站

吐司
在线生成,提示词,模型下载
https://tusi.cn/

huggingface
国内不能访问
https://huggingface.co/

c站
Civitai 专门收集和分享Stable Diffusion相关的模型
收集并分享1700+经过筛选的Stable Diffusion模型文件,免费下载。
提供1万+带提示语的示例图片,供用户学习描述内容的方法。
https://www.civita.com
国内镜像
https://civitai.work/

哩布哩布AI
收集了很多不同类型的Stable Diffusion模型,在线生成,下载这些AI生成图片和视频的模型
https://www.liblib.art/

AI提示词
https://www.4b3.com/

AI 作图知识库(教程): https://guide.novelai.dev/
标签超市(解析组合): https://tags.novelai.dev/
原图提取标签: https://spell.novelai.dev/

模型分享下载链接:https://pan.quark.cn/s/32f374eef667

AI绘图的用法拓展

  • ControlNet
    ControlNet的原理主要是使用OpenCV的一些识别算法,如姿势识别、手势识别、边缘检测、深度检测等,先对参考图做一层机器视觉方面的预处理,生成身体骨骼特征点、手势骨骼特征点、描边图、深度图等中间图,然后再让AI参考这些中间图进行创作

  • 制作全景图
    并接360图片

  • mov2mov插件
    https://github.com/Scholar01/sd-webui-mov2mov
    生成同步的视频文件

  • 人物换装

  • OutPainting
    outpainting是官方提供的一项AI图像扩展技术,AI可以根据已有的图像来扩展图像之外的画面。

  • SadTalker
    唇型同步

  • 生成类似的图
    这个功能在通过photoshopP图之后,使用AI来融合P图中的看起来比较违和的元素时非常有用,有了这个功能我们就可以快速的使用PS往图中加入我们想要的元素,然后使用AI来融合,当然出图的效果还是取决于大模型;

  • 小图高清重绘

  • 超清放大

准备虚拟机

virtualbox 虚拟一台rock9.3
10核/17G内存/120G硬盘

查看环境

查看系统版本

cat /etc/redhat-release
Rocky Linux release 9.3 (Blue Onyx)

查看内核版本

uname -a
Linux localhost.localdomain 5.14.0-362.8.1.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 8 17:36:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

查看ssh及openssl 版本

ssh -V
OpenSSH_8.7p1, OpenSSL 3.0.7 1 Nov 2022
不升级

python版本

python -V
Python 3.9.18

glibc版本

ldd –version
ldd (GNU libc) 2.34

rocky9上打开加密兼容

便于低版本ssh 连接
update-crypto-policies –show
update-crypto-policies –set LEGACY

修改ip

查看当前ip

ip a
nmcli device show
nmcli con show

使用配制文件修改

vi /etc/NetworkManager/system-connections/enp0s3.nmconnection

[ipv4]
method=manual
address1=192.168.244.14/24,192.168.244.1
dns=223.5.5.5;1.1.1.1

重新载入生效

nmcli connection reload
nmcli connection down enp0s3 && nmcli connection up enp0s3

主机名设定

hostnamectl set-hostname dev-ai-diffusion.local

hostnamectl status
 Static hostname: dev-ai-diffusion.local
       Icon name: computer-vm
         Chassis: vm 

                            Machine ID: 45d4ec6ccf3646248a8b9cc382baf29d
         Boot ID: 0b6cfd83e8534a1f81f08de3962b8ba9
  Virtualization: oracle
Operating System: Rocky Linux 9.3 (Blue Onyx)      
     CPE OS Name: cpe:/o:rocky:rocky:9::baseos
          Kernel: Linux 5.14.0-362.8.1.el9_3.x86_64
    Architecture: x86-64
 Hardware Vendor: innotek GmbH
  Hardware Model: VirtualBox
Firmware Version: VirtualBox

安装中文语言包

localectl list-locales |grep zh
dnf list |grep glibc-langpack
dnf install glibc-langpack-zh

关防火墙

systemctl stop firewalld
systemctl disable firewalld

selinux

setenforce 0 && sed -i ‘/SELINUX/s/enforcing/disabled/’ /etc/selinux/config

vim 打开黏贴模

echo ‘set paste’ >> ~/.vimrc

时区

timedatectl set-timezone Asia/Shanghai

启用启动脚本

systemctl enable rc-local.service
systemctl start rc-local.service
systemctl status rc-local.service

chmod +x /etc/rc.d/rc.local
chmod +x /etc/rc.local

常用工具

yum -y install wget vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 tar curl lrzsz rsync psmisc sysstat lsof

docker yum

step 1: 安装必要的一些系统工具

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Step 2: 添加软件源信息

sudo yum-config-manager –add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Step 3

sudo sed -i ‘s+download.docker.com+mirrors.aliyun.com/docker-ce+’ /etc/yum.repos.d/docker-ce.repo

Step 4: 更新并安装Docker-CE

sudo yum makecache fast
sudo yum -y install docker-ce

Installed:
  container-selinux-3:2.229.0-1.el9.noarch          containerd.io-1.6.31-3.1.el9.x86_64           docker-buildx-plugin-0.14.0-1.el9.x86_64     docker-ce-3:26.1.2-1.el9.x86_64      docker-ce-cli-1:26.1.2-1.el9.x86_64    
  docker-ce-rootless-extras-26.1.2-1.el9.x86_64     docker-compose-plugin-2.27.0-1.el9.x86_64     fuse-common-3.10.2-8.el9.x86_64              fuse-overlayfs-1.13-1.el9.x86_64     fuse3-3.10.2-8.el9.x86_64              
  fuse3-libs-3.10.2-8.el9.x86_64                    libslirp-4.4.0-7.el9.x86_64                   slirp4netns-1.2.3-1.el9.x86_64               tar-2:1.34-6.el9_1.x86_64           

注意:官方软件源默认启用了最新的软件,安装指定版本的Docker-CE
yum list docker-ce.x86_64 –showduplicates | sort -r

docker-ce.x86_64               3:26.1.2-1.el9                  docker-ce-stable 
docker-ce.x86_64               3:26.1.2-1.el9                  @docker-ce-stable
docker-ce.x86_64               3:26.1.1-1.el9                  docker-ce-stable 

yum -y install docker-ce-[VERSION]

Step 5: 开启Docker服务

sudo service docker start

查看版本

docker version

Engine:
  Version:          26.1.2
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.10
  Git commit:       ef1912d
  Built:            Wed May  8 13:59:54 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.31
  GitCommit:        e377cd56a71523140ca6ae87e30244719194a521
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker加速

vi /etc/docker/daemon.json

{
"registry-mirrors":["https://docker.nju.edu.cn/"]
}

docker nvidia驱动

非必要,使用N卡时安装

yum install -y nvidia-docker2

systemctl daemon-reload
systemctl enable docker
systemctl restart docker

git 安装

yum install git

使用三方现有镜像安装

下载Stable Diffusion Docker 相关镜像

要下载Stable Diffusion的Docker镜像,您可以使用以下命令:
官方镜像
docker pull diffusion/stable:latest
docker pull kestr3l/stable-diffusion-webui

其它

腾讯加速
docker pull gpulab.tencentcloudcr.com/ai/stable-diffusion:1.0.8

docker.io 中的民间镜像
docker pull bobzhao1210/diffusion-webui

国外镜像

docker pull siutin/stable-diffusion-webui-docker:latest-cpu
docker pull siutin/stable-diffusion-webui-docker:latest-cuda

国内镜像

docker pull registry.cn-hangzhou.aliyuncs.com/sunsharing/stable-diffusion-webui-docker:latest-cpu

阿里官方gpu镜像,比较新

docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/sd-webui:4.3.5-full-pytorch2.1.2-ubuntu22.04

使用阿里gpu镜像

docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/sd-webui:4.3.5-full-pytorch2.1.2-ubuntu22.04

docker images

REPOSITORY                                              TAG                                   IMAGE ID       CREATED         SIZE
egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/sd-webui   4.3.5-full-pytorch2.1.2-ubuntu22.04   22226c589a11   3 weeks ago     34.7GB

镜像中相关版本
version: 1.6.0  •  python: 3.10.10  •  torch: 2.1.2+cu118  •  xformers: N/A  •  gradio: 3.41.2

下载标准sd-1.5模型:

存放到models/Stable-diffusion ,使用GPU时下载,不支持cpu运行 ,如果没有ui启动时会尝试自动下载 ,4g左右

mkdir /host  
cd /host  
wget https://aiacc-inference-public-v2.oss-accelerate.aliyuncs.com/aiacc-inference-webui/models/v1-5-pruned-emaonly.safetensors

下载国内 AnythingV5_v5PrtRE.safetensors 2g左右

wget https://api.tusiart.com/community-web/v1/model/file/url/v2?modelFileId=600998819589521712 AnythingV5_v5PrtRE.safetensors

webui启动时必需要一个大模型,这个支持cpu.

Stable-diffusion WebUI文件夹功能

  • localizations: 汉化包
  • configs: 配制文件
  • embeddings:存放美术风格文件的目录,美术风格文件一般以.pt结尾,大小在几十K左右;
  • extensions:存放扩展插件的目录,我们下载的WebUI的插件就放在这个目录里,WebUI启动时会自动读取插件,插件目录都是git库,可以直接通过git更新;
  • extensions-builtin:存放WebUI内置的扩展;
  • models/hypernetworks:存放风格化文件的目录,风格化文件是配合Prompt使用的,以使AI会出对应风格的图片,风格化文件也以.pt结尾,大小在几百MB左右;
  • models/Lora:存放Lora的文件的目录,Lora文件是用来调整模型的,可以重映射模型文件的Prompt映射,使AI在相应的提示词下按照Lora的样式绘制,Lora文件一般以.safetensors结尾,大小在几百MB左右;
  • models/Stable-diffusion:存放模型的文件的目录,AI绘画时的采样基本从这个文件里采,影响图片的整体样式与画风,一般以.ckpt或.safetensors结尾,大小在几个G左右;
  • models/VAE:存放VAE文件的目录,VAE文件会影响图片整体的色调,如在刚开始玩WebUI时画出的图都比较灰,就是因为WebUI默认没有为我们设置VAE导致的,VAE文件一般以.ckpt或.vae.pt结尾,大小在几百MB或几个G不等;
  • outputs/extras-images:AI放大的原图的默认保存路径;
  • outputs/img2img-grids:批量图生图时的缩略图原图的默认保存路径;
  • outputs/img2img-images:图生图的原图的默认保存路径;
  • outputs/txt2img-grids:批量文生图时的缩略图原图的默认保存路径;
  • outputs/txt2img-images:文生图的原图的默认保存路径;

这些路径我们是可以在WebUI的设置界面修改成自定义路径的。

  • scripts:存放第三方脚本的目录;
  • venv:这个文件夹是WebUI首次运行时配置运行环境自己创建的,出现运行环境的问题时,可以删掉它让WebUI重新生成。
  • repositories: 启动后载入python模块代码,会影响是用gpu还是cpu,stablediffusion,taming-transfomers,stable-diffusion,k-diffusion-sd,CodeFormer,BLIP
  • style.csv 存储当前Prompt和Nagetive prompt的提示词作为提示词模板,目前WebUI没有提供在UI中删除提示词模板的功能,想要删除不需要的提示词模板,我们需要通过修改styles.csv文件

拉取cpu版stable-diffusion-webui

拉取cpu版stable-diffusion-webui
cd /host && git clone https://github.com/openvinotoolkit/stable-diffusion-webui.git ./stable-diffusion-webui-cpu

cpu版和gpu版主要差异文件

/modules/launch_utils.py
/requirements.txt
/requirements_versions.txt
/scripts/openvino_accelerate.py

创建启动变量及目录

h_dir=/host # host dir
h_mdir=/host/models # host models dir
c_dir=/workspace/stable-diffusion-webui # container dir
c_mdir=/workspace/stable-diffusion-webui/models # container dir

#mkdir -p ${h_dir}/{localizations,configs,config_states,outputs,embeddings,extensions,extensions-builtin,scripts,repositories}
#mkdir -p ${h_mdir}/{Stable-diffusion,hypernetworks,ControlNet,Lora,VAE,VAE-approx}
cd $h_mdir

cat >> /etc/profile <<EOF
export h_dir=/host
export h_mdir=/host/models # host models dir
export c_dir=/workspace/stable-diffusion-webui # container dir
export c_mdir=/workspace/stable-diffusion-webui/models # container dir
EOF

数据持久化

#先临时启动
docker run -itd --network=host --name=sd
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
-p 7860:7860 egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/sd-webui:4.3.5-full-pytorch2.1.2-ubuntu22.04 bash

#复制容器内已存在文件到宿主机
cdirname='localizations configs outputs embeddings extensions extensions-builtin scripts'  
for cname in $cdirname; do echo ${c_dir}/${cname}; docker cp sd:${c_dir}/${cname} ${h_dir}/; done
docker cp sd:${c_dir}/webui-user.sh .

docker exec sd ls models/Stable-diffusion

docker stop sd
docker rm sd

ls ${h_dir}/models/Stable-diffusion
anything-v5-PrtRE.safetensors  'Put Stable Diffusion checkpoints here.txt'

# 复制python包 8.4G
docker cp sd:/root/miniconda3/lib/python3.10/site-packages /host/

# 复制大模型
mv /host/v1-5-pruned-emaonly.safetensors  /host/models/Stable-diffusion

# 复制出config.json
docker cp sd:/workspace/stable-diffusion-webui/config.json .

准备start.sh
vi start.sh
source ./webui-user.sh
python -u ./launch.py --listen --port 5001  --skip-install

启动容器

docker run -itd --network=host --name=sd \
-e COMMANDLINE_ARGS='--use-cpu all --skip-torch-cuda-test --precision full --no-half --no-half-vae  --api' \
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
-v ${h_dir}/site-packages:/root/miniconda3/lib/python3.10/site-packages \
-v ${h_dir}/stable-diffusion-webui-cpu:/workspace/stable-diffusion-webui \
-v ${h_mdir}/Stable-diffusion/:${c_mdir}/Stable-diffusion/ \
-v ${h_mdir}/ControlNet/:${c_mdir}/ControlNet/ \
-v ${h_mdir}/VAE/:${c_mdir}/VAE/ \
-v ${h_mdir}/Lora/:${c_mdir}/Lora/ \
-v ${h_mdir}/hypernetworks/:${c_mdir}/hypernetworks/ \
-v ${h_dir}/style.csv:${c_dir}/style.csv \
-v ${h_dir}/outputs/:${c_dir}/outputs/ \
-v ${h_dir}/localizations/:${c_dir}/localizations/ \
-v ${h_dir}/configs/:${c_dir}/configs/ \
-v ${h_dir}/config_states/:${c_dir}/config_states/ \
-v ${h_dir}/repositories/:${c_dir}/repositories/ \
-v ${h_dir}/extensions/:${c_dir}/extensions/ \
-v ${h_dir}/extensions-builtin/:${c_dir}/extensions-builtin/ \
-v ${h_dir}/webui-user.sh:${c_dir}/webui-user.sh \
-v ${h_dir}/start.sh:${c_dir}/start.sh \
egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/sd-webui:4.3.5-full-pytorch2.1.2-ubuntu22.04 bash ./start.sh

容器启动后,SD-webui服务自动启动。如果需要自定义服务启动命令或者端口,请参照下列命令进行修改:

docker exec -it sd /bin/bash

gpu方式运行

python -u ./launch.py –ckpt-dir ./ckpts –listen –port 7860 –enable-insecure-extension-access –disable-safe-unpickle –api –xformers –skip-install

cpu方式运行变量,方便docker 启动时运行

cd /workspace/stable-diffusion-webui
echo ‘export COMMANDLINE_ARGS="–use-cpu all –skip-torch-cuda-test –precision full –no-half –no-half-vae"’ >> ./webui-user.sh

cpu 方式手动运行

python -u ./launch.py –listen –port 5001 –use-cpu all –skip-torch-cuda-test –precision full –no-half –no-half-vae –api –skip-install
python -u ./launch.py –listen –port 5001 –skip-install

开机启动

echo ‘docker start sd’ >> /etc/rc.local

访问

运行成功后在浏览器输入 http://ip:5001 就可以使用

控制台会输出日志

Python 3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0]
Version: 1.6.0
Commit hash: e5a634da06c62d72dbdc764b16c65ef3408aa588
Launching Web UI with arguments: --listen --port 5001 --skip-install --use-cpu all --skip-torch-cuda-test --precision full --no-half --no-half-vae
WARNING[XFORMERS]: xFormers can't load C++/CUDA extensions. xFormers was built for:
    PyTorch 2.1.2+cu118 with CUDA 1108 (you have 2.1.0+cu121)
    Python  3.10.13 (you have 3.10.10)
  Please reinstall xformers (see https://github.com/facebookresearch/xformers#installing-xformers)
  Memory-efficient attention, SwiGLU, sparse and more won't be available.
  Set XFORMERS_MORE_DETAILS=1 for more details
No module 'xformers'. Proceeding without it.
Warning: caught exception 'Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx', memory monitor disabled
Loading weights [7f96a1a9ca] from /workspace/stable-diffusion-webui/models/Stable-diffusion/anything-v5-PrtRE.safetensors
Creating model from config: /workspace/stable-diffusion-webui/configs/v1-inference.yaml
2024-05-15 15:07:04,153 - ControlNet - INFO - ControlNet UI callback registered.
Running on local URL:  http://0.0.0.0:5001

To create a public link, set `share=True` in `launch()`.
IIB Database file has been successfully backed up to the backup folder.
Startup time: 19.6s (import torch: 2.5s, import gradio: 0.8s, setup paths: 0.3s, other imports: 0.4s, load scripts: 11.3s, create ui: 1.4s, gradio launch: 2.3s, app_started_callback: 0.3s).
Loading VAE weights specified in settings: /workspace/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors
Applying attention optimization: InvokeAI... done.
Model loaded in 4.9s (load weights from disk: 0.1s, create model: 1.8s, apply weights to model: 1.1s, apply float(): 1.5s, load VAE: 0.2s, calculate empty prompt: 0.1s).
[W NNPACK.cpp:64] Could not initialize NNPACK! Reason: Unsupported hardware.
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [02:58<00:00,  8.94s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [02:56<00:00,  8.83s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [02:56<00:00,  8.65s/it]

CPU生成一张础基图3分钟左右

docker全新安装

以ubuntu:22.04 做基础镜像
docker pull ubuntu:22.04
docker images

REPOSITORY                                              TAG                                   IMAGE ID       CREATED         SIZE
ubuntu                                                  22.04                                 52882761a72a   2 weeks ago     77.9MB

进入容器

docker run -itd --network=host --name=ub \
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
ubuntu:22.04 bash 
docker exec -it ub /bin/bash

cat /etc/issue.net 
Ubuntu 22.04.4 LTS

apt-get update
apt-get -y install --reinstall ca-certificates
cp /etc/apt/sources.list /etc/apt/sources.bak.list 

cat /etc/apt/sources.list

#阿里加速
#sed -n 's/http:\/\/.*ubuntu.com/https:\/\/mirrors.aliyun.com/p' /etc/apt/sources.list
sed -i 's/http:\/\/.*ubuntu.com/https:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list

#清华加速
#echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse\ndeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse\ndeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse\ndeb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse" > /etc/apt/sources.list

#工具包
apt-get update && apt-get install -y sudo net-tools inetutils-ping procps curl wget vim telnet locales git zhcon fonts-wqy-microhei fonts-wqy-zenhei xfonts-wqy lsrzsz unzip tree

#相关包
apt-get install -y mesa-utils libglib2.0-0

sed -i 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen && locale-gen

miniconda 安装

https://docs.anaconda.com/free/miniconda/

cd /root
mkdir -p ~/miniconda3
#wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
wget https://repo.anaconda.com/miniconda/Miniconda3-py310_22.11.1-1-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda.sh

~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

no change     /root/miniconda3/condabin/conda
no change     /root/miniconda3/bin/conda
no change     /root/miniconda3/bin/conda-env
no change     /root/miniconda3/bin/activate
no change     /root/miniconda3/bin/deactivate
no change     /root/miniconda3/etc/profile.d/conda.sh
no change     /root/miniconda3/etc/fish/conf.d/conda.fish
no change     /root/miniconda3/shell/condabin/Conda.psm1
no change     /root/miniconda3/shell/condabin/conda-hook.ps1
no change     /root/miniconda3/lib/python3.10/site-packages/xontrib/conda.xsh
no change     /root/miniconda3/etc/profile.d/conda.csh
modified      /root/.bashrc

#载入变量
source /root/.bashrc

(base) root@dev-ai-diffusion:~# conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
(base) root@dev-ai-diffusion:~# conda config --set show_channel_urls yes
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

conda -V
conda 22.11.1

设置不自动激活conda base 环境

再次重启终端之后,你会发现每次打开终端默认都会自动进入到miniconda的base环境中,终端中多了“base”字样。这样会拖慢终端打开速度,并且有可能干扰到其它软件的安装。要退出的话,必须每次打开终端之后先执行conda deactivate命令,让人很难受。执行如下命令,便可以解决终端每次打开都进入conda的base环境的问题
conda config –set auto_activate_base false

python –version
Python 3.10.8

创建python3.10.10

conda create -n python_3.10.10 python=3.10.10
(python_3.10.10) root@dev-ai-diffusion:~# python –version
Python 3.10.10

查看有多少个环境

conda info –envs #

# conda environments:
#
base                     /root/miniconda3
python_3.10.10        *  /root/miniconda3/envs/python_3.10.10

设定默认环境
conda config –set default_environment python_3.10.10

激活环境
conda activate python_3.10.10

退出
conda deactivate

删除环境
conda remove –name test –all

pip install –upgrade pip setuptools wheel

下载 stable-diffusion-webui 仓库
mkdir /workspace && cd /workspace
groupadd -g 1000 website && \
useradd -u 1000 -G website -s /sbin/nologin www

stable-diffusion-webui

40m左右
cpu版
git clone https://github.com/openvinotoolkit/stable-diffusion-webui.git ./stable-diffusion-webui-cpu
gpu版
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git ./stable-diffusion-webui-gpu

ln -s stable-diffusion-webui-cpu stable-diffusion-webui

然后依次下载以下仓库,并切换到合适的版本以避免可能的版本适配问题:

mkdir -p /workspace/common/repositories && cd /workspace/common/repositories

stablediffusion
git clone https://github.com/Stability-AI/stablediffusion.git ./repositories/stable-diffusion-stability-ai

taming-transformers
git clone https://github.com/CompVis/taming-transformers.git ./repositories/taming-transformers

k-diffusion
git clone https://github.com/crowsonkb/k-diffusion.git ./repositories/k-diffusion

CodeFormer
git clone https://github.com/sczhou/CodeFormer.git ./repositories/CodeFormer

BLIP
git clone https://github.com/salesforce/BLIP.git ./repositories/BLIP

generative-models
git clone https://github.com/Stability-AI/generative-models.git ./repositories/generative-models

一共758M

cd /workspace/stable-diffusion-webui/
cat requirements.txt
安装 requirements.txt 中的依赖包

首先确定torch版本和下载的url,不同平台cpu/gpu 使用下列url来确认
https://pytorch.org/get-started/locally/

pip install torch torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple

cpu

pip install torch==2.1.0 torchvision==0.16.0 torchaudio –index-url https://download.pytorch.org/whl/cpu

pip install tb-nightly
pip install tb-nightly -i https://mirrors.aliyun.com/pypi/simple

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

说明:

必须先安装 torch:因为 basicsr 的安装程序会 import torch.
安装 tb-nightly:清华源缺少这个包,但安装 basicsr 时需要它,所以先不换源把它安装上。
安装其他依赖包
pip install ftfy regex tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install git+https://github.com/openai/CLIP.git
pip install open_clip_torch xformers -i https://pypi.tuna.tsinghua.edu.cn/simple

Collecting open_clip_torch
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d9/d2/6ae2ee32d0d2ea9982774920e0ef96d439ee332f459f6d8a941149b1b4ad/open_clip_torch-2.24.0-py3-none-any.whl (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 2.3 MB/s eta 0:00:00
Collecting xformers
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/32/e7/27003645ef99e7571fb6964cd2f39da3f1b3f3011aa00bb2d3ac9b790757/xformers-0.0.26.post1-cp310-cp310-manylinux2014_x86_64.whl (222.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 222.7/222.7 MB 2.0 MB/s eta 0:00:00
Requirement already satisfied: torch>=1.9.0 in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (2.1.0+cpu)
Requirement already satisfied: torchvision in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (0.16.0+cpu)
Requirement already satisfied: regex in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (2024.5.10)
Requirement already satisfied: ftfy in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (6.2.0)
Requirement already satisfied: tqdm in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (4.66.4)
Collecting huggingface-hub (from open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/2b/516f82c5ba9beb184b24c11976be2ad5e80fb7fe6b2796c887087144445e/huggingface_hub-0.23.0-py3-none-any.whl (401 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 401.2/401.2 kB 2.6 MB/s eta 0:00:00
Collecting sentencepiece (from open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a6/27/33019685023221ca8ed98e8ceb7ae5e166032686fa3662c68f1f1edf334e/sentencepiece-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 4.5 MB/s eta 0:00:00
Requirement already satisfied: protobuf in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from open_clip_torch) (4.25.3)
Collecting timm (from open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/68/99/2018622d268f6017ddfa5ee71f070bad5d07590374793166baa102849d17/timm-0.9.16-py3-none-any.whl (2.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2/2.2 MB 3.1 MB/s eta 0:00:00
Requirement already satisfied: numpy in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from xformers) (1.26.3)
Collecting torch>=1.9.0 (from open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/43/e5/2ddae60ae999b224aceb74490abeb885ee118227f866cb12046f0481d4c9/torch-2.3.0-cp310-cp310-manylinux1_x86_64.whl (779.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 779.1/779.1 MB 917.4 kB/s eta 0:00:00
Requirement already satisfied: filelock in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (4.9.0)
Requirement already satisfied: sympy in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (1.12)
Requirement already satisfied: networkx in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (3.2.1)
Requirement already satisfied: jinja2 in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (3.1.3)
Requirement already satisfied: fsspec in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from torch>=1.9.0->open_clip_torch) (2024.2.0)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b6/9f/c64c03f49d6fbc56196664d05dba14e3a561038a81a638eeb47f4d4cfd48/nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.7/23.7 MB 3.7 MB/s eta 0:00:00
Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/eb/d5/c68b1d2cdfcc59e72e8a5949a37ddb22ae6cade80cd4a57a84d4c8b55472/nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 823.6/823.6 kB 4.2 MB/s eta 0:00:00
Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7e/00/6b218edd739ecfc60524e585ba8e6b00554dd908de2c9c66c1af3e44e18d/nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.1/14.1 MB 4.0 MB/s eta 0:00:00
Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ff/74/a2e2be7fb83aaedec84f391f082cf765dfb635e7caa9b49065f73e4835d8/nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 731.7/731.7 MB 832.3 kB/s eta 0:00:00
Collecting nvidia-cublas-cu12==12.1.3.1 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/37/6d/121efd7382d5b0284239f4ab1fc1590d86d34ed4a4a2fdb13b30ca8e5740/nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 410.6/410.6 MB 1.8 MB/s eta 0:00:00
Collecting nvidia-cufft-cu12==11.0.2.54 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/86/94/eb540db023ce1d162e7bea9f8f5aa781d57c65aed513c33ee9a5123ead4d/nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.6/121.6 MB 2.5 MB/s eta 0:00:00
Collecting nvidia-curand-cu12==10.3.2.106 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/44/31/4890b1c9abc496303412947fc7dcea3d14861720642b49e8ceed89636705/nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.5/56.5 MB 3.1 MB/s eta 0:00:00
Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/1d/8de1e5c67099015c834315e333911273a8c6aaba78923dd1d1e25fc5f217/nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 MB 2.3 MB/s eta 0:00:00
Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/65/5b/cfaeebf25cd9fdec14338ccb16f6b2c4c7fa9163aefcf057d86b9cc248bb/nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 196.0/196.0 MB 3.4 MB/s eta 0:00:00
Collecting nvidia-nccl-cu12==2.20.5 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4b/2a/0a131f572aa09f741c30ccd45a8e56316e8be8dfc7bc19bf0ab7cfef7b19/nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 176.2/176.2 MB 1.9 MB/s eta 0:00:00
Collecting nvidia-nvtx-cu12==12.1.105 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/da/d3/8057f0587683ed2fcd4dbfbdfdfa807b9160b809976099d36b8f60d08f03/nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 99.1/99.1 kB 1.8 MB/s eta 0:00:00
Collecting triton==2.3.0 (from torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/db/ee/8d50d44ed5b63677bb387f4ee67a7dbaaded0189b320ffe82685a6827728/triton-2.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (168.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 168.1/168.1 MB 1.8 MB/s eta 0:00:00
Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.9.0->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ff/ff/847841bacfbefc97a00036e0fce5a0f086b640756dc38caea5e1bb002655/nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.1/21.1 MB 4.7 MB/s eta 0:00:00
Requirement already satisfied: wcwidth<0.3.0,>=0.2.12 in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from ftfy->open_clip_torch) (0.2.13)
Collecting packaging>=20.9 (from huggingface-hub->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/49/df/1fceb2f8900f8639e278b056416d49134fb8d84c5942ffaa01ad34782422/packaging-24.0-py3-none-any.whl (53 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.5/53.5 kB 3.6 MB/s eta 0:00:00
Collecting pyyaml>=5.1 (from huggingface-hub->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/29/61/bf33c6c85c55bc45a29eee3195848ff2d518d84735eb0e2d8cb42e0d285e/PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (705 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 705.5/705.5 kB 1.7 MB/s eta 0:00:00
Requirement already satisfied: requests in /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages (from huggingface-hub->open_clip_torch) (2.28.1)
Collecting safetensors (from timm->open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8f/05/969e1a976b84283285181b00028cf73d78434b77a6627fc2a94194cca265/safetensors-0.4.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 2.2 MB/s eta 0:00:00
INFO: pip is looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while.
Collecting torchvision (from open_clip_torch)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d4/7e/d41b771dbffa927b9cc37372b1e18c881348cd18a0e4ad73f2c6bdf56c0e/torchvision-0.18.0-cp310-cp310-manylinux1_x86_64.whl (7.0 MB)

du -sh /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages/
5.5G

pip install -r "requirements.txt" –prefer-binary -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r "requirements_versions.txt" –prefer-binary -i https://pypi.tuna.tsinghua.edu.cn/simple

du -sh /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages/
6.6G /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages/

pip install -r repositories/CodeFormer/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/BLIP/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/k-diffusion/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/stable-diffusion-stability-ai/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

error: can’t find Rust compiler
ERROR: Failed building wheel for tokenizers
这个错误通常是由于缺少Rust编译器引起的,因为tokenizers库需要Rust编译器来构建。
apt-get install build-essential

git clone https://github.com/huggingface/tokenizers.git

安装rust

echo "export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static" >> ~/.bashrc
echo "export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup" >> ~/.bashrc
source ~/.bashrc
curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh

ust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish

pip install tokenizers

载入
source $HOME/.cargo/env

   SSL error: unknown error; class=Ssl (16)
  error: cargo metadata --manifest-path Cargo.toml --format-version 1 failed with code 101
  -- Output captured from stdout:

模型

下载基础模型放在 ./models/Stable-diffusion/
下载VAE模型放在 ./models/VAE/

先做一个共享目录
cd /workspace/common/
cp -ar /workspace/stable-diffusion-webui/models /workspace/common/
mv /workspace/stable-diffusion-webui/models /workspace/stable-diffusion-webui/models_org
ln -s /workspace/common/models/ /workspace/stable-diffusion-webui/models

退出docker,在宿主机复制现有模型到内部
docker cp Stable-diffusion/anything-v5-PrtRE.safetensors ub:/workspace/common/models/Stable-diffusion/

进docker
python -u ./launch.py –listen –port 5001 –use-cpu all –skip-torch-cuda-test –precision full –no-half –no-half-vae –api –skip-install

Python 3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0]
Version: 1.6.0
Commit hash: e5a634da06c62d72dbdc764b16c65ef3408aa588
Launching Web UI with arguments: --listen --port 5001 --use-cpu all --skip-torch-cuda-test --precision full --no-half --no-half-vae --api
WARNING:xformers:WARNING[XFORMERS]: xFormers can't load C++/CUDA extensions. xFormers was built for:
    PyTorch 2.3.0+cu121 with CUDA 1201 (you have 2.1.0+cpu)
    Python  3.10.14 (you have 3.10.10)
  Please reinstall xformers (see https://github.com/facebookresearch/xformers#installing-xformers)
  Memory-efficient attention, SwiGLU, sparse and more won't be available.
  Set XFORMERS_MORE_DETAILS=1 for more details
No module 'xformers'. Proceeding without it.
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
Loading weights [7f96a1a9ca] from /workspace/stable-diffusion-webui-cpu/models/Stable-diffusion/anything-v5-PrtRE.safetensors
Creating model from config: /workspace/stable-diffusion-webui-cpu/configs/v1-inference.yaml
Running on local URL:  http://0.0.0.0:5001

To create a public link, set `share=True` in `launch()`.
Startup time: 5.7s (import torch: 2.4s, import gradio: 0.6s, setup paths: 0.7s, other imports: 0.3s, load scripts: 0.7s, create ui: 0.7s, gradio launch: 0.2s).
Applying attention optimization: InvokeAI... done.
Model loaded in 4.9s (load weights from disk: 0.8s, create model: 0.6s, apply weights to model: 2.7s, apply float(): 0.7s).
[W NNPACK.cpp:64] Could not initialize NNPACK! Reason: Unsupported hardware.

浏览器访问

http://127.0.0.1:5001
ersion: 1.6.0  •  python: 3.10.10  •  torch: 2.1.0+cpu  •  xformers: N/A  •  gradio: 3.41.2

报错

libGL.so.1

libGL.so.1: cannot open shared object file: No such file or directory

apt install mesa-utils

libgthread-2.0.so.0

libgthread-2.0.so.0: cannot open shared object file: No such file or directory

apt-get install libglib2.0-0

torchvision.transforms.functional_tenso

No module named ‘torchvision.transforms.functional_tensor’

pip list |grep torch

clip-anytorch             2.6.0
dctorch                   0.1.2
open-clip-torch           2.20.0
pytorch-lightning         1.9.4
torch                     2.3.0
torchaudio                2.1.0+cpu
torchdiffeq               0.2.3
torchmetrics              1.4.0
torchsde                  0.2.5
torchvision               0.18.0

重新安装支持cpu的torch版本
pip install torch==2.1.0 torchvision==0.16.0 torchaudio –index-url https://download.pytorch.org/whl/cpu

pip list |grep torch

clip-anytorch             2.6.0
dctorch                   0.1.2
open-clip-torch           2.20.0
pytorch-lightning         1.9.4
torch                     2.1.0+cpu
torchaudio                2.1.0+cpu
torchdiffeq               0.2.3
torchmetrics              1.4.0
torchsde                  0.2.5
torchvision               0.16.0+cpu

clip-vit-large-patch14

OSError: Can’t load tokenizer for ‘openai/clip-vit-large-patch14’. If you were trying to load it from ‘https://huggingface.co/models‘, make sure you don’t have a local directory with the same name. Otherwise, make sure ‘openai/clip-vit-large-patch14’ is the correct path to a directory containing all relevant files for a CLIPTokenizer tokenizer.

下载clip-vit-large-patch14 放到以下

tree /root/.cache/huggingface/hub/models--openai--clip-vit-large-patch14/
/root/.cache/huggingface/hub/models--openai--clip-vit-large-patch14/
|-- blobs
|-- refs
|   `-- main
`-- snapshots
    `-- 8d052a0f05efbaefbc9e8786ba291cfdf93e5bff
        |-- config.json
        |-- merges.txt
        |-- special_tokens_map.json
        |-- tokenizer_config.json
        `-- vocab.json

4 directories, 6 files

制作 完整版docker 镜像

docker ps

CONTAINER ID   IMAGE          COMMAND   CREATED        STATUS        PORTS     NAMES
0180e369be03   ubuntu:22.04   "bash"    19 hours ago   Up 19 hours             ub

docker commit -m "ubuntu:22.04,stable-diffusion,cpu" ub mysd:cpu

docker images

REPOSITORY                                              TAG                                   IMAGE ID       CREATED          SIZE
mysd                                                    cpu                                   93a4de4e2952   27 seconds ago   16GB

制作优化版docker 镜像

将python包和repositories 放到宿主机。
备份出repositories
docker exec -it ub ls /workspace
docker cp ub:/workspace/common/repositories /host/cpu_repositories
840M

备份出site-package

docker cp ub:/root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages/ /host/cpu_site-package
6G

进入镜像清理

docker exec -it ub bash
apt-get autoremove && apt-get clean && rm -rf /var/lib/apt/lists/*
rm -rf /root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages/
exit

镜像

docker commit -m "ubuntu:22.04,stable-diffusion,cpu" ub mysd:cpu-slim

docker images

REPOSITORY                                              TAG                                   IMAGE ID       CREATED          SIZE
mysd                                                    cpu-slim                              9e5113633411   19 seconds ago   10GB

启动文件

vi ./start.sh

/root/miniconda3/condabin/conda init  bash
conda activate python_3.10.10
export PATH="/root/miniconda3/envs/python_3.10.10/bin:$PATH"
git config --global --add safe.directory "*"
cd /workspace/stable-diffusion-webui
python -u ./launch.py --listen --port 5001 --use-cpu all --skip-torch-cuda-test --precision full --no-half --no-half-vae --api --skip-install

启动变量

h_dir=/host # host dir
h_mdir=/host/models # host models dir
c_dir=/workspace/stable-diffusion-webui # container dir
c_mdir=/workspace/stable-diffusion-webui/models # container dir

docker run -itd –network=host –name=cpusd \
-e COMMANDLINE_ARGS=’–use-cpu all –skip-torch-cuda-test –precision full –no-half –no-half-vae –api’ \
-e CONDA_DEFAULT_ENV=’python_3.10.10′ \
-v ${h_dir}/hub/:/root/.cache/huggingface/hub \
-v ${h_dir}/cpu_site-package:/root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages \
-v ${h_mdir}/:${c_mdir}/ \
-v ${h_dir}/style.csv:${c_dir}/style.csv \
-v ${h_dir}/outputs/:${c_dir}/outputs/ \
-v ${h_dir}/localizations/:${c_dir}/localizations/ \
-v ${h_dir}/configs/:${c_dir}/configs/ \
-v ${h_dir}/config_states/:${c_dir}/config_states/ \
-v ${h_dir}/cpu_repositories/:${c_dir}/repositories/ \
-v ${h_dir}/extensions/:${c_dir}/extensions/ \
-v ${h_dir}/extensions-builtin/:${c_dir}/extensions-builtin/ \
-v ${h_dir}/webui-user.sh:${c_dir}/webui-user.sh \
-v ${h_dir}/start.sh:${c_dir}/start.sh \
mysd:cpu-slim bash -c ‘source /root/.bashrc; conda init bash; conda activate python_3.10.10;conda info –envs;/workspace/stable-diffusion-webui/start.sh’

docker exec -it cpusd bash
docker logs -f cpusd

echo ‘docker start cpusd’ >> /etc/rc.local

sd-webui-bilingual-localization 双语对照翻译插件

cd /host/extensions
git clone https://github.com/journey-ad/sd-webui-bilingual-localization /host/extensions/sd-webui-bilingual-localization

settings->bilingual-localization
本地化文件 zh-Hans(stable)
同时请将"用户界面" – “本地化翻译”选项设置为“无”

保存并重启ui

制作使用dockerfile 镜像

cd /host/
从github下载准备好代码,并从zip转成tar,方便dockerfile解压
stable-diffusion-webui-cpu.tar
stable-diffusion-webui-1.6.0.tar

FROM ubuntu:22.04

LABEL maintainer="[email protected]" \
description="Ubuntu 22.04.4 LTS,miniconda3,Stable-Diffusion-webui" 

#docker build ./ --progress=plain -t sdw_cpu:1.0

ARG RUNUID="1000"
ARG RUNGID="1000"
ARG RUNUSER="www"
ARG RUNGROUP="website"
#ARG LANG="en_US.UTF-8"
ARG LANG="zh_CN.utf8"

ENV TZ=Asia/Shanghai
ENV RUNUID=${RUNUID}
ENV RUNGID=${RUNGID}
ENV RUNUSER=${RUNUSER}
ENV RUNGROUP=${RUNGROUP}
ENV LANG=${LANG}
ENV LC_ALL=${LANG}
ENV LANGUAGE=${LANG}

WORKDIR /workspace

USER root
RUN     groupadd -g $RUNUID $RUNGROUP && \
    useradd -u $RUNGID -G $RUNGROUP  -s /sbin/nologin $RUNUSER && \
    apt-get update && apt-get -y install --reinstall ca-certificates && \
    sed -i 's/http:\/\/.*ubuntu.com/https:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    apt-get update && apt-get update && apt-get install -y sudo net-tools inetutils-ping procps curl wget vim telnet locales git zhcon  \
    fonts-wqy-microhei fonts-wqy-zenhei xfonts-wqy lrzsz unzip tree mesa-utils libglib2.0-0 && \
    apt-get autoremove && apt-get clean && rm -rf /var/lib/apt/lists/*  && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    sed -i 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen && locale-gen && \
    echo "$TZ" > /etc/timezone && echo "alias ll='ls -l'" >> /etc/profile && \
    cd /root && mkdir -p ~/miniconda3 && wget https://repo.anaconda.com/miniconda/Miniconda3-py310_22.11.1-1-Linux-x86_64.sh -O ~/miniconda.sh  && \
    bash ~/miniconda.sh -b -u -p ~/miniconda3 && ~/miniconda3/bin/conda init bash && . /root/.bashrc && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ && \
    conda config --set show_channel_urls yes && conda create -n python_3.10.10 python=3.10.10 && conda activate python_3.10.10 && \
    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip install --upgrade pip setuptools wheel && \
    cd /workspace && mkdir -p /workspace/common/{repositories,models}  
    #git clone https://github.com/openvinotoolkit/stable-diffusion-webui.git ./stable-diffusion-webui-cpu 
ADD stable-diffusion-webui-cpu.tar .
ADD stable-diffusion-webui-1.6.0.tar .
RUN  ln -s  stable-diffusion-webui-cpu  stable-diffusion-webui 

WORKDIR /workspace/stable-diffusion-webui 
EXPOSE 5001

#CMD ["/opt/bin/entry_point.sh"]
CMD ["tail","-f","/dev/null"]

docker build ./ –progress=plain -t sdw_cpu:1.0
镜像可以缩到1.6G

docker run -itd --network=host --name=cpusd \
-e COMMANDLINE_ARGS='--use-cpu all --skip-torch-cuda-test --precision full --no-half --no-half-vae  --api' \
-e CONDA_DEFAULT_ENV='python_3.10.10' \
-v ${h_dir}/hub:/root/.cache/huggingface/hub \
-v ${h_dir}/cpu_site-package:/root/miniconda3/envs/python_3.10.10/lib/python3.10/site-packages \
-v ${h_mdir}/:${c_mdir}/ \
-v ${h_dir}/style.csv:${c_dir}/style.csv \
-v ${h_dir}/outputs/:${c_dir}/outputs/ \
-v ${h_dir}/localizations/:${c_dir}/localizations/ \
-v ${h_dir}/configs/:${c_dir}/configs/ \
-v ${h_dir}/config.json:${c_dir}/config.json \
-v ${h_dir}/config_states/:${c_dir}/config_states/ \
-v ${h_dir}/cpu_repositories/:${c_dir}/repositories/ \
-v ${h_dir}/extensions/:${c_dir}/extensions/ \
-v ${h_dir}/extensions-builtin/:${c_dir}/extensions-builtin/ \
-v ${h_dir}/webui-user.sh:${c_dir}/webui-user.sh \
-v ${h_dir}/start.sh:${c_dir}/start.sh \
sdw_cpu:1.0  bash  -c 'source /root/.bashrc; conda init bash; conda activate python_3.10.10;conda info --envs;/workspace/stable-diffusion-webui/start.sh'

docker exec -it cpusd bash

Posted in AIGC, 技术.

Tagged with , , , , .


JumpServer v2 v3版本单机及多可用docker Compose离线安装

JumpServer v2 v3版本单机及多可用docker Compose离线安装

JumpServer简介

JumpServer是FIT2CLOUD飞致云旗下品牌
JumpServer 开源堡垒机是一款运维安全审计系统产品,提供身份验证、授权控制、账号管理、安全审计等功能支持,帮助企业 快速构建运维安全审计能力。
JumpServer 使用 Python 开发,配备了业界领先的 Web Terminal 方案,交互界面美观、用户体验好。
JumpServer 采纳分布式架构,支持多机房跨区域部署,支持横向扩展,无资产数量及并发限制。

官方网址

https://www.jumpserver.org/

在线体验

https://demo.jumpserver.org/

当前最新版本

v3.10.7-lts
2024-03-27 10:25:04
v3.10.5-lts
2024-03-05 16:43:19
v2.28.21
2023-10-26 18:28:00
JumpServer V2版本(社区版)维护支持截止日期为2023年12月31日,JumpServer V2版本(企业版)维护支持截止日期为2025年12月31日.推荐安装v3版本。

企业版带X-Pack 增强包,按资产数包年付费

https://www.fit2cloud.com/jumpserver/enterprise.html

近期漏洞

JumpServer作业管理中文件管理批量传输功能存在不安全直接对象引用(IDOR)漏洞,CVE编号为CVE-2024-29024
JumpServer v3.0.0-v3.10.6版本
JumpServer 密码重置漏洞(CVE-2023-42820):
2.24 <= jumpserver v 2.x <= 2.28.20
jumpserver v 3.x <= 3.6.4
JumpServer 任意密码重置漏洞(CVE-2023-43650):
2.24 <= jumpserver v 2.x <= 2.28.20
jumpserver v 3.x <= 3.6.4
Jumpserver 目录遍历漏洞(CVE-2023-42819):
3.0.0 <= jumpserver <= 3.6.4
JumpServer koko 远程命令执行漏洞(CVE-2023-43651):
2.24 <= jumpserver v 2.x <= 2.28.20
jumpserver v 3.x <= 3.6.4

环境依赖

  • Linux x86_64
  • Kernel 大于 4.0
  • Python>= 3.8版本必须3.6及以上
  • mysql>=5.7 版本必须大于5.6,需要TLS/SSL
  • redis>=6 支持Sentinel,不支持Cluster,需要TLS/SSL

all-in-one 部署方式不支持 Client 相关功能, 仅支持在 纯 B/S 架构 Web 端使用

  • 外置数据库要求 MariaDB 版本大于等于 10.6;
  • 外置 Redis 要求 Redis 版本大于等于 6.2。

组件项目

项目  状态  描述
Lina    Lina release    JumpServer Web UI 项目
Luna    Luna release    JumpServer Web Terminal 项目 Lina 和 Luna 为纯静态文件,最终由 nginx 整合
KoKo    Koko release    JumpServer 字符协议 Connector 项目,替代原来 Python 版本的 Coco
koko组件实现了SSH Server 和 Web Terminal Server的组件,提高SSH和WebSocket接口,使用Paramiko和Flask开发,可以让我们在线去像xshell一样在网页上去敲命令。他是golang编程语言开发的。和之前的coco组件(py写的)相比,性能更高。

Lion    Lion release    JumpServer 图形协议 Connector 项目,依赖 Apache Guacamole
Guacamole 为 RDP 协议和 VNC 协议资产组件, 用户可以通过 Web Terminal 来连接 RDP 协议和 VNC 协议资产 (暂时只能通过 Web Terminal 来访问)

Magnus  Magnus release  JumpServer 数据库代理 Connector 项目
Clients Clients release JumpServer 客户端 项目
Installer   Installer release   JumpServer 安装包 项目

端口说明

Jumpserver 默认 Web 端口为 8080/tcp, 默认 WS 端口为 8070/tcp, 配置文件 jumpserver/config.yml
koko 默认 SSH 端口为 2222/tcp, 默认 Web Terminal 端口为 5000/tcp 配置文件在 koko/config.yml
Guacamole 默认端口为 8081/tcp, 配置文件 /config/tomcat9/conf/server.xml
Nginx 默认端口为 80/tcp
Redis 默认端口为 6379/tcp
Mysql 默认端口为 3306/tcp

安装方式

Linux部署jumpserver堡垒机及问题汇总
https://blog.csdn.net/weixin_48878440/article/details/130836286?utm_medium=distribute.pc_relevant. none-task-blog-2~default~baidujs_baidulandingword~default-1-130836286-blog-98357973.235^v43^pc_blog_bottom_relevance_base1&spm=1001.2101.3001.4242.2&utm_relevant_index=4

[快速安装]https://docs.jumpserver.org/zh/master/install/setup_by_fast/
[负载均衡安装]https://docs.jumpserver.org/zh/master/install/setup_by_lb/
[编译安装]https://docs.jumpserver.org/zh/master/dev/build/#_5

  • 方式一 标准安装分在线和离线,在线安装就是下载包后进行离线安装,
    用docker compose管理多个docker,可以集成mysql和redis,也可以支持外部mysql和redis
    安装完成后有./jmsctl.sh脚本进行控制。

  • 方式二 all-in-one的dockifle,将db以外的服务装入一个docker中,使用docker自已控制,不支持 Client 相关功能.

  • 方式三 helm方式为k8s中安装。

  • 方式四 编译安装每个组件。

使用方式一标准离线安装。
标准离线安装内置mysql
标准离线安装外置mysql

基础准备

系统准备

cat /etc/redhat-release 
Rocky Linux release 9.3 (Blue Onyx)

selinux

sestatus
cat /etc/selinux/config
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
setenforce 0

网卡ip

ip a

Rocky Linux9之前
文件路径:/etc/sysconfig/network-scripts/
文件名格式:ifcfg-ens33
Rocky Linux9之后
/etc/NetworkManager/system-connections/
ens33.nmconnection

cat /etc/NetworkManager/system-connections/ens3.nmconnection
systemctl status NetworkManager //查看NetworkManager状态
systemctl restart NetworkManager //开启NetworkManager

ping www.baidu.com

更改主机名

hostnamectl set-hostname jump1

配置时间和ntp

timedatectl
timedatectl set-timezone Asia/Shanghai

cat /etc/chrony.conf |grep server

路由转发和优化

modprobe br_netfilter

cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_keepalive_intvl=30
net.ipv4.tcp_keepalive_probes=10
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0 
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
net.ipv4.ip_local_port_range= 32768 60999
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
net.ipv4.tcp_tw_reuse=0
net.ipv4.tcp_max_tw_buckets=6000
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_synack_retries=2
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.netfilter.nf_conntrack_max=2310720
#net.netfilter.nf_conntrack_tcp_timeout_established=300
#net.netfilter.nf_conntrack_buckets=655360
net.ipv4.neigh.default.gc_thresh1=1024
net.ipv4.neigh.default.gc_thresh2=4096
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv6.neigh.default.gc_thresh1=8192
net.ipv6.neigh.default.gc_thresh2=32768
net.ipv6.neigh.default.gc_thresh3=65536
net.core.netdev_max_backlog=16384

net.core.rmem_max = 16777216 
net.core.wmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 8096 
net.core.somaxconn = 32768 
fs.inotify.max_user_instances=8192 
fs.inotify.max_user_watches=524288 
fs.file-max=52706963
fs.nr_open=52706963
kernel.pid_max = 4194303
net.bridge.bridge-nf-call-arptables=1
vm.swappiness=0 
vm.overcommit_memory=1 
vm.panic_on_oom=0 
vm.max_map_count = 262144
EOF

sysctl -p
sysctl –system
sysctl -a|grep vm.max_map_count

宿主ulimit

cat /etc/security/limits.conf

cat > /etc/security/limits.d/20-nofile.conf <<EOF
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
cat > /etc/security/limits.d/20-nproc.conf <<EOF
*    -     nproc   65535
root soft  nproc  unlimited
root hard  nproc  unlimited
EOF
echo "ulimit -HSn 65535" >> /etc/rc.local
ulimit -a 
sysctl -p

system.conf

cat /etc/systemd/system.conf|grep DefaultLimitNOFILE

vi /etc/systemd/system.conf

DefaultLimitNOFILE=65335

或者sed替换

cat /etc/systemd/system.conf|grep DefaultLimitNOFILE

sed -n ‘s/#DefaultLimitNOFILE=/DefaultLimitNOFILE=65335/p’ /etc/systemd/system.conf

sed -i ‘s/^#DefaultLimitNOFILE=/DefaultLimitNOFILE=65335/’ /etc/systemd/system.conf

#rocky9
sed -n 's/#DefaultLimitNOFILE=1024/DefaultLimitNOFILE=65335/p' /etc/systemd/system.conf
sed -i 's/^#DefaultLimitNOFILE=1024/DefaultLimitNOFILE=65335/' /etc/systemd/system.conf
systemctl daemon-reexec

rocky9上打开加密兼容

兼容高低版本的ssh互连

update-crypto-policies --show
update-crypto-policies --set LEGACY

设置 swappiness(可选)

设置swappiness,控制运行时内存的相对权重,过多的交换空间会引起GC耗时的激增.
临时设置指令:

sysctl -w vm.swappiness=10

cat /proc/sys/vm/swappiness
永久设置指令:

echo vm.swappiness = 10 >> /etc/sysctl.conf

关闭透明大页面transparent_hugepage(可选)

临时关闭指令1:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

临时关闭指令2:

echo never > /sys/kernel/mm/transparent_hugepage/defrag

永久关闭,配置信息落盘到配置文件,机器重新有效。

vi /etc/rc.d/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled;
then echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag;
then echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

chmod +x /etc/rc.d/rc.local

yum

RHEL 8 / CentOS 8上的软件包管理工具 DNF(Dandified YUM)已设置为默认值。
但是,[yum]命令也作为指向[dnf]的链接而存在,因此可以以相同的用法使用[yum]或[dnf]。

# 设置镜像变量
MIRROR=mirrors.aliyun.com/rockylinux
# 执行替换
sed -i.bak \
-e "s|^mirrorlist=|#mirrorlist=|" \
-e "s|^#baseurl=|baseurl=|" \
-e "s|dl.rockylinux.org/\$contentdir|$MIRROR|" \
/etc/yum.repos.d/rocky*.repo

更新缓存

#yum makecache
#yum list
dnf makecache
dnf list

安装基码组件

dnf install -y bash-completion vim lrzsz expect net-tools nc nmap tree dos2unix iotop unzip openldap-devel 
dnf install -y wget curl telnet make gcc perl rpm-build zlib zlib-devel gcc-c++ make autoconf automake  pcre-devel pam-devel
dnf install -y gettext iptables iptables-services
dnf install -y glibc  #可选
dnf install -y openssl openssl-devel #可选

关闭自带 firewall,切换成iptables

systemctl stop firewalld
systemctl mask firewalld
systemctl disable firewalld

#显示当前 firewall状态 
systemctl status firewalld

开启iptables

#添加入开机自启
systemctl enable iptables
#启动 iptables
systemctl start iptables
#显示当前 iptables 状态
systemctl status iptables.service
#开放端口(根据自己需求删减)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

将iptables 中 reject-with icmp-host-prohibited 删除,影响mysql连接
iptables -D INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -D FORWARD -j REJECT --reject-with icmp-host-prohibited

iptables -nL

#保存防火墙配置
service iptables save

#设置默认规则
#iptables -P INPUT DROP  //设置filter表INPUT默认规则是 DROP  

内网中这里不设置默认DROP,默认都充许通过。

#显示活动的服务列表
systemctl -t service

python-pip安装

可以这里手功安装也可以让脚本自动安装

python -V
Python 3.9.18

dnf install python-pip
pip -V
pip 21.2.3 from /usr/lib/python3.9/site-packages/pip (python 3.9)

ssh -V
OpenSSH_8.7p1, OpenSSL 3.0.7 1 Nov 2022

更换pip3源

mkdir ~/.pip && cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
EOF

安装docker

cd && mkdir src && cd src

脚本安装失败

curl -fsSL https://get.docker.com -o install-docker.sh
sh install-docker.sh --dry-run --mirror Aliyun
ERROR: Unsupported distribution 'rocky'

添加docker用户和组

vi /etc/group
将docker gid指定成1000
groupadd -g 1000 docker
useradd -u 1000 -g docker -s /sbin/nologin docker

usermod -a -G docker andychu

安装yum-utils

dnf install yum-utils

添加docker仓库

dnf config-manager \
   --add-repo \
   https://download.docker.com/linux/centos/docker-ce.repo

安装最新版本的docker

#安装docker包括:docker Engine, containerd, and Docker Compose
dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

配置docker-compose环境变量

echo 'export PATH="/usr/libexec/docker/cli-plugins:$PATH"' >> /etc/profile
#加载环境变量使其生效
source /etc/profile

添加软链接

ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/bin/

docker更改为国内腾讯镜像源

mkdir -p /etc/docker
cd /etc/docker
tee /etc/docker/daemon.json<<-'EOF'
{
"registry-mirrors": [
        "https://mirror.ccs.tencentyun.com"
]
}
EOF
cat /etc/docker/daemon.json

增加limit

# containerd.service
sed -i 's/LimitNOFILE=infinity/LimitNOFILE=65535/' /usr/lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl restart containerd

docker服务

# 重启daemon
systemctl daemon-reload

#启动docker
systemctl start docker

#查看docker 是否启动成功
ps -ef|grep docker

#设置开机自动启动docker服务
systemctl enable docker.service

# 查看docker信息
 docker info
Client: Docker Engine - Community
 Version:    25.0.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.12.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.24.6
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

验证安装

docker-compose –version
Docker Compose version v2.24.6

Docker 日志文件 /var/log/docker

下载jumpserver

自行选择安装什么版本,这里进行测试都下载。

https://github.com/jumpserver/installer
下载离线版,需注册一下用户.
https://community.fit2cloud.com/#/download/jumpserver/v2-28-21

https://community.fit2cloud.com/#/download/jumpserver/v3-10-5-lts

wget "https://cdn0-download-offline-installer.fit2cloud.com/jumpserver/jumpserver-offline-installer-v2.28.21-amd64.tar.gz" -O jumpserver-offline-installer-v2.28.21-amd64.tar.gz

wget "https://cdn0-download-offline-installer.fit2cloud.com/jumpserver/jumpserver-offline-installer-v3.10.5-amd64.tar.gz" -O jumpserver-offline-installer-v3.10.5-amd64.tar.gz
注意JumpServer v3.0.0-v3.10.6版本 含有漏洞,请使用最新版本。

jumpserver-offline-installer-v2.28.21 离线内置单机安装

解压到/opt目录下

tar zxf jumpserver-offline-installer-v2.28.21-amd64.tar.gz -C /opt/
cd /opt
ln -s jumpserver-offline-installer-v2.28.21-amd64 jumpserver
cd /opt/jumpserver-offline-installer-v2.28.21-amd64/

cat config-example.txt


# JumpServer configuration file example.
#
# 如果不了解用途可以跳过修改此配置文件, 系统会自动填入
# 完整参数文档 https://docs.jumpserver.org/zh/master/admin-guide/env/

################################## 镜像配置 ###################################
#
# 国内连接 docker.io 会超时或下载速度较慢, 开启此选项使用华为云镜像加速
# 取代旧版本 DOCKER_IMAGE_PREFIX
#
# DOCKER_IMAGE_MIRROR=1

################################## 安装配置 ###################################
#
# JumpServer 数据库持久化目录, 默认情况下录像、任务日志都在此目录
# 请根据实际情况修改, 升级时备份的数据库文件(.sql)和配置文件也会保存到该目录
#
VOLUME_DIR=/data/jumpserver

# 加密密钥, 迁移请保证 SECRET_KEY 与旧环境一致, 请勿使用特殊字符串
# (*) Warning: Keep this value secret.
# (*) 勿向任何人泄露 SECRET_KEY
#
SECRET_KEY=

# 组件向 core 注册使用的 token, 迁移请保持 BOOTSTRAP_TOKEN 与旧环境一致,
# 请勿使用特殊字符串
# (*) Warning: Keep this value secret.
# (*) 勿向任何人泄露 BOOTSTRAP_TOKEN
#
BOOTSTRAP_TOKEN=

# 日志等级 INFO, WARN, ERROR
#
LOG_LEVEL=ERROR

# JumpServer 容器使用的网段, 请勿与现有的网络冲突, 根据实际情况自行修改
#
DOCKER_SUBNET=192.168.250.0/24

# ipv6 nat, 正常情况下无需开启
# 如果宿主不支持 ipv6 开启此选项将会导致无法获取真实的客户端 ip 地址
#
USE_IPV6=0
DOCKER_SUBNET_IPV6=fc00:1010:1111:200::/64

################################# MySQL 配置 ##################################
# 外置 MySQL 需要输入正确的 MySQL 信息, 内置 MySQL 系统会自动处理
#
DB_HOST=mysql
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_NAME=jumpserver

# 如果外置 MySQL 需要开启 TLS/SSL 连接, 参考 https://docs.jumpserver.org/zh/mast                                                                                                                                                             er/install/install_security/#ssl
#
# DB_USE_SSL=True

################################# Redis 配置 ##################################
# 外置 Redis 需要请输入正确的 Redis 信息, 内置 Redis 系统会自动处理
#
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=

# 如果使用外置 Redis Sentinel, 请手动填写下面内容
#
# REDIS_SENTINEL_HOSTS=mymaster/192.168.100.1:26379,192.168.100.1:26380,192.168.                                                                                                                                                             100.1:26381
# REDIS_SENTINEL_PASSWORD=your_sentinel_password
# REDIS_PASSWORD=your_redis_password
# REDIS_SENTINEL_SOCKET_TIMEOUT=5

# 如果外置 Redis 需要开启 TLS/SSL 连接, 参考 https://docs.jumpserver.org/zh/mast                                                                                                                                                             er/install/install_security/#redis-ssl
#
# REDIS_USE_SSL=True

################################## 访问配置 ###################################
# 对外提供服务端口, 如果与现有服务冲突请自行修改
# 如果不想对外提供访问可以使用 127.0.0.1:<port>, eg: 127.0.0.1:33060
#
HTTP_PORT=80
SSH_PORT=2222
MAGNUS_PORTS=30000-30100

################################# HTTPS 配置 #################################
# 参考 https://docs.jumpserver.org/zh/master/admin-guide/proxy/ 配置
#
# HTTPS_PORT=443
# SERVER_NAME=your_domain_name
# SSL_CERTIFICATE=your_cert
# SSL_CERTIFICATE_KEY=your_cert_key
#

# Nginx 文件上传下载大小限制
#
CLIENT_MAX_BODY_SIZE=4096m

################################## 组件配置 ###################################
# 组件注册使用, 默认情况下向 core 容器注册, 集群环境需要修改为集群 vip 地址
#
CORE_HOST=http://core:8080
PERIOD_TASK_ENABLED=True

# Core Session 定义,
# SESSION_COOKIE_AGE 表示闲置多少秒后 session 过期,
# SESSION_EXPIRE_AT_BROWSER_CLOSE=true 表示关闭浏览器即 session 过期
#
# SESSION_COOKIE_AGE=86400
SESSION_EXPIRE_AT_BROWSER_CLOSE=True

# Lion 开启字体平滑, 优化体验
#
JUMPSERVER_ENABLE_FONT_SMOOTHING=True

################################# XPack 配置 #################################
# XPack 包, 开源版本设置无效
#
RDP_PORT=3389

################################## 其他配置 ##################################
# 终端使用宿主 HOSTNAME 标识, 首次安装自动生成
#
SERVER_HOSTNAME=${HOSTNAME}

# 当前运行的 JumpServer 版本号, 安装和升级完成后自动生成
#
CURRENT_VERSION=

修改config.txt

安装时会自动生成配制文件到/opt/jumpserver/config/config.txt

mkdir -p /opt/jumpserver/config/
cp ./config-example.txt /opt/jumpserver/config/config.txt

修改网段,不要和现有网段冲突

  • A类地址范围:10.0.0.0—10.255.255.255,即10.0.0.0/8
  • B类地址范围:172.16.0.0—172.31.255.555,即172.16.0.0/12
  • C类地址范围:192.168.0.0—192.168.255.255,即192.168.0.0/16

vi /opt/jumpserver/config/config.txt

DOCKER_SUBNET=172.30.75.0/24
  • 大概步骤就是,先运行脚本让你选择是否使用mysql、redis外置服务器等。然后安装docker、docker-compose,+ 紧接着下载相关镜像然后部署docker-compose项目。
  • 是否配置持久化目录(默认/opt/jumpserver)
  • 是否配置支持IPV6(默认不配置)
  • 是否配置外置MySQL数据库(默认不配置,可选择外部数据库,数据库集群等)
  • 是否配置外置Redis(默认不配置,外部redis,redis主从、集群等)
  • 是否配置对外端口(默认80)

安装,版本是在 static.env 指定的

./jmsctl.sh install

1. Check Configuration File
Path to Configuration file: /opt/jumpserver/config
/opt/jumpserver/config/config.txt        [ √ ]
/opt/jumpserver/config/nginx/cert/server.crt     [ √ ]
/opt/jumpserver/config/nginx/cert/server.key     [ √ ]
complete

>>> Install and Configure JumpServer
1. Configure Private Key
SECRETE_KEY:     MTA4kDahKurQSDV4f7CV7a2NxnvyXBin2xN9oUQz1JgKCTs4
BOOTSTRAP_TOKEN: UikuHUAnia08ZKyFHT4ZccJU
complete

2. Configure Persistent Directory
Do you need custom persistent store, will use the default directory /data/jumpserver? (y/n)  (default n):

[+] Running 1/0
 ✘ Network jms_net  Error                                                                                                                                                                                                               0.0s
failed to create network jms_net: Error response from daemon: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-e252febdc69d -j RETURN: iptables: No chain/target/match by that name.

安装后mysql和redis密码会生成到config.txt

DB_USER=root
DB_PASSWORD=BMgqGBxJoIwaqqwgwLessz6kUw
REDIS_PASSWORD=P9Z3tV6WTK0il53CYRB8oAgqT0

测试连接

docker exec -it jms_mysql bash
mysql -uroot -p
BMgqGBxJoIwaqqwgwLessz6kUw
SHOW VARIABLES LIKE '%ssl%';
+---------------------+-----------------------------+
| have_openssl        | YES                         |
| have_ssl            | DISABLED                    |

管理jumpserver服务

# 启动
$ ./jmsctl.sh start

# 重启
$ ./jmsctl.sh restart

# 关闭, 不包含数据库
$ ./jmsctl.sh stop

# 关闭所有
$ ./jmsctl.sh down

# 备份数据库
$ ./jmsctl.sh backup_db

# 查看日志
$ ./jmsctl.sh tail

# 备份
$ ./jmsctl.sh backup

配置文件说明

配置文件将会放在 /opt/jumpserver/config 中
tree .

.
├── config.txt       # 主配置文件
├── mysql
│   └── my.cnf       # mysql 配置文件
|── mariadb
|   └── mariadb.cnf  # mariadb 配置文件
├── nginx            # nginx 配置文件
│   ├── cert
│   │   ├── server.crt
│   │   └── server.key
│   ├── lb_http_server.conf
│   └── lb_ssh_server.conf
├── README.md
└── redis
    └── redis.conf  # redis 配置文件

6 directories, 11 files

Web access

http://192.168.244.10:80
Default username: admin Default password: admin

SSH/SFTP access

ssh -p2222 [email protected]
sftp -P2222 [email protected]

docker ps -a

CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS                    PORTS                                  NAMES
c0807fb55638   jumpserver/lion:v2.28.21     "./entrypoint.sh"        6 minutes ago    Up 5 minutes (healthy)    4822/tcp, 8081/tcp                     jms_lion
f387025045e3   jumpserver/koko:v2.28.21     "./entrypoint.sh"        6 minutes ago    Up 5 minutes (healthy)    0.0.0.0:2222->2222/tcp, 5000/tcp       jms_koko
baf3dff0742e   jumpserver/web:v2.28.21      "/docker-entrypoint.…"   6 minutes ago    Up 5 minutes (healthy)    0.0.0.0:80->80/tcp                     jms_web
98dc4c33df81   jumpserver/magnus:v2.28.21   "./entrypoint.sh"        6 minutes ago    Up 5 minutes (healthy)    0.0.0.0:30000-30100->30000-30100/tcp   jms_magnus
5ce9d501748b   jumpserver/core:v2.28.21     "./entrypoint.sh sta…"   6 minutes ago    Up 5 minutes (healthy)    8070/tcp, 8080/tcp                     jms_celery
a0f24cdb8f8c   jumpserver/core:v2.28.21     "./entrypoint.sh sta…"   6 minutes ago    Up 6 minutes (healthy)    8070/tcp, 8080/tcp                     jms_core
ba76aec76c38   jumpserver/redis:6.2         "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes (healthy)   6379/tcp                               jms_redis
a3167ee779f6   jumpserver/mariadb:10.6      "docker-entrypoint.s…"   14 minutes ago   Up 14 minutes (healthy)   3306/tcp                               jms_mysql

systemd开机启动

vi /usr/lib/systemd/system/jms.service

[Unit]
Description=jms
After=network.target

[Service]
Type=forking
Environment="PATH=/opt/py3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/libexec/docker/cli-plugins"
ExecStart=/opt/jumpserver/jmsctl.sh start
ExecReload=/opt/jumpserver/jmsctl.sh restart
ExecStop=/opt/jumpserver/jmsctl.sh down
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
#启动服务
systemctl start jms.service
#设置开机自启动
systemctl enable jms.service
systemctl status jms.service
systemctl stop jms.service

失败,不能启动

systemv添加权限并设置开机自启

chmod +x /etc/rc.local
echo '/opt/jumpserver/jmsctl.sh start' >> /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service
#检查状态:
systemctl status rc-local.service
#更改自启脚本后,需要重新运行一下命令已使其生效
systemctl enable rc-local
systemctl start rc-local.service

访问地址

http://localhost
root/Pass1234
test/Pass123456

JumpServer 客户端

http://localhost/core/download/
JumpServer 客户端,目前用来唤起 特定客户端程序 连接资产, 目前仅支持 RDP SSH 客户端,Telnet 会在未来支持

=======================

jumpserver redis和mysql外部高可用安装

测试服务器

这里以四台机器进行测试
20C,8G,100G规格 rocky9.3
服务器名称   IP地址    备注
jump01      192.168.5.101   docker
jump02      192.168.5.102   docker+Sentinel+nfs
jumpdb01    192.168.5.103   Sentinel+redis副本节点+mysql master
jumpdb02    192.168.5.104   Sentinel+redis主节点+mysql slave

说明

jumpserver web 以docker方式部署在jump01和jump02,安装docker版jumperver,复制config目录,可以随时横向扩展。
数据放在jump02上通过nfs进行共享,正式使用应使用cephfs等更可靠方式。
redis使用主从2个主从节点+3个Sentinel,在jumpserver 的config中配置sentinel就行.如使用TLS验证那需要每年给全部客户端更换证书。
mysql使用主从复制,并可选配制SSL连接.

外部 redis+Sentinel 安装略过

获取Sentinel 的ip及访问密钥

mysql安装

jumpdb01+jumpdb02 上安装

添加用户

#groupadd mysql
#useradd mysql -M -g mysql -s /sbin/nologin

创建数据目录

mkdir -p /mysql/{data,tmpdir,logs}
chown -R mysql:mysql /mysql
chmod -R 775 /mysql

rpm安装 mysql-community-common-5.7.44-1

cd /root/src
wget http://mirrors.ustc.edu.cn/mysql-repo/mysql57-community-release-el7.rpm
yum -y localinstall mysql57-community-release-el7.rpm
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server

卸载自带的connector包

报错

file /etc/my.cnf from install of mysql-community-server-5.7.44-1.el7.x86_64 conflicts with file from package mariadb-connector-c-config-3.2.6-1.el9_0.noarch

rpm -qa | grep mariadb
mariadb-connector-c-config-3.2.6-1.el9_0.noarch
mariadb-connector-c-3.2.6-1.el9_0.x86_64

yum remove mariadb-connector-c-config-3.2.6-1.el9_0
或者
rpm -e –nodeps mariadb-connector-c-config-3.2.6-1.el9_0.noarch
rpm -e –nodeps mariadb-connector-c-3.2.6-1.el9_0.x86_64

上传本地my.cnf,这里不提供

修改其中

bind-address              = 0.0.0.0

初始化mysql

initialize-insecure的作用
initialize-insecure是MySQL安装程序的一个选项,它用于初始化MySQL数据库,使其处于可用状态。与其他初始化选项不同,initialize-insecure是一个不安全的选项,它将跳过常规的安全设置,如设置root密码和创建SSL证书。因此,只有在测试环境或临时环境中才应该使用这个选项

#mysqld --initialize-insecure --user=mysql  --datadir=/mysql/data --basedir=/usr  
mysqld --initialize --user=mysql  --datadir=/mysql/data --basedir=/usr

cat /mysql/logs/error.err|grep 'temporary password'  
SKg>2j*JhDBv

vi /usr/lib/systemd/system/mysqld.service
PIDFile=/mysql/data/mysql.pid
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/mysql/data/mysql.pid $MYSQLD_OPTS

启动测试

更改本地root密码
新增admin远程用户
新增jumserver远程用户

mysqld &  
mysql -uroot -p  
SKg>2j*JhDBv  

set password for root@localhost = password('X1lrffa234b'); 
flush privileges;

GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'X1lrffa234b' WITH GRANT OPTION;
flush privileges;

create database jumpserver default charset 'utf8';
Query OK, 1 row affected (0.00 sec)

mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> create user 'jumpserver'@'%' identified by 'fwJTx622g';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on jumpserver.* to 'jumpserver'@'%';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

MySQL 5.7 新增默认账号 mysql.session和mysql.sys
mysql.infoschema:系统用户,管理和访问系统自带的information_schema数据库
mysql.session:mysql的插件将会使用该用户访问mysql数据库服务器。客户端不能直接使用该用户进行链接
mysql.sys:该用户避免数据库管理重命名或者删除root用户时发生的问题,客户端不能直接使用该用户进行链接
root:mysql的超级用户,用于管理mysql数据库,拥有所有权限,可执行任何操作,不建议使用该用户操作mysql数据库。

select * from sys.session\G;

启动 MySQL

systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
systemctl stop mysqld

查看server_id,各mysql需唯一

mysql -S /mysql/data/mysql.sock -e "select @@server_id" -uroot -p
| @@server_id |
+-------------+
|         75102 |

记得修改server_id

iptables方式,根所系统中服务二选一

iptables -A INPUT -p tcp -s 192.168.5.0/24 -m multiport --dports 3306 -j ACCEPT
service iptables save

firewall 方式,根所系统中服务二选一

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.5.0/24" port protocol="tcp" port="3306" accept"
firewall-cmd --reload

文件权限

chown mysql:mysql /etc/my.cnf

jumpdb02上安装mysql

同上,略过

jumpdb01 master上添加repl复制用户

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'g43^2rfsfRF';
FLUSH PRIVILEGES;

show processlist\G;
SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

jumpdb02 slave从机上配制复制

mysql -uroot -p
X1lrffa234b
# 配制复制
CHANGE MASTER TO
MASTER_HOST='192.168.5.102',
MASTER_USER='repl',
MASTER_PASSWORD='g43^2rfsfRF',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154;

# 开始复制
START SLAVE;

# 在slave上查看
show slave status\G;
 Slave_IO_State: Connecting to master
                  Master_Host: 192.168.5.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes

SHOW PROCESSLIST;
show processlist\G;

主从复制完成

ssl主从复制查看,mysql是否支持have_ssl

SHOW VARIABLES LIKE '%ssl%';
+-------------------------------------+-----------------+
| Variable_name                       | Value           |
+-------------------------------------+-----------------+
| have_openssl                        | YES             |
| have_ssl                            | YES             |
| performance_schema_show_processlist | OFF             |
| ssl_ca                              | ca.pem          |
| ssl_capath                          |                 |
| ssl_cert                            | server-cert.pem |
| ssl_cipher                          |                 |
| ssl_crl                             |                 |
| ssl_crlpath                         |                 |
| ssl_key                             | server-key.pem  |
+-------------------------------------+-----------------+

测试ssl,可以连本机,但ssl不能连slave

mysql -h192.168.5.102 -uadmin -p
mysql -h192.168.5.103 -uadmin -p
mysql --ssl-ca=/mysql/data/ca.pem -h192.168.5.102 -uadmin -p
mysql --ssl-ca=/mysql/data/ca.pem -h192.168.5.103 -uadmin -p

查看是否是ssl连接

SHOW SESSION STATUS LIKE 'Ssl_cipher';
 \s;

在主 mysql jumpdb01 创建SSL/RSA 文件,并分发给从机

mysql_ssl_rsa_setup  --user=mysql  --basedir=/mysql/ssl

cd /mysql/data/
scp ca.pem client-cert.pem client-key.pem [email protected]:.

mysql主从增加证书路径

vi /etc/my.cnf
修改主配置文件,在[mysqld]中添加如下几项

ssl_ca=ca.pem
ssl_cert=client-cert.pem
ssl_key=client-key.pem

mysql主从复制证书并重启生效

systemctl stop mysqld

ll /root/*.pem
cp /root/*.pem /mysql/data/

systemctl start mysqld

测试复制

mysql -uroot -p
X1lrffa234b

show slave status\G;

STOP SLAVE;
show slave status\G;

         Slave_IO_State: 
            Master_Host: 192.168.5.102
            Master_User: repl
            Master_Port: 3306
          Connect_Retry: 60
        Master_Log_File: mysql-bin.000006
    Read_Master_Log_Pos: 28170819
         Relay_Log_File: relay-bin.000013
          Relay_Log_Pos: 27207
  Relay_Master_Log_File: mysql-bin.000006
       Slave_IO_Running: No
      Slave_SQL_Running: No
        Replicate_Do_DB: 

CHANGE MASTER TO
MASTER_HOST='192.168.5.102',
MASTER_USER='repl',
MASTER_PASSWORD='g43^2rfsfRF',
MASTER_LOG_FILE='mysql-bin.000006',
MASTER_LOG_POS=28170819,
master_ssl=1,
master_ssl_cert='client-cert.pem',
master_ssl_key='client-key.pem',
master_ssl_ca='ca.pem';

START SLAVE;

主从ssl复制完成

如何设置某用户使用强制使用ssl

仅做示例,这里不强制

//新建用户
mysql> grant select on *.* to 'dba'@'%' identified by 'xxx' REQUIRE SSL; 
//修改用户
mysql> ALTER USER 'dba'@'%' REQUIRE SSL; 
flush privileges;

jumpserver web 端ssl

在jumpdb01上复制mysql的ssl证书给jumpserver web端

cd /mysql/data/
scp ca.pem [email protected]:.
scp ca.pem [email protected]:.

在jump01,jump02,当前web 前端还没安装,后续操作

mv ca.pem /opt/jumpserver/config/certs/db_ca.pem

vi /opt/jumpserver/config/config.txt

# 在配置文件配置使用 DB SSL
DB_USE_SSL=True

重新启动web服务

cd /opt/jumpserver
./jmsctl.sh down
./jmsctl.sh start

完成web数据库 SSL 连接

=======

nfs 共享数据节点

在jump02 192.168.5.101 上

yum install nfs-utils rpcbind
 ```

### 启动 NFS

systemctl enable rpcbind nfs-server
systemctl start rpcbind nfs-server

mkdir /data
chmod 777 -R /data


vi /etc/exports

设置 NFS 访问权限, /data 是刚才创建的将被共享的目录, 192.168.100. 表示整个 192.168.100. 的资产都有括号里面的权限

也可以写具体的授权对象 /data 192.168.100.30(rw,sync,no_root_squash) 192.168.100.31(rw,sync,no_root_squash)

注意挂载时用192.168.5.*会报没有权限

/data 192.168.5.0/24(rw,insecure,sync,no_root_squash)

```
exportfs -a
exportfs -rv

showmount -e
Export list for jump02.c1g.test.sh.local:
/data 192.168.5.0/24
rpcinfo -p

nfs客户端

jump01 192.168.5.100 上

安装

yum -y install nfs-utils

开启服务

systemctl enable rpcbind.service
systemctl start rpcbind.service
systemctl status rpcbind.service

修改默认挂载为Soft方式

echo 'Soft=True' >> /etc/nfsmount.conf

查看

showmount -e 192.168.5.101
Export list for 192.168.5.101:
/data 192.168.5.0/24

挂载

mkdir -p /opt/jumpdata
mount -t nfs 192.168.5.101:/data /opt/jumpdata
mount -t nfs -o nolock -o tcp 192.168.5.101:/data /opt/jumpdata
ll /opt/jumpdata

# 可以写入到 /etc/fstab, 重启自动挂载. 注意: 设置后如果 nfs 损坏或者无法连接该服务器将无法启动
echo "192.168.5.101:/data /opt/jumpdata nfs defaults 0 0" >> /etc/fstab

=========

jumpserver 前端web安装

jumpserver 离线版安装

分发离线文件到各web节点

scp -P 22 -r  jumpserver-offline-installer-v3.10.5-amd64.tar.gz [email protected]:.

解压到/opt目录下

cd /root/src/
tar zxvf jumpserver-offline-installer-v3.10.5-amd64.tar.gz -C /opt/
cd /opt

ln -s jumpserver-offline-installer-v3.10.5-amd64 jumpserver
cd /opt/jumpserver-offline-installer-v3.10.5-amd64/

mkdir -p /opt/jumpserver/config/
cp ./config-example.txt /opt/jumpserver/config/config.txt

获取SECRET_KEY

很重要,记录下来,后续使用.

if [ "$SECRET_KEY" = "" ]; then SECRET_KEY=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 50` ; echo "SECRET_KEY=$SECRET_KEY" >> ~/.bashrc; echo $SECRET_KEY; else echo $SECRET_KEY; fi
5SV8k2BoYHGltlwJeQxMFDsQO0wFUzV2Dg33qa8N2exbpeHMuh

if [ "$BOOTSTRAP_TOKEN" = "" ]; then BOOTSTRAP_TOKEN=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 16`; echo "BOOTSTRAP_TOKEN=$BOOTSTRAP_TOKEN" >> ~/.bashrc; echo $BOOTSTRAP_TOKEN; else echo $BOOTSTRAP_TOKEN; fi
vBgHciALfP8dkQtU

修改jumpserver配制

修改SECRET_KEY,BOOTSTRAP_TOKEN,DOCKER_SUBNET
修改mysql及redis配制

vi /opt/jumpserver/config/config.txt

VOLUME_DIR=/opt/jumpdata
SECRET_KEY=5SV8k2BoYHGltlwJeQxMFDsQO0wFUzV2Dg33qa8N2exbpeHMuh
BOOTSTRAP_TOKEN=vBgHciALfP8dkQtU
DOCKER_SUBNET=172.30.75.0/24

DB_HOST=192.168.5.102
DB_PORT=3306
DB_USER=jumpserver
DB_PASSWORD=kx8wJTr8
DB_NAME=jumpserver

REDIS_SENTINEL_HOSTS=mymaster1/192.168.5.103:26379,192.168.5.102:26379,192.168.5.101:26379
RDIS_SENTINEL_PASSWORD=VBHMpbPXGltlwJe
REDIS_PASSWORD=VBHMpbPXGltlwJe
REDIS_SENTINEL_SOCKET_TIMEOUT=5

安装jumpser web端

注意确认安装过程中的信息

./jmsctl.sh install

django.db.utils.OperationalError: (1045, "Access denied for user 'jumpserver'@'192.168.5.100' (using password: YES)")
[ERROR] Failed to change the table structure!

重新设密码,可能密码中有$会出错

mysql -h 192.168.5.102 -uadmin -p 
X1lrffa234b
SET PASSWORD FOR 'jumpserver'@'%' = PASSWORD('fwJTx622g');
flush privileges;

注意交互输入

  File "/opt/py3/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 223, in get_connection_params
    kwargs["port"] = int(settings_dict["PORT"])
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'y'
[ERROR] Failed to change the table structure!
>>> The Installation is Complete
1. You can use the following command to start, and then visit
cd /opt/jumpserver
./jmsctl.sh start

2. Other management commands
./jmsctl.sh stop
./jmsctl.sh restart
./jmsctl.sh backup
./jmsctl.sh upgrade
For more commands, you can enter ./jmsctl.sh --help to understand

3. Web access
http://172.30.75.1:80
Default username: admin  Default password: admin

4. SSH/SFTP access
ssh -p2222 [email protected]
sftp -P2222 [email protected]

5. More information
Official Website: https://www.jumpserver.org/
Documentation: https://docs.jumpserver.org/

防火墙iptables方式

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -j ACCEPT
iptables -A INPUT -p tcp --dport 33060 -j ACCEPT
iptables -A INPUT -p tcp --dport 33061 -j ACCEPT
service iptables save

防火墙firewall方式

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --permanent --zone=public --add-port=2222/tcp
firewall-cmd --permanent --zone=public --add-port=33060/tcp
firewall-cmd --permanent --zone=public --add-port=33061/tcp

访问密码

http://localhost

admin/Pass1234
test/Pass1234

Posted in 安全, 容器.

Tagged with , , .


JumpServer多个高危漏洞安全风险通告v3.0.0-v3.10.6

2024年3月,有用户反馈发现JumpServer开源堡垒机存在安全漏洞,并向JumpServer开源项目组进行上报。此次发现的漏洞为:

■ JumpServer作业管理中Ansible Playbook文件存在Jinja2模板代码注入导致的Celery容器远程执行漏洞,CVE编号为CVE-2024-29202。漏洞详情:https://github.com/jumpserver/jumpserver/security/advisories/GHSA-2vvr-vmvx-73ch。 13

■ JumpServer作业管理中Ansible Playbook文件存在参数验证缺陷导致的Celery容器远程执行漏洞,CVE编号为CVE-2024-29201。漏洞详情:Insecure Ansible playbook validation leads to RCE in Celery · Advisory · jumpserver/jumpserver · GitHub 5 。

■ JumpServer作业管理中Ansible Playbook文件存在用户权限未隔离的漏洞,CVE编号为CVE-2024-29020。漏洞详情:An authorized attacker can get sensitive information in playbook files when playbook_id is leaked · Advisory · jumpserver/jumpserver · GitHub 3 。

■ JumpServer作业管理中文件管理批量传输功能存在不安全直接对象引用(IDOR)漏洞,CVE编号为CVE-2024-29024。漏洞详情:https://github.com/jumpserver/jumpserver/security/advisories/GHSA-8wqm-rfc7-q27q。 4

以上漏洞影响版本为:

JumpServer v3.0.0-v3.10.6版本

安全版本为:

JumpServer版本>=v3.10.7版本

修复方案

**■ 永久修复方案:**升级JumpServer软件至上述安全版本。

**■ 临时修复方案:**关闭作业中心功能。关闭作业中心功能的具体步骤为:

以管理员身份登录至JumpServer堡垒机。依次选择“系统设置”→“功能设置”→“任务中心”,在打开的页面中关闭作业中心功能。

Posted in 安全通告.


OpenSSH ProxyCommand命令注入漏洞(CVE-2023-51385)

背景描述

OpenSSH是SSH(Secure SHell)协议的免费开源实现。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据,并由此来代替原来的类似服务。

OpenSSH发布了安全更新,修复了一个命令注入漏洞,编号命名为CVE-2023-51385。攻击者可以利用该漏洞注入恶意shell字符进行命令注入攻击。

该漏洞利用条件较为苛刻,但由于漏洞利用POC已公开,建议受影响用户及时进行修补更新,做好相关防护措施。

二、严重等级

高危

三、漏洞描述

该漏洞与配置文件ssh_config有关,攻击者可以修改配置文件中的ProxyCommand、LocalCommand、Match exec相关命令,通过%h、%p或类似的扩展标记引用主机名或用户名,在建立SSH连接时,注入恶意shell字符进行命令注入攻击。

、影响范围

OpenSSH < 9.6

五、CVE-2023-51385 验证方法:

首先需要在~/.ssh/config增加如下

host *.example.com
ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p
.gitmodules文件语句中存在命令注入

url = ssh://echo helloworld > cve.txtfoo.example.com/bar
配置完成后,执行下面的指令触发

git clone https://github.com/zls1793/CVE-2023-51385_test –recurse-submodules
如果成功执行将会在CVE-2023-51385_test目录下生成cve.txt文件

六、解决方案

目前官方已修复该漏洞,受影响用户请根据自身情况尽快更新至对应的安全版本:https://www.openssh.com/openbsd.html

该漏洞具体详情可参考如下链接:

https://vin01.github.io/piptagole/ssh/security/openssh/libssh/remote-code-execution/2023/12/20/openssh-proxycommand-libssh-rce.html

Posted in 安全通告.

Tagged with .


Linux平台主流压缩工具XZ被曝后门,标记为 CVE-2024-3094

该漏洞发生在压缩软件包xz,红帽已经该漏洞标记为 CVE-2024-3094
Red Hat 警告表示:

目前的调查表明,这些软件包仅存在于 Red Hat 社区生态系统内的 Fedora 41 和 Fedora Rawhide 中,Red Hat Enterprise Linux (RHEL) 的所有版本均不受影响。
我们已经在适用于 Debian unstable(Sid)发行版的 XZ 5.6.x 版本中找到相关证据,证明存在后门,可以注入相关代码。

Debian 安全团队今天也发布公告,表示当前没有发现有稳定版 Debian 使用问题 XZ 软件包,在受影响的 Debian 测试版、不稳定版和实验版中,XZ 已被还原为上游的 5.4.5 代码。

Fedora 41 中最新版本的 xz 5.6.0/5.6.1 工具和库中包含恶意代码,可能允许未经授权的远程系统访问。
这篇文章详细的介绍了整个过程, https://boehs.org/node/everything-i-know-about-the-xz-backdoor

rocky9.3 不受影响
xz -V
xz (XZ Utils) 5.2.5
liblzma 5.2.5

参考:https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2024-3094

Posted in 安全通告.


OpenSSH ssh-agent 客户端远程代码执行漏洞CVE-2023-38408

漏洞描述

SSH-Agent是SSH的一部分,它是一个用于管理私钥并支持公钥身份验证的程序。

用户使用 SSH-Agent 转发代理功能连接攻击者恶意服务器时,由于 SSH-Agent 未对加载的共享库进行限制,攻击者可通过将恶意共享库作为参数传递给 SSH-Agent 并通过其调用dlopen/dlclose函数加载/卸载位于用户客户端主机的共享库,实现远程代码执行。

影响范围

ssh-agent@(-∞, 9.3_p2)

openssh@(-∞, 9.3p2-1)

解决方案

为了有效解决和防范CVE-2023-38408,请遵循以下综合步骤:

首先升级到OpenSSH 9.3p2或更高版本:升级到最新版本的OpenSSH至关重要,因为它包含缓解漏洞的关键补丁。确保所有相关系统和服务器及时更新至推荐版本或更高版本。

另外采取预防措施来避免被利用:

建议在仅仅OpenSSH用于远程主机管理的机器,通过Openssh配置(sshd_config)、防火墙,安全组ACL等限制来源访问IP为白名单仅可信IP地址,同时,非必要,关闭SSH代理转发功能,禁止在有关主机启用ssh隧道等。

关闭SSH代理转发功能方法为:

配置/etc/ssh/sshd_config

AllowTcpForwarding NO

参考链接

https://www.oscs1024.com/hd/MPS-hwop-qus5

https://nvd.nist.gov/vuln/detail/CVE-2023-38408

Posted in 安全通告.

Tagged with , , .