©2015 - 2022 Chad’s Technoworks. Disclaimer and Terms of Use

Chad’s TechnoWorks My Journal On Technology

Information Technology

Sample Sync Gateway Installation And Configuration Procedure

TABLE OF CONTENTS


Purpose Of Sync Gateway

System Configuration Requirements

  Required Kernel Parameter Changes

  Set Open Socket Range

  Create Couchbase Group

  Create Couchbase User

  Set File Descriptor Limits

Install Sync Gateway As Root

Starting And Stopping Sync Gateway

Create Sync Gateway Bucket

Create Couchbase User For Sync Gateway

Sample Sync Gateway Configuration

Verify The Gateway Database

Guide To Uninstall Sync Gateway



PURPOSE OF SYNC GATEWAY


I am re-posting the key points of what has been described as the Purpose of Sync Gateway.


NOTE: The Sync gateway goes beyond just having access control for mobile users but also it allows you to create virtual databases for specific group of users.


SYSTEM CONFIGURATION REQUIREMENTS


REQUIRED KERNEL PARAMETER CHANGES


Modify /etc/sysctl.conf to include the following parameters:


#

### Recommended kernel parameters for Couchbase:

#


# Engage swapping only when memory is exhausted

vm.swappiness = 0


# the interval between subsequential keepalive probes

# Increase the frequency of keepalive probes, identifying and timing out dead connections faster.

net.ipv4.tcp_keepalive_intvl = 30


# the number of unacknowledged probes to send before considering the connection dead and notifying the application layer

net.ipv4.tcp_keepalive_probes = 4


# the interval between the last data packet sent (simple ACKs are not considered data)

# and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further

net.ipv4.tcp_keepalive_time = 900


# Enables reuse of TIME-WAIT sockets to reduce the time spent constructing connections. This benefits new outgoing connections only.

# This setting is safe for all Couchbase protocols.

net.ipv4.tcp_tw_reuse = 1


# Limits the maximum memory used to 200MB before pdflush is involved.

# The default 20% of total system memory can overwhelm the storage system

# once flushed.

vm.dirty_bytes = 209715200

# The lower threshold for vm.dirty_bytes

vm.dirty_background_bytes = 104857600

# Do not set dirty ratio if dirty bytes are defined. Use one or the other parameter.

#vm.dirty_ratio = 10

#vm.dirty_background_ratio = 3


# Increases the rate at which data is flushed to disk

vm.dirty_expire_centisecs = 300

vm.dirty_writeback_centisecs = 100


# Increases the max open file handles.

fs.file-max = 500000


# Disables NUMA zone reclaim algorithm.  This tends to decrease read latencies.

vm.zone_reclaim_mode = 0



SET OPEN SOCKET RANGE


Adjust Open Socket range if heavy traffic is anticipated.


Default system settings:

[root@lxnode15 ~]# cat /proc/sys/net/ipv4/ip_local_port_range

32768   61000

[root@lxnode15 ~]#


Modify to increase open socket range:


vi /proc/sys/net/ipv4/ip_local_port_range

5000 64000


Open sockets allowed in a system is the difference of the above port ranges (59,000).


Reboot the Linux hosts once system changes are applied.



CREATE COUCHBASE GROUP


[root@lxnode15 ~]# groupadd -g 800 couchbase

[root@lxnode15 ~]#


CREATE COUCHBASE USER


[root@lxnode15 ~]# useradd -g couchbase -s /bin/bash -m -d /opt/couchbase -c "Couchbase system user" -u 801 couchbase

[root@lxnode15 ~]#


[root@lxnode15 ~]# passwd couchbase

Changing password for user couchbase.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

[root@lxnode15 ~]#


SET FILE DESCRIPTOR LIMITS


vi /etc/security/limits.conf

couchbase     soft  nofile  71000

couchbase     hard  nofile  71000

sync_gateway  soft  nofile  51200

sync_gateway  hard  nofile  51200


NOTE: For couchbase user, 70000 is the minimum required open file descriptor limit. But here we set at 71000.



INSTALL SYNC GATEWAY AS ROOT


Download the sync gateway installer for your platform.


[chad@lxnode15 ~]$

[chad@lxnode15 ~]$ su -

Password:

[root@lxnode15 ~]# rpm --install /home/chad/couchbase-sync-gateway-enterprise_2.0.0_x86_64.rpm

sync_gateway start/running, process 7572


You have successfully installed Couchbase sync_gateway.


You can find sample sync_gateway configuration files in the /opt/couchbase-sync-gateway/examples folder.


You can control the Couchbase sync_gateway service by using the following command:


  initctl start sync_gateway


That's it! sync_gateway is now running on port 4984. We've setup a simple in-memory database

