Skip to content

Commit e6ba72d

Browse files
authored
More enhancements to Terraform script and Ansible playbook (#27)
- Polish the environment variables in Ansible playbook. - Use Terraform template to render the Ansible inventory. - Fine tune the security groups. - Reorganize the Terraform scripts for different components. - Add more Terraform output informations.
1 parent 2018343 commit e6ba72d

15 files changed

+368
-161
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ aws/.terraform/
1515
aws/.terraform.lock.hcl
1616
aws/terraform.tfstate
1717
aws/terraform.tfstate.backup
18+
ansible/local.var.yaml
1819
ansible/inventory
20+
!ansible/inventory/template

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,32 @@ After confirming connectivity, proceed to install Apache SkyWalking using the An
167167
```
168168
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user --private-key "$SSH_KEY_FILE" playbooks/install-skywalking.yml
169169
```
170+
171+
### 4. Configurations
172+
173+
The Ansible playbook can be customized to install Apache SkyWalking with
174+
different configurations. The following variables can be modified to suit your
175+
needs:
176+
177+
> For full configurations, refer to the
178+
> [ansible/roles/skywalking/vars/main.yml](ansible/roles/skywalking/vars/main.yml).
179+
> file.
180+
181+
```yaml
182+
# `skywalking_tarball` can be a remote URL or a local path, if it's a remote URL
183+
# the remote file will be downloaded to the remote host and then extracted,
184+
# if it's a local path, the local file will be copied to the remote host and
185+
# then extracted.
186+
skywalking_tarball: "https://dist.apache.org/repos/dist/release/skywalking/9.5.0/apache-skywalking-apm-9.5.0.tar.gz"
187+
188+
# `skywalking_ui_environment` is a dictionary of environment variables that will
189+
# be sourced when running the skywalking-ui service. All environment variables
190+
# that are supported by SkyWalking webapp can be set here.
191+
skywalking_ui_environment: {}
192+
193+
# `skywalking_oap_environment` is a dictionary of environment variables that will
194+
# be sourced when running the skywalking-oap service. All environment variables
195+
# that are supported by SkyWalking OAP can be set here.
196+
skywalking_oap_environment: {}
197+
198+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
skywalking:
20+
children:
21+
skywalking_oap:
22+
skywalking_ui:
23+
24+
skywalking_oap:
25+
hosts:
26+
%{ for oap in oap_instances ~}
27+
${oap.public_ip}:
28+
private_ip: ${oap.private_ip}
29+
%{ endfor ~}
30+
31+
skywalking_ui:
32+
hosts:
33+
%{ for ui in ui_instances ~}
34+
${ui.public_ip}:
35+
private_ip: ${ui.private_ip}
36+
%{ endfor ~}

ansible/roles/skywalking/tasks/main.yml

+26-21
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
group: skywalking
2424
mode: "0755"
2525

26-
- name: Download Apache SkyWalking tarball
26+
- name: Download remote Apache SkyWalking tarball
2727
when: skywalking_tarball is match('^https?://.*')
2828
get_url:
2929
url: "{{ skywalking_tarball }}"
30-
timeout: 120
3130
dest: /usr/local/skywalking/apache-skywalking-apm.tar.gz
3231

33-
- name: Upload Local Apache SkyWalking tarball
32+
- name: Upload local Apache SkyWalking tarball
3433
when: skywalking_tarball is not match('^https?://.*')
3534
ansible.builtin.copy:
3635
src: "{{ skywalking_tarball }}"
@@ -39,26 +38,39 @@
3938
group: skywalking
4039
mode: '0755'
4140

42-
- name: Extract tar file
41+
- name: Extract tarball
4342
unarchive:
4443
src: /usr/local/skywalking/apache-skywalking-apm.tar.gz
45-
dest: "/usr/local/skywalking"
44+
dest: /usr/local/skywalking
4645
remote_src: yes
46+
owner: skywalking
47+
group: skywalking
4748
extra_opts: [--strip-components=1]
4849

50+
- name: Generate environment file for webui service
51+
template:
52+
src: skywalking-ui.env.j2
53+
dest: /home/skywalking/webapp.env
54+
owner: skywalking
55+
mode: "0660"
56+
when: inventory_hostname in groups['skywalking_ui']
57+
58+
- name: Generate environment file for OAP service
59+
template:
60+
src: skywalking-oap.env.j2
61+
dest: /home/skywalking/oap.env
62+
owner: skywalking
63+
mode: "0660"
64+
when: inventory_hostname in groups['skywalking_oap']
65+
4966
- name: Check hostgroup size
5067
set_fact:
5168
group_size: "{{ groups['skywalking_oap'] | length }}"
52-
oap_standalone: "{{ [groups['skywalking_oap'][0]] }}"
53-
oap_bundled: "{{ groups['skywalking_oap'][1:] if groups['skywalking_oap'] | length > 1 else [] }}"
69+
oap_init_node: "{{ [groups['skywalking_oap'][0]] }}"
5470

5571
- name: Run the OAPSericeInit script
56-
shell: "sudo -u skywalking /usr/local/skywalking/bin/oapServiceInit.sh"
57-
when: inventory_hostname in oap_standalone
58-
59-
- name: Run the OAPSericeNoInit script
60-
shell: "sudo -u skywalking /usr/local/skywalking/bin/oapServiceNoInit.sh"
61-
when: inventory_hostname in oap_bundled
72+
command: "sudo -u skywalking /usr/local/skywalking/bin/oapServiceInit.sh"
73+
when: inventory_hostname in oap_init_node
6274

6375
- name: Generate systemd unit file for oap service
6476
template:
@@ -76,14 +88,6 @@
7688
mode: "0660"
7789
when: inventory_hostname in groups['skywalking_ui']
7890

79-
- name: Registration of OAP Server address within WebUI environment file
80-
ansible.builtin.lineinfile:
81-
path: "{{ env_file }}"
82-
line: "{{ item.key }}={{ item.value }}"
83-
create: yes
84-
loop: "{{ sw_ui_env_vars | dict2items }}"
85-
when: inventory_hostname in groups['skywalking_ui']
86-
8791
- name: Reload systemd
8892
systemd:
8993
daemon_reload: yes
@@ -111,3 +115,4 @@
111115
name: skywalking-ui
112116
state: started
113117
when: inventory_hostname in groups['skywalking_ui']
118+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
{% for key, value in skywalking_oap_environment.items() %}
20+
{{ key }}="{{ value }}"
21+
{% endfor %}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
{% for key, value in skywalking_ui_environment.items() %}
20+
{{ key }}="{{ value }}"
21+
{% endfor %}
22+
23+
SW_OAP_ADDRESS="{% for host in groups['skywalking_oap'] %}http://{{ hostvars[host].private_ip }}:{{ skywalking_ui_environment['SW_CORE_GRPC_PORT'] | default ('12800') }}{% if not loop.last %},{% endif %}{% endfor %}"
24+
SW_ZIPKIN_ADDRESS="{% for host in groups['skywalking_oap'] %}http://{{ hostvars[host].private_ip }}:{{ skywalking_ui_environment['SW_QUERY_ZIPKIN_REST_PORT'] | default ('9412') }}{% if not loop.last %},{% endif %}{% endfor %}"
25+

ansible/roles/skywalking/templates/skywalking-ui.service.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ After=network.target
1919

2020
[Service]
2121
Type=simple
22-
EnvironmentFile=/usr/local/skywalking/webapp/sw_ui_env_file
22+
EnvironmentFile=/home/skywalking/webapp.env
2323
User=skywalking
2424
Group=skywalking
2525
ExecStart=/usr/local/skywalking/bin/webappService.sh

ansible/roles/skywalking/vars/main.yml

+11-9
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
# limitations under the License.
1515

1616
---
17-
# skywalking_tarball can be a remote URL or a local path, if it's a remote URL
17+
# `skywalking_tarball` can be a remote URL or a local path, if it's a remote URL
1818
# the remote file will be downloaded to the remote host and then extracted,
1919
# if it's a local path, the local file will be copied to the remote host and
2020
# then extracted.
2121
skywalking_tarball: "https://dist.apache.org/repos/dist/release/skywalking/9.5.0/apache-skywalking-apm-9.5.0.tar.gz"
22-
sw_ui_server_port: "8080"
23-
sw_oap_server_port: "12800"
24-
sw_zipkin_address: "9412"
2522

26-
sw_ui_env_vars:
27-
SW_SERVER_PORT: "{{ sw_ui_server_port }}"
28-
SW_OAP_ADDRESS: "{% for host in groups['skywalking_oap'] %}http://{{ hostvars[host].inventory_hostname }}:{{ sw_oap_server_port }}{% if not loop.last %},{% endif %}{% endfor %}"
29-
SW_ZIPKIN_ADDRESS: "{% for host in groups['skywalking_oap'] %}http://{{ hostvars[host].inventory_hostname }}:{{ sw_zipkin_address }}{% if not loop.last %},{% endif %}{% endfor %}"
30-
env_file: /usr/local/skywalking/webapp/sw_ui_env_file
23+
# `skywalking_ui_environment` is a dictionary of environment variables that will
24+
# be sourced when running the skywalking-ui service. All environment variables
25+
# that are supported by SkyWalking webapp can be set here.
26+
skywalking_ui_environment: {}
27+
28+
# `skywalking_oap_environment` is a dictionary of environment variables that will
29+
# be sourced when running the skywalking-oap service. All environment variables
30+
# that are supported by SkyWalking OAP can be set here.
31+
skywalking_oap_environment: {}
32+

aws/ec2-main.tf

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
provider "aws" {
17+
region = var.region
18+
access_key = var.access_key
19+
secret_key = var.secret_key
20+
}
21+
22+
resource "aws_security_group" "ssh-access" {
23+
name = "ssh-access"
24+
description = "Allow SSH access from the Internet"
25+
ingress = [
26+
{
27+
from_port = 22
28+
to_port = 22
29+
protocol = "tcp"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
description = "Allow SSH access from the Internet"
32+
ipv6_cidr_blocks = []
33+
prefix_list_ids = []
34+
security_groups = []
35+
self = false
36+
}
37+
]
38+
tags = var.extra_tags
39+
}
40+
41+
resource "aws_security_group" "public-egress-access" {
42+
name = "public-egress-access"
43+
description = "Allow access to the Internet"
44+
egress = [
45+
{
46+
from_port = 0
47+
to_port = 0
48+
protocol = -1
49+
cidr_blocks = ["0.0.0.0/0"]
50+
description = "Allow access to the Internet"
51+
ipv6_cidr_blocks = []
52+
prefix_list_ids = []
53+
security_groups = []
54+
self = false
55+
}
56+
]
57+
tags = var.extra_tags
58+
}
59+
60+
resource "local_file" "inventories" {
61+
filename = "${path.module}/../ansible/inventory/skywalking.yaml"
62+
file_permission = "0600"
63+
content = templatefile("${path.module}/../ansible/inventory/template/skywalking.yaml.tftpl", {
64+
oap_instances = aws_instance.skywalking-oap
65+
ui_instances = aws_instance.skywalking-ui
66+
})
67+
}

0 commit comments

Comments
 (0)