WIP: More To Come
Microservices seem to be the talk of the town for almost all developers, and I’m no different. My usecase isn’t the traditional kind though.
So, what’s my unorthodox plan? Well, I like to tinker and Docker has allowed me to speed up prototyping drastically. Only one thing was missing, and that’s ease of directing traffic to my experiments… Enter Kong..
Now, I typically don’t like writing scripts like this one as they tend to go out of date quickly. However, this wasn’t too bad and given my level of interest in Microservice API Gateway products I know this will be maintained.
#!/bin/bash
cd /tmp
yum -y update
yum -y install epel-release wget vim net-tools htop
# ====== ====== PostgreSQL Repo ====== ======
wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm -O /tmp/pgdg-centos96-9.6-3.noarch.rpm
yum -y localinstall /tmp/pgdg-centos96-9.6-3.noarch.rpm
# ====== ====== PostgreSQL Configuration ====== ======
yum -y update
yum -y install postgresql96-server postgresql96-contrib
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl start postgresql-9.6
systemctl enable postgresql-9.6
user="postgres"
password="P0sTgr3s"
adduser $user
echo $password | passwd --stdin $user
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'P0sTgr3s';"
sudo -u postgres psql -c "CREATE USER kongusr;"
sudo -u postgres psql -c "CREATE DATABASE kongdb OWNER kongusr;"
sudo -u postgres psql -c "ALTER USER kongusr WITH PASSWORD 'k0nGVsar';"
rm -f /var/lib/pgsql/9.6/data/pg_hba.conf
cat <<EOT >> /var/lib/pgsql/9.6/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all ident
# IPv4 local connections:
host all all 127.0.0.1/32 password
# IPv6 local connections:
host all all ::1/128 password
EOT
# restart postgres
systemctl restart postgresql-9.6
# ====== ====== Kong Community Edition Configuration ====== ======
# Kong Repo
rm -f /etc/yum.repos.d/bintray-kong-kong-community-edition-rpm.repo
cat <<EOT >> /etc/yum.repos.d/bintray-kong-kong-community-edition-rpm.repo
#bintray--kong-kong-community-edition-rpm - packages by from Bintray
[bintray--kong-kong-community-edition-rpm]
name=bintray--kong-kong-community-edition-rpm
baseurl=https://kong.bintray.com/kong-community-edition-rpm/centos/7
gpgcheck=0
repo_gpgcheck=0
enabled=1
EOT
# Install Kong From Repo
yum -y update
yum -y install kong-community-edition
# Simlink for ease of use
rm -f /bin/kong
ln -s /usr/local/bin/kong /bin/kong
# Kong Config
mkdir -p /etc/kong
rm -f /etc/kong/kong.conf
cat <<EOT >> /etc/kong/kong.conf
proxy_listen = 0.0.0.0:80, 0.0.0.0:443 ssl
admin_listen = 0.0.0.0:18001, 0.0.0.0:18444 ssl
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_user = kongusr
pg_password = k0nGVsar
pg_database = kongdb
EOT
# Kong Service
rm -f /etc/systemd/system/kongd.service
cat <<EOT >> /etc/systemd/system/kongd.service
#/etc/systemd/system/kongd.service
[Unit]
Description=Kong Community Edition Gateway Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/kong start --conf /etc/kong/kong.conf
RestartSec=10
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=kongd
[Install]
WantedBy=multi-user.target
EOT
#bootstrap kong
/bin/kong migrations bootstrap
ulimit -n 4096
systemctl daemon-reload
systemctl enable kongd
systemctl start kongd