which works great for exploring sync_gateway's capabilities. A limited console is available

by opening your browser to http://localhost:4985/_admin/.


The command-line options are:


  -adminInterface=":4985": Address to bind admin interface to

  -bucket="sync_gateway": Name of bucket

  -dbname="": Name to assign to this database (defaults to name of bucket)

  -interface=":4984": Address to bind to

  -log="": Log keywords, comma separated

  -personaOrigin="": Base URL that clients use to connect to the server

  -pool="default": Name of pool

  -pretty=false: Pretty-print JSON responses

  -url="walrus:": Address of Couchbase server

  -verbose=false: Log more info about requests



By using this software you agree to the End User License Agreement.

See /opt/couchbase-sync-gateway/LICENSE.txt.


[root@lxnode15 ~]#


There are 2 users created by the installer:


[chad@lxnode15 ~]$ tail -2 /etc/passwd

couchbase:x:801:800:Couchbase system user:/opt/couchbase:/bin/bash

sync_gateway:x:802:802::/home/sync_gateway:/bin/bash

[chad@lxnode15 ~]$


The couchbase user is the owner of the binaries (/OPT/COUCHBASE) while the sync_gateway user is the runtime user and has the ownership of the config file ($HOME/sync_gateway.json).


The default configuration of Sync Gateway primarily connects to its local Walrus database:


[chad@lxnode15 ~]$ su -

Password:

[root@lxnode15 ~]# su - sync_gateway

[sync_gateway@lxnode15 ~]$ pwd

/home/sync_gateway

[sync_gateway@lxnode15 ~]$ ls -l

total 12

drwxr-xr-x. 2 sync_gateway sync_gateway 4096 Jul 10 16:22 data

drwxr-xr-x. 2 sync_gateway sync_gateway 4096 Jul 10 16:22 logs

-rw-r--r--. 1 sync_gateway sync_gateway  285 Jul 10 16:22 sync_gateway.json

[sync_gateway@lxnode15 ~]$


[sync_gateway@lxnode15 ~]$ cat sync_gateway.json

{

        "log": ["HTTP+"],

        "adminInterface": "127.0.0.1:4985",

        "interface": "0.0.0.0:4984",

        "databases": {

                "db": {

                        "server": "walrus:data",

                        "users": {

                                "GUEST": {"disabled": false, "admin_channels": ["*"] }

                        },

      "allow_conflicts": false,

      "revs_limit": 20

                }

        }

}

[sync_gateway@lxnode15 ~]$


The port 4984 is the standard port for client connections (also known as PUBLIC REST API port) and normally is the only port you'll have to open in the firewall.

Port 4985 is for administrative purpose.


To test that your Sync Gateway port is reachable, a version response to should be displayed:


[sync_gateway@lxnode15 ~]$ curl http://localhost:4984/

{"couchdb":"Welcome","vendor":{"name":"Couchbase Sync Gateway","version":"2.0"},"version":"Couchbase Sync Gateway/2.0.0(832;2d8a6c0)"}

[sync_gateway@lxnode15 ~]$


STARTING AND STOPPING SYNC GATEWAY


CentOS/RedHat/Oracle 6


initctl start sync_gateway

initctl stop sync_gateway


CentOS/RedHat/Oracle 7


systemctl start sync_gateway

systemctl stop sync_gateway


Note: You need sudo privs to run the command from a user other than root.


CREATE SYNC GATEWAY BUCKET


If not already available, you need to create a bucket dedicated for sync gateway.

For this exercise, I have created a bucket - demoSyncData.

CREATE COUCHBASE USER FOR SYNC GATEWAY


Create a user for Sync Gateway with RBAC roles - Bucket Full Access and Read Only Admin.

In my case, I've created demoSyncData_user.

SAMPLE SYNC GATEWAY CONFIGURATION


As mentioned earlier, a newly installed Sync Gateway has a default config connecting to its local Walrus database.

The procedures below is for us to create a sample configuration connecting to a Couchbase cluster.


1. Shutdown the Sync Gateway.


[root@lxnode15 ~]# initctl status sync_gateway

sync_gateway start/running, process 8597

[root@lxnode15 ~]#


[root@lxnode15 ~]# initctl stop sync_gateway

sync_gateway stop/waiting

[root@lxnode15 ~]#


2. Backup the original config file.


[sync_gateway@lxnode15 ~]$ pwd

/home/sync_gateway

[sync_gateway@lxnode15 ~]$

[sync_gateway@lxnode15 ~]$ ls

data  logs  sync_gateway.json

[sync_gateway@lxnode15 ~]$ mv sync_gateway.json sync_gateway.json.orig.bak

[sync_gateway@lxnode15 ~]$

