Section 16: 애플리케이션 배포 Docker Container - KwangtaekJung/MSA-SpringCloud-user-service GitHub Wiki
Section 16: 애플리케이션 배포 Docker Container
- Design Deployment
- Configuration server
- Eureka Discovery
- API Gateway
- MariaDB
- Kafka
- Zipkin
- Monitoring
- Microervices
- Multiple Environments
Running Microservices
- Intellij IDEA
- Exported JAR file
- Docker Container
Running Microservices in Local
Create Bridge Network
- Bridge network
- Host network
- None network
PS C:\Users\kt79j> docker network create --gateway 172.18.0.1 --subnet 172.18.0.0/16 ecommerce-network
c7ddc2c48bff97fcaa1514c2e92ca08d93cff09cbf21f0c9b586500334bbf7b1
PS C:\Users\kt79j> docker inspect ecommerce-network
[
{
"Name": "ecommerce-network",
"Id": "c7ddc2c48bff97fcaa1514c2e92ca08d93cff09cbf21f0c9b586500334bbf7b1",
"Created": "2022-04-08T12:33:14.8773572Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
PS C:\Users\kt79j> docker run -d --name rabbitmq --network ecommerce-network -p 15672:15672 -p 5672:5672 -p 15671:15671 -p 4369:4369 -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest rabbitmq:management
Unable to find image 'rabbitmq:management' locally
management: Pulling from library/rabbitmq
e0b25ef51634: Already exists
62ed3501fa5f: Pull complete
203d3cfb296c: Pull complete
64bcd2776fd6: Pull complete
c706755ca577: Pull complete
462f63b0d480: Pull complete
c0adbd0b99d8: Pull complete
e63a49b3e166: Pull complete
fd744509516e: Pull complete
16b0f07d5dc6: Pull complete
Digest: sha256:4eb967e3adb024f232f5aec77ddc559256b826d00f630e9cfb0d4b2b39e6b003
Status: Downloaded newer image for rabbitmq:management
7b3374a77f713be450a
PS C:\Users\kt79j> docker inspect ecommerce-network
[
{
"Name": "ecommerce-network",
"Id": "c7ddc2c48bff97fcaa1514c2e92ca08d93cff09cbf21f0c9b586500334bbf7b1",
"Created": "2022-04-08T12:33:14.8773572Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"7b3374a77f713be450ae8d307566160d63796dcf5814736d1c1cc97e539da06e": {
"Name": "rabbitmq",
"EndpointID": "e689c1362f36cbf517ab3ef06ec84d2cef4b14f6c6045f8d37d57df1ece34ffd",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
Run RabbitMq
PS C:\Users\kt79j> docker run -d --name rabbitmq --network ecommerce-network -p 15672:15672 -p 5672:5672 -p 15671:15671 -p 4369:4369 -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest rabbitmq:management
Create Config Server Docker Image
$ ./mvnw clean compile package -DskipTests=true
$ docker build -t kt79jung/config-service:1.0 .
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kt79jung/config-service 1.0 746dc47c241d 11 seconds ago 441MB
$ docker run -d -p 8888:8888 --network ecommerce-network -e "spring.rabbitmq.host=rabbitmq" -e "spring.profiles.active=default" --name config-service kt79jung/config-service:1.0
838db9b0f046aa4d5a8467f0bdbc13a0c5618c4c4b884aea6c4f3bd75900f7b9
kt79j@DESKTOP-72EP283 MINGW64 /c/WorkSpace/study/inflearn/springcloud/config-service (main)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME
S
838db9b0f046 kt79jung/config-service:1.0 "java -jar ConfigSer…" 6 seconds ago Up 5 seconds 0.0.0.0:8888->8888/tcp con
fig-service
$ docker inspect ecommerce-network
[
{
"Name": "ecommerce-network",
"Id": "c7ddc2c48bff97fcaa1514c2e92ca08d93cff09cbf21f0c9b586500334bbf7b1",
"Created": "2022-04-08T12:33:14.8773572Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"7b3374a77f713be450ae8d307566160d63796dcf5814736d1c1cc97e539da06e": {
"Name": "rabbitmq",
"EndpointID": "7cd554c3212b1463061d911a29f4e1df4fb1639217a239717d586f6d169eef84",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"838db9b0f046aa4d5a8467f0bdbc13a0c5618c4c4b884aea6c4f3bd75900f7b9": {
"Name": "config-service",
"EndpointID": "cd5d07337240126fa78931083351e73fe72746ed3139cc2f55a4fccfa7e164e5",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
$ docker logs config-service
Discovery Service
$ docker build -t kt79jung/discovery-service:1.0 .
$ docker run -d -p 8761:8761 --network ecommerce-network -e "spring.cloud.config.uri=http://config-server:8888" --name discovery-service kt79jung/discovery-service:1.0
$ docker network inspect ecommerce-network
[
{
"Name": "ecommerce-network",
"Id": "c7ddc2c48bff97fcaa1514c2e92ca08d93cff09cbf21f0c9b586500334bbf7b1",
"Created": "2022-04-08T12:33:14.8773572Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"68a77228a7ade2fa9e5f635cf1c6cb6fcdac65611c6f0f8ba6adcd35baef3586": {
"Name": "discovery-service",
"EndpointID": "1afb1ee1c5813ea155be75219ae0d3ccb1d9197d8502c39f8662d31c38f9bb8f",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"7b3374a77f713be450ae8d307566160d63796dcf5814736d1c1cc97e539da06e": {
"Name": "rabbitmq",
"EndpointID": "7cd554c3212b1463061d911a29f4e1df4fb1639217a239717d586f6d169eef84",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"f5d9c19f6e730867baac6e5d30b54c858ef55a78d32bcfa887da849de2e7f210": {
"Name": "config-service",
"EndpointID": "543ed28b7f1631c94209e1915a9431d40f4ba7f09b7e7ed7ad74527422c21dec",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Apigateway Service
- $ ./mvnw clean compile package -DskipTests=true
- $ docker build -t kt79jung/apigateway-service:1.0 .
- $ docker push kt79jung/apigateway-service:1.0
$ docker run -d -p 8000:8000 --network ecommerce-network \
-e "spring.cloud.config.uri=http://config-service:8888" \
-e "spring.rabbitmq.host=rabbitmq" \
-e "eureka.client.serviceUrl.defaultZone=http://discovery-service:8761/eureka/" \
--name apigateway-service kt79jung/apigateway-service:1.0
Build, Run MariaDB
- 로컬에 설치한 MariaDB에 만들어 놨던 DB를 컨테이너 MariaDB에 복사하는 방식으로 만들어 본다.
FROM mariadb
ENV MYSQL_ROOT_PASSWORD jung050@14
ENV MYSQL_DATABASE mydb
COPY ./mysql_data/data /var/lib/mysql
EXPOSE 3306
ENTRYPOINT ["mysqld", "--user=root"]
PS C:\WorkSpace\study\inflearn\springcloud\docker-files> docker exec -it mariadb /bin/bash
root@9c9a988d625f:/# mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.7.3-MariaDB-1:10.7.3+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.000 sec)
MariaDB [(none)]> use mydb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| my_topic_users |
| orders |
| users |
+----------------+
3 rows in set (0.000 sec)
MariaDB [mydb]>
Run Kafka Server
-
Zookeeper + Kafka Standalone
- docker-compose로 실행
- git clone https://github.com/wurstmeister/kafka-docker
- docker-compose-single-broker.yml 수정
-
$ docker-compose -f docker-compose-single-broker.yml up -d
Run zipkin
- docker run -d -p 9411:9411 --network ecommerce-network --name zipkin openzipkin/zipkin
PS C:\WorkSpace\study\inflearn\springcloud\docker-files> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
35c660c94a33 openzipkin/zipkin "start-zipkin" 46 minutes ago Up 46 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp zipkin
- docker logs zipkin
Run Prometheus + Grafana
- 기존에 설치해 놓았던 prometheus의 yml 파일 수정하고 볼륨마운트한다. (docker -v 옵션)
docker run -d -p 9090:9090 --network ecommerce-network --name prometheus -v C:\WorkSpace\tools\prometheus-2.34.0.windows-amd64\promethues.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run -d -p 3000:3000 --network ecommerce-network --name grafana grafana/grafana
Deployed Services
- Docker container
- IP address, Port
Build, Run Users Microservice
- apigateway의 ip를 찾아서 config-server에서 읽어오는 git hub에 수정해준다.
docker inspect apigateway-service
docker run -d --network ecommerce-network --name user-service -e "spring.cloud.config.uri=http://config-service:8888" -e "spring.rabbitmq.host=rabbitmq" -e "spring.zipkin.base-url=http://zipkin:9411" -e "eureka.client.serviceUrl.defaultZone=http://discovery-service:8761/eureka/" kt79jung/user-service:1.0
Build, Run Orders Microservices
$ ./mvnw clean compile package -DskipTests=true
$ docker build -t kt79jung/order-service:1.0 .
$ docker run -d --network ecommerce-network --name order-service -e "spring.cloud.config.uri=http://config-service:8888" -e "spring.rabbitmq.host=rabbitmq" -e "spring.zipkin.base-url=http://zipkin:9411" -e "eureka.client.serviceUrl.defaultZone=http://discovery-service:8761/eureka/" -e "logging.file=/api-logs/orders-ws.log" -e "spring.datasource.url=jdbc:mariadb://mariadb:3306/mydb" kt79jung/order-service:1.0
Build, Run Catalog Microservice
$ docker run -d --network ecommerce-network --name catalog-service -e "spring.cloud.config.uri=http://config-service:8888" -e "spring.rabbitmq.host=rabbitmq" -e "eureka.client.serviceUrl.defaultZone=http://discovery-service:8761/eureka/" -e "logging.file=/api-logs/catalogs-ws.log" kt79jung/catalog-service:1.0