【Docker实战】华为FlexusX与Docker+Nginx的高效整合之路
前言
华为FlexusX携手Docker+Nginx,高效整合,云端性能再升级!FlexusX服务器,依托华为强大的技术实力,为Docker容器与Nginx服务器提供了完美的运行环境。这一高效整合方案,不仅实现了资源的最大化利用,还大幅提升了Web应用的访问速度与稳定性。
链接直达:华为云Flexus云服务器X实例
!
Docekr的介绍
Docker是一种开源的容器化平台,它利用容器化技术将应用程序及其依赖项打包成一个独立的、可移植的容器。
容器的特点:隔离性、轻量级、可移植性、弹性伸缩、生态系统。
Docker的技术架构
在整个架构中,Docker 客户端与 Docker 主机通过 API 接口进行通信,Docker 主机负责管理容器的运行和资源隔离。镜像仓库则是存储和分享镜像的中央仓库,用户可以从中拉取镜像或将自己创建的镜像推送至其中。这样,Docker 技术架构实现了应用程序的打包、分发和部署,提供了一种便捷、可移植和可扩展的容器化环境。
华为FlexusX实例安装Docker的好处
- 为华为FlexusX实例安装Docker可提升环境隔离与可移植性,确保应用跨环境稳定运行。
- Docker的轻量级特性优化了资源利用,适合多租户或多应用环境下的高效管理。
- Docker简化部署流程,支持快速启动和自动扩展,满足业务增长需求。
- Docker促进了开发、测试与生产环境的一致性,提高了开发效率和运维便捷性。
- Docker的容器化技术增强了应用安全性,为华为FlexusX实例提供了更安全的运行环境。
Docker环境搭建
操作系统 | 内存 | 处理器 | 硬盘 | 网络 |
---|---|---|---|---|
Huawei Cloud EulerOS 2.0 | 12 GB | 4 | 50GB | 弹性IP |
安装基础软件包
# 安装必要工具
[root@flexusx-251f ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
# 或者退出重新登录,为了自动补全
[root@flexusx-251f ~]# bash
下载docker-ce.repo
本实例我们选择 华为开源镜像站 的
docker-ce.repo
文件
# 设置稳定的docker仓库(本实例我们选择华为云的docker仓库)
[root@flexusx-251f ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
--2024-08-31 21:12:30-- https://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
Resolving mirrors.huaweicloud.com (mirrors.huaweicloud.com)... 120.46.63.139, 120.46.2.67, 123.249.118.101, ...
Connecting to mirrors.huaweicloud.com (mirrors.huaweicloud.com)|120.46.63.139|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1919 (1.9K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/docker-ce.repo’
/etc/yum.repos.d/docker- 100%[================================>] 1.87K --.-KB/s in 0s
2024-08-31 21:12:34 (67.2 MB/s) - ‘/etc/yum.repos.d/docker-ce.repo’ saved [1919/1919]
[root@flexusx-251f ~]# cd /etc/yum.repos.d/
[root@flexusx-251f yum.repos.d]# ls
docker-ce.repo hce.repo
因为我们的系统是
Huawei Cloud EulerOS 2.0
,没有对应的镜像仓库,我们基于提供的centos
仓库进行修改
# 将docker-ce.repo中的$releasever 替换为具体的版本号,例如:9.9
[root@flexusx-251f yum.repos.d]# cat docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/source/stable
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/source/test
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/9.9/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
# 清理缓存
[root@flexusx-251f yum.repos.d]# yum clean all
19 files removed
# 构建缓存
[root@flexusx-251f yum.repos.d]# yum makecache
Docker CE Stable - x86_64 771 kB/s | 79 kB 00:00
HCE 2.0 base 58 MB/s | 6.1 MB 00:00
HCE 2.0 updates 82 MB/s | 14 MB 00:00
Metadata cache created.
# 至此docker仓库配置成功
安装docker
不指定版本默认安装最新版,安装之前确保主机没有安装
docker
# 查看docker版本
[root@docker ~]# yum list docker-ce --showduplicates | sort -r
# 安装docker,默认最新版
[root@flexusx-251f yum.repos.d]# yum install -y docker-ce
Last metadata expiration check: 0:00:31 ago on Sat 31 Aug 2024 09:30:10 PM CST.
Dependencies resolved.
==================================================================================================
Package Architecture Version Repository Size
==================================================================================================
Installing:
docker-ce x86_64 3:27.1.1-1.el9 docker-ce-stable 27 M
Installing dependencies:
container-selinux noarch 2:2.138-4.hce2 base 33 k
containerd.io x86_64 1.7.19-3.1.el9 docker-ce-stable 43 M
docker-ce-cli x86_64 1:27.1.1-1.el9 docker-ce-stable 7.8 M
Installing weak dependencies:
docker-buildx-plugin x86_64 0.16.1-1.el9 docker-ce-stable 14 M
docker-compose-plugin x86_64 2.29.1-1.el9 docker-ce-stable 13 M
Transaction Summary
==================================================================================================
Install 6 Packages
Total download size: 105 M
Installed size: 407 M
Downloading Packages:
(1/6): docker-buildx-plugin-0.16.1-1.el9.x86_64.rpm 1.3 MB/s | 14 MB 00:10
(2/6): docker-ce-cli-27.1.1-1.el9.x86_64.rpm 1.3 MB/s | 7.8 MB 00:06
(3/6): docker-compose-plugin-2.29.1-1.el9.x86_64.rpm 1.2 MB/s | 13 MB 00:10
(4/6): container-selinux-2.138-4.hce2.noarch.rpm 1.2 MB/s | 33 kB 00:00
(5/6): containerd.io-1.7.19-3.1.el9.x86_64.rpm 735 kB/s | 43 MB 01:00
(6/6): docker-ce-27.1.1-1.el9.x86_64.rpm 376 kB/s | 27 MB 01:12
--------------------------------------------------------------------------------------------------
Total 1.4 MB/s | 105 MB 01:12
Docker CE Stable - x86_64 29 kB/s | 1.6 kB 00:00
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <docker@docker.com>"
Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
From : https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : container-selinux-2:2.138-4.hce2.noarch 1/6
Running scriptlet: container-selinux-2:2.138-4.hce2.noarch 1/6
Installing : docker-compose-plugin-2.29.1-1.el9.x86_64 2/6
Running scriptlet: docker-compose-plugin-2.29.1-1.el9.x86_64 2/6
Installing : containerd.io-1.7.19-3.1.el9.x86_64 3/6
Running scriptlet: containerd.io-1.7.19-3.1.el9.x86_64 3/6
Installing : docker-buildx-plugin-0.16.1-1.el9.x86_64 4/6
Running scriptlet: docker-buildx-plugin-0.16.1-1.el9.x86_64 4/6
Installing : docker-ce-cli-1:27.1.1-1.el9.x86_64 5/6
Running scriptlet: docker-ce-cli-1:27.1.1-1.el9.x86_64 5/6
Installing : docker-ce-3:27.1.1-1.el9.x86_64 6/6
Running scriptlet: docker-ce-3:27.1.1-1.el9.x86_64 6/6
Verifying : containerd.io-1.7.19-3.1.el9.x86_64 1/6
Verifying : docker-buildx-plugin-0.16.1-1.el9.x86_64 2/6
Verifying : docker-ce-3:27.1.1-1.el9.x86_64 3/6
Verifying : docker-ce-cli-1:27.1.1-1.el9.x86_64 4/6
Verifying : docker-compose-plugin-2.29.1-1.el9.x86_64 5/6
Verifying : container-selinux-2:2.138-4.hce2.noarch 6/6
Installed:
container-selinux-2:2.138-4.hce2.noarch containerd.io-1.7.19-3.1.el9.x86_64
docker-buildx-plugin-0.16.1-1.el9.x86_64 docker-ce-3:27.1.1-1.el9.x86_64
docker-ce-cli-1:27.1.1-1.el9.x86_64 docker-compose-plugin-2.29.1-1.el9.x86_64
Complete!
启动
# 查看docker版本
[root@flexusx-251f ~]# docker -v
Docker version 27.1.1, build 6312585
# 启动docker服务,并设置开机自启
[root@flexusx-251f ~]# systemctl status docker.service
[root@flexusx-251f ~]# systemctl start docker.service
[root@flexusx-251f ~]# systemctl enable docker.service
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
# 运行正常
[root@flexusx-251f ~]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2024-08-31 21:38:14 CST; 12h ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 178445 (dockerd)
Tasks: 13
Memory: 177.9M
CGroup: /system.slice/docker.service
└─ 178445 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
........................
配置镜像加速器
本实例我们选择华为云镜像加速器,也可根据自身需求配置相应的镜像加速器
# 创建指定文件夹docker
[root@flexusx-251f ~]# mkdir -p /etc/docker
# 创建并配置文件
[root@flexusx-251f ~]# vim /etc/docker/daemon.json
[root@flexusx-251f ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": [ "https://2a6bf1988cb6428c877f723ec7530dbc.mirror.swr.myhuaweicloud.com" ]
}
# 重新加载配置文件
[root@flexusx-251f ~]# systemctl daemon-reload
# 重新启动docker服务
[root@flexusx-251f ~]# systemctl restart docker.service
# 查看修改镜像加速
[root@flexusx-251f ~]# docker info
.................................
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://2a6bf1988cb6428c877f723ec7530dbc.mirror.swr.myhuaweicloud.com/
Live Restore Enabled: false
Docker基本操作
进行基本命令操作,看是否存在错误
# 镜像下载
[root@flexusx-251f ~]# docker pull nginx
[root@flexusx-251f ~]# docker pull mysql
# 镜像重命名
[root@flexusx-251f ~]# docker tag nginx:latest nginx:2004
[root@flexusx-251f ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 2004 298ec0e28760 3 years ago 133MB
nginx latest 298ec0e28760 3 years ago 133MB
# 镜像的删除
[root@docker ~]# docker rmi mysql:2004
# 镜像历史信息
[root@docker ~]# docker history nginx:latest
# 镜像保存
[root@docker ~]# docker save mysql alpine > /tmp/all.tar
# 镜像导入
[root@docker ~]# docker load -i /tmp/all.tar
创建容器进行测试
# 拉取镜像
[root@flexusx-251f ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
45b42c59be33: Pull complete
d0d9e9ea897e: Pull complete
66e650438339: Pull complete
76a3dfe4406b: Pull complete
410ff9d97480: Pull complete
Digest: sha256:1a53eb723d17523512bd25c27299046cfa034cce309f4ed330c943a304513f59
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 创建容器
[root@flexusx-251f ~]# docker run -itd --name web --restart always nginx
028587f892741e201dd8070b981259b7a919c563e7f5f38f6d0d25a2be97a736
# 查看正在运行的容器
[root@flexusx-251f ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
028587f89274 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp web
# 查看容器详细信息
[root@flexusx-251f ~]# docker inspect web
.............
"5d5c32b45307462cfa0536aa016b73c8fe38846ca663380452851e59d0eec731",
"EndpointID": "098959a4d374b72fab35df3e53d27a2c04505563015491bd6c2358b5337f8b5f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DNSNames": null
}
}
}
}
]
# 访问nginx服务
[root@flexusx-251f ~]# curl -s 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
以上方式创建容器,只能在主机内部访问,那么有没有办法能够使用公网ip对外提供业务访问呢?
# 删除容器
[root@flexusx-251f ~]# docker rm -f web
web
# 重新创建容器,指定与主机绑定的端口号
[root@flexusx-251f ~]# docker run -itd --name web --restart always -p 8888:80 nginx
20e2f0f5140715f9df9ea3b30dca71673d977344b57234c879aa5c4436f76466
[root@flexusx-251f ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20e2f0f51407 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:8888->80/tcp, :::8888->80/tcp web
访问测试
配置安全组放行8888端口
然后通过宿主机浏览器访问 linux虚拟机:端口
http://192.168.100.177:8080/
修改内容再次进行测试
# 进入容器
[root@flexusx-251f ~]# docker exec -it web /bin/bash
root@20e2f0f51407:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
# 进入服务目录
root@20e2f0f51407:/# cd /usr/share/nginx/html/
root@20e2f0f51407:/usr/share/nginx/html# ls
50x.html index.html
# 备份文件
root@20e2f0f51407:/usr/share/nginx/html# cp index.html index.html.bak
x.html0e2f0f51407:/usr/share/nginx/html# sed -i 's/Welcome to nginx!/Hello Flexus X/g' index.html
root@20e2f0f51407:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello Flexus X</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Hello Flexus X</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
至此nginx服务已经通过docker成功部署
体验和感受
华为云服务器FlexusX实例
,以其卓越的性能、灵活的资源调度能力和高可靠性,成为了众多企业数字化转型的首选。而今,FlexusX与Docker+Nginx的高效整合,更是为企业用户带来了前所未有的云端效能体验。
Docker
,作为轻量级的容器化技术,让应用的部署、迁移和管理变得更加简单快捷。Nginx,作为高性能的Web服务器和反向代理服务器,以其出色的并发处理能力和低资源消耗,赢得了市场的广泛认可。当Flexus X实例与Docker+Nginx相遇,三者之间的优势互补,共同打造了一个高效、稳定的云端部署方案。
- 点赞
- 收藏
- 关注作者
评论(0)