הגדרה של אשכול מנויים לשכפול לוגי

בוחרים גרסה של מאמרי העזרה:

במסמך הזה מובאות דוגמאות שממחישות איך ליצור ולהגדיר באופן ידני אשכול מנויים. אשכול מנויים הוא אשכול מסדי נתונים שמקבל נתונים ששוכפלו מאשכול בעלי אפליקציות.

קטעי הקוד בדף הזה הם דוגמאות שאפשר להשתמש בהן כמודלים, ולהחליף את הערכים בערכים של משאבי AlloyDB Omni.

לפני שמתחילים

יצירה והגדרה של אשכול המינויים

  1. יוצרים אשכול מנויים.

    $ cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: Secret
    metadata:
      name: db-pw-DB_CLUSTER_NAME
    type: Opaque
    data:
      DB_CLUSTER_NAME: "ENCODED_PASSWORD" # Password is odspassword
    ---
    apiVersion: alloydbomni.dbadmin.goog/v1
    kind: DBCluster
    metadata:
      name: subscriber
    spec:
      databaseVersion: "15.12.0"
      primarySpec:
        adminUser:
          passwordRef:
            name: db-pw-DB_CLUSTER_NAME
        resources:
          memory: MEMORY_SIZE
          cpu: CPU_COUNT
          disks:
          - name: DataDisk
            size: DISK_SIZE
    EOF
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫DB_CLUSTER_NAME: השם של אשכול מסד הנתונים הזה, לדוגמה subscriber-cluster.
    • ‫ENCODED_PASSWORD: סיסמת ההתחברות למסד הנתונים עבור תפקיד המשתמש שמוגדר כברירת מחדל postgres, בקידוד base64. לדוגמה, Q2hhbmdlTWUxMjM= עבור ChangeMe123.

    • ‫CPU_COUNT: מספר המעבדים הזמינים לכל מופע של מסד נתונים באשכול מסדי הנתונים הזה.

    • ‫MEMORY_SIZE: כמות הזיכרון לכל מופע של מסד נתונים באשכול מסדי הנתונים הזה. מומלץ להגדיר את הערך הזה ל-8GB לכל מעבד (CPU). לדוגמה, אם הגדרתם את cpu ל-2 בשלב מוקדם יותר במניפסט, מומלץ להגדיר את memory ל-16Gi.

    • ‫DISK_SIZE: גודל הדיסק לכל מכונה של מסד נתונים, לדוגמה 10Gi.

  2. מוצאים את הפוד הרצוי.

    $ kubectl get pod -l "alloydbomni.internal.dbadmin.goog/dbcluster=DB_CLUSTER_NAME, alloydbomni.internal.dbadmin.goog/task-type=database, dbs.internal.dbadmin.goog/ha-role=Primary"
    
  3. מתחברים ל-pod של מסד הנתונים של אשכול המנויים.

    $ kubectl get pod
    NAME                                          READY   STATUS    RESTARTS   AGE
    al-2bce-publisher-0                           3/3     Running   0          20h
    
    $ kubectl exec -ti SUBSCRIBER_POD_NAME  -- /bin/bash
    Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init)
    postgres@al-3513-subscriber-0:/$
    

    מחליפים את SUBSCRIBER_POD_NAME בשם של pod המנוי.

  4. מאתרים את כתובת ה-IP של מאזן העומסים באשכול מסד הנתונים של המפרסם, למשל 10.116.14.190.

    $ kubectl get service
    NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
    al-publisher-rw-ilb      ClusterIP      10.116.14.190   <none>         5432/TCP         21h
    
  5. מגבים את הסכימה מהמפרסם כעותק ראשוני של הנתונים שפורסמו במסד הנתונים של המפרסם. שכפול לוגי לא תומך בשכפול DDL. סכימה או טבלה שמתכננים לשכפל צריכות להיות קיימות ביעד (אשכול מנויים) לפני שמתחילים בשכפול לוגי.

    postgres@al-3513-subscriber-0:/$ pg_dump -h PUBLISHER_IP_ADDRESS -U postgres --create --schema-only customer > /tmp/customer.schema-only.sql
    

    מחליפים את PUBLISHER_IP_ADDRESS בכתובת ה-IP של מאזן העומסים באשכול מסד הנתונים של המפרסם.

  6. מחילים את הגיבוי במסד הנתונים של המנוי.

    postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres < /tmp/customer.schema-only.sql
    
  7. אופציונלי: מוודאים שאין נתונים בטבלה.

    # There is no data in table company
    customer=# select * from company;
    id | name | age | salary
    ----+------+-----+--------
    (0 rows)
    
  8. יוצרים מינוי למסד הנתונים. מוודאים שהפרסום כבר נוצר ב-DBCluster של בעל התוכן הדיגיטלי.

    postgres@al-3513-subscriber-0:/$ CREATE SUBSCRIPTION sub_customer CONNECTION 'host=PUBLISHER_IP_ADDRESS port=5432 user=REPLICATION_USER dbname=DATABASE_NAME password=PUBLISHER_CLUSTER_PASSWORD sslmode=require' PUBLICATION PUBLICATION_NAME WITH (slot_name='REPLICATION_SLOT_NAME');
    

    מחליפים את מה שכתוב בשדות הבאים:

    • ‫REPLICATION_USER: שם המשתמש שמתחבר למשבצת הרפליקציה.
    • ‫DATABASE_NAME: השם של מסד הנתונים שאת השינויים שלו רוצים להעביר בסטרימינג מחריץ הרפליקציה.
    • ‫PUBLISHER_CLUSTER_PASSWORD: סיסמת ההתחברות למסד הנתונים של המשתמש postgres ב-DBCluster של המפרסם.
    • ‫PUBLICATION_NAME: שם הפרסום שהמנוי נרשם אליו.
    • ‫REPLICATION_SLOT_NAME: השם של משבצת הרפליקציה שנוצרה ב-DBCluster של המפרסם.
  9. אופציונלי: מאמתים את הרפליקציה באשכול המנויים.

    postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres DATABASE_NAME
    customer=# select * from public.company;
    id | name  | age | salary
    ----+-------+-----+--------
      1 | Quinn |  25 |  65000
      2 | Kim   |  22 |  72250
      3 | Bola  |  31 |  53000
      4 | Sasha |  33 | 105000
      5 | Yuri  |  27 |  85000
    (5 rows)
    
  10. באשכול של בעל האפליקציה, מוסיפים שורה לטבלה.

    # On the publisher database
    $ kubectl exec -ti al-2bce-publisher-0  -- /bin/bash
    Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init)
    postgres@al-2bce-publisher-0:/$ psql -h localhost -U postgres DATABASE_NAME
    customer=# insert into TABLE_NAME (id, name, age, salary) values (6, 'Alex', 39, 100000);
    

    מחליפים את TABLE_NAME בשם הטבלה ב-DBCluster של המפרסם שאליו המנוי רשום.

  11. באשכול המנויים, מוודאים שהשורה שנוספה לטבלה באשכול המפרסמים שוכפלה לטבלה באשכול המנויים.

    # On the subscriber database, data is synced.
    postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres DATABASE_NAME
    customer=# select * from TABLE_NAME;
    id | name  | age | salary
    ----+-------+-----+--------
      1 | Quinn |  25 |  65000
      2 | Kim   |  22 |  72250
      3 | Bola  |  31 |  53000
      4 | Sasha |  33 | 105000
      5 | Yuri  |  27 |  85000
      6 | Alex  |  39 | 100000
    (6 rows)
    

