Network server can't connect to App Server

In an attempt to install Network server and App Server on two separate machines (NS on Raspberry Pi and AS on a VM/server), it seems that the NS is failing at some type of connection to the AS, although I was able to add the NS to the AS.

Here is the error log I am seeing on the NS side:

Dec 17 19:13:34 rak-gateway chirpstack-network-server[12707]: time="2019-12-17T19:13:34-05:00" level=warning msg="creating insecure application-server client" server="localhost:8001" Dec 17 19:13:34 rak-gateway chirpstack-network-server[12707]: time="2019-12-17T19:13:34-05:00" level=warning msg="grpc: addrConn.createTransport failed to connect to {localhost:8001 0 <nil>}. Err :connection error: desc = \"transport: Error while dialing dial tcp [::1]:8001: connect: connection refused\". Reconnecting..." Dec 17 19:13:34 rak-gateway chirpstack-network-server[12707]: time="2019-12-17T19:13:34-05:00" level=error msg="gateway: handle gateway stats error" ctx_id=9dc441f2-e103-4484-8cec-ab2f3855a2be error="get application-server client error: get application-server client error: create application-server api client error: dial application-server api error: context deadline exceeded"

I don’t see any errors from AS log. Any idea what I might have set up wrong? I think it has to do with the bind ip’s but I am not sure exactly which parameter. My config files for both AS and NS are below:

NS:

# This configuration configures ChirpStack Network Server for the EU868 band using a MQTT
# broker to communicate with the gateways. Many options and defaults have been
# omitted for simplicity.
# 
# For other bands, see the ./examples/ sub-directory.
#
# See https://www.chirpstack.io/network-server/install/config/ for a full
# configuration example and documentation.


# PostgreSQL settings.
#
# Please note that PostgreSQL 9.5+ is required.
[postgresql]
# PostgreSQL dsn (e.g.: postgres://user:password@hostname/database?sslmode=disable).
#
# Besides using an URL (e.g. 'postgres://user:password@hostname/database?sslmode=disable')
# it is also possible to use the following format:
# 'user=chirpstack_ns dbname=chirpstack_ns sslmode=disable'.
#
# The following connection parameters are supported:
#
# * dbname - The name of the database to connect to
# * user - The user to sign in as
# * password - The user's password
# * host - The host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
# * port - The port to bind to. (default is 5432)
# * sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
# * fallback_application_name - An application_name to fall back to if one isn't provided.
# * connect_timeout - Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.
# * sslcert - Cert file location. The file must contain PEM encoded data.
# * sslkey - Key file location. The file must contain PEM encoded data.
# * sslrootcert - The location of the root certificate file. The file must contain PEM encoded data.
#
# Valid values for sslmode are:
#
# * disable - No SSL
# * require - Always SSL (skip verification)
# * verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
# * verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
dsn="postgres://chirpstack_ns:dbpassword@localhost/chirpstack_ns?sslmode=disable"

# Redis settings
#
# Please note that Redis 2.6.0+ is required.
[redis]
# Redis url (e.g. redis://user:password@hostname/0)
#
# For more information about the Redis URL format, see:
# https://www.iana.org/assignments/uri-schemes/prov/redis
url="redis://localhost:6379"


# Network-server settings.
[network_server]
# Network identifier (NetID, 3 bytes) encoded as HEX (e.g. 010203)
net_id="000000"


  # LoRaWAN regional band configuration.
  #
  # Note that you might want to consult the LoRaWAN Regional Parameters
  # specification for valid values that apply to your region.
  # See: https://www.lora-alliance.org/lorawan-for-developers
  [network_server.band]
  name="US_902_928"


  # LoRaWAN network related settings.
  [network_server.network_settings]
  enabled_uplink_channels=[8,9,10,11,12,13,14,15]
  # Disable ADR
  #
  # When set, this globally disables ADR.
  disable_adr=true
  
    # Extra channel configuration.
    #
    # Use this for LoRaWAN regions where it is possible to extend the by default
    # available channels with additional channels (e.g. the EU band).
    # The first 5 channels will be configured as part of the OTAA join-response
    # (using the CFList field).
    # The other channels (or channel / data-rate changes) will be (re)configured
    # using the NewChannelReq mac-command.
    #
