From 2852c04616d4f1088c5a26036dc225a4073db787 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Mon, 22 Apr 2024 10:15:30 +0300 Subject: [PATCH 1/3] Add the GCP Jinja template for clustering deployment. --- .../antmedia-firewall-template.jinja | 45 ++++++++++++++ .../antmedia-instance-group-template.jinja | 25 ++++++++ .../antmedia-instance-template.jinja | 60 +++++++++++++++++++ .../antmedia-mongodb-template.jinja | 47 +++++++++++++++ .../antmedia-vpc-template.jinja | 33 ++++++++++ gcp-jinja-template/antmedia.jinja | 43 +++++++++++++ gcp-jinja-template/antmedia.yaml | 25 ++++++++ gcp-jinja-template/backend.jinja | 16 +++++ 8 files changed, 294 insertions(+) create mode 100644 gcp-jinja-template/antmedia-firewall-template.jinja create mode 100644 gcp-jinja-template/antmedia-instance-group-template.jinja create mode 100644 gcp-jinja-template/antmedia-instance-template.jinja create mode 100644 gcp-jinja-template/antmedia-mongodb-template.jinja create mode 100644 gcp-jinja-template/antmedia-vpc-template.jinja create mode 100644 gcp-jinja-template/antmedia.jinja create mode 100644 gcp-jinja-template/antmedia.yaml create mode 100644 gcp-jinja-template/backend.jinja diff --git a/gcp-jinja-template/antmedia-firewall-template.jinja b/gcp-jinja-template/antmedia-firewall-template.jinja new file mode 100644 index 00000000..e33e9ec1 --- /dev/null +++ b/gcp-jinja-template/antmedia-firewall-template.jinja @@ -0,0 +1,45 @@ +{# +Copyright 2016 Google Inc. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +#} + +resources: +- type: compute.v1.firewall + name: ams-mongodb-internal + properties: + network: $(ref.antmedia-vpc-network.selfLink) + sourceRanges: + - 10.0.0.0/16 + targetTags: + - antmedia-mongodb + allowed: + - IPProtocol: tcp + ports: + - "27017" + - "22" + +- type: compute.v1.firewall + name: ams-antmedia-external + properties: + network: $(ref.antmedia-vpc-network.selfLink) + sourceRanges: + - 0.0.0.0/0 + targetTags: + - antmedia + allowed: + - IPProtocol: tcp + ports: + - "5080" + - "22" + - "1935" + diff --git a/gcp-jinja-template/antmedia-instance-group-template.jinja b/gcp-jinja-template/antmedia-instance-group-template.jinja new file mode 100644 index 00000000..314cb5aa --- /dev/null +++ b/gcp-jinja-template/antmedia-instance-group-template.jinja @@ -0,0 +1,25 @@ +resources: +- name: origin-instance-group + type: compute.v1.instanceGroupManager + properties: + zone: {{ default_zone }} + targetSize: 1 + baseInstanceName: my-instance + instanceTemplate: projects/{{ env["project"] }}/global/instanceTemplates/ams-origin-template + namedPorts: + - name: http + port: 5080 + +- name: origin-autoscaler + type: compute.v1.autoscaler + properties: + zone: {{ default_zone }} + target: $(ref.origin-instance-group.selfLink) + autoscalingPolicy: + minNumReplicas: 1 + maxNumReplicas: 10 + coolDownPeriodSec: 60 + cpuUtilization: + utilizationTarget: 0.6 + + diff --git a/gcp-jinja-template/antmedia-instance-template.jinja b/gcp-jinja-template/antmedia-instance-template.jinja new file mode 100644 index 00000000..62f8b10c --- /dev/null +++ b/gcp-jinja-template/antmedia-instance-template.jinja @@ -0,0 +1,60 @@ +resources: +- name: ams-origin-template + type: compute.v1.instanceTemplate + properties: + properties: + zone: {{ properties["zone"] }} + machineType: {{ properties["origin_machine_type"] }} + metadata: + items: + - key: startup-script + value: |- + #!/bin/bash + rm -rf /usr/local/antmedia/conf/instanceId + rm -rf /usr/local/antmedia/*.db.* + rm -rf /usr/local/antmedia/*.db + cd /usr/local/antmedia + ./change_server_mode.sh cluster $(ref.{{ env["deployment"] }}-mongodb.networkInterfaces[0].networkIP) + disks: + - deviceName: boot + type: PERSISTENT + boot: true + autoDelete: true + initializeParams: + sourceImage: projects/antmedia-public-385620/global/images/ams-marketplace282 + networkInterfaces: + - network: $(ref.antmedia-vpc-network.selfLink) + subnetwork: $(ref.origin-subnet.selfLink) + accessConfigs: + - name: External NAT + type: ONE_TO_ONE_NAT + +- name: ams-edge-template + type: compute.v1.instanceTemplate + properties: + properties: + zone: {{ properties["zone"] }} + machineType: {{ properties["edge_machine_type"] }} + metadata: + items: + - key: startup-script + value: |- + #!/bin/bash + rm -rf /usr/local/antmedia/conf/instanceId + rm -rf /usr/local/antmedia/*.db.* + rm -rf /usr/local/antmedia/*.db + cd /usr/local/antmedia + ./change_server_mode.sh cluster $(ref.{{ env["deployment"] }}-mongodb.networkInterfaces[0].networkIP) + disks: + - deviceName: boot + type: PERSISTENT + boot: true + autoDelete: true + initializeParams: + sourceImage: projects/antmedia-public-385620/global/images/ams-marketplace282 + networkInterfaces: + - network: $(ref.antmedia-vpc-network.selfLink) + subnetwork: $(ref.edge-subnet.selfLink) + accessConfigs: + - name: External NAT + type: ONE_TO_ONE_NAT diff --git a/gcp-jinja-template/antmedia-mongodb-template.jinja b/gcp-jinja-template/antmedia-mongodb-template.jinja new file mode 100644 index 00000000..2f43a0a2 --- /dev/null +++ b/gcp-jinja-template/antmedia-mongodb-template.jinja @@ -0,0 +1,47 @@ +{# +Copyright 2016 Google Inc. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +#} + +resources: +- type: compute.v1.instance + name: {{ env["deployment"] }}-mongodb + properties: + zone: {{ properties["zone"] }} + machineType: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/zones/{{ properties["zone"] }}/machineTypes/{{ properties["machine_type"] }} + metadata: + items: + # For more ways to use startup scripts on an instance, see: + # https://cloud.google.com/compute/docs/startupscript + - key: startup-script + value: | + #!/bin/bash + wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_mongodb.sh + bash ./install_mongodb.sh + disks: + - deviceName: boot + type: PERSISTENT + boot: true + autoDelete: true + initializeParams: + sourceImage: https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts + networkInterfaces: + - network: $(ref.antmedia-vpc-network.selfLink) + subnetwork: $(ref.origin-subnet.selfLink) + # Access Config required to give the instance a public IP address + accessConfigs: + - name: External NAT + type: ONE_TO_ONE_NAT + tags: + items: + - antmedia-mongodb diff --git a/gcp-jinja-template/antmedia-vpc-template.jinja b/gcp-jinja-template/antmedia-vpc-template.jinja new file mode 100644 index 00000000..2e365253 --- /dev/null +++ b/gcp-jinja-template/antmedia-vpc-template.jinja @@ -0,0 +1,33 @@ +resources: +- name: antmedia-vpc-network + type: compute.v1.network + properties: + region: {{ properties["region"] }} + autoCreateSubnetworks: false + +- name: origin-subnet + type: compute.v1.subnetwork + properties: + ipCidrRange: 10.0.1.0/24 + network: $(ref.antmedia-vpc-network.selfLink) + region: {{ properties["region"] }} + +- name: edge-subnet + type: compute.v1.subnetwork + properties: + ipCidrRange: 10.0.2.0/24 + network: $(ref.antmedia-vpc-network.selfLink) + region: {{ properties["region"] }} + +- name: firewall-rule + type: compute.v1.firewall + properties: + network: $(ref.antmedia-vpc-network.selfLink) + sourceRanges: + - 0.0.0.0/0 + allowed: + - IPProtocol: tcp + ports: + - "80" + - "443" + - "22" diff --git a/gcp-jinja-template/antmedia.jinja b/gcp-jinja-template/antmedia.jinja new file mode 100644 index 00000000..d3d287ea --- /dev/null +++ b/gcp-jinja-template/antmedia.jinja @@ -0,0 +1,43 @@ +{# +Copyright 2016 Google Inc. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +#} + +{% set default_zone = "us-central1-a" %} +{% set default_region = "us-central1" %} +{% set mongodb_machine_type = "e2-standard-2" %} +{% set origin_machine_type = "e2-standard-2" %} +{% set edge_machine_type = "e2-standard-2" %} + + +resources: +- name: antmedia-mongodb + type: antmedia-mongodb-template.jinja + properties: + zone: {{ default_zone }} + machine_type: {{ mongodb_machine_type }} +- name: antmedia-instance + type: antmedia-instance-template.jinja + properties: + zone: {{ default_zone }} + origin_machine_type: {{ origin_machine_type }} + edge_machine_type: {{ edge_machine_type }} +- name: antmedia-firewall + type: antmedia-firewall-template.jinja + properties: + zone: {{ default_zone}} +- name: antmedia-vpc-network + type: antmedia-vpc-template.jinja + properties: + region: {{ default_region}} + autoCreateSubnetworks: false diff --git a/gcp-jinja-template/antmedia.yaml b/gcp-jinja-template/antmedia.yaml new file mode 100644 index 00000000..47faf0b7 --- /dev/null +++ b/gcp-jinja-template/antmedia.yaml @@ -0,0 +1,25 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +imports: +- path: antmedia-mongodb-template.jinja +- path: antmedia-instance-template.jinja +- path: antmedia-firewall-template.jinja +- path: antmedia-vpc-template.jinja +- path: antmedia.jinja + +resources: +- name: antmedia + type: antmedia.jinja + diff --git a/gcp-jinja-template/backend.jinja b/gcp-jinja-template/backend.jinja new file mode 100644 index 00000000..3f018c9c --- /dev/null +++ b/gcp-jinja-template/backend.jinja @@ -0,0 +1,16 @@ +--- +# Your resource definitions go here under resources key +resources: + - name: my-backend-service # Replace with your desired name + type: compute.v1.backendService # Adjust type based on resource + properties: + portName: http + protocol: HTTP + backend: + group: origin-instance-group # Assuming your instance group name + healthChecks: + - https://www.googleapis.com/compute/v1/healthChecks/default + +# You can add other outputs here if needed +output: + # ... From 6c2633607b5fa31cd4fa87f27d15a7bd5a6ed988 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Sun, 5 May 2024 23:40:46 +0300 Subject: [PATCH 2/3] Update Load Balancer --- .../antmedia-instance-group-template.jinja | 33 ++++++++- .../antmedia-instance-template.jinja | 11 ++- .../antmedia-loadbalancer-template.jinja | 74 +++++++++++++++++++ gcp-jinja-template/antmedia.jinja | 15 +++- gcp-jinja-template/antmedia.yaml | 5 +- 5 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 gcp-jinja-template/antmedia-loadbalancer-template.jinja diff --git a/gcp-jinja-template/antmedia-instance-group-template.jinja b/gcp-jinja-template/antmedia-instance-group-template.jinja index 314cb5aa..bce7bc96 100644 --- a/gcp-jinja-template/antmedia-instance-group-template.jinja +++ b/gcp-jinja-template/antmedia-instance-group-template.jinja @@ -2,18 +2,21 @@ resources: - name: origin-instance-group type: compute.v1.instanceGroupManager properties: - zone: {{ default_zone }} + zone: {{ properties["zone"] }} targetSize: 1 - baseInstanceName: my-instance + baseInstanceName: antmedia-origin instanceTemplate: projects/{{ env["project"] }}/global/instanceTemplates/ams-origin-template namedPorts: - name: http port: 5080 + metadata: + dependsOn: + - ams-origin-template - name: origin-autoscaler type: compute.v1.autoscaler properties: - zone: {{ default_zone }} + zone: {{ properties["zone"] }} target: $(ref.origin-instance-group.selfLink) autoscalingPolicy: minNumReplicas: 1 @@ -22,4 +25,28 @@ resources: cpuUtilization: utilizationTarget: 0.6 +- name: edge-instance-group + type: compute.v1.instanceGroupManager + properties: + zone: {{ properties["zone"] }} + targetSize: 1 + baseInstanceName: antmedia-edge + instanceTemplate: projects/{{ env["project"] }}/global/instanceTemplates/ams-edge-template + namedPorts: + - name: http + port: 5080 + metadata: + dependsOn: + - ams-edge-template +- name: edge-autoscaler + type: compute.v1.autoscaler + properties: + zone: {{ properties["zone"] }} + target: $(ref.edge-instance-group.selfLink) + autoscalingPolicy: + minNumReplicas: 1 + maxNumReplicas: 10 + coolDownPeriodSec: 60 + cpuUtilization: + utilizationTarget: 0.6 diff --git a/gcp-jinja-template/antmedia-instance-template.jinja b/gcp-jinja-template/antmedia-instance-template.jinja index 62f8b10c..f39aeef2 100644 --- a/gcp-jinja-template/antmedia-instance-template.jinja +++ b/gcp-jinja-template/antmedia-instance-template.jinja @@ -21,14 +21,16 @@ resources: boot: true autoDelete: true initializeParams: - sourceImage: projects/antmedia-public-385620/global/images/ams-marketplace282 + sourceImage: projects/{{ env["project"] }}/global/images/ams-marketplace282 networkInterfaces: - network: $(ref.antmedia-vpc-network.selfLink) subnetwork: $(ref.origin-subnet.selfLink) accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT - + tags: + items: + - antmedia - name: ams-edge-template type: compute.v1.instanceTemplate properties: @@ -51,10 +53,13 @@ resources: boot: true autoDelete: true initializeParams: - sourceImage: projects/antmedia-public-385620/global/images/ams-marketplace282 + sourceImage: projects/{{ env["project"] }}/global/images/ams-marketplace282 networkInterfaces: - network: $(ref.antmedia-vpc-network.selfLink) subnetwork: $(ref.edge-subnet.selfLink) accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT + tags: + items: + - antmedia diff --git a/gcp-jinja-template/antmedia-loadbalancer-template.jinja b/gcp-jinja-template/antmedia-loadbalancer-template.jinja new file mode 100644 index 00000000..ad3e801f --- /dev/null +++ b/gcp-jinja-template/antmedia-loadbalancer-template.jinja @@ -0,0 +1,74 @@ +{% set scenarios = ['origin', 'edge'] %} + + +resources: +{% for scenario in scenarios %} +- name: ams-load-balancer-{{ scenario }} + type: compute.v1.globalForwardingRule + properties: + region: {{ properties["region"] }} + loadBalancingScheme: EXTERNAL + target: $(ref.ams-target-proxy-{{ scenario }}.selfLink) + IPAddress: $(ref.lb-ipaddress-{{ scenario }}.address) + IPProtocol: TCP + portRange: 443-443 + +- name: ams-target-proxy-{{ scenario }} + type: compute.v1.targetHttpsProxy + properties: + urlMap: $(ref.ams-{{ scenario }}.selfLink) + sslCertificates: + - $(ref.ams-ssl-cert-{{ scenario }}.selfLink) + +- name: lb-ipaddress-{{ scenario }} + type: compute.v1.globalAddress + +- name: ams-{{ scenario }} + type: compute.v1.urlMap + properties: + defaultService: $(ref.ams-backend-{{ scenario }}.selfLink) + +- name: ams-ssl-cert-{{ scenario }} + type: compute.v1.sslCertificate + properties: + certificate: | + -----BEGIN CERTIFICATE----- + + -----END CERTIFICATE----- + privateKey: | + -----BEGIN PRIVATE KEY----- + + -----END PRIVATE KEY----- + + +- name: ams-backend-{{ scenario }} + type: compute.v1.backendService + properties: + port: 5080 + portName: http + protocol: HTTP + backends: + - name: backend + balancingMode: UTILIZATION + capacityScaler: 1.0 + group: projects/{{ env["project"] }}/zones/{{ properties["zone"] }}/instanceGroups/{{ scenario }}-instance-group + + maxUtilization: 0.8 + connectionDraining: + drainingTimeoutSec: 300 + healthChecks: + - $(ref.ams-health-check-{{ scenario }}.selfLink) + metadata: + dependsOn: + - {{ scenario }}-instance-group + - ams-health-check-{{ scenario }} + +- name: ams-health-check-{{ scenario }} + type: compute.v1.healthCheck + properties: + type: HTTP + httpHealthCheck: + port: 5080 + requestPath: / + +{% endfor %} diff --git a/gcp-jinja-template/antmedia.jinja b/gcp-jinja-template/antmedia.jinja index d3d287ea..988008bd 100644 --- a/gcp-jinja-template/antmedia.jinja +++ b/gcp-jinja-template/antmedia.jinja @@ -16,8 +16,8 @@ limitations under the License. {% set default_zone = "us-central1-a" %} {% set default_region = "us-central1" %} {% set mongodb_machine_type = "e2-standard-2" %} -{% set origin_machine_type = "e2-standard-2" %} -{% set edge_machine_type = "e2-standard-2" %} +{% set origin_machine_type = "c2d-standard-4" %} +{% set edge_machine_type = "c2d-standard-4" %} resources: @@ -32,6 +32,15 @@ resources: zone: {{ default_zone }} origin_machine_type: {{ origin_machine_type }} edge_machine_type: {{ edge_machine_type }} +- name: antmedia-instance-group + type: antmedia-instance-group-template.jinja + properties: + zone: {{ default_zone }} +- name: antmedia-loadbalancer + type: antmedia-loadbalancer-template.jinja + properties: + zone: {{ default_zone }} + region: {{ default_region}} - name: antmedia-firewall type: antmedia-firewall-template.jinja properties: @@ -41,3 +50,5 @@ resources: properties: region: {{ default_region}} autoCreateSubnetworks: false + + \ No newline at end of file diff --git a/gcp-jinja-template/antmedia.yaml b/gcp-jinja-template/antmedia.yaml index 47faf0b7..f1baae8c 100644 --- a/gcp-jinja-template/antmedia.yaml +++ b/gcp-jinja-template/antmedia.yaml @@ -13,13 +13,14 @@ # limitations under the License. imports: +- path: antmedia-vpc-template.jinja - path: antmedia-mongodb-template.jinja - path: antmedia-instance-template.jinja +- path: antmedia-instance-group-template.jinja - path: antmedia-firewall-template.jinja -- path: antmedia-vpc-template.jinja +- path: antmedia-loadbalancer-template.jinja - path: antmedia.jinja resources: - name: antmedia type: antmedia.jinja - From 663e99ef9067eff4d2a1834686563396f763a495 Mon Sep 17 00:00:00 2001 From: Murat Ugur Eminoglu Date: Thu, 30 May 2024 19:39:11 +0300 Subject: [PATCH 3/3] Add Image ID as a parameter --- gcp-jinja-template/antmedia-instance-template.jinja | 4 ++-- gcp-jinja-template/antmedia.jinja | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/gcp-jinja-template/antmedia-instance-template.jinja b/gcp-jinja-template/antmedia-instance-template.jinja index f39aeef2..90495a5e 100644 --- a/gcp-jinja-template/antmedia-instance-template.jinja +++ b/gcp-jinja-template/antmedia-instance-template.jinja @@ -21,7 +21,7 @@ resources: boot: true autoDelete: true initializeParams: - sourceImage: projects/{{ env["project"] }}/global/images/ams-marketplace282 + sourceImage: projects/{{ env["project"] }}/global/images/{{ properties["image_id"] }} networkInterfaces: - network: $(ref.antmedia-vpc-network.selfLink) subnetwork: $(ref.origin-subnet.selfLink) @@ -53,7 +53,7 @@ resources: boot: true autoDelete: true initializeParams: - sourceImage: projects/{{ env["project"] }}/global/images/ams-marketplace282 + sourceImage: projects/{{ env["project"] }}/global/images/{{ properties["image_id"] }} networkInterfaces: - network: $(ref.antmedia-vpc-network.selfLink) subnetwork: $(ref.edge-subnet.selfLink) diff --git a/gcp-jinja-template/antmedia.jinja b/gcp-jinja-template/antmedia.jinja index 988008bd..8af57d07 100644 --- a/gcp-jinja-template/antmedia.jinja +++ b/gcp-jinja-template/antmedia.jinja @@ -18,6 +18,8 @@ limitations under the License. {% set mongodb_machine_type = "e2-standard-2" %} {% set origin_machine_type = "c2d-standard-4" %} {% set edge_machine_type = "c2d-standard-4" %} +{% set image_id = "ams-latest" %} + resources: @@ -32,6 +34,8 @@ resources: zone: {{ default_zone }} origin_machine_type: {{ origin_machine_type }} edge_machine_type: {{ edge_machine_type }} + image_id: {{ image_id }} + - name: antmedia-instance-group type: antmedia-instance-group-template.jinja properties: