写在前面
- 博文内容为
华为云欧拉操作系统入门级开发者认证(HCCDA – Huawei Cloud EulerOS)
实验笔记整理 - 认证地址:https://edu.huaweicloud.com/certificationindex/developer/9bf91efb086a448ab4331a2f53a4d3a1
- 博文内容涉及一个传统 Springboot 应用HCE部署, 以及相关数据库 Redis、Postgresql、Nginx 的容器化部署
- 理解不足小伙伴帮忙指正
不必太纠结于当下,也不必太忧虑未来,当你经历过一些事情的时候,眼前的风景已经和从前不一样了。——村上春树
当前默认已经安装了 docker 而且配置了仓库地址
redis 容器化部署
下载最新 Redis 镜像
[root@ecs-hce hce-x86-server]# cd ~ && docker pull redisUsing default tag: latestlatest: Pulling from library/redis6f28985ad184: Pull complete60e8b46025d8: Pull complete122fe26e50b0: Pull completede3ca1eb2e20: Pull complete4813a7e5bd57: Pull complete99dd8d3a66f2: Pull completeDigest: sha256:c82cacd2eee119e912ad61abe2a60b2ee728ef06fbb3c0fa1555728e4188bc69Status: Downloaded newer image for redis:latest
查看镜像
[root@ecs-hce ~]# docker imagesREPOSITORY TAG IMAGE IDCREATED SIZEhce/hce-x86-server 2024017b8fb730c9f62 minutes ago 518MBredislatesta617c1c927742 years ago 105MB
启动 Redis 容器,注意这里我们指定了运行容器名字为redis-sys
,默认情况下,同一网络,docker 会自动把当前容器名字写入 DNS 映射。
[root@ecs-hce ~]# docker run -itd--name redis-sys -p 6379:6379 redis97cf7d59fd8a40ccb370c3c899e680d744bbcc38b621182fad4c1e33fe81907c
查看容器
[root@ecs-hce ~]# docker psCONTAINER IDIMAGE COMMANDCREATED STATUSPORTSNAMES97cf7d59fd8aredis "docker-entrypoint.s…" 5 seconds ago Up 4 seconds0.0.0.0:6379->6379/tcp redis-sysad84d6222925hce/hce-x86-server:202401 "/bin/bash"2 minutes ago Up 2 minutes festive_matsumoto
进入 Redis 容器,进入 Redis 客户端,Redis 部署成功确认
[root@ecs-hce ~]# docker exec -it redis-sys /bin/bashroot@97cf7d59fd8a:/data# redis-cli127.0.0.1:6379>root@97cf7d59fd8a:/data# exit[root@ecs-hce ~]#
Postgresql 容器化部署
拉取 Postgresql 镜像
[root@ecs-hce ~]# cd ~ &&docker pull postgres:1313: Pulling from library/postgres6f28985ad184: Already exists163a60947b3a: Pull complete1791984387e5: Pull completeccf9c39579c4: Pull complete1d8dd50a5ee9: Pull complete3991abc55a94: Pull complete4cf2cdef0857: Pull completeed1bec410498: Pull complete0930368b9a14: Pull completea9302936fdb5: Pull completebb3d505cd0cb: Pull complete4f1bb2dd6f16: Pull complete8d3f6ff7b2da: Pull complete687caf1b1f9b: Pull completeDigest: sha256:b94ab3a31950e7d25654d024044ac217c2b3a94eff426e3415424c1c16ca3fe6Status: Downloaded newer image for postgres:13
查看镜像
[root@ecs-hce ~]# docker images | grep popostgres 13c5ec7353d87d2 years ago 314MB
创建容器,这里需要我们添加的变量,不同的镜像版本变量略有不同,同样通过 name 指定 DNS 域名映射。
[root@ecs-hce ~]# docker run -p 5432:5432 -it --name postgres --restart always -e POSTGRES_PASSWORD=123456 -e ALLOW_IP_RANGE=0.0.0.0/0 -v /home/postgres/data:/var/lib/postgresql -d postgres:138743d3c98d38c8c42db3beeb9745c4b182c9378e8a6f907a27d70709418a5390
进入数据库容器,创建数据库
[root@ecs-hce ~]# docker exec -it postgres bashroot@8743d3c98d38:/# psql -U postgres -WPassword:psql (13.2 (Debian 13.2-1.pgdg100+1))Type "help" for help.postgres=# create database oasys;CREATE DATABASEpostgres=# \qroot@8743d3c98d38:/# exit
拷贝 pg_hba.conf
配置文件至本机
[root@ecs-hce ~]# sudo docker cp postgres:/var/lib/postgresql/data/pg_hba.conf /home
修改 pg_hba.conf 文件,在# IPv4 local connections:后添加以下内容,并保存
pg_hba.conf 文件用于配置客户端对 PostgreSQL 数据库的连接权限,最后一行配置了一条规则,允许来自任意 IP 地址(0.0.0.0/0)的所有用户(all)以 “trust” 方式进行身份验证访问所有的数据库(all)
[root@ecs-hce ~]# vi /home/pg_hba.conf[root@ecs-hce ~]# tail -1 /home/pg_hba.confhostall all 0.0.0.0/0 trust
将 pg_hba.conf 文件拷贝回容器,进入容器,重启 postgresql 并使配置生效
[root@ecs-hce ~]# sudo docker cp /home/pg_hba.conf postgres:/var/lib/postgresql/data[root@ecs-hce ~]# docker exec -it postgres bashroot@8743d3c98d38:/# su postgrespostgres@8743d3c98d38:/$ ./usr/lib/postgresql/13/bin/pg_ctl restartwaiting for server to shut down....[root@ecs-hce ~]#[root@ecs-hce ~]#
安装 git,获取项目数据
[root@ecs-hce ~]# yum install -y git >> /dev/nullFailed to set locale, defaulting to C.UTF-8
创建 code 目录并进入,拉取代码及数据文件
[root@ecs-hce ~]# mkdir /home/code[root@ecs-hce ~]# cd /home/code[root@ecs-hce code]# git clone https://codehub.devcloud.cn-north-4.huaweicloud.com/oasys00001/oasys.gitCloning into 'oasys'...remote: Enumerating objects: 1238, done.remote: Counting objects: 100% (1238/1238), done.remote: Compressing objects: 100% (971/971), done.remote: Total 1238 (delta 213), reused 1212 (delta 202), pack-reused 0Receiving objects: 100% (1238/1238), 39.24 MiB | 58.15 MiB/s, done.Resolving deltas: 100% (213/213), done.
拷贝 SQL 数据文件至 PG 容器,并且进入容器导入数据
[root@ecs-hce code]# sudo docker cp ./oasys/oasys-pgsql-data.sql postgres:/var/lib/postgresql/data[root@ecs-hce code]# sudo docker cp ./oasys/oasys-pgsql-table.sql postgres:/var/lib/postgresql/data[root@ecs-hce code]# docker exec -it postgres bashroot@8743d3c98d38:/# psql -U postgres -d oasys -a -f /var/lib/postgresql/data/oasys-pgsql-table.sqlroot@8743d3c98d38:/# psql -U postgres -d oasys -a -f /var/lib/postgresql/data/oasys-pgsql-data.sql
部署 Springboot 项目
下载 JDK 和 HCE 镜像包
[root@ecs-hce code]# mkdir /usr/java && cd /usr/java[root@ecs-hce java]# wget https://sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com/20220411/jdk-8u321-linux-x64.tar.gz--2024-02-07 00:37:07--https://sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com/20220411/jdk-8u321-linux-x64.tar.gzResolving sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com (sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com)... 114.115.192.98, 114.115.192.27, 114.115.192.163Connecting to sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com (sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com)|114.115.192.98|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 146815279 (140M) [application/gzip]Saving to: 'jdk-8u321-linux-x64.tar.gz'jdk-8u321-linux-x64.tar.gz 100%[===========================================>] 140.01M1.24MB/sin 39s2024-02-07 00:37:46 (3.59 MB/s) - 'jdk-8u321-linux-x64.tar.gz' saved [146815279/146815279][root@ecs-hce java]# tar -zxf jdk-8u321-linux-x64.tar.gz[root@ecs-hce java]# mv jdk1.8.0_321 jdk1.8
获取 Java 程序包
[root@ecs-hce java]#wget https://sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com/20220412/oasys-0.0.1-SNAPSHOT.jar--2024-02-07 00:39:11--https://sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com/20220412/oasys-0.0.1-SNAPSHOT.jarResolving sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com (sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com)... 114.115.192.163, 114.115.192.98, 114.115.192.27Connecting to sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com (sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com)|114.115.192.163|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 43206230 (41M) [application/java-archive]Saving to: 'oasys-0.0.1-SNAPSHOT.jar'oasys-0.0.1-SNAPSHOT.jar 100%[===========================================>]41.20M 184MB/sin 0.2s2024-02-07 00:39:12 (184 MB/s) - 'oasys-0.0.1-SNAPSHOT.jar' saved [43206230/43206230]
创建 Docker 网络
创建了一个名为 “oa-net” 的 Docker 网络。该命令返回一个长字符串作为网络标识符
[root@ecs-hce java]# docker network create oa-net8858411315a892cd61bbe8b31411595dd8b013792fd05ab68806f3ca5572c3a7[root@ecs-hce java]# docker network connect oa-net postgres[root@ecs-hce java]# docker network connect oa-net redis-sys
将名为 “postgres” 的容器连接到 “oa-net” 网络中。这将使 “postgres” 容器能够与 “oa-net” 网络中的其他容器进行通信。
将名为 “redis-sys” 的容器连接到 “oa-net” 网络中。这样, “redis-sys” 容器也可以与 “oa-net” 网络中的其他容器进行通信。
通过上面的方式连接之后,即可以通过容器名字
作为域名
直接访问容器对应的服务,在最开始的版本中,docker 会在 hosts 文件主动写入映射关系,从 Docker 1.11 版本开始,Docker 不再将容器的 DNS 映射关系写入宿主机的 /etc/hosts
文件
修改 jar 包配置文件,使用 vim 打开 jar 包,这里主要修改对应的 PG
和 redis
地址
[root@ecs-hce java]# vim oasys-0.0.1-SNAPSHOT.jar[root@ecs-hce java]# vim oasys-0.0.1-SNAPSHOT.jar[root@ecs-hce java]#
输入 /application.properties
搜索该文件,并敲回车键2次进入该文件
修改spring.datasource.url
地址为jdbc:postgresql://postgres:5432/oasys
,修改spring.redis.host
地址为redis-sys
### 创建Dockerfile
[root@ecs-hce java]# vim Dockerfile[root@ecs-hce java]# cat DockerfileFROM hce/hce-x86-server:202401WORKDIR /homeCOPY jdk1.8/home/javaCOPY oasys-0.0.1-SNAPSHOT.jar /homeENVJAVA_HOME=/home/javaENV PATH=$JAVA_HOME/bin:$PATHENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jarEXPOSE 8088CMD [ "java","-jar","oasys-0.0.1-SNAPSHOT.jar"]
输入以下命令,创建docker镜像
[root@ecs-hce java]# docker build -t hce/hce_java_oa:202401 .Sending build context to Docker daemon556.2MBStep 1/9 : FROM hce/hce-x86-server:202401 ---> 7b8fb730c9f6Step 2/9 : WORKDIR /home ---> Running in 54517151e09cRemoving intermediate container 54517151e09c ---> 315f482c8b50Step 3/9 : COPY jdk1.8/home/java ---> a2fb5d6132d2Step 4/9 : COPY oasys-0.0.1-SNAPSHOT.jar /home ---> 1f96dbcdbcbaStep 5/9 : ENVJAVA_HOME=/home/java ---> Running in 0bc3c8cc82fdRemoving intermediate container 0bc3c8cc82fd ---> 778b1bc77f3dStep 6/9 : ENV PATH=$JAVA_HOME/bin:$PATH ---> Running in 559dea2ff7d0Removing intermediate container 559dea2ff7d0 ---> 4817d8859121Step 7/9 : ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar ---> Running in b0ab0b19ad6dRemoving intermediate container b0ab0b19ad6d ---> 34250c614a8cStep 8/9 : EXPOSE 8088 ---> Running in a927aa883de0Removing intermediate container a927aa883de0 ---> 78ca8e81d9afStep 9/9 : CMD [ "java","-jar","oasys-0.0.1-SNAPSHOT.jar"] ---> Running in 7b8757fd2ff4Removing intermediate container 7b8757fd2ff4 ---> fbc537581fd1Successfully built fbc537581fd1Successfully tagged hce/hce_java_oa:202401
查看镜像
[root@ecs-hce java]# docker imagesREPOSITORY TAG IMAGE IDCREATED SIZEhce/hce_java_oa202401fbc537581fd127 seconds ago927MBhce/hce-x86-server 2024017b8fb730c9f621 minutes ago518MBredislatesta617c1c927742 years ago 105MBpostgres 13c5ec7353d87d2 years ago 314MB
启动镜像
[root@ecs-hce java]# docker run -itd --name=oa-java --network=oa-net -p 8088:8088 hce/hce_java_oa:2024015d8e571ddb52f3951d2c066664b4e10fde1cee8505a95cf64c60824e9c7655c3a
这里需要注意下 --network=oa-net
参数,用于将容器连接到名为 “oa-net” 的网络。
确实启动结果
[root@ecs-hce java]# docker exec -it oa-java /bin/bash[root@5d8e571ddb52 home]# jps1 jar63 Jps[root@5d8e571ddb52 home]# exit[root@ecs-hce java]#
浏览器访问oa,输入eip:8088访问,账号soli,密码123456(eip可在华为云控制台获取)
Nginx容器化部署并配置反向代理
[root@ecs-hce java]# docker images|grep nginxnginxlatest298ec0e287602 years ago 133MB
这里需要注意下,同样 --network=oa-net
参数,用于将容器连接到名为 “oa-net” 的网络。
[root@ecs-hce java]# docker run -d --network=oa-net --name=nginx-p 80:80 nginxe0a0d8fd0a3489740e234cd9b409ffb5cfea436e76fe51e23d6b80fe3163673a[root@ecs-hce java]# docker ps | grep nge0a0d8fd0a34nginx "/docker-entrypoint.…" About a minute ago Up 59 seconds 0.0.0.0:80->80/tcp nginx
添加反向代理配置
[root@ecs-hce java]# docker exec -it nginx /bin/bashroot@e0a0d8fd0a34:/# cd /etc/nginx/conf.d/root@e0a0d8fd0a34:/etc/nginx/conf.d# apt-get updateroot@e0a0d8fd0a34:/etc/nginx/conf.d# apt-get install -y vimroot@e0a0d8fd0a34:/etc/nginx/conf.d# vim default.conf
location / {#root /usr/share/nginx/html;#indexindex.html index.htm;proxy_pass http://120.46.64.191:8088;}
验证文件是否配置正常, 重启Nginx容器
root@e0a0d8fd0a34:/etc/nginx/conf.d# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulroot@e0a0d8fd0a34:/etc/nginx/conf.d# exit[root@ecs-hce java]# docker restart nginxnginx[root@ecs-hce java]#
访问测试
博文部分内容参考
© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知
https://edu.huaweicloud.com/certificationindex/developer/9bf91efb086a448ab4331a2f53a4d3a1
© 2018-2024 liruilonger@gmail.com, All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)