#    [[network_server.network_settings.extra_channels]]
#    frequency=867100000
#    min_dr=0
#    max_dr=5

#    [[network_server.network_settings.extra_channels]]
#    frequency=867300000
#    min_dr=0
#    max_dr=5

#    [[network_server.network_settings.extra_channels]]
#    frequency=867500000
#    min_dr=0
#    max_dr=5

#    [[network_server.network_settings.extra_channels]]
#    frequency=867700000
#    min_dr=0
#    max_dr=5

#    [[network_server.network_settings.extra_channels]]
#    frequency=867900000
#    min_dr=0
#    max_dr=5


    # Class B settings
    [network_server.network_settings.class_b]
    # Ping-slot data-rate.
    ping_slot_dr=0

    # Ping-slot frequency (Hz)
    #
    # Set this to 0 to use the default frequency plan for the configured region
    # (which could be frequency hopping).
    ping_slot_frequency=0


  # Network-server API
  #
  # This is the network-server API that is used by ChirpStack Application Server or other
  # custom components interacting with ChirpStack Network Server.
  [network_server.api]
  # ip:port to bind the api server
  bind="0.0.0.0:8000"
  #bind="raspi_ip:8000"

  # Backend defines the gateway backend settings.
  #
  # The gateway backend handles the communication with the gateway(s) part of
  # the LoRaWAN network.
  [network_server.gateway.backend]
  # Backend
  type="mqtt"


    # MQTT gateway backend settings.
    #
    # This is the backend communicating with the LoRa gateways over a MQTT broker.
    [network_server.gateway.backend.mqtt]
    # MQTT topic templates for the different MQTT topics.
    #
    # The meaning of these topics are documented at:
    # https://www.chirpstack.io/gateway-bridge/
    #
    # The default values match the default expected configuration of the
    # ChirpStack Gateway Bridge MQTT backend. Therefore only change these values when
    # absolutely needed.

    # Event topic template.
    event_topic="gateway/+/event/+"

    # Command topic template.
    #
    # Use:
    #   * "{{ .GatewayID }}" as an substitution for the LoRa gateway ID
    #   * "{{ .CommandType }}" as an substitution for the command type
    command_topic_template="gateway/{{ .GatewayID }}/command/{{ .CommandType }}"

    # MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
    server="tcp://localhost:1883"

    # Connect with the given username (optional)
    username=""

    # Connect with the given password (optional)
    password=""


# Metrics collection settings.
[metrics]
# Timezone
#
# The timezone is used for correctly aggregating the metrics (e.g. per hour,
# day or month).
# Example: "Europe/Amsterdam" or "Local" for the the system's local time zone.
timezone="Local"


# Join-server settings.
[join_server]

  # Default join-server settings.
  #
  # This join-server will be used when resolving the JoinEUI is set to false
  # or as a fallback when resolving the JoinEUI fails.
  [join_server.default]
  # hostname:port of the default join-server
  #
  # This API is provided by ChirpStack Application Server.
  #server="http://localhost:8003"
  server="http://server_ip:8003"

AS:

# This configuration sets the required settings and configures an integration
# with a MQTT broker. Many options and defaults have been omitted for
# simplicity.
#
# See https://www.chirpstack.io/application-server/install/config/ for a full
# configuration example and documentation.
[general]
log_level=4


# PostgreSQL settings.
#
# Please note that PostgreSQL 9.5+ is required.
[postgresql]
# PostgreSQL dsn (e.g.: postgres://user:password@hostname/database?sslmode=disable).
#
# Besides using an URL (e.g. 'postgres://user:password@hostname/database?sslmode=disable')
# it is also possible to use the following format:
# 'user=chirpstack_as dbname=chirpstack_as sslmode=disable'.
#
# The following connection parameters are supported:
#
# * dbname - The name of the database to connect to
# * user - The user to sign in as
# * password - The user's password
# * host - The host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
# * port - The port to bind to. (default is 5432)
# * sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
# * fallback_application_name - An application_name to fall back to if one isn't provided.
# * connect_timeout - Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.
# * sslcert - Cert file location. The file must contain PEM encoded data.
# * sslkey - Key file location. The file must contain PEM encoded data.
# * sslrootcert - The location of the root certificate file. The file must contain PEM encoded data.
#
# Valid values for sslmode are:
#
# * disable - No SSL
# * require - Always SSL (skip verification)
# * verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
# * verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
#dsn="postgres://localhost/chirpstack_as?sslmode=disable"
dsn="postgres://chirpstack_as:dbpassword@localhost/chirpstack_as?sslmode=disable"