יצירת טבלאות נוספות באופן ידני

רפליקציה לוגית לא מסנכרנת באופן אוטומטי שינויים ב-DDL, בניגוד ל-replicate_ddl_command ב-pglogical. הכלי pgl_ddl_deploy בקוד פתוח מציע פתרון, אבל אפשר גם להריץ פקודות DDL באופן ידני במנוי.

  1. כדי להמחיש את זה, יוצרים טבלה חדשה בשם finance בcustomerמסד הנתונים באשכול של בעל התוכן הדיגיטלי.

    # On the publisher database
    $ kubectl exec -ti al-2bce-publisher-0  -- /bin/bash
    Defaulted container "database" out of: database, logrotate-agent, memoryagent, dbinit (init)
    postgres@al-2bce-publisher-0:/$ psql -h localhost -U postgres customer
    customer=# create table finance (row text);
    CREATE TABLE
    customer=# insert into finance values ('critical data');
    INSERT 0 1
    customer=# ALTER PUBLICATION pub_customer ADD TABLE finance;
    ALTER PUBLICATION
    
  2. כשמוסיפים טבלה חדשה לאשכול של בעל התוכן הדיגיטלי, צריך להחיל ידנית את ה-DDL (יצירת הטבלה) במנוי, ואז לאמת את השכפול על ידי הפעלת הפקודה הבאה באשכול של המנוי.

    postgres@al-3513-subscriber-0:/$ psql -h localhost -U postgres customer
    customer=# create table finance (row text);
    CREATE TABLE
    customer=# ALTER SUBSCRIPTION sub_customer REFRESH PUBLICATION;
    ALTER SUBSCRIPTION
    customer=# select * from finance;
          row
    ---------------
    critical data
    (1 row)
    

המאמרים הבאים