Skip to content

Commit 3fbc7c1

Browse files
authored
PMM-14170: Add Valkey setup and configuration scripts (#154)
* PMM-14170: Add Valkey setup and configuration scripts * PMM-14170 Fixes peter review * PMM-14170 Fix peter comments * PMM-14170 Fix comments * PMM-14170 Fix loop for pmm client * PMM-14170 Variables setup * PMM-14170 Fix OL-10 Password * PMM-14170 fix loop * test * test loop * test loop * PMM-14170 Review of Peter * PMM-14170 Update cleanup.sh * PMM-14170 Review from Vasyl * PMM-14170 Fix random number
1 parent efa383d commit 3fbc7c1

File tree

7 files changed

+343
-0
lines changed

7 files changed

+343
-0
lines changed

pmm_qa/pmm-framework.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,31 @@ def setup_dockerclients(db_type, db_version=None, db_config=None, args=None):
789789
# Call the function to run the setup_docker_client_images script
790790
execute_shell_scripts(shell_scripts, shell_scripts_path, env_vars, args)
791791

792+
def setup_valkey(db_type, db_version=None, db_config=None, args=None):
793+
794+
# Check if PMM server is running
795+
container_name = get_running_container_name()
796+
if container_name is None and args.pmm_server_ip is None:
797+
print(f"Check if PMM Server is Up and Running..Exiting")
798+
exit()
799+
800+
# Gather Version details
801+
valkey_version = os.getenv('VALKEY_VERSION') or db_version or database_configs[db_type]["versions"][-1]
802+
803+
# Define environment variables for playbook
804+
env_vars = {
805+
'PMM_SERVER_IP': args.pmm_server_ip or container_name or '127.0.0.1',
806+
'VALKEY_VERSION': valkey_version,
807+
'CLIENT_VERSION': get_value('CLIENT_VERSION', db_type, args, db_config),
808+
'ADMIN_PASSWORD': os.getenv('ADMIN_PASSWORD') or args.pmm_server_password or 'admin',
809+
'PMM_QA_GIT_BRANCH': os.getenv('PMM_QA_GIT_BRANCH') or 'v3'
810+
}
811+
812+
# Ansible playbook filename
813+
playbook_filename = 'valkey/valkey.yml'
814+
815+
# Call the function to run the Ansible playbook
816+
run_ansible_playbook(playbook_filename, env_vars, args)
792817

793818
# Set up databases based on arguments received
794819
def setup_database(db_type, db_version=None, db_config=None, args=None):
@@ -835,6 +860,8 @@ def setup_database(db_type, db_version=None, db_config=None, args=None):
835860
setup_ssl_mlaunch(db_type, db_version, db_config, args)
836861
elif db_type == 'BUCKET':
837862
setup_bucket(db_type, db_version, db_config, args)
863+
elif db_type == 'VALKEY':
864+
setup_valkey(db_type, db_version, db_config, args)
838865
else:
839866
print(f"Database type {db_type} is not recognised, Exiting...")
840867
exit(1)

pmm_qa/scripts/database_options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,9 @@
7676
},
7777
"BUCKET": {
7878
"configurations": {"BUCKET_NAMES": 'bcp'}
79+
},
80+
"VALKEY": {
81+
"versions": ["7", "8"],
82+
"configurations": {"CLIENT_VERSION": "3-dev-latest", "SETUP_TYPE": "", "TARBALL": ""}
7983
}
8084
}

pmm_qa/valkey/cleanup.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash -e
2+
3+
docker exec -it pmm-server pmm-admin remove valkey valkey-primary-svc || :
4+
docker exec -it pmm-server pmm-admin remove valkey valkey-replica1-svc || :
5+
docker exec -it pmm-server pmm-admin remove valkey valkey-replica2-svc || :
6+
docker exec -it pmm-server pmm-admin remove valkey sentinel1-svc || :
7+
docker exec -it pmm-server pmm-admin remove valkey sentinel2-svc || :
8+
docker exec -it pmm-server pmm-admin remove valkey sentinel3-svc || :
9+
10+
docker rm -vf valkey-primary valkey-replica-1 valkey-replica-2 || :
11+
docker rm -vf sentinel-1 sentinel-2 sentinel-3 || :
12+
13+
docker volume rm -f valkey-primary-data valkey-replica-1-data valkey-replica-2-data || :
14+
15+
rm -rf "$HOME/valkey"

