Saturday, 29 December 2018

Installation and Configuration of PostgreSQL on CentOS 7


Postgres, is a relational database management system that provides an implementation of the SQL querying language. It is a popular choice for many small and large projects and has the advantage of being standards-compliant and having many advanced features like reliable transactions and concurrency without read locks.

In This post we will discuss how to install and configure PostgreSQL on CentOS 7.

1) CentOS's default repositories contain Postgres packages, so we can install them without a hassle using the yum package system.
yum install postgres* -y

2) Once the PostgreSQL software is installed, we have to perform a few steps before we can use it.
postgresql-setup initdb

3) Start and enable postgresql service
sudo systemctl start postgresql
sudo systemctl enable postgresql

4) PostgreSQL installation will create a new user called postgres in VM which can auto login to psql.
By default, PostgreSQL does not allow password authentication.
To disable this first we need set a password for postgres user by following below steps.

su postgres
psql   # you will be login to postgresql client interface
\password  # it will prompt for password for postgresql
\q     # it will quit from psql

Then we need to edit its host-based authentication (HBA) configuration.
vi /var/lib/pgsql/data/pg_hba.conf
Before Configuration change
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

After configuration changes
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5


5) Restart the postgresql service to reflect config changes.

sudo systemctl restart postgresql

6) Now please try to connect to postgresql with provided creds earlier and start creating your databases and schemas as per your application requirements.

How to configure Nexus as Docker Trusted Repository

In this tutorial we will  cover how to configure Nexus as Docker repository.
We are assuming you have already installed and configured Nexus Repository manager. If not please follow Nexus Repository Manager installation.

1) Please login to Nexus Repository Manager.

2) Click on Repositories and select docker ( hosted )

3) Please Name your Private docker hub repository and update all configurations as mentioned below and click on create repository.


4) Enable Docker Bearer token Realm in Nexus.


5) Please try to authenticate yourself to newly created docker repository by providing nexus credentials.

docker login http://{hostname}:{httpport}

6) If you are using http protocol to login to Docker Registry then please add below line to /etc/docker/daemon.json and restart docker service. 
{ "insecure-registries":["192.168.56.101:5000"] }

Once the authentication is success you are good to proceed with pushing docker image to this private docker repository. 


Note: Make sure your docker image name should be in {hostname}:{httpport}/{image_name}:{image_tag} to push to this repo. You can also configure DNS name for this repo to improve readability.





Nexus Repository Manager installation on CentOS 8

In this tutorial we will go through how to install and configure Sonatype Nexus Repository manager in CentOS 7/8.


1) Login to your Linux server and update it.

sudo yum update -y
sudo yum install epel-release -y
sudo yum install java-11-openjdk -y

2) Create a directory named app and cd into the directory.
sudo mkdir /app && cd /app

3) Download the latest nexus. You can get the latest download links fo for nexus from
sudo wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz -O nexus.tar.gz
sudo tar -zxvf nexus.tar.gz
sudo ln -s nexus-* nexus

4) Create Nexus user and provide permissions to nexus home directory.
useradd nexus
chown -R nexus:nexus /app/nexus

5) Configure Nexus to run as Service in Linux machine.
sudo ln -s /app/nexus/bin/nexus /etc/init.d/nexus
sudo chown nexus:nexus /etc/init.d/nexus
sudo chkconfig --add nexus
sudo systemctl enable nexus

6) Start the nexus service.
sudo service nexus start

7) After few mins please access the Nexus Repository Manager at http://{hostname}:8081

Please add below line in Vagrantfile and restart your vm to access nexus UI on your browser. 
jenkins_config.vm.network "forwarded_port", guest: 8081, host: 8081


8) In the recent versions of nexus we need to configure admin password during our first time login. Onetime password will be located in below location. we need to use this password to configure admin password.

Run below command to see the default password.
cat /app/sonatype-work/nexus3/admin.password



Friday, 28 December 2018

Docker interview questions | Part - 1


What is Docker?
Docker is a containerization platform which packages your application and all its dependencies together in the form of containers so as to ensure that your application works seamlessly in any environment be it development or test or production.

What is Docker image?

Docker image is the source of Docker container. In other words, Docker images are used to create containers. Images are created with the build command, and they’ll produce a container when started with run. Images are stored in a Docker registry.

What is Docker container?

Docker containers are basically runtime instances of Docker images.

Docker containers include the application and all of its dependencies, but share the kernel with other containers, running as isolated processes in user space on the host operating system. Docker containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud.

 

What is Docker hub?
Docker hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
 What is Dockerfile used for?
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Can I use json instead of yaml for my compose file in Docker?
You can use json instead of yaml for your compose file, to use json file with compose, specify the filename to use for
docker-compose -f docker-compose.json up
What are the various states that a Docker container can be in at any given point in time?
There are four states that a Docker container can be in, at any given point in time. Those states are as given as follows:
§  Running
§  Paused
§  Restarting
§  Exited

 

Is there a way to identify the status of a Docker container?

We can identify the status of a Docker container by running the command

