0 系统配置

0.1 静态网络配置

0.1.1 查看Network信息

以VMware+CentOS 7为例打开VMware的虚拟网络编辑器,找到类型为NAT模式的Network,点击NAT设置
image.png
image.png

0.0.2 设置Centos网络

查看Cnetos网络
ip addr
image.png
编辑配置文件
**vi /etc/sysconfig/network-scripts/ifcfg-ens33**

  1. TYPE=Ethernet
  2. PROXY_METHOD=none
  3. BROWSER_ONLY=no
  4. BOOTPROTO=static
  5. DEFROUTE=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_FAILURE_FATAL=no
  11. IPV6_ADDR_GEN_MODE=stable-privacy
  12. NAME=ens33
  13. UUID=5b21dde1-b451-41e9-ac94-b3aaa329daa4
  14. DEVICE=ens33
  15. ONBOOT=yes
  16. IPADDR=192.168.182.5
  17. PREFIX=24
  18. GATEWAY=192.168.182.2
  19. DNS1=192.168.182.2
  20. IPV6_PRIVACY=yes
  21. ZONE=public

重启网络
service network restart
验证网络是否通畅
ping www.baidu.com -c 3
image.png

1 安装docker

1.1 离线安装

官网教程

1.1.1 下载地址

https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz

  1. # 解压docker
  2. tar -xvf docker-20.10.9.tgz
  3. # 移动文件到/usr/bin目录
  4. cp docker/* /usr/bin/

1.1.2 docker注册为service

vi /etc/systemd/system/docker.service
将下面的内容拷贝至docker.service文件中

  1. [Unit]
  2. Description=Docker Application Container Engine
  3. Documentation=https://docs.docker.com
  4. After=network-online.target firewalld.service
  5. Wants=network-online.target
  6. [Service]
  7. Type=notify
  8. ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375
  9. ExecReload=/bin/kill -s HUP $MAINPID
  10. LimitNOFILE=infinity
  11. LimitNPROC=infinity
  12. TimeoutStartSec=0
  13. Delegate=yes
  14. KillMode=process
  15. Restart=on-failure
  16. StartLimitBurst=3
  17. StartLimitInterval=60s
  18. [Install]
  19. WantedBy=multi-user.target

1.2.3 启动

# 加文件权限
chmod +x /etc/systemd/system/docker.service
# 重载unit配置文件
systemctl daemon-reload
# 启动Docker
systemctl start docker
# 设置开机自启
systemctl enable docker.service
# 查看docker版本
docker -v

1.2 线上安装

官方教程

1.2.1 移除以前docker相关包

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

1.2.2 配置yum源

sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1.2.3 安装docker

sudo yum install -y docker-ce docker-ce-cli containerd.io


#以下是在安装k8s的时候使用
yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7  containerd.io-1.4.6

1.2.4 修改配置文件

开启远程访问,在ExecStart行添加”tcp://0.0.0.0:2375”配置

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

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

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

1.2.5 启动

systemctl enable docker --now

1.3 配置加速

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://g6xn8hnd.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2 安装docker-compose

2.1 二进制安装

# github下载
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

# github太慢可以用daocloud下载
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

# 添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 测试安装结果
docker-compose --version

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

2.2 pip安装

sudo pip install docker-compose

2.3 离线安装

下载最新版本后上传服务器:https://github.com/docker/compose/releases/

# 移动到目录
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

# 添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 测试安装结果
docker-compose --version

3 安装harbor

3.1 下载

# 下载
wget https://storage.googleapis.com/harbor-releases/release-2.5.0/harbor-offline-installer-v2.5.0.tgz

# 解压
tar -xvf harbor-offline-installer-v2.5.0.tgz -C /home

3.2、openssl证书

mkdir /home/harbor/ssl

# CA
openssl req \
    -newkey rsa:4096 -nodes -sha256 -keyout ca.key \
    -x509 -days 365 -out ca.crt \
    -subj '/C=CN/ST=Hunan/L=Changsha/O=Ysstech/CN=docker.ysstech.com'

# 签名
openssl req \
    -newkey rsa:4096 -nodes -sha256 -keyout harbor.key \
    -out harbor.csr \
    -subj '/C=CN/ST=Hunan/L=Changsha/O=Ysstech/CN=docker.ysstech.com'

# 生成证书
echo 'subjectAltName = IP:192.168.182.4' > extfile.cnf
openssl x509 -req -days 365 -in harbor.csr \
  -CA ca.crt -CAkey ca.key -CAcreateserial \
  -extfile extfile.cnf -out harbor.crt

# 客户端证书
openssl x509 -inform PEM -in harbor.crt -out harbor.cert

3.3、配置harbor.yml

cd /home/harbor

cp harbor.yml.tmpl harbor.yml

修改以下内容


hostname: docker.ysstech.com

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 8888

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /home/harbor/ssl/harbor.crt
  private_key: /home/harbor/ssl/harbor.key

# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: root123

# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123
  # The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
  max_idle_conns: 100
  # The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
  # Note: the default number of connections is 1024 for postgres of harbor.
  max_open_conns: 900

# The default data volume
data_volume: /home/harbor/data

# Log configurations
log:
  # options are debug, info, warning, error, fatal
  level: info
  # configs for logs in local storage
  local:
    # Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
    rotate_count: 50
    # Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes.
    # If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G
    # are all valid.
    rotate_size: 200M
    # The directory on your host that store log
    location: /home/harbor/log

3.2 、Docker配置

cd /home/harbor/ssl

# 创建目录并复制证书
mkdir -p /etc/docker/certs.d/docker.ysstech.com
cp harbor.cert /etc/docker/certs.d/docker.ysstech.com
cp harbor.key /etc/docker/certs.d/docker.ysstech.com
cp ca.crt /etc/docker/certs.d/docker.ysstech.com

#重启docker
systemctl restart docker

3.4、启动与停止

# 此命令将会停止 up 命令所启动的容器,并移除网络(慎用)
docker-compose down -v

# 启动
docker-compose up -d

# 停止
docker-compose stop

# 启动
docker-compose start

3.5、上传镜像

# 登录Username:admin,Password:root123
docker login https://docker.ysstech.com:8888

# tag
tag docker-demo:V1.1 docker.ysstech.com/library/docker-demo:V1.1

# 推送镜像
docker push docker.ysstech.com/library/docker-demo:V1.1