Dockerizing Services - TheOpenCloudEngine/uEngine-cloud GitHub Wiki

ํ˜„์žฌ๊นŒ์ง€์˜ ์˜ˆ์ œ๋“ค์€ local์—์„œ port number๋ฅผ ํ•˜์—ฌ, spring-boot:run์„ ํ•˜์—ฌ ์‹คํ–‰์„ ํ•˜์˜€๋‹ค.
์ด์ œ ํ•œ local ๋จธ์‹ ์—์„œ ์ž๋™์œผ๋กœ port number๋ฅผ ๋ถ€์—ฌํ•˜๊ณ , ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ
์›Œํฌ๋กœ๋“œ ๋ถ„์‚ฐ ์—”์ง„์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. (workload distribution engine)

workload distribution engine

1๋ฒˆ ํ•ญ๋ชฉ๋“ค์€ microservice ๋“ค ์ด๋‹ค.
3๋ฒˆ ํ•ญ๋ชฉ์€ ์‹ค์ œ ๋จธ์‹ ์ด๋‹ค. 1์ฐจ์ ์œผ๋กœ ์ด ํ•˜๋“œ์›จ์–ด๋“ค์„ ๊ฐ€์ƒ๋จธ์‹ ์œผ๋กœ ํฌ์žฅ์„ ํ•˜์—ฌ 2๋ฒˆ ํ•ญ๋ชฉ์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
๊ทธ๋ฆผ์—๋Š” 10๊ฐœ์ •๋„์˜ ๊ฐ€์ƒ๋จธ์‹ ์˜ ํ’€(VM)๋กœ ์„ค์ •์„ ํ•˜์˜€๋‹ค. ์•„๋งˆ์กด์˜ EC2๊ฐ€ ๊ทธ๋ฆผ์˜ 3๋ฒˆ๊ณผ 2๋ฒˆ ์˜์—ญ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
1๊ฐœ์˜ ํ’€์„ ๋ชจ๋‘ ์‚ฌ์šฉ ํ•˜๋ฉด ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ƒ๋จธ์‹ ์˜ ํ’€์— ์‹ค์ œ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋“ค์„ ์ž˜๊ฐœ ์ชผ๊ฐœ์„œ ์œ„์น˜์‹œํ‚จ๋‹ค. ๋นจ๊ฐ„์ƒ‰ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ 1๊ฐœ์— ๋ชจ๋‘ ์ฑ„์šธ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ฌ๋Ÿฌ๊ฐœ์— ๋ถ„์‚ฐํ•˜์—ฌ ์œ„์น˜ ์‹œํ‚จ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
๊ฐ VM์•ˆ์— workload agent๊ฐ€ ์ˆจ์–ด์ ธ ์žˆ์–ด์„œ, health check๋ฅผ ๊ณ„์† ํ•œ๋‹ค.
์ด๋Ÿฌํ•œ ์ž‘์—…๋“ค์„ ์ž๋™ํ™” ์‹œํ‚จ ๊ฒƒ์ด ์›Œํฌ๋กœ๋“œ ๋ถ„์‚ฐ ์—”์ง„์ด๋ผ๊ณ  ํ•œ๋‹ค.

์›Œํฌ๋กœ๋“œ ๋ถ„์‚ฐ ์—”์ง„์€ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.
์ตœ๊ทผ์— ๊ฐ€์žฅ ๋œจ๊ณ ์žˆ๋Š” ์—”์ง„์€ Kubernetes ์ด๋‹ค. Kubernetes๋Š” ํƒœ์–ด๋‚œ์ง€ ์–ผ๋งˆ ์•ˆ๋˜์—ˆ์ง€๋งŒ google์—์„œ ์‚ฌ์šฉ์ค‘์ด๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ตญ๋‚ด์— ๋งŽ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ๋Š” ์—”์ง„์€ twiter์—์„œ ์‚ฌ์šฉ์ค‘์ธ DC/OS ๊ฐ€ ์žˆ๊ณ ,
๊ฐ€์žฅ ํ• ์•„๋ฒ„์ง€ ๊ฒฉ์ด cloudfoundry ๋‹ค. ์ง€๊ธˆ๋„ ๋งŽ์ด ์“ฐ์ด๊ณ  ์žˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ docker๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์˜์—ญ์€ 2๋ฒˆํ•ญ๋ชฉ์˜ ๋นจ๊ฐ„์ƒ‰ box๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
์œ ๋ฆฌ๋ฒฝ์ฒ˜๋Ÿผ ๊ธˆ๋ฐฉ๊ธˆ๋ฐฉ ๋งŒ๋“ค์—ˆ๋‹ค๊ฐ€, ์‚ฌ๋ผ์ง€๋Š” ์กด์žฌ์ด๋‹ค. ๋ฌผ๋ฆฌ์ ์ธ ํ˜•ํƒœ๋Š” CPU์•ˆ์˜ ํ•˜๋‚˜์˜ Process์ด๋‹ค.
์ด๊ฒƒ๋“ค์„ ์ž˜ managingํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด Docker Engine์ด๊ณ , Docker์—์„œ ์ž์ฒด์ ์ธ ์›Œํฌ๋กœ๋“œ ๋ถ„์‚ฐ ์—”์ง„์„
๋งŒ๋“ ๊ฒƒ์ด docker Swarm ์ด๋‹ค. Kubernetes ์™€ ๊ฒฝ์Ÿ์„ ํ•˜๋‹ค๊ฐ€ ๋ฐ€๋ฆฐ ํ›„ ํ˜„์žฌ๋Š” ๊ต์œก์šฉ ๋ชฉ์ ์œผ๋กœ ๋งŽ์ด ์“ฐ์ธ๋‹ค.

