Thursday, May 31, 2018

Setup EC2 Jenkins Slaves with SSH Keys


Setup EC2 Jenkins Slaves with SSH Keys

Setting up a Jenkins slave with SSH key is suppose straight forward, but for whatever reason,  Coudbees instruction does not work for Redhat/Centos on AWS EC2 instance. Therefore, I just did it my way and get it working perfectly.
I hope this cheatsheet can help you add a slave quickly in AWS.

Slave server setup

  • Setup a Redhat/Centos EC2 instance
  • Install all the needed tools, such as Java, maven and etc
  • Following is example for installing OpenJDK on Redhat 
curl -O http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y epel-release
yum install -y java-1.8.0-openjdk.x86_64
yum install -y java-1.8.0-openjdk-devel.x86_64
alternatives --install /usr/bin/java java /usr/java/latest/bin/java 200000
alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 200000
alternatives --install /usr/bin/jar jar /usr/java/latest/bin/jar 200000
java -version
  • Enable password login, edit /etc/ssh/sshd_config
sudo vi /etc/ssh/sshd_config
  • Change "PasswordAuthentication" to yes 
#PasswordAuthentication no
PasswordAuthentication yes
  • Restart sshd
systemctl restart sshd
  • Create a Linux user call jenkins
sudo useradd -d /var/lib/jenkins jenkins
  • Set a password for the jenkins user ID
sudo passwd jenkins
  • AS the user jenkins, create /var/lib/jenkins/.ssh directory 
mkdir ~/.ssh
chmod 700 ~/.ssh
  • Test login to jenkins user from the master server
ssh jenkins@<slaver server IP>
You should be able to login as jenkins on the slave node after entering the password.

Master server

  • login to Jenkins master server
  • Edit /etc/passwd and change jenkins login with bash shell. 
sudo vi /etc/passwd
Change from
jenkins:x:846:861:Jenkins Automation Server:/var/lib/jenkins:/bin/false
To
jenkins:x:846:861:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
  • Create a .ssh directory for user jenkins
sudo su - jenkins
mkdir .ssh
chmod 700 .ssh
  • Create SSH Key
ssh-keygen -t rsa
  • Output is two files
    • ~/.ssh/id_rsa --- private key
    • ~/.ssh/id_rsa.pub --- public key
  • Append Jenkins master's new public key to Jenkins Slave host's ~/.ssh/authroized_keys and ~/.ssh/authroized_keys2
cat ~/.ssh/id_rsa.pub | ssh jenkins@<slave machine IP> 'cat >> .ssh/authorized_keys
cat ~/.ssh/id_rsa.pub | ssh jenkins@<slave machine IP> 'cat >> .ssh/authorized_keys2
Note: For Centos/Redhat7, you need the ~/.ssh/authorized_keys2.
  • On the jenkins master server, change the permission of ~/.ssh/authorized.keys and ~/.ssh/authorized.keys2 to 640
ssh jenkins@<slave machine IP> 'chmod 640 ~/.ssh/authorized_keys*'
  • New you can try to login from client to remote host without key
ssh jenkins@<slave machine IP>
you should be able to login without entering any password

Jenkins Configuration

  • Go to Master node's Jenkins dashboard -> Manage Jenkins ->Manage Nodes.
  • Click on new Node pic1
  • Enter a name and select "permananet agent" option and click OK 

Note: After setting the first slace, another option, “Copy Existing Node” can be select, which is self-explanatory.
  • Enter Slave details 

  • Click on "Add" on for the credentials
select and enter all the credentials as shown below and click ok.

  • Select the key

  • Click "Save"
  • Now you have master and slave machine pic5

Debug slave connection problem

  • If you get return like the folliowing

  • Click on slave host name 
  • Click on "See log for more details" to check what went wrong

Test the Slave

To test the slave, create a sample project and select the option as shown below. You need to select the node using the label option. If you start to type the letter the node list will show up.
pic6

Tuesday, May 22, 2018

MongoDB Replica Set Upgrade

MongoDB Replica Set Upgrade

MongoDB software suppose to be straight forward but it does not mean you would make a mistake and destroy the cluster.
This article give you a well documented procedure on upgrading a 3 nodes MongoDB replica set running on Centos/Redhat 7.

Peform database backup

It is important to backup the database before any upgrade!
Personally, I prefer exporting the database as backup.