[sync_gateway@lxnode15 ~]$ ls

data  logs  sync_gateway.json.orig.bak

[sync_gateway@lxnode15 ~]$


3. Create a new log directory.

Sync Gateway logs all changes and errors and tends to be high in volume in production implementations.

The default log path is located on sync_gateway user $HOME/logs directory which in most implementations had smaller disk space.

It is best that you create a separate directory on a mount point with more disk space and set your log path.


[root@lxnode15 ~]# mkdir /app

[root@lxnode15 ~]# chmod 777 /app

[root@lxnode15 ~]# ls -ld /app

drwxrwxrwx. 2 root root 4096 Jul 10 17:55 /app

[root@lxnode15 ~]# su - sync_gateway

[sync_gateway@lxnode15 ~]$

[sync_gateway@lxnode15 ~]$ mkdir -p /app/syncgateway/log

[sync_gateway@lxnode15 ~]$ ls -l /app

total 4

drwxrwxr-x. 3 sync_gateway sync_gateway 4096 Jul 10 17:57 syncgateway

[sync_gateway@lxnode15 ~]$


3. Create a sync gateway config file connecting to your Couchbase cluster.


file: sync_gateway.json


{

    "interface":":4984",

    "adminInterface":"lxnode15.vlabs.net:4985",

    "log": ["*"],

    "logging":{

                "default": {

                        "logFilePath": "/app/syncgateway/log/sync_gateway_error.log",

                        "rotation": {

                                "maxsize": 250,

                                "maxage": 14,

                                "maxbackups": 30

                        }

                }

        },

    "databases": {

        "syncdb": {

            "server": "http://lxnode11.vlabs.net,lxnode12.vlabs.net:8091",

            "bucket": "demoSyncData",

            "username": "demoSyncData_user",

            "password": "demo123",

            "enable_shared_bucket_access": true,

            "import_docs": "continuous",

            "users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } },

            "sync": `function (doc, oldDoc) {

                if (doc.sdk) {

                    channel(doc.sdk);

                }

            }`

        }

    }

}


NOTE: When naming sync gateway database, use only lowercase characters and numbers; otherwise, an "error 400 Illegal database name" occurs.


logging.default.rotation

  maxsize  (integer, default 100)  The maximum size in MB of the log file before it gets rotated.

  maxage   (integer)  The maximum number of days to retain old log files.

  maxbackups (integer)  The maximum number of old log files to retain.


databases.db

  import_docs (boolean) If set to true, causes the Sync Gateway to scan the bucket on startup looking for documents

      that have no _sync metadata, and adding that metadata. You only want to have this on when you do the first import,

      because it will slow down the startup of the Sync Gateway. You also only want to run a single Sync Gateway while

      in this mode, otherwise they'll step on each other's toes trying to generate the _sync metadata at the same time.

      

      In Sync Gateway 1.5, the behaviour of this property has changed to work in tandem with the enable_shared_bucket_access

      property. When shared bucket access is enabled, setting this property to continuous ("import_docs": "continuous")

      indicates that this Sync Gateway node should perform import processing. If shared bucket access is not enabled,

      Sync Gateway continues to support the previously existing import functionality.

      

      In a deployment with shared bucket access enabled and Sync Gateway Accelerator nodes, this property is not required

      because the import process is always shared across Sync Gateway Accelerator.


  enable_shared_bucket_access (boolean, default false)

      Starting in Sync Gateway 1.5, Couchbase Server and Mobile applications can now read from and write to the same bucket.

      This property specifies whether to enable the interopability between Couchbase Mobile and Couchbase Server.

      On start-up, Sync Gateway will generate the mobile-specific metadata for all the pre-existing documents in the

      Couchbase Server bucket. From then on, documents can be inserted on the Server directly (with N1QL or SDKs) or through

      the Sync Gateway REST API.

      Mobile applications require additional metadata in order to manage security and replication. In previous versions of

      Sync Gateway, this information has always been stored in the document body. Sync Gateway 1.5 utilizes a new feature of

      Couchbase Server 5.0 called XATTRs (x-attributes) to store that metadata into an external document fragment.


For a complete description of configuration parameters, please refer to the online guide - Sync Gateway Configuration File



4. Startup the Sync Gateway with the new config.


This will then create the initial meta info in the bucket.


[root@lxnode15 ~]# initctl start sync_gateway

sync_gateway start/running, process 9692

[root@lxnode15 ~]#


Sample log snippet:


[sync_gateway@lxnode15 log]$ tail -f sync_gateway_error.log

2018-07-10T18:35:31.779-05:00 ==== Couchbase Sync Gateway/2.0.0(832;2d8a6c0) ====