docker ์„ค์น˜

์—ฌ๊ธฐ์„œ๋Š” linux centos7.2 ๊ธฐ์ค€์œผ๋กœ ์„ค์น˜๋ฅผ ํ•˜๋„๋ก ํ•œ๋‹ค.

## ๋„์ปค ์„ค์น˜
$ curl -fsSL https://get.docker.com/ | sudo sh
$ docker version
## ๋„์ปค ์‹คํ–‰
$ sudo systemctl start docker

Dockerizing Services

Dockerize๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ธฐ์กด์— ์œ ์ง€ํ•˜๊ณ  ์žˆ๋˜ service๋ฅผ docker ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ”๊พผ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
์ž‘์€ ์˜๋ฏธ์—์„œ๋Š” ๋‹จ์ˆœํžˆ docker image ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๊ณ  ํฐ ์˜๋ฏธ๋Š” docker ๋ฅผ ํ™œ์šฉํ•ด์„œ cluster ๋ฅผ ๊พธ๋ฏธ๊ณ  ์—ฌ๊ธฐ์— ๊ธฐ์กด์˜ ์„œ๋น„์Šค๋ฅผ ๋นŒ๋“œ/๋ฐฐํฌํ•  ์ฒด๊ณ„๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

Dockerize๋ฅผ ์‹œํ—˜ํ•ด ๋ณด๊ธฐ ์œ„ํ•˜์—ฌ ๊ธฐ์กด์— ๋ฐ›์•˜๋˜ MSAํ”„๋กœ์ ํŠธ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

$ git clone https://github.com/uengine-oss/msa-tutorial-class-management-msa.git
$ cd msa-tutorial-class-management-msa
$ cd clazz
$ mvn package -B

mvn package ๋Š” ํŒŒ์ผ์„ compileํ•˜๊ณ  JAR๊ฐ™์€ ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ํ˜•์‹์œผ๋กœ ํŒจํ‚ค์ง€๋ฅผ ํ•˜๋ผ๋Š” ๋ช…๋ น์ด๋‹ค.
'-B' ์˜ต์…˜์€ ๋น„-๋Œ€ํ™”ํ˜• ๋ชจ๋“œ๋กœ mvn ์‹คํ–‰ ์ค‘ ๋Œ€ํ™”ํ˜• ์ž…๋ ฅ ๋ชจ๋“œ๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

target์— ๋‘๊ฐœ์˜ jarํŒŒ์ผ์ด ์ƒ์„ฑ์ด ๋˜์—ˆ๋‹ค.
-exec.jar ํŒŒ์ผ์ด ์‹คํ–‰์šฉ jarํŒŒ์ผ์ด๊ณ , ๊ทธ๋ƒฅ jarํŒŒ์ผ์€ library์šฉ jarํŒŒ์ผ์ด๋‹ค.
์‹คํ–‰์šฉ jar ํŒŒ์ผ์€ ๊ทธ๋ƒฅ ์ƒ์„ฑ์ด ๋˜๋Š”๊ฒƒ์ด ์•„๋‹Œ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์„œ ์ƒ์„ฑ์ด ๋œ ๊ฒƒ์ด๋‹ค.
clazz/pom.xml ์„ ๋ณด๊ฒŒ๋˜๋ฉด uengine-five ๋ผ๋Š” parent์—์„œ ์ƒ์†์„ ๋ฐ›์•˜๋‹ค.

clazz/pom.xml

    <parent>
        <groupId>org.uengine</groupId>
        <artifactId>uengine-five</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