Schedule down time (optional)

Although hot upgrade is being performed here, but like any other software upgrades, there is a risk on upgrade failure and roll back might be needed. 

Therefore, please scheduling down time to give yourself some breathing room.

Verification before upgrades

FeatureCompatibilityVersion

  • Login to the MongoDB databsae with mongo shell
mongo "mongodb://host1:27017,host2:27017,host3:27017/admin?replicaSet=myrelicaset"
  • Check the FeatureCompatibilityVersion value
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
It should return a result that like the following
"featureCompatibilityVersion": "3.4"
  • If the value is not previous version (3.4 here), set the setFeatureCompatibilityVersion to the previous version with the follwing command
db.adminCommand( { setFeatureCompatibilityVersion: “3.4” } )

Replication status between nodes

  • Using mongo shell and login to the MongoDB databsae replica set
  • Run the follwing command:
rs.printSlaveReplicationInfo()
Ensure that no replica set member is in ROLLBACK or RECOVERING state.

MongoDB Replica status

  • Using mongo shel and loginto the MongoDB databsae
  • Run the follwing command:
rs.status()
Make sure you have two healthy SECONDARY nodes

Force a particular node as primary node (optional)

If you want to set a particular node as the primary node, you can setup node prioriy with the following commnad:
x=rs.config()
x.members[0].priority=1000
rs.reconfig(x)
Note: The above set the first node (members[0]) as primary
Check the result
rs.status()

MongoDB replica set upgrade

Stop the application (Optional)

Stopping the applicaiton using the database.

Upgrade the 1st SECONDARY node

  • Copy or download the MongoDB 3.6 rpms to the node
  • SSH to the secondary node
  • Update /etc/mongod.conf file and make sure you have
    net:
    bindIP: 0.0.0.0
    Note: Starting on MongoDB 3.6, bindIP is default to localhost if it is not defined.
  • Stop the mongod node
    sudo systemctl stop mongod
  • Install mongodb 3.6 software.yum install -y mongodb-enterprise-server-3.6.4-1.el7.x86_64.rpm yum install -y mongodb-enterprise-mongos-3.6.4-1.el7.x86_64.rpm yum install -y mongodb-enterprise-tools-3.6.4-1.el7.x86_64.rpm yum install -y mongodb-enterprise-shell-3.6.4-1.el7.x86_64.rpm
  • Start the mongod node
    sudo systemctl start mongod
  • Check if the node join the cluster
    • Using Mongoshell to login to the replica set
    • Run 
    rs.status()
    • Make sure the node has join the replica set
    • Note: For whatever reason, I need to restart mongod a 2nd time to make the node join the replica set.

Upgrade the 2nd SECONDARY node

Repeat the above steps to upgrade the 2nd SECONDARY node

Upgrade PRIMARY node

Step down the primary

Before doing upgrade, you need to step down the primary node to a secondary node
  • login to replica set with Mongo shell
  • Run the follwoing command
rs.stepDown()
  • check the replica status and make sure the primary node has been step down as secondary node
rs.status()

Upgrade the last node

  • Repeat the secondary node upgrade procedure
  • You are done

Oracle AWS RDS Import/Export with datapump

Oracle AWS RDS Import/Export with datapump

Introduction

The AWS documentation about using Oracle data pump is very un-clear and very difficult for non-DBA to understand. This article provides you an easy to follow example on how to use data pump with AWS RDS Oracle:
  • We have a need to export AWS RDS oracle database data from one VPC to another VPC.
  • There is no direct network connectivites between these two VPC. 
  • We access these VPCs from a 3rd network via SSH.
  • AWS RDS Oracle database support datadump as a mean to export/import data.
  • To accomplish the task, we set the following: 

Taskes on the source VPC

Setup an Oracle instance on EC2 instance in source VPC

Take a look on the "oracle-12C-installation" article that I wrote.

Setup Directory Object on Source EC2 Oracle Instance

  • SSH to the EC2 instance with Oracle installed.
  • Create a directory
mkdir /home/oracle/dumps
  • login to Oracle as sys user to the database and create a directory object
$ sqlplus / as sysdba
reate directory mydumps as '/home/oracle/dumps';
Directory created.
The above example create a data dump directory call "mydumps" in Oracle and point to the local direct "/home/oracle/home"
  • You can use the user system to perform backup
  • If you want to use different user, such as backup_user to perform the backup, create and grant permission to this user