docker  ps –a

which will in turn list down all the available docker containers with its corresponding statuses on the host. From there we can easily identify the container of interest to check its status correspondingly.

 

What are the most common instructions in Dockerfile?

Some of the common instructions in Dockerfile are as follows:
§  FROM: We use FROM to set the base image for subsequent instructions. In every valid Dockerfile, FROM is the first instruction.
§  LABEL: We use LABEL to organize our images as per project, module, licensing etc. We can also use LABEL to help in automation. In LABEL we specify a key value pair that can be later used for programmatically handling the Dockerfile.
§  RUN: We use RUN command to execute any instructions in a new layer on top of the current image. With each RUN command we add something on top of the image and use it in subsequent steps in Dockerfile.
§  CMD: We use CMD command to provide default values of an executing container. In a Dockerfile, if we include multiple CMD commands, then only the last instruction is used.

What type of applications - Stateless or Stateful are more suitable for Docker Container?

It is preferable to create Stateless application for Docker Container. We can create a container out of our application and take out the configurable state parameters from application. Now we can run same container in Production as well as QA environments with different parameters. This helps in reusing the same Image in different scenarios. Also a stateless application is much easier to scale with Docker Containers than a stateful application.

 

Explain basic Docker usage workflow

1.     Everything starts with the Dockerfile. The Dockerfile is the source code of the Image.
2.     Once the Dockerfile is created, you build it to create the image of the container. The image is just the "compiled version" of the "source code" which is the Dockerfile.
3.     Once you have the image of the container, you should redistribute it using the registry. The registry is like a git repository -- you can push and pull images.
4.     Next, you can use the image to run containers. A running container is very similar, in many aspects, to a virtual machine (but without the hypervisor).

What is the difference between the COPY and ADD commands in a Dockerfile?

COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.

What is the difference between Docker Image and Layer?

§  Image: A Docker image is built up from a series of read-only layers
§  Layer: Each layer represents an instruction in the image’s Dockerfile.

Difference between Docker Image and container?

Docker container is the runtime instance of docker image.
Docker Image does not have a state and its state never changes as it is just set of files whereas docker container has its execution state.

What is an orphant volume and how to remove it?

An orphant volume is a volume without any containers attached to it. 

How is Docker different from a virtual machine?

Docker isn't a virtualization methodology. It relies on other tools that actually implement container-based virtualization or operating system level virtualization. For that, Docker was initially using LXC driver, then moved to libcontainer which is now renamed asrunc. Docker primarily focuses on automating the deployment of applications inside application containers. Application containers are designed to package and run a single service, whereas system containers are designed to run multiple processes, like virtual machines. So, Docker is considered as a container management or application deployment tool on containerized systems.

§  Unlike a virtual machine, a container does not need to boot the operating system kernel, so containers can be created in less than a second. This feature makes container-based virtualization unique and desirable than other virtualization approaches.
§  Since container-based virtualization adds little or no overhead to the host machine, container-based virtualization has near-native performance
§  For container-based virtualization, no additional software is required, unlike other virtualizations.
§  All containers on a host machine share the scheduler of the host machine saving need of extra resources.
§  Container states (Docker or LXC images) are small in size compared to virtual machine images, so container images are easy to distribute.
§  Resource management in containers is achieved through cgroups. Cgroups does not allow containers to consume more resources than allocated to them. However, as of now, all resources of host machine are visible in virtual machines, but can't be used. This can be realized by running top or htop on containers and host machine at the same time. The output across all environments will look similar.

Can you explain Dockerfile ONBUILD instruction?

The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. This is useful if you are building an image which will be used as a base to build other images, for example an application build environment or a daemon which may be customized with user-specific configuration.

Is it good practice to run stateful applications on Docker? What are the scenarios where Docker best fits in?

The problem with statefull docker aplications is that they by default store their state (data) in the containers filesystem. Once you update your software version or want to move to another machine its hard to retrieve the data from there.
What you need to do is bind a volume to the container and store any data in the volume.
if you run your container with: docker run -v hostFolder:/containerfolder any changes to /containerfolder will be persisted on the hostfolder. Something similar can be done with a nfs drive. Then you can run you application on any host machine and the state will be saved in the nfs drive.

 

What are the differences between Docker and Hypervisors?

Features
Hypervisors
Docker
Default Security Support
To a great degree
To a slightly less degree
Memory on disk required
Complete OS plus apps
App requirement only
Time Taken to start up
Substantially longer as it requires boot of OS plus app loading
Substantially shorter as apps only need to start as the kernel is already running
Portability
Portable with proper preparation
Portable within image format; typically smaller
Operating System
Supports multiple OS
It uses the host OS

 

 

Monday, 24 December 2018

Vagrant Interview Questions


1) What is Vagrant?

A) Vagrant is an open-source software product for building and maintaining portable virtual software development environments, e.g. for VirtualBox, Hyper-V, Docker, VMware, and AWS.

2) Vagrant is written in which language?

A) Vagrant is written in Ruby language.