2018-07-10T18:35:31.779-05:00 requestedSoftFDLimit < currentSoftFdLimit (5000 < 51200) no action needed

2018-07-10T18:35:31.779-05:00 Opening db /syncdb as bucket "demoSyncData", pool "default", server <http://lxnode11.vlabs.net,lxnode12.vlabs.net:8091>

2018-07-10T18:35:31.780-05:00 GoCBCustomSGTranscoder Opening Couchbase database demoSyncData on <http://lxnode11.vlabs.net,lxnode12.vlabs.net:8091> as user "demoSyncData_user"

2018-07-10T18:35:31.867-05:00 Design docs for current view version (2.0) do not exist - creating...

2018-07-10T18:35:32.239-05:00 Design docs successfully created for view version 2.0.

2018-07-10T18:35:32.239-05:00 Verifying view availability for bucket demoSyncData...

2018-07-10T18:35:32.497-05:00 Views ready for bucket demoSyncData.

2018-07-10T18:35:32.498-05:00 Cache: Initializing changes cache with options {ChannelCacheOptions:{ChannelCacheMinLength:0 ChannelCacheMaxLength:0 ChannelCacheAge:0s} CachePendingSeqMaxWait:5s CachePendingSeqMaxNum:10000 CacheSkippedSeqMaxWait:1h0m0s}

2018-07-10T18:35:32.498-05:00 Initializing changes cache for database syncdb with sequence: 0

2018-07-10T18:35:32.498-05:00 Feed: Starting mutation feed on bucket demoSyncData due to either channel cache mode or doc tracking (auto-import/bucketshadow)

2018-07-10T18:35:32.498-05:00 Feed: Using DCP feed for bucket: "demoSyncData" (based on feed_type specified in config file)

2018-07-10T18:35:32.546-05:00 Feed+: Initializing DCP feed based on persisted checkpoints

2018-07-10T18:35:32.583-05:00 Feed+: Initializing DCP feed based on persisted checkpoints

2018-07-10T18:35:32.592-05:00 Feed+: Connecting to new bucket datasource.  URLs:[http://lxnode11.vlabs.net:8091 http://lxnode12.vlabs.net:8091], pool:default, bucket:demoSyncData

2018-07-10T18:35:32.592-05:00 WARNING: Unable to retrieve server's metadata purge interval - will use default value. Get http://lxnode11.vlabs.net,lxnode12.vlabs.net:8091/pools/default/buckets/demoSyncData: dial tcp: lookup lxnode11.vlabs.net,lxnode12.vlabs.net: no such host -- db.NewDatabaseContext() at database.go:356

2018-07-10T18:35:32.592-05:00 Using metadata purge interval of 1.25 days for tombstone compaction.

2018-07-10T18:35:32.596-05:00 Auth: Saved _sync:user:: &{{ *:1  %!s(uint64=1)  %!s(*uint16=<nil>)} { %!s(bool=false)  <nil>   []} %!s(*auth.Authenticator=&{0xc420302140 0xc4203f2000 SyncGatewaySession}) []}

2018-07-10T18:35:32.596-05:00     Reset guest user to config

2018-07-10T18:35:32.596-05:00 Starting admin server on lxnode15.vlabs.net:4985

2018-07-10T18:35:32.603-05:00 Starting server on :4984 ...

2018-07-10T18:35:32.642-05:00 [INFO] Using plain authentication for user demoSyncData_user

2018-07-10T18:35:32.643-05:00 [INFO] Using plain authentication for user demoSyncData_user

2018-07-10T18:35:32.720-05:00 Cache: Received #1 ("_user/")

2018-07-10T18:35:32.720-05:00 Cache: Initialized cache for channel "*" with options: &{ChannelCacheMinLength:50 ChannelCacheMaxLength:500 ChannelCacheAge:1m0s}

2018-07-10T18:35:32.720-05:00 Cache:     #1 ==> channel "*"

2018-07-10T18:35:32.720-05:00 Changes+: Notifying that "demoSyncData" changed (keys="{*}") count=2

2018-07-10T18:35:32.720-05:00 Changes+: Notifying that "demoSyncData" changed (keys="{_sync:user:}") count=3


At this point your sync gateway is now ready to accept document inputs and changes.



VERIFY THE GATEWAY DATABASE


[chad@lxnode15 ~]$ curl http://lxnode15.vlabs.net:4984/syncdb/

{"committed_update_seq":1,"compact_running":false,"db_name":"syncdb","disk_format_version":0,"instance_start_time":1531265732497424,"purge_seq":0,"state":"Online","update_seq":1}

[chad@lxnode15 ~]$



If you’d like to learn the basics of Sync Gateway data updates, please refer to my article - Sample Sync Gateway Data Update Using CURL



  1 2 > Next