# Redis settings
#
# Please note that Redis 2.6.0+ is required.
[redis]
# Redis url (e.g. redis://user:password@hostname/0)
#
# For more information about the Redis URL format, see:
# https://www.iana.org/assignments/uri-schemes/prov/redis
url="redis://localhost:6379"


# Application-server settings.
[application_server]

  # Integration configures the data integration.
  #
  # This is the data integration which is available for all applications,
  # besides the extra integrations that can be added on a per-application
  # basis.
  [application_server.integration]
  # Enabled integrations.
  enabled=["mqtt"]

    # MQTT integration backend.
    [application_server.integration.mqtt]
    # MQTT topic templates for the different MQTT topics.
    #
    # The meaning of these topics are documented at:
    # https://www.chirpstack.io/application-server/integrate/data/
    #
    # The following substitutions can be used:
    # * "{{ .ApplicationID }}" for the application id.
    # * "{{ .DevEUI }}" for the DevEUI of the device.
    #
    # Note: the downlink_topic_template must contain both the application id and
    # DevEUI substitution!
    uplink_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/rx"
    downlink_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/tx"
    join_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/join"
    ack_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/ack"
    error_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/error"
    status_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/status"
    location_topic_template="application/{{ .ApplicationID }}/device/{{ .DevEUI }}/location"

    # MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
    server="tcp://localhost:1883"

    # Connect with the given username (optional)
    username=""

    # Connect with the given password (optional)
    password=""


    # Settings for the "internal api"
    #
    # This is the API used by ChirpStack Network Server to communicate with ChirpStack Application Server
    # and should not be exposed to the end-user.
    [application_server.api]
    # ip:port to bind the api server
    #bind="0.0.0.0:8001"
    bind="server_ip:8001"

    # Public ip:port of the application-server API.
    #
    # This is used by ChirpStack Network Server to connect to ChirpStack Application Server. When running
    # ChirpStack Application Server on a different host than ChirpStack Network Server, make sure to set
    # this to the host:ip on which ChirpStack Network Server can reach ChirpStack Application Server.
    # The port must be equal to the port configured by the 'bind' flag
    # above.
    #public_host="localhost:8001"
    public_host="server_ip:8001"
    #public_host="0.0.0.0:8001"

    # Settings for the "external api"
    #
    # This is the API and web-interface exposed to the end-user.
    [application_server.external_api]
    # ip:port to bind the (user facing) http server to (web-interface and REST / gRPC api)
    bind="0.0.0.0:8080"
    #bind="server_ip:8080"

    # http server TLS certificate (optional)
    tls_cert=""

    # http server TLS key (optional)
    tls_key=""

    # JWT secret used for api authentication / authorization
    # You could generate this by executing 'openssl rand -base64 32' for example
    jwt_secret=""


# Join-server configuration.
#
# ChirpStack Application Server implements a (subset) of the join-api specified by the
# LoRaWAN Backend Interfaces specification. This API is used by ChirpStack Network Server
# to handle join-requests.
[join_server]
# ip:port to bind the join-server api interface to
bind="0.0.0.0:8003"
#bind="server_ip:8003"

Just following up, anyone?

You must set public_host="server_ip:8001" to the (public) IP address of your AS server. Then you must update your Network-Server entry in the web-interface to make sure this public_host is updated in the NS database. Just saving should work.

Thank you @brocaar! For some reason, the IP address field (as_id column) in the routing_profile table of the NS database was not updating even after updating the network server and saving as you mentioned. I got it fixed by manually updating the as_id field of that table.