Docker平台构建&nginx&tmcat容器
一.虚拟机化概念&搭建虚拟机化平台
1 虚拟化技术的概念
- 虚拟化技术主要是将物理的资源转变为逻辑上可以管理的资源, 以打破物理资源结构之间的壁垒, 让计算的元件运行在虚拟的基础上, 而不是直接运行在物理资源上
- 虚拟化技术的底层是要进行虚拟的硬件设备物理机资源, 通过虚拟化技术软件可以将物理机虚拟机虚拟生成N台虚拟机, 应用程序, 软件服务(Nginx, MySQL, Redis, MQ, ZK, CEPH, Hadoop)
2 虚拟化技术的意义
- 虚拟化技术主要是为了最大化的利用高配硬件设备的资源, 提高物理资源的利用率, 可以淘汰老, 旧服务器资源, 对老旧服务器资源进行重组, 重用, 可以实现对企业服务器资源进行统一的调度和管理, 加快企业自动化运维&Devops脚本进程
自动化运维: 软件层, 硬件层, 系统层, 网络层, 四层面上体现
3 虚拟化技术的种类
- VMware Esxi;
- XEN;
- KVM;
- Open-vz
- Hyper-v;
- Docker;
4 Docker虚拟化技术的概念
- Docker是众多虚拟化技术中的一种, 是目前互联网IT企业使用最多的虚拟化技术软件, 其特点是: 轻量级, 高性能, 安全, 稳定等, 主要是为了解决企业轻量级操作系统和应用容器资源而诞生的
5 Docker官方部署Docker平台的底层Linux操作系统对应的Linux内核版本至少3.8+, 在实际的企业生产环境中, 推荐Linux内核3.10+, 对应的Linux操作系统发行版本;
RHEL7.x; CentOS 7.x; Ubuntu16.x
[root@gs ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@gs ~]# uname -a
Linux gs 3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@gs ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 909M 0 909M 0% /dev
tmpfs 919M 0 919M 0% /dev/shm
tmpfs 919M 8.6M 911M 1% /run
tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/vda1 40G 2.1G 36G 6% /
tmpfs 184M 0 184M 0% /run/user/0
6 基于centos 7.x Linux操作, 使用yum工具进行部署docker虚拟化平台, 主要部署指令和方法如下:
1.安装第三方扩展源epel-release;
yum install -y epel-release;
ll /etc/yum.repos.d/lgrep -w epel
[root@gs ~]# yum install -y epel-release
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00
epel | 4.7 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
(1/7): base/7/x86_64/group_gz | 153 kB 00:00
(2/7): epel/x86_64/group_gz | 98 kB 00:00
(3/7): epel/x86_64/primary_db | 7.0 MB 00:00
(4/7): extras/7/x86_64/primary_db | 249 kB 00:00
(5/7): epel/x86_64/updateinfo | 1.0 MB 00:00
(6/7): base/7/x86_64/primary_db | 6.1 MB 00:01
(7/7): updates/7/x86_64/primary_db | 19 MB 00:04
Package epel-release-7-14.noarch already installed and latest version
Nothing to do
[root@gs ~]# ll /etc/yum.repos.d|grep -w epel
-rw-r--r-- 1 root root 927 Feb 10 2022 epel.repo
-rw-r--r-- 1 root root 1358 Sep 5 2021 epel.repo.rpmnew
-rw-r--r-- 1 root root 1457 Sep 5 2021 epel-testing.repo
-
内核版本升级
yum update -y
2.安装Docker软件包
yum install docker -y
[root@gs ~]# yum install docker -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
...
yajl.x86_64 0:2.0.4-4.el7
Complete!
3.检测Docker软件是否部署成功
yum list docker
rpm -qalgrep docker
[root@gs ~]# yum list docker
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
: manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
Installed Packages
docker.x86_64 2:1.13.1-209.git7d71120.el7.centos @extras
[root@gs ~]# rpm -qalgrep docker
rpm: one type of query/verify may be performed at a time
4.启动docker引擎服务
systemctl start docker.service
[root@gs ~]# systemctl start docker.service
# 不让用service启动
[root@gs ~]# service docker.service start
Redirecting to /bin/systemctl start docker.service
5.查看docker版本信息;
docker --version
[root@gs ~]# docker --version
Docker version 1.13.1, build 7d71120/1.13.1
docker version
[root@gs ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Experimental: false
二.docker创建容器(Nginx,Tomcat)
-
docker部署完毕能够正常运行
-
docker虚拟化主要是为了解决企业轻量级服务器操作系统和应用容器而诞生的, 其产物是docker容器, docker容器也被称为轻量级虚拟机, 在企业中主要有两种应用方式
-
启动应用容器(软件服务
-
启动Guest OS(操作系统
-
-
基于Docker虚拟化平台, 直接启动应用容器, 启动一台Nginx WEB服务器平台, 启动Nginx服务, 同时对外监听80端口, 用户通过浏览器能够实现web页面访问.
- 在Docker仓库中搜索Nginx镜像;
# 这里通过搜索发现第一个使用最多, 那就我们也用这个 [root@gs ~]# docker search nginx INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/nginx Official build of Nginx. 17846 [OK] docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 181 docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 149 [OK] docker.io docker.io/ubuntu/nginx Nginx, a high-performance reverse proxy & ... 71 ...
-
从Docker仓库中下载镜像
docker pull xxxxx
[root@gs ~]# docker pull docker.io/nginx Using default tag: latest Trying to pull repository docker.io/library/nginx ... latest: Pulling from docker.io/library/nginx 3f4ca61aafcd: Pull complete 50c68654b16f: Pull complete 3ed295c083ec: Pull complete 40b838968eea: Pull complete 88d3ab68332d: Pull complete 5f63362a3fa3: Pull complete Digest: sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286 Status: Downloaded newer image for docker.io/nginx:latest
-
查看Nginx镜像列表和文件路径
docker images
docker images | grep -i nginx
ll /var/lib/docker/image
[root@gs ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 1403e55ab369 4 hours ago 142 MB [root@gs ~]# docker images | grep -i nginx docker.io/nginx latest 1403e55ab369 4 hours ago 142 MB [root@gs ~]# ll /var/lib/docker/image/ total 4 drwx------ 5 root root 4096 Dec 21 23:44 overlay2
-
镜像查看
docker images
[root@gs ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 1403e55ab369 4 hours ago 142 MB
-
删除镜像
docker rmi -f xxxxx
[root@gs ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 1403e55ab369 4 hours ago 142 MB [root@gs ~]# docker rmi -f 1403e55ab369 Untagged: docker.io/nginx:latest Untagged: docker.io/nginx@sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286 Deleted: sha256:1403e55ab369cd1c8039c34e6b4 d47ca40bbde39c371254c7cba14756f472f52 Deleted: sha256:0274f249eda4c376bde7cbe0b719ea3aef10201846d7262f37f7a0fc0b4fcf90 Deleted: sha256:e01fc49cb889c5dd6b11390e9863ba00f886315c5a403ee5955fb5c88d2aa576 Deleted: sha256:b2a367ee540c5d40c704fdece005b422f55f85a61b96a25bd99d6847669958a0 Deleted: sha256:2c1c6d39cbcc4767b0798aacc03f203951057e77c5edebca1fdfbcd4997f2919 Deleted: sha256:d260638126e1d2d3202dec36b67f124624fbcdad3afedd334e7260bf75dad8da Deleted: sha256:8a70d251b65364698f195f5a0b424e0d67de81307b79afbe662abd797068a069
- 启动Nginx镜像容器
docker run -itd -p 80:80 docker.io/nginx
- 通过浏览器访问宿主机IP+80端口(默认80端口)
# 查看服务 [root@gs ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e805b12bef2a docker.io/nginx "/docker-entrypoin..." 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp epic_bartik # 查看容器内部ip地址 [root@gs ~]# docker inspect e805b12bef2a | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", # 访问docker内ip地址, 如下成功 [root@gs ~]# curl 172.17.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
三.Nginx容器负载均衡Tomcat集群&动静分离
- 拉去tomcat容器
[root@gs ~]# docker pull docker.io/tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
0e29546d541c: Pull complete
9b829c73b52b: Pull complete
cb5b7ae36172: Pull complete
6494e4811622: Pull complete
668f6fcc5fa5: Pull complete
dc120c3e0290: Pull complete
8f7c0eebb7b1: Pull complete
77b694f83996: Pull complete
0f611256ec3a: Pull complete
4f25def12f23: Pull complete
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for docker.io/tomcat:latest
- 启动tomcat容器
[root@gs ~]# docker run -itd -p 8080:8080 docker.io/tomcat
33fe85dc1bce2e24b466d0198948b824c89dec297cabc540a734070121da5a7c
[root@gs ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33fe85dc1bce docker.io/tomcat "catalina.sh run" 6 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp zealous_gates
e805b12bef2a docker.io/nginx "/docker-entrypoin..." 9 minutes ago Up 9 minutes 0.0.0.0:80->80/tcp epic_bartik
- 访问网页
[root@gs ~]# curl 172.17.0.1:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>[root@gs ~]#
标签:tmcat,gs,00,nginx,io,docker,root,Docker
From: https://www.cnblogs.com/zgsgo/p/16997525.html