SQL > grant read,write on directory dumps to backup_user;
SQL > grant datapump_exp_full_database backup_user;

Wednesday, May 9, 2018

Oracle-12c quiet installation on Redhat EC2 instance

Oracle-12c quiet installation on Redhat  EC2 instance


This purpose of this document is providing you a quick and easy way to install Oracle 12C without using X-windows.

Prerequisites

  • EC2 instance with Redhat Linux 7.x
  • At least 30 GB of available disk space
  • You need at least 2GB of RAM, so you should use T2.small or bigger.
  • Swap space with size of the RAM
  • Oracle 12c software
  • In this example, we are going to use the following Oracle setting:
  1. ORACL_HOME: /u01/app/oracle/product/12.1.0/db_1
  2. OSDBA group: dba
  3. Global database name:ORAC12C
  4. Database user password: oracle

Installation

Download Oracle 12c software

  • Down load location: https://www.oracle.com/downloads/index.html
  • You need to sign up an Oracle user account first.
  • Please download the standard edition, instruction here use standard edition.

Install needed RPMs on Redhat Linux

  • Enable the RHEL optional and/or extras channels.
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
  • Install need RPMs
# yum install -y \
binutils.x86_64 \
compat-libcap1.x86_64 \
gcc.x86_64 gcc-c++.x86_64 \
glibc.i686 \
glibc.x86_64 \
glibc-devel.i686 glibc-devel.x86_64 \
ksh \
compat-libstdc++-33 \
libaio.i686 libaio.x86_64 \
libaio-devel.i686 libaio-devel.x86_64 \
libgcc.i686 libgcc.x86_64 \
libstdc++.i686 libstdc++.x86_64 \
libstdc++-devel.i686 libstdc++-devel.x86_64 \
libXi.i686 libXi.x86_64 \
libXtst.i686 libXtst.x86_64 \
make.x86_64 \
sysstat.x86_64 \
zip unzip

Setup Oracle User accounts and OS configuration

  • Add the following kernel parameters to /etc/sysctl.conf file.
#
# Configuration for Oracle DB
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 8329226240
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
  • apply the seting
# sysctl -p
# sysctl -a
  • Allow oracle user ID to read /etc/sysctl.conf during installation
chmod 644 /etc/sysctl.conf
  • Set the limits for oracle in /etc/security/limits.conf file.
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
  • Create swap file, the swap file should be bigger or equal to the RAM size
Following example creates a 10G swapfile
dd if=/dev/zero of=/swapfile bs=10M count=1000
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
  • Make swap file at startup
vi /etc/fstab
Add
/swapfile none swap sw 0 0
  • Create the user account and groups for Oracle.
groupadd oinstall
groupadd dba
useradd -g oinstall -G dba oracle
passwd oracle
  • Create a directory called /stage
mkdir /stage
chown -R oracle:oinstall /stage
  • Create /u01 be for the actual installation, and assign the necessary permissions.# mkdir /u01 # chown oracle:oracle /u01
mkdir /u01
chown -R oracle:oinstall /u01
chmod -R 775 /u01
chmod g+s /u01
  • Sudo as oracle
sudo su - oracle
  • Extract the zipped installation file 
unzip linuxamd64_12102_database_se2_1of2.zip -d /stage/
unzip linuxamd64_12102_database_se2_2of2.zip -d /stage/


EC2 security group

To allow connections from outside the server, you will need to open the following ports with the security group.
1521/TCP
5500/TCP
5520/TCP
3938/TCP

Oracle Quiet Installation

Response files

After unpacking the binary Oracle Database, in the response directory, you will find example response under the database/response directory for installing Oracle software, database creation and listener in silent mode.
These files are:
  • netca.rsp – configure SQL net services
  • db_install.rsp – Install and optionally create a database
  • dbca.rsp – create a database
$ cd /stage/database/response
$ ls
dbca.rsp  db_install.rsp  netca.rsp

Install Oracle

THIS EXAMPLE IS FOR STANDARD EDITION
  • Login to the EC2 instance and sudo as oracle
  • Create a file called db.rsp file under /stage/database/response directory with the following:
oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v12.1.0