pmm_qa/valkey/sentinel.conf.j2

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# sentinel.conf
2+
bind 0.0.0.0
3+
4+
port 26379
5+
6+
# Monitor the master
7+
sentinel monitor valkey-primary valkey-primary 6379 {{ sentinel_quorum }}
8+
sentinel auth-user valkey-primary default
9+
sentinel auth-pass valkey-primary "{{ valkey_password }}"
10+
sentinel resolve-hostnames yes
11+
12+
# Failover timeouts
13+
sentinel down-after-milliseconds valkey-primary 5000
14+
sentinel failover-timeout valkey-primary 10000
15+
sentinel parallel-syncs valkey-primary 1
16+
17+
# Security
18+
protected-mode no
19+
20+
# Logging
21+
loglevel notice
22+
logfile ""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Basic Valkey configuration for primary
2+
bind 0.0.0.0
3+
port 6379
4+
5+
requirepass "{{ valkey_password }}"
6+
masterauth "{{ valkey_password }}"
7+
8+
# Persistence
9+
save 900 1
10+
save 300 10
11+
save 60 10000
12+
13+
# Replication
14+
replica-serve-stale-data yes
15+
replica-read-only yes
16+
repl-diskless-sync no
17+
repl-diskless-sync-delay 5
18+
19+
# Security
20+
protected-mode no
21+
22+
# Logging
23+
loglevel notice
24+
logfile ""
25+
26+
# Memory management
27+
maxmemory-policy allkeys-lru
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Basic Valkey configuration for replica
2+
bind 0.0.0.0
3+
port 6379
4+
5+
requirepass "{{ valkey_password }}"
6+
masterauth "{{ valkey_password }}"
7+
8+
# Replication
9+
replicaof valkey-primary 6379
10+
replica-serve-stale-data yes
11+
replica-read-only yes
12+
repl-diskless-sync no
13+
repl-diskless-sync-delay 5
14+
15+
# Persistence
16+
save 900 1
17+
save 300 10
18+
save 60 10000
19+
20+
# Security
21+
protected-mode no
22+
23+
# Logging
24+
loglevel notice
25+
logfile ""
26+
27+
# Memory management
28+
maxmemory-policy allkeys-lru

