From f8e6fb409c091c70349a2534fad45494a3f32493 Mon Sep 17 00:00:00 2001 From: Mara-Ioana Nicolae Date: Thu, 19 May 2022 14:17:38 +0300 Subject: [PATCH 1/3] Add portainer stack and scripts for running and cleaning Signed-off-by: Mara-Ioana Nicolae --- README.md | 28 +++++++++++++++++++++++----- clean-portainer.sh | 5 +++++ portainer-agent-stack.yml | 38 ++++++++++++++++++++++++++++++++++++++ run-portainer.sh | 4 ++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100755 clean-portainer.sh create mode 100644 portainer-agent-stack.yml create mode 100755 run-portainer.sh diff --git a/README.md b/README.md index e0a707d9..fbc6506f 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # What has been done so far - Monitoring using Prometheus, Grafana, Node-explorer & CAdvisor -- api gateway +- api gateway - docker-swarm for deployment - Assure that you can create newer images based on BE & FE updates +- persistent DB +- management of Docker Swarm services with Portainer # What needs to be done -- Make DB persistent - Loki for logging - rabbitMQ - pipeline CI/CD @@ -15,7 +16,7 @@ - docker-swarm file - scripts for running and removing containers -# Ports +# Ports - FE runs on port 3000 - BE runs on port 8080 - Kong routes traffic from localhost:8080 to localhost:8000 @@ -25,5 +26,22 @@ - Node-explorer runs on port 9100 # Running -- after running `run.sh` script give it around 2 mins before backend will be up and running -- use `clean.sh` to stop services and remove pruned containers \ No newline at end of file +- after running `run.sh` script give it around 5 mins before backend will be up and running +- use `clean.sh` to stop services and remove pruned containers + +# Portainer +Management of the Docker Swarm infrastructure. It is used as an visual interface to control the container environment. + +To start the portainer containers run: +``` +./run-portainer +``` + +To stop the portainer containers run: +``` +./clean-portainer +``` + +Check that the services in the *portainer* stack are fully created, then access it throught `localhost:9010` in the web browser. Log in using the username *admin* and password *adminidp2022*. + +Add a new stack with the name *idp_project* and paste the content of the `stack.yml` file, wait until all services are up and running. diff --git a/clean-portainer.sh b/clean-portainer.sh new file mode 100755 index 00000000..efbb6da2 --- /dev/null +++ b/clean-portainer.sh @@ -0,0 +1,5 @@ +#! /bin/bash +docker stack rm portainer +docker kill $(docker ps -q) +docker container prune +docker image prune \ No newline at end of file diff --git a/portainer-agent-stack.yml b/portainer-agent-stack.yml new file mode 100644 index 00000000..0979ea88 --- /dev/null +++ b/portainer-agent-stack.yml @@ -0,0 +1,38 @@ +version: '3.2' + +services: + agent: + image: portainer/agent + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - /var/lib/docker/volumes:/var/lib/docker/volumes + networks: + - agent_network + deploy: + mode: global + placement: + constraints: [node.platform.os == linux] + + portainer: + image: portainer/portainer-ce + command: -H tcp://tasks.agent:9001 --tlsskipverify + ports: + - "9010:9000" + - "8020:8000" + volumes: + - portainer_data:/data + networks: + - agent_network + deploy: + mode: replicated + replicas: 1 + placement: + constraints: [node.role == manager] + +networks: + agent_network: + driver: overlay + attachable: true + +volumes: + portainer_data: diff --git a/run-portainer.sh b/run-portainer.sh new file mode 100755 index 00000000..413d706f --- /dev/null +++ b/run-portainer.sh @@ -0,0 +1,4 @@ +#! /bin/bash +docker pull portainer/agent +docker pull portainer/portainer-ce +docker stack deploy -c portainer-agent-stack.yml portainer From dbe878de52f64237c29a706e70199407e6fc1dae Mon Sep 17 00:00:00 2001 From: Mara-Ioana Nicolae Date: Thu, 19 May 2022 18:12:47 +0300 Subject: [PATCH 2/3] Add CI/CD functionality Signed-off-by: Mara-Ioana Nicolae --- README.md | 53 ++++++++++++++++- gitlab-stack.yml | 149 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 gitlab-stack.yml diff --git a/README.md b/README.md index fbc6506f..8a5efdb5 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@ # What has been done so far -- Monitoring using Prometheus, Grafana, Node-explorer & CAdvisor +- monitoring using Prometheus, Grafana, Node-explorer & CAdvisor - api gateway - docker-swarm for deployment -- Assure that you can create newer images based on BE & FE updates +- create newer images based on BE & FE updates - persistent DB - management of Docker Swarm services with Portainer +- pipeline CI/CD (no stage for testing yet) # What needs to be done - Loki for logging - rabbitMQ -- pipeline CI/CD + # Contents - Kong - Prometheus @@ -45,3 +46,49 @@ To stop the portainer containers run: Check that the services in the *portainer* stack are fully created, then access it throught `localhost:9010` in the web browser. Log in using the username *admin* and password *adminidp2022*. Add a new stack with the name *idp_project* and paste the content of the `stack.yml` file, wait until all services are up and running. + + +# Gitlab CI/CD +Created two separate repositories for the front-end and back-end and add their own `Dockerfile` to create the images accordingly. Added `.gitlab-ci.yml` in both repos. (the address of the webhook needs to be added manually) + +Create images for front-end and back-end and add them to the registry: +``` +docker login gitlab.cs.pub.ro:5050 +docker build -t gitlab.cs.pub.ro:5050/pw-idp-2022/pw-backend . +docker push gitlab.cs.pub.ro:5050/pw-idp-2022/pw-backend + +docker login gitlab.cs.pub.ro:5050 +docker build -t gitlab.cs.pub.ro:5050/pw-idp-2022/pw-frontend . +docker push gitlab.cs.pub.ro:5050/pw-idp-2022/pw-frontend +``` + +Create the Gitlab runner with the following commands: +``` +docker run -d --name gitlab-runner --restart always -v gitlab-runner-config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest + +docker run --rm -it -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner register + +# to register use de following: +# https://gitlab.cs.pub.ro/ +# Registration token +# Name of the runner +# Some tags +# docker +# docker:19.03 + +docker run -it -v gitlab-runner-config:/test alpine +``` + +Modify the `config.toml` file to grant privileged access. +``` +privileged = true +volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"] + +``` + +Restart the runner: +``` +sudo docker restart gitlab-runner +``` + +To test, try making changes for the master branch and see if anything changes. :) diff --git a/gitlab-stack.yml b/gitlab-stack.yml new file mode 100644 index 00000000..fe9fb386 --- /dev/null +++ b/gitlab-stack.yml @@ -0,0 +1,149 @@ +version: "3.8" +services: + frontend: + image: gitlab.cs.pub.ro:5050/pw-idp-2022/pw-frontend + ports: + - "3000:3000" + depends_on: + - backend + networks: + - idp_network + deploy: + replicas: 2 + placement: + max_replicas_per_node: 2 + update_config: + parallelism: 2 + order: start-first + rollback_config: + parallelism: 2 + order: stop-first + + backend: + image: gitlab.cs.pub.ro:5050/pw-idp-2022/pw-backend + ports: + - "8080:8080" + depends_on: + - db + networks: + - idp_network + environment: + SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/proiect?createDatabaseIfNotExist=true + SPRING_DATASOURCE_USERNAME: root + SPRING_DATASOURCE_PASSWORD: admin + deploy: + replicas: 2 + placement: + max_replicas_per_node: 2 + update_config: + parallelism: 2 + order: start-first + rollback_config: + parallelism: 2 + order: stop-first + + db: + image: mysql + environment: + MYSQL_ROOT_PASSWORD: admin + MYSQL_DATABASE: proiect + MYSQL_USER: admin + ports: + - "3306:3306" + networks: + - idp_network + deploy: + placement: + constraints: + - "node.role==manager" + volumes: + - db-volume:/var/lib/mysql + + kong: + image: kong:latest + volumes: + - ./Kong:/usr/local/kong/declarative + environment: + KONG_DATABASE: 'off' + KONG_DECLARATIVE_CONFIG: /usr/local/kong/declarative/kong.yml + KONG_PROXY_ACCESS_LOG: /dev/stdout + KONG_ADMIN_ACCESS_LOG: /dev/stdout + KONG_PROXY_ERROR_LOG: /dev/stderr + KONG_ADMIN_ERROR_LOG: /dev/stderr + KONG_ADMIN_LISTEN: 0.0.0.0:8001, 0.0.0.0:8444 ssl + ports: + - 8000:8000 + - 8443:8443 + - 8001:8001 + - 8444:8444 + deploy: + placement: + constraints: [node.role == manager] + networks: + - idp_network + + grafana: + image: grafana/grafana:latest + volumes: + - grafana-volume:/var/lib/grafana + ports: + - 3001:3000 + depends_on: + - prometheus + deploy: + placement: + constraints: [node.role == manager] + networks: + - idp_network + + prometheus: + image: prom/prometheus + volumes: + - ./prometheus/config.yml:/etc/prometheus/prometheus.yml + ports: + - 9090:9090 + deploy: + placement: + constraints: [node.role == manager] + networks: + - idp_network + + node_exporter: + image: prom/node-exporter + deploy: + mode: global + volumes: + - /proc:/host/proc:ro + - /sys:/host/sys:ro + - /:/rootfs:ro + command: + - '--path.procfs=/host/proc' + - '--path.rootfs=/rootfs' + - '--path.sysfs=/host/sys' + - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)' + ports: + - 9100:9100 + networks: + - idp_network + + cadvisor: + image: gcr.io/cadvisor/cadvisor + deploy: + mode: global + volumes: + - /:/rootfs:ro + - /var/run:/var/run:ro + - /sys:/sys:ro + - /var/lib/docker/:/var/lib/docker:ro + - /dev/disk:/dev/disk/:ro + ports: + - 8081:8080 + networks: + - idp_network + +volumes: + grafana-volume: + db-volume: + +networks: + idp_network: From 170221bd00545bb22c5f02c07ae30faae4110b8c Mon Sep 17 00:00:00 2001 From: HyperSmoit Date: Sat, 21 May 2022 12:05:12 +0300 Subject: [PATCH 3/3] updates --- gitlab-stack.yml | 4 ++-- portainer-agent-stack.yml | 9 +++++---- run-portainer.sh | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gitlab-stack.yml b/gitlab-stack.yml index fe9fb386..fd3cb58f 100644 --- a/gitlab-stack.yml +++ b/gitlab-stack.yml @@ -62,7 +62,7 @@ services: kong: image: kong:latest volumes: - - ./Kong:/usr/local/kong/declarative + - /home/hypersmoit/Desktop/idp_pweb/idp/Kong:/usr/local/kong/declarative environment: KONG_DATABASE: 'off' KONG_DECLARATIVE_CONFIG: /usr/local/kong/declarative/kong.yml @@ -99,7 +99,7 @@ services: prometheus: image: prom/prometheus volumes: - - ./prometheus/config.yml:/etc/prometheus/prometheus.yml + - /home/hypersmoit/Desktop/idp_pweb/idp/prometheus/config.yml:/etc/prometheus/prometheus.yml ports: - 9090:9090 deploy: diff --git a/portainer-agent-stack.yml b/portainer-agent-stack.yml index 0979ea88..8215c4c4 100644 --- a/portainer-agent-stack.yml +++ b/portainer-agent-stack.yml @@ -2,7 +2,7 @@ version: '3.2' services: agent: - image: portainer/agent + image: portainer/agent:2.11.1 volumes: - /var/run/docker.sock:/var/run/docker.sock - /var/lib/docker/volumes:/var/lib/docker/volumes @@ -14,11 +14,12 @@ services: constraints: [node.platform.os == linux] portainer: - image: portainer/portainer-ce + image: portainer/portainer-ce:2.11.1 command: -H tcp://tasks.agent:9001 --tlsskipverify ports: - - "9010:9000" - - "8020:8000" + - "9443:9443" + - "9000:9000" + - "8002:8000" volumes: - portainer_data:/data networks: diff --git a/run-portainer.sh b/run-portainer.sh index 413d706f..d583437d 100755 --- a/run-portainer.sh +++ b/run-portainer.sh @@ -1,4 +1,4 @@ #! /bin/bash -docker pull portainer/agent -docker pull portainer/portainer-ce +# docker pull portainer/agent +# docker pull portainer/portainer-ce docker stack deploy -c portainer-agent-stack.yml portainer