oracle.install.option=INSTALL_DB_SWONLY
ORACLE_HOSTNAME=ip-10-204-138-18
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=/u01/app/oraInventory
SELECTED_LANGUAGES=en
ORACLE_HOME=/u01/app/oracle/product/12.1.0/db_1
ORACLE_BASE=/u01/app/oracle
oracle.install.db.InstallEdition=SE
oracle.install.db.DBA_GROUP=dba
oracle.install.db.OPER_GROUP=
oracle.install.db.BACKUPDBA_GROUP=dba
oracle.install.db.DGDBA_GROUP=dba
oracle.install.db.KMDBA_GROUP=dba

### following are optionals
# RAC options
oracle.install.db.rac.configurationType=
oracle.install.db.CLUSTER_NODES=
oracle.install.db.isRACOneInstall=false
oracle.install.db.racOneServiceName=
oracle.install.db.rac.serverpoolName=
oracle.install.db.rac.serverpoolCardinality=0

# starter database
oracle.install.db.config.starterdb.type=GENERAL_PURPOSE
oracle.install.db.config.starterdb.globalDBName=
oracle.install.db.config.starterdb.SID=
oracle.install.db.ConfigureAsContainerDB=false
oracle.install.db.config.PDBName=
oracle.install.db.config.starterdb.characterSet=
oracle.install.db.config.starterdb.memoryOption=false
oracle.install.db.config.starterdb.memoryLimit=
oracle.install.db.config.starterdb.installExampleSchemas=false
oracle.install.db.config.starterdb.password.ALL=
oracle.install.db.config.starterdb.password.SYS=
oracle.install.db.config.starterdb.password.SYSTEM=
oracle.install.db.config.starterdb.password.DBSNMP=
oracle.install.db.config.starterdb.password.PDBADMIN=
oracle.install.db.config.starterdb.managementOption=DEFAULT

# cloud options
oracle.install.db.config.starterdb.omsHost=
oracle.install.db.config.starterdb.omsPort=0
oracle.install.db.config.starterdb.emAdminUser=
oracle.install.db.config.starterdb.emAdminPassword=

# startdb options
oracle.install.db.config.starterdb.enableRecovery=false
oracle.install.db.config.starterdb.storageType=
oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=
oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=

# asm options
oracle.install.db.config.asm.diskGroup=
oracle.install.db.config.asm.ASMSNMPPassword=

# my oracle support
MYORACLESUPPORT_USERNAME=
MYORACLESUPPORT_PASSWORD=
SECURITY_UPDATES_VIA_MYORACLESUPPORT=false
DECLINE_SECURITY_UPDATES=true
PROXY_HOST=
PROXY_PORT=
PROXY_USER=
PROXY_PWD=
COLLECTOR_SUPPORTHUB_URL=
  • Run the following to install Oracle software
cd /stage/database
./runInstaller -silent -responseFile /stage/database/response/db.rsp
Output of the command:
Starting Oracle Universal Installer...

Checking Temp space: must be greater than 500 MB.   Actual 52872 MB    Passed
Checking swap space: must be greater than 150 MB.   Actual 9999 MB    Passed
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2018-05-08_01-31-44PM. Please wait ...[oracle@gcc-deploy-uat101 database]$ You can find the log of this install session at:
 /u01/app/oraInventory/logs/installActions2018-05-08_01-31-44PM.log
If you see any validation failure, fix the problem and re-run the installation.
The installation is runing in the background, the only way to tell if the install completes is tailing the log file.
If everything success, you should see the following in the log file
INFO: Terminating all background operations
INFO: Terminated all background operations
INFO: Successfully executed the flow in SILENT mode
INFO: Dispose the current Session instance
INFO: Dispose the install area control object
INFO: Update the state machine to STATE_CLEAN
INFO: Finding the most appropriate exit status for the current application
INFO: Exit Status is 0
INFO: Shutdown Oracle Database 12c Release 1 Installer
INFO: Unloading Setup Driver
  • After the installation completes, execute the following scripts as root user:
/u01/app/oraInventory/orainstRoot.sh
/u01/app/oracle/product/12.1.0/db_1/root.sh

Setup ~/.bash_profile for oracle user