3) What is a BOX in Vagrant?

A) A box is a packaged Vagrant environment, typically a virtual machine.

4) What is Provider in Vagrant?

A) A provider is the location in which the virtual environment runs. It can be local (the default is to use VirtualBox), remote, or even a special case like a Docker container.

5) What is Provisioner in Vagrant?

A) A provisioner is a tool to set up the virtual environment, and can be as simple as a shell script, but alternatively a more advanced tool like Chef, Puppet, or Ansible can be used.

6) What are the subcommands associated with Box command?

A) Box command used to manage (add, remove, etc.) boxes.
Command: vagrant box
The main functionality of this command is exposed via even more subcommands:
Ø  add
Ø  list
Ø  outdated
Ø  prune
Ø  remove
Ø  repackage
Ø  update

7) Explain Box Add Command in Vagrant?

A) Command: vagrant box add ADDRESS
This adds a box with the given address to Vagrant. The address can be one of three things:
·         A shorthand name from the public catalog of available Vagrant images, such as "hashicorp/precise64".
·         File path or HTTP URL to a box in a catalog. For HTTP, basic authentication is supported and http_proxyenvironmental variables are respected. HTTPS is also supported.

·         URL directly a box file. In this case, you must specify a --name flag (see below) and versioning/updates will not work.

8) What is Box List command in Vagrant?

A) Command: vagrant box list
This command lists all the boxes that are installed into Vagrant.

9) What is Box Outdated command in Vagrant?

A) Command: vagrant box outdated
This command tells you whether or not the box you are using in your current Vagrant environment is outdated.

10) What is Box Prune command in Vagrant?

A) Command: vagrant box prune
This command removes old versions of installed boxes. If the box is currently in use vagrant will ask for confirmation.

11) What is Box Remove command in Vagrant?

A) Command: vagrant box remove NAME
This command removes a box from Vagrant that matches the given name.

12) What is Box Repackage command in Vagrant?

A) Command: vagrant box repackage NAME PROVIDER VERSION
This command repackages the given box and puts it in the current directory so you can redistribute it. The name, provider, and version of the box can be retrieved using vagrant box list.

13) What is Box Update command in Vagrant?

A) Command: vagrant box update
This command updates the box for the current Vagrant environment if there are updates available.

14) What is Connect command in Vagrant?

A) Command: vagrant connect NAME
The connect command complements the share command by enabling access to shared environments.

15) Destroy command in Vagrant

A) Command: vagrant destroy [name|id]

16) Global Status command in Vagrant

A) Command: vagrant global-status

17) What is Vagrant Share?

A) Vagrant Share allows you to share your Vagrant environment with anyone in the world, enabling collaboration directly in your Vagrant environment in almost any network environment with just a single command: vagrant share.
This command will tell you the state of all active Vagrant environments on the system for the currently logged in user.
This command stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process.

18) What is Vagrantfile?

A) The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines.

19) What is Provisioning in Vagrant?

A) Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process.

20) What are Synced Folders in Vagrant?

A) Synced folders enable Vagrant to sync a folder on the host machine to the guest machine, allowing you to continue working on your project’s files on your host machine, but use the resources in the guest machine to compile or run your project.

21) What is Multi-Machine environment in Vagrant?

A) Vagrant is able to define and control multiple guest machines per Vagrantfile. This is known as a “multi-machine” environment.
These machines are generally able to work together or are somehow associated with each other. Here are some use-cases people are using multi-machine environments for today:
Accurately modeling a multi-server production topology, such as separating a web and database server.
Modeling a distributed system and how they interact with each other.
Testing an interface, such as an API to a service component.
Disaster-case testing: machines dying, network partitions, slow networks, inconsistent world views, etc.

22) How do you define multiple machines in Vagrant?

A) Multiple machines are defined within the same project Vagrantfile using the config.vm.define method call.

23) What are Providers in Vagrant?


A) While Vagrant ships out of the box with support for VirtualBox, Hyper-V, and Docker, Vagrant has the ability to manage other types of machines as well. This is done by using other providers with Vagrant.

24) What are Plugins in Vagrant and how they assist?

A) Vagrant comes with many great features out of the box to get your environments up and running. Sometimes, however, you want to change the way Vagrant does something or add additional functionality to Vagrant. This can be done via Vagrant plugins.

25) What is Vagrant Push?

A) Vagrant is capable of deploying or “pushing” application code in the same directory as your Vagrantfile to a remote such as an FTP server.
Pushes are defined in an application’s Vagrantfile and are invoked using the vagrant push subcommand.

26) What is vagrant in DevOps?

A) DevOps is a lot more than configuration management.  Vagrant is another tool to help your organization transition to a DevOps culture. Vagrant also helps improve your entire workflow of using Puppet, improving development and process for both developers and operations.

27) What is a vagrant image?

A) The Vagrantfile has some information that will be merged into your Vagrantfile that is created when you run vagrant init boxname in a folder. The box-disk.vmdk is the virtual hard disk drive. The box.ovf defines the virtual hardware for thebox.