uengine-five/pom.xml

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <addResources>true</addResources>
         </configuration>
         <executions>
            <execution>
               <goals>
                  <goal>repackage</goal>
               </goals>
               <configuration>
                  <classifier>exec</classifier>
               </configuration>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <groupId>com.spotify</groupId>
         <artifactId>docker-maven-plugin</artifactId>
         <version>1.0.0</version>
         <configuration>
            <imageName>${project.artifactId}</imageName>
            <dockerDirectory>.</dockerDirectory>
            <resources>
               <resource>
                  <targetPath>/</targetPath>
                  <directory>${project.build.directory}</directory>
                  <include>${project.build.finalName}.jar</include>
               </resource>
            </resources>
         </configuration>
      </plugin>
   </plugins>
</build>

repackage ๋ฅผ ์‚ฌ์šฉํ•ด์„œ exec jarํŒŒ์ผ์„ ํ•˜๋‚˜๋” ์ƒ์„ฑ ํ•˜์˜€๋‹ค.
docker-maven-plugin ์„ ์‚ฌ์šฉํ•˜์—ฌ maven์—์„œ ๋ช…๋ น ํ•˜๋‚˜๋กœ docker image๊นŒ์ง€ ๋งŒ๋“œ๋Š” plugin์ด๋‹ค.
์„ค์ •๋œ ${project.build.finalName}.jar ์ด๋ฆ„์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ docker repository์— ๋“ฑ๋ก๊นŒ์ง€ ํ•ด์ค€๋‹ค.

์ด์ œ ์ด jar ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
maven ๊ณผ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์€ ๋™์ผํ•˜๊ณ , ์ด๋ ‡๊ฒŒ jarํŒŒ์ผ๋กœ ๋งŒ๋“  ๊ฒƒ์€ maven์˜ dependency๋ฅผ ํ•ด์ œ ์‹œํ‚จ๊ฒƒ์ด๋‹ค.

$ java -jar clazz-1.0.0-SNAPSHOT-exec.jar

์ด์ œ java์˜ dependency๋ฅผ ํ•ด์ œ ์‹œํ‚ค๋ ค๋ฉด docker๋กœ ์‹คํ–‰์„ ์‹œํ‚ค๋ฉด ๋œ๋‹ค.
docker๋กœ ์‹คํ–‰์„ ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” docker build ๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰์‹œ์ผœ์•ผ ํ•˜๋Š”๋ฐ,
ํ•ด๋‹น ๋ช…๋ น์–ด์—๋Š” Dockerfile ์ด ํ•„์š”ํ•˜๋‹ค. ํ™•์žฅ์ž ์—†์ด ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

clazz/Dockerfile

FROM openjdk:8u111-jdk-alpine
VOLUME /tmp
ADD /target/*-exec.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Xmx256M","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

์•ˆ์˜ ๋ช…๋ น์–ด๋ฅผ ํ•ด์„ํ•˜์ž๋ฉด FROM openjdk:8u111-jdk-alpine ์œ„์—์„œ ์‹คํ–‰์ผ ํ• ๊ฒƒ์ด๊ณ ,
/tmp ๋ผ๋Š” VOLUME์„ mountํ• ๊ฒƒ์ด๋‹ค. ํ˜„์žฌํด๋”์˜ exec.jar๋ฅผ app.jar๋กœ image์•ˆ์— ๋„ฃ์œผ๋ผ๋Š” ๋œป์ด๋‹ค.
EXPOSE ์œผ๋กœ 8080 ํฌํŠธ๋ฅผ openํ•˜๊ณ , ENTRYPOINT ๋กœ ์‹คํ–‰์‹œ ์˜ต์…˜์„ ์ฃผ์—ˆ๋‹ค.

ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์˜€์œผ๋ฉด ์ด์ œ docker build ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

$ sudo docker build . -t clazz-service
Sending build context to Docker daemon  119.3MB
Step 1/5 : FROM openjdk:8u111-jdk-alpine
8u111-jdk-alpine: Pulling from library/openjdk
709515475419: Pull complete
38a1c0aaa6fd: Pull complete
5b58c996e33e: Downloading  7.073MB/49.36MB
...
Successfully built bd09f73f2ee1
Successfully tagged clazz-service:latest

ํ•ด์„ํ•˜์ž๋ฉด ํ˜„์žฌ ํด๋” (.) ์—์„œ -t(ํƒœ๊ทธ) ๋ฅผ ์ฃผ์–ด์„œ docker build๋ฅผ ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.
docker images ๋ฅผ ํ•˜์—ฌ ๋ฐฉ๊ธˆ ๋งŒ๋“  image๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
clazz-service       latest              bd09f73f2ee1        4 minutes ago       264MB

์ด์ œ ๋งŒ๋“ค์–ด์ง„ docker๋ฅผ ์‹คํ–‰ ์‹œํ‚ฌ ์ฐจ๋ก€์ด๋‹ค.

$ sudo docker run clazz-service -P 8080:8081

-P ์˜ต์…˜์€ ํ˜ธ์ŠคํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐ(ํฌ์›Œ๋”ฉ)

โš ๏ธ **GitHub.com Fallback** โš ๏ธ