Let's GoLang

GoLang Sharing

欢迎加入GoLang技术俱乐部~


CentOS容器环境规范化部署流程

1. 环境优化

1.1 配置yum国内源

# 这里使用阿里云yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache

1.2 安装EPEL

EPEL(Extra Packages for Enterprise Linux)是由Fedora Special Interest Group维护的Enterprise Linux(RHEL、CentOS)中经常用到的包。

yum install -y epel-release

1.3 安装常用工具

$ yum install -y vim htop lsof telnet lrzsz wget net-tools ntp bind-utils mtr

2. 安装Docker

安装docker-ce 17,以docker-ce-17.03.2为例:

# 下载Docker安装包(两个)
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

# 安装Docker
yum localinstall -y docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
yum localinstall -y docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm
docker -v # 检查版本

安装docker-ce 18,以docker-ce-18.06.3为例:

# 添加docker源
yum install -y yum-utils
yum-config-manager  --add-repo  https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker 18
yum install -y docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io
docker -v # 检查版本

安装docker-ce 19,以docker-ce-19.03.2为例:

# 添加docker源
yum install -y yum-utils
yum-config-manager  --add-repo  https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker 19
yum install -y docker-ce-19.03.2-3.el7 docker-ce-cli-19.03.2-3.el7 containerd.io
docker -v # 检查版本

设置docker为开机启动:

# CentOS 7
systemctl start docker # 开机启动Docker

运行hello-world容器,测试docker是否安装成功

$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

3. 修改镜像仓库

确认此配置文件存在:/etc/docker/daemon.json。如果没有这个文件,请自行创建。修改daemon.json如下:


# 私搭Harbor内网环境
$ vim /etc/docker/daemon.json
{
  "live-restore": true,
  "registry-mirrors": ["https://o3trwnyj.mirror.aliyuncs.com"],
  "insecure-registries":["192.168.26.101"] 
}

# 公网环境或华为云内网环境

{
  "registry-mirrors": ["https://n5n8y14v.mirror.swr.myhuaweicloud.com"]
}


# 公网环境或阿里云内网环境
$ vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://o3trwnyj.mirror.aliyuncs.com"]
}

systemctl daemon-reload   # 让SystemD重新加载配置文件
systemctl restart docker  # 重启Docker Daemon

4. 修改Docker存储目录

Docker默认的数据存储路径是/var/lib/docker,属于系统盘的一部分,镜像、容器、数据卷等都会存储在这里。但是因为多数系统的系统盘都不大,如果有挂载数据卷,请修改Docker数据存储路径。

4.1 清理环境

docker system prune命令:清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)。
docker system df命令:查看Docker的磁盘使用情况。
docker system prune -a命令:清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意,这个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉。所以使用之前一定要想清楚.。

4.2 迁移/var/lib/docker目录

(1) 停止docker服务

$ systemctl stop docker

(2) 创建新的docker目录

$ df -h # 查看系统挂载的数据卷 
$ mkdir -p /data/docker/lib

(3) 迁移/var/lib/docker目录

$ cp -R /var/lib/docker/* /data/docker/lib/

注意:如果文件内容很多的话,为了确保成功最好是一个文件一个文件的拷贝。如果在拷贝过程中报“设备上没有空间”的错误,就使用mv剪切。

(4) 修改docker启动配置

$ vim /usr/lib/systemd/system/docker.service

# --graph或-g标识已经从v17.05开始被废弃,不要再使用,改用--data-root标识
ExecStart=/usr/bin/dockerd --graph /data/docker/lib/ 
ExecStart=/usr/bin/dockerd --data-root /data/docker/lib/ 
# 或者修改daemon.json
{
  "registry-mirrors": ["https://o3trwnyj.mirror.aliyuncs.com"],
  "data-root": "/data/docker/lib/"
}

(5) 重启docker

systemctl daemon-reload 
systemctl restart docker
docker info | grep 'Docker Root Dir'

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