As the oracle user, edit ~/.bash_profile
export TMP=/tmp
export ORACLE_HOSTNAME=localhost
export ORACLE_UNQNAME=ORA12C
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/12.1.0/db_1
export ORACLE_SID=ORA12C
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export CLASSPATH=ORACLE_HOME/jlib:ORACLE_HOME/rdbms/jlib;
  • source in profile
source ~/.bash_profile
  • Test the Oracle installation. You should able to login with the following commands:
source ~/.bash_profile
sqlplus / as sysdba

Listener quiet installation

There is no need to edit the netca.rsp file, run the following as oracle user to configure the LISTENER with standard settings.
netca -silent -responseFile /stage/database/response/netca.rsp
It should generate something like the following:
Parsing command line arguments:
    Parameter "silent" = true
    Parameter "responsefile" = /stage/database/response/netca.rsp
Done parsing command line arguments.
Oracle Net Services Configuration:
Profile configuration complete.
Oracle Net Listener Startup:
    Running Listener Control:
      /u01/app/oracle/product/12.1.0/db_1/bin/lsnrctl start LISTENER
    Listener Control complete.
    Listener started successfully.
Listener configuration complete.
Oracle Net Services configuration successful. The exit code is 0
This command will create the listener file /u01/app/oracle/product/12.1.0/db_1/network/admin/listener.ora
  • Finally, replace the localhost with 0.0.0.0 on /u01/app/oracle/product/12.1.0/db_1/network/admin/listener.ora
# vi $ORACLE_HOME/network/admin/listener.ora
From:
# listener.ora Network Configuration File: /u01/app/oracle/product/12.1.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )
To
# listener.ora Network Configuration File: /u01/app/oracle/product/12.1.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

Re-start listener

  • Run the following as oracle
lsnrctl stop
lsnrctl start
  • Check the status
lsnrctl status

Quiet database creation

Here I’m going to install a single database instance called ORA12C.
  • Make the directories for data files

su oracle
mkdir /u01/app/oracle/oradata
mkdir /u01/app/oracle/flash_recovery_area
  • Create the database by running the following comand:
dbca \
-silent \
-createDatabase \
-templateName General_Purpose.dbc   \
-gdbName ORA12C \
-pdbadminPassword oracle \
-SysPassword oracle \
-SystemPassword oracle \
-emConfiguration NONE \
-datafileDestination /u01/app/oracle/oradata \
-asmSysPassword oracle \
-characterSet AL32UTF8 \
-totalMemory 1024 \
-recoveryAreaDestination /u01/app/oracle/flash_recovery_area

TOTALMEMORY is In KB , this value can be up to 70% of your total memory.


  • Now execute the below command to create the database.
dbca \
-silent \
-responseFile /ora01/app/oracle/distribs/database/response/dbca.rsp

Test login to database

  • Make sure tnsnames.ora has the right values
$ cat tnsnames.ora
It should has the following:
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/12.1.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

ORA12C =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORA12C)
    )
  )

LISTENER_ORA12C =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
  • Login to ORA12C database:
 sqlplus system/oracle@ORA12C


Now, you have an Oracle database installed and running.

DB start and stop

Using the dbstart and dbshut command

  • Make sure you have the ORACLE_BASE environment variable set
  • To start Oracle database and listener, run
dbstart $ORACLE_BASE
  • To stop Oracle database and listener, run
dbshut $ORACLE_BASE

Enabling Oracle to Start on System Boot

  • login as root
  • Add the following lines to /etc/systemd/system/oracle-rdbms.service file.
# /etc/systemd/system/oracle-rdbms.service
# Invoking Oracle scripts to start/shutdown Instances defined in /etc/oratab
# and starts Listener
[Unit]
Description=Oracle Database(s) and Listener
Requires=network.target
[Service]
Type=forking
Restart=no
ORACLE_HOME=/u01/app/oracle/product/12.1.0/db_1
ExecStart=$ORACLE_BASE/bin/dbstart $ORACLE_HOME
ExecStop=$ORACLE_BASE/bin/dbshut $ORACLE_HOME
User=oracle
[Install]
WantedBy=multi-user.target
  • Edit /etc/oratab and change the last file "N" to "Y"
ORA12C:/u01/app/oracle/product/12.1.0/db_1:N
To
ORA12C:/u01/app/oracle/product/12.1.0/db_1:Y
  • Eanble oracle to start on boot
systemctl daemon-reload
systemctl enable oracle-rdbms