pmm_qa/valkey/valkey.yml

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
---
2+
- name: Deploy Valkey Cluster with Sentinel
3+
hosts: localhost
4+
gather_facts: false
5+
vars:
6+
pmm_server_ip: "{{ lookup('vars', 'extra_pmm_server_ip', default=lookup('env','PMM_SERVER_IP') | default('127.0.0.1', true) ) }}"
7+
metrics_mode: "{{ lookup('env', 'metrics_mode') }}"
8+
client_version: "{{ lookup('vars', 'extra_client_version', default=lookup('env','CLIENT_VERSION') | default('3-dev-latest', true) ) }}"
9+
admin_password: "{{ lookup('vars', 'extra_admin_password', default=lookup('env','ADMIN_PASSWORD') | default('admin', true) ) }}"
10+
valkey_version: "{{ lookup('env', 'VALKEY_VERSION') | default('7', true) }}"
11+
valkey_image: "valkey/valkey:{{ valkey_version }}"
12+
valkey_network_name: "pmm-qa"
13+
valkey_password: "VKvl41568AsE"
14+
valkey_data_dir: "{{ lookup('env', 'HOME') }}/valkey/data"
15+
valkey_config_dir: "{{ lookup('env', 'HOME') }}/valkey/config"
16+
valkey_primary_port: 6379
17+
valkey_replica_count: 2
18+
valkey_replica_start_port: 6380
19+
sentinel_count: 3
20+
sentinel_start_port: 26379
21+
sentinel_quorum: 2
22+
23+
pmm_server_name: "pmm-server"
24+
25+
tasks:
26+
- name: Set Random Number Fact
27+
set_fact:
28+
random_number: "{{ (10000 | random) | int }}"
29+
30+
- name: Create Docker network
31+
community.docker.docker_network:
32+
name: "{{ valkey_network_name }}"
33+
driver: bridge
34+
state: present
35+
36+
- name: Create config directory
37+
file:
38+
path: "{{ valkey_config_dir }}"
39+
state: directory
40+
mode: '0755'
41+
42+
- name: Create a config directory per Sentinel
43+
file:
44+
path: "{{ valkey_config_dir }}/sentinel-{{ item }}"
45+
state: directory
46+
mode: '0755'
47+
loop: "{{ range(1, sentinel_count + 1) | list }}"
48+
49+
- name: Create data directory
50+
file:
51+
path: "{{ valkey_data_dir }}"
52+
state: directory
53+
mode: '0755'
54+
55+
- name: Generate Valkey primary configuration
56+
template:
57+
src: valkey-primary.conf.j2
58+
dest: "{{ valkey_config_dir }}/valkey-primary.conf"
59+
mode: '0644'
60+
61+
- name: Generate Valkey replica configurations
62+
template:
63+
src: valkey-replica.conf.j2
64+
dest: "{{ valkey_config_dir }}/valkey-replica-{{ item }}.conf"
65+
mode: '0644'
66+
loop: "{{ range(1, valkey_replica_count + 1) | list }}"
67+
68+
- name: Generate Sentinel configurations
69+
template:
70+
src: sentinel.conf.j2
71+
dest: "{{ valkey_config_dir }}/sentinel-{{ item }}/sentinel.conf"
72+
mode: '0664'
73+
loop: "{{ range(1, sentinel_count + 1) | list }}"
74+
75+
- name: Create Docker volume for primary data
76+
community.docker.docker_volume:
77+
name: "valkey-primary-data"
78+
state: present
79+
80+
- name: Create Docker volumes for replica data
81+
community.docker.docker_volume:
82+
name: "valkey-replica-{{ item }}-data"
83+
state: present
84+
loop: "{{ range(1, valkey_replica_count + 1) | list }}"
85+
86+
- name: Start Valkey primary container
87+
community.docker.docker_container:
88+
name: "valkey-primary"
89+
image: "{{ valkey_image }}"
90+
state: started
91+
restart_policy: unless-stopped
92+
networks:
93+
- name: "{{ valkey_network_name }}"
94+
ports:
95+
- "{{ valkey_primary_port }}:6379"
96+
volumes:
97+
- "valkey-primary-data:/data"
98+
- "{{ valkey_config_dir }}/valkey-primary.conf:/usr/local/etc/valkey/valkey.conf:ro"
99+
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
100+
healthcheck:
101+
test: ["CMD", "valkey-cli", "-a", "{{ valkey_password }}", "ping"]
102+
interval: 10s
103+
timeout: 5s
104+
retries: 5
105+
106+
- name: Wait for the primary to be ready
107+
wait_for:
108+
host: localhost
109+
port: "{{ valkey_primary_port }}"
110+
timeout: 30
111+
delay: 1
112+
113+
- name: Start Valkey replica containers
114+
community.docker.docker_container:
115+
name: "valkey-replica-{{ item }}"
116+
image: "{{ valkey_image }}"
117+
state: started
118+
restart_policy: unless-stopped
119+
networks:
120+
- name: "{{ valkey_network_name }}"
121+
ports:
122+
- "{{ valkey_replica_start_port + item - 1 }}:6379"
123+
volumes:
124+
- "valkey-replica-{{ item }}-data:/data"
125+
- "{{ valkey_config_dir }}/valkey-replica-{{ item }}.conf:/usr/local/etc/valkey/valkey.conf:ro"
126+
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
127+
healthcheck:
128+
test: ["CMD", "valkey-cli", "-a", "{{ valkey_password }}", "ping"]
129+
interval: 10s
130+
timeout: 5s
131+
retries: 5
132+
loop: "{{ range(1, valkey_replica_count + 1) | list }}"
133+
134+
- name: Wait for replicas to be ready
135+
wait_for:
136+
host: localhost
137+
port: "{{ valkey_replica_start_port + item - 1 }}"
138+
timeout: 30
139+
delay: 1
140+
loop: "{{ range(1, valkey_replica_count + 1) | list }}"
141+
142+
- name: Start Sentinel containers
143+
community.docker.docker_container:
144+
name: "sentinel-{{ item }}"
145+
image: "{{ valkey_image }}"
146+
state: started
147+
restart_policy: unless-stopped
148+
networks:
149+
- name: "{{ valkey_network_name }}"
150+
ports:
151+
- "{{ sentinel_start_port + item - 1 }}:26379"
152+
volumes:
153+
- "{{ valkey_config_dir }}/sentinel-{{ item }}:/usr/local/etc/valkey"
154+
command: ["valkey-sentinel", "/usr/local/etc/valkey/sentinel.conf"]
155+
healthcheck:
156+
test: ["CMD", "valkey-cli", "-p", "{{ sentinel_start_port }}", "ping"]
157+
interval: 10s
158+
timeout: 5s
159+
retries: 5
160+
loop: "{{ range(1, sentinel_count + 1) | list }}"
161+
162+
- name: Wait for Sentinels to be ready
163+
wait_for:
164+
host: localhost
165+
port: "{{ sentinel_start_port + item - 1 }}"
166+
timeout: 30
167+
delay: 1
168+
loop: "{{ range(1, sentinel_count + 1) | list }}"
169+
170+
- name: Verify cluster status
171+
community.docker.docker_container_exec:
172+
container: "valkey-primary"
173+
command: valkey-cli -a "{{ valkey_password }}" info replication
174+
register: cluster_status
175+
176+
- name: Display cluster status
177+
debug:
178+
msg: "{{ cluster_status.stdout_lines }}"
179+
180+
- name: Run Sentinel status command
181+
community.docker.docker_container_exec:
182+
container: "sentinel-1"
183+
command: valkey-cli -p {{ sentinel_start_port }} sentinel masters
184+
register: sentinel_status
185+
186+
- name: Display Sentinel status
187+
debug:
188+
msg: "{{ sentinel_status.stdout_lines }}"
189+
190+
- name: Install PMM Client in each container
191+
ansible.builtin.include_tasks: ../tasks/install_pmm_client.yml
192+
loop: >-
193+
{{ ['valkey-primary']
194+
+ (range(1, valkey_replica_count + 1) | map('string') | map('regex_replace', '^(.*)$', 'valkey-replica-\1') | list)
195+
+ (range(1, sentinel_count + 1) | map('string') | map('regex_replace', '^(.*)$', 'sentinel-\1') | list)
196+
}}
197+
loop_control:
198+
loop_var: current_container_name
199+
vars:
200+
container_name: "{{ current_container_name }}"
201+
202+
- name: Add the primary to monitoring
203+
community.docker.docker_container_exec:
204+
container: "valkey-primary"
205+
command: pmm-admin add valkey --cluster=valkey-cluster --replication-set=valkey-repl --environment=valkey-test --username=default --password="{{ valkey_password }}" --service-name=valkey-primary-svc-{{ random_number }} --host=valkey-primary --port=6379 --custom-labels='role=primary'
206+
ignore_errors: yes
207+
208+
- name: Add the replicas to monitoring
209+
community.docker.docker_container_exec:
210+
container: "valkey-replica-{{ item }}"
211+
command: pmm-admin add valkey --cluster=valkey-cluster --replication-set=valkey-repl --environment=valkey-test --username=default --password="{{ valkey_password }}" --service-name=valkey-replica{{ item }}-svc-{{ random_number }} --host=valkey-replica-{{ item }} --port=6379 --custom-labels='role=replica'
212+
loop: "{{ range(1, valkey_replica_count + 1) | list }}"
213+
ignore_errors: yes
214+
215+
- name: Add Sentinels to monitoring
216+
community.docker.docker_container_exec:
217+
container: "sentinel-{{ item }}"
218+
command: pmm-admin add valkey --cluster=valkey-cluster --environment=valkey-test --username=default --password="{{ valkey_password }}" --service-name=sentinel{{ item }}-svc-{{ random_number }} --host=sentinel-{{ item }} --port={{ sentinel_start_port }} --custom-labels='role=sentinel'
219+
loop: "{{ range(1, sentinel_count + 1) | list }}"
220+
ignore_errors: yes

0 commit comments

Comments
 (0)