Accessing Postgres RDS from Kubernetes cluster in AWS

自古美人都是妖i 提交于 2019-12-04 07:26:06

This has to do with DNS resolving. When you use the RDS dns name INSIDE the same VPC it will be resolved to a private ip. When you use the same dns name on the internet or another VPC you will get the public ip of the RDS instance.

This is a problem because from another VPC you can not make use of the load balancing feature unless you expose the RDS instance to the public internet.

It's been a while the issue was resolved.
Don't exactly remember now, which step I missed that caused connection problem. But, below are the steps that did work for me.

Pre-requisite: kubernetes cluster is set up with vpc ('k8s-vpc')

  1. Create VPC SUBNET
    Go to vpc dashboard, ensure same aws region as k8s minion. (you will see existing 'k8s-vpc')
    Create subnet with each availability zone.
    Select 'k8s-vpc' as vpc from drop-down.
    CIDR could be 172.20.16.0/24 or 172.20.32.0/24

  2. Create DB SUBNET and SUBNET GROUP FOR VPC of k8s minion if not already available.
    Go to RDS Dashboard.
    Create subnet group (e.g. my-db-subnet-group) for DB and add all subnet from step 1 to create subnet group.

  3. From RDS Dashboard create Parameter Group
    (e.g. my-db-param-group) for Postgres (version 9.5 in this example)
    Copy value for max_connections to the max_prepared_transactions field and save

  4. Create RDS instance for Postgres DB
    Launch DB instance -> select Engine Postgres -> Choose stage (Production or Dev/Test) -> Give instance spec.s (instance type & disk space etc.) and specify DB settings (user/password) -> Configure Advanced settings

    1. vpc selection as 'k8s-vpc'
    2. DB subnet should be one created in previous step (my-db-subnet-group)
    3. VPC security group should be from Kubernetes minions - so that no additional config. required for access from minions
    4. Select Publicly Accessible - to connect to postgres from internet
    5. Select Parameter Group as 'my-db-param-group'.
    6. Specify Database options, backup and maintenance options and finally launch the instance
  5. Also check security group of VPC and add inbound rule to allow connection to postgres port.

  6. You can test connection from one of the k8s pod (kubectl exec -it) where postgres client is installed.
    Make sure to change user to postgres.
    Connect to RDS using psql as shown below:
    $ psql --host=my-rds-dev.cyhi3va0y2or.ap-northeast-1.rds.amazonaws.com --port=5432 --username=<masterUserName> --password --dbname=<masterDB>

If everything is set up correctly, it should prompt you for password of db user.
Providing correct password will finally connect to RDS.

This article was of great help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!