You are on page 1of 53

er

Vagrant-
Layer on top of Virtual box, a CLI used for provisioning virtual machines.
Using below URL, we can download the virtual machine base boxes. Which usually contains
a box name and user name.
https://app.vagrantup.com/boxes/search

select the box name – Ex- ubuntu/trusty64

Execute below command in CLI-


vagrant box add my-box name-of-the-box.box
Ex- vagrant box add centos-7.0-x86_64.box –name centos

 Vagrant file- Vagrant file contains configuration details that are require to provision
virtual machines and also VM provider info. Refer below file for reference.
Vagrant.configure("2") do |config|
# Box
config.vm.box = "centos/7"

config.vm.provider "virtualbox" do |v|


v.memory = 2048
v.cpus = 2
end

config.vm.provision "shell", inline: $generalscript


end
Few commands fwhile using vagrant

 vagrant list- will list the number of vms provisioned


 vagrant halt- will shutdown the machine that up and running
 vagrant destroy – will destroy the machine and memory used by VM is freed and
can be used to provision another VM
 vagrant up- To start a VM, do.
 vagrant ssh- To SSH into a VM, run.

After this step you are inside the Linux kernel of your VM.
There are few important features that will be useful for developer. Ex- Synced folder, port
forwarding which can configured using vagrant file itself. Let’s elaborate on those.
a) Synced folder- This will sync the folders from host drive to vagrant machine folder.
Below is the command.
config.vm.synced_folder "src/", "/srv/website"
b) Port forwarding- Using vagrant file
config.vm.network:forwarded_port, guest:80, host:80
c) In Vagrant file, we can set private IP address also.
config.vm.network “private_network”, ip: “162.18.0.101”
d) Set memory for virtual machine
config.vm.provider “virtualbox” do |vb|
vb.memory = “2000”
end
e) Set virtual CPUs-
cat proc/cpuinfo
config.vm.provider “virtualbox” do |vb|
vb.cpus = “6”
end

--05/08/2020 and 06/08/2020


Vagrant installation-
make the setup
==> open vagrant script with PowerShell
==> vagrant up
==>exit (to logout)
==> vagrant destroy -f
==> vagrant halt
Note -should not update the machine every day.

--07/08/2020
ssh into CentOS
pwd //present working directory will be displayed
mkdir //creates directory
cd /dir/ //switches directory
ls //list view for particular directory
ls -al // lists hidden files as well
ll //to see permissions on the folder or directory
ctrl + l ==> clear screen shortcut
sudo yum install httpd -y
sudo service httpd status
netstat -tulnp
cd /etc/httpd/conf.d/
cd ../conf
ls
free -th  check total and free RAM on linux
sudo vi httpd.conf --> file contains configuration information and ports

sudo service httpd restart //to restart the server


sudo setenforce 0
change the port number if 80 doesn't work

Any web server will run on port 80 and secure server will run on 443
Note-to run the server using vagrant, add port 80 in below location.
virtual box>>networking>>advanced>>port forwarding>>add host port and guest port as '80'
enter “localhost” in web browser (virtual box server accessed by vagrant)

Query -
1. yesterday tried to create a folder, folder got created but could not see it even after being a root
user.
I’m using AWS Linux instance.
2.i'm using AWS for Linux server as I could not do vagrant up. I was returned with error code 416.
curious to know why was the error?

--10/08/2020

To host our own web server.


create index.html file in /var/www/html/
use public IP address in web browser to view the hosted application
if you are linking any files in html file. The linked files also should be available in same
/var/www/html/

--11/08/2020
Creating users and give sudo access to the users
sudo useradd username
sudo passwd username (prompts change password)
sudo vi /etc/ssh/sshd_config
Tip-To search for a word in file /word
 Change password authentication to yes in sshd_config file
To login the terminal with the user created
su harini –
exit harini user
sudo visudo
add username ALL=(ALL) NOPASSWD: ALL in the users below root user
sudo reboot //reboots the server
ssh with the user created and enter password

vim install.sh
sh install.sh //to execute the script
#!/bin/bash
sudo yum update -y
touch index.html
echo “Hi Harini welcome to first class” >index.html
sudo chown -R root:root index.html
sudo yum install httpd -y
sudo service httpd start
cp index.html /var/www/html/
sudo service httpd restart

13/08/2020
Class-Learnt to create a script using which we can automate processes such as installation, updation
for packages.
Script name created by me -Installscript.sh
#!/bin/bash
echo "welcome to installation services"

read -p "enter what you want to do today:" process

read -p "enter which package you want to $process :" package

if [ $process == install ]
then if [ $package == httpd ]
then if [ -f /usr/lib/systemd/system/httpd.service ]
then echo "process already exists"
else
sudo yum install httpd -y
sudo service httpd start
fi
elif [ $package == all ]
then sudo yum update -y
else
echo "invalid package"
fi
elif [ $process == uninstall]
then if [ $package == httpd ]
then if [ -f /usr/lib/systemd/system/httpd.service ]
then
echo "package exists, removing the package"
sudo yum remove httpd -y
else
echo "package unavailable"
fi
elif [ $package == all ]
then sudo yum update -y
else
echo "invalid package"
fi
else
echo "invalid process"
fi

Task:
Write a script which creates a file and move it to directory /var/app/ which is to be created by
another sh script.
Script name created by me -bashcript.sh
#!/bin/bash
#updating the server
sudo yum update -y

#reading the file name


read -p "please enter the file name: " file

#Reading the conents to be written in file"


read -p "contents in the file $file :" data

# Updating the file with contents


echo $data > $file

#creating another shell script


touch dir.sh
chmod 777 dir.sh
#reading the directory name from the user"
read -p "please enter dir name:" dir

#reading the sub directory from user


read -p "enter sub dir name:" subdir

#creating the directories which user entered in .sh


echo "#!/bin/bash
# checking and removing if directory is already present
if [ -e $dir/ ]
then if [ -e $dir/$subdir/ ]
then rm -rf $dir/$subdir/
rm -rf $dir
fi
else
rm -rf $dir
fi
mkdir $dir
chmod 777 $dir
cd $dir
mkdir $subdir
chmod 777 $subdir
cd ..
exit 0" > dir.sh
# executing the script which creates directory
sh dir.sh

#copying the file to created directory


mv "$file" $dir/$subdir/

14/08/2020
Taught for and while loops
Task assigned:
Write a shell script which executes the following
1) Create a directory with your name which is provided through user input
2) create sub directories which are the following dev qa stage prod
3) now using conditional looping check when the I value in for is dev, qa, stage, prod each then a file
should be created inside the respective sub directory with the name of the file as that of the sub dir
name.
Script-name created by me -loops.sh
#!/bin/sh

#take user input for main directory


read -p "enter the parent directory :" dir
if [ -e $dir ]
then
rm -rf $dir
fi

mkdir $dir
chmod 777 $dir

for i in dev qa stage prod


do
if [ -e $dir/$i ]
then
rm -rf $dir/$i
fi
cd $dir
mkdir $i

cd $i
touch ${i}file1.txt
cd ..
done

#Count the number of strings in array which contains uppercase


letters
#!/bin/bash

arr=("Harini", "usha", "Sai", "sudeep", "Vivek" )

i=0
# Loop upto size of array
for var in "${arr[@]}"
do
#echo $var
# To print index, ith
# element
if [[ $var =~ [A-Z] ]]
then
echo $var
# Increment the i = i + 1
((i+=1))
fi
done

echo "count of upper case string is $i"

17/08/2020-
Gitlab-

#!bin/bash/

#Getting the dependencies


sudo yum install -y curl policycoreutils openssh-server postfix

#Running and Enabling postfix


sudo service postfix start
sudo chkconfig postfix on

#Running the script for repository


curl
https://packages.gitlab.com/install/repositories/gitlab/gitlab-
ce/script.rpm.sh | sudo bash

#Installing the gitlab on centos


sudo yum install -y gitlab-ce

After executing the above script execute below commands to modify external URL as
“localhost”/ ec2 IP address in gitlab.rb file
sudo vim etc/gitlab/gitlab.rb
sudo gitlab-ctl reconfigure
After the reconfiguration is done, all users will be forced to use the HTTPS protocol when
accessing the GitLab site.

18-08-2020-
Create a new project in GitLab.
Clone the project to local machine.

git clone url


cd projectfolder-name
ls -to view the files
[Copy a file to project location.]
git status
//copied file will be shown as untracked file
git add . ( ./ filename to add the file to branch)
git commit -m “msg” (making a commit to branch)
git push (this will move the newly created branch to git repository with changes
made)

19-08-2020-
Created a new project projecttest-002
Create branches and push the code to new branch.
git clone url
cd projectfolder (shows master branch)
git checkout -b dev (creates a branch and switch to new branch)
//create a file here
git status (new file will appear as untracked file)
git add . (adding the file to branch)
git commit -m “msg”
git push –set-upstream dev (this will move the newly created branch to git
repository with changes made)

 Script to automate branch creation branches.sh

#!/bin/bash

#taking directory input from user


read -p "Enter the directory to create branches of it: " $dir

cd $dir

for i in qa stage prod


do
git checkout -b origin/$i
git checkout origin/master
done

echo "created standard branches successfully"

Error- could not execute sh script but were able to manually create the branches.

What I did-I have created dev branch and committed the changes to the branch. While I
stay in dev branch, I created qa branch.(all the changes that are in dev branch will be
copied to qa branch). Then I switched to master branch and created other branches
stage and prod ,it contains the copy of master.
Note to self -always created branches from master branch during practice.
 Push branches to git
Switch to master branch
git push --set-upstream origin qa
git push --set-upstream origin stage
git push --set-upstream origin prod

 To set user permissions, go to members



 To protect a repository, settings protected repositoryselect the branch and
permissions that needs to be set.
 Tagging a git branch
git tag -a v1.0 -m “First version”
git push origin --tags

 Renaming git branch,


Git branch -m “new name”
Git push origin bname
 Deleting a branch from local repo
Git branch -d bname

 Deleting from remote repo


git push origin –delete bname

 To clone using SSH,


 Check if there is a key in users/Harini/.ssh/ with .pub extension
 If no, create an ssh key using ssh-keygen, enter name and details
 Cat .pub file
 Copy and paste SSH key to git repository in GitLab, settings  SSH keys
 Also open port 22 in VM manager(port forwarding).

20/08/2020-
Create merge requests
 Protect Dev, QA, Stage, Prod branches, settings protected repositoryselect
the branch maintainer for merge and no one for push.
 Git checkout dev
 Modify/create a file and try to push, git says access rejected.
 Go to UI, create merge request, select source and destination branches. Select
necessary details.
 As I’m the admin approve and merge the request.
 Git pull in git bash (pulls all the modifications to Qa branch in local machine)
21/08/2020-
 http://40.87.97.212/root/real_time-101

25/08/2020
Create a new vagrant machine
Update
Install java

sudo yum install java-1.8.0-openjdk-y


Install wget command
sudo yum install wget -y

sudo wget -O /etc/yum.repos.d/jenkins.repo


https://pkg.jenkins.io/redhat/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key

sudo yum install jenkins -y

forward port to 8080 on VM manager

sudo service Jenkins start

username -admin
password-jenkins

view workspace in terminal


/var/lib/ jenkins/workspace/

Verify Jenkins log to troubleshoot issues with jenkins


01/09/2020-
Junit- Provides more visibility on test scenarios in the code.
Matrix Authorization strategy- allows to provide granular access to the users
SSH build agents- allows SSH communication for nodes.
Jenkins Pipeline-
We have scripted pipelines and declarative pipelines.
Syntactically, it is easy to use declarative pipelines as they are more readable and east to
start with.

Declarative Pipeline

http://13.233.65.13:8080/env-vars.html/

Webhooks- Trigger a Jenkins job whenever a new commit is detected on SCM.


Steps to configure webhooks-
1. Go to webhooks in SCM tool.
2. Select add webhook and enter Jenkins url with a tag name in payloadurl field.
3. In the Jenkins , create a freestyle or pipeline job, select github triggers for scm
polling.
4. If necessary install Github integration plugin
Ansible

Ansible is CI and Configuration management tool. We use playbooks which are YAML scripts
for configuring or deploying servers, other tools are chef, puppet.
Ansible installation-
sudo yum install epel-release -y

sudo yum install ansible -y


---
-hosts: localhost
Become: yes
Tasks:
-name: Installation of httpd
Yum:
Name: httpd
State: installed

-name: Starting the service


service:
Name: httpd
State: started

For execution of yaml file -Ansible-playbook httpd.yml(file name)


---
- hosts: localhost
become: yes
tasks:
- name: Installing dependencies
yum:
name:
- openssh
- policycoreutils
- curl
- postfix
state: installed

- name: Starting the postfix server


service:
name: postfix
state: started
enabled: yes

- name: Getting the shell script for repo file


get_url:
url:
https://packages.gitlab.com/install/repositories/gitlab/gitlab-
ce/script.rpm.sh
dest: /home/vagrant
owner: vagrant
group: vagrant

- name: Running the repo file script


script: /home/vagrant/script.rpm.sh
- name: Installing gitlab
yum:
name: gitlab-ce
state: installed

- name: Editing the config


lineinfile:
path: /etc/gitlab/gitlab.rb
regexp: '^external_url'
line: external_url 'http://localhost'

- name: Reconfiguring gitlab


command: gitlab-ctl reconfigure

Yaml file for Jenkins


02/09/2020-

 In ansible machine, install git


 Create aws instance with port 22 and 80 allowed.
 Login AWS instance, set locale ( http://automate.abhishek-a.in/) reboot machine.
 There are 2 ways to connect to remote servers.
1. We need to establish communication between ansible local machine and aws
machine for that create ssh keys,
o Generate ssh keys in ansible machine. Ssh-keygen
o Ls >> cd ~/.ssh >> ls>>cat id_rsa.pub (copy the ssh key)
o Cd .ssh/ in AWS instance >> ls >> cat authorized_keys >> echo ‘copied ssh key
from local machine’ >> authorized_keys (keys should be concatenated with
existing one)
o Copy the public IP of aws instance, in vagrant machine (from .ssh folder) give
Ssh centos @ipaddress
2. In aws instance, sudo vi /etc/ssh/sshd_config (enable password authentication to
yes)
Sudo passwd centos (change pwd)
Sudo service sshd restart
In local ansible machine, Ssh-copy-id centos@ipaddress (enter pwd to login)

 Sudo vi /etc/ansible/hosts
[test]
Centos@ipaddress
 From gitlab digital lync, clone ansible- playbooks-101 on ansible machine
 Cd ansible- playbooks-101
 Create update.yml file -to update the machine.
-Hosts: test
Become: yes
Tasks:
- name : Updating the machine
Yum:
Name: ‘*’
State: latest

Ansible-playbook update.yml –vv(for verbose)

 Install gitlab in remote machine .


---
- hosts: test
become: yes
tasks:
- name: Installing dependencies
yum:
name:
- openssh
- policycoreutils
- curl
- postfix
state: installeds

- name: Starting the postfix server


service:
name: postfix
state: started
enabled: yes

- name: Getting the shell script for repo file


get_url:
url:
https://packages.gitlab.com/install/repositories/gitlab/gitlab-
ce/script.rpm.sh
dest: /home/centos
owner: centos
group: centos

- name: Running the repo file script


command: sh /home/centos/script.rpm.sh

- name: Installing gitlab


yum:
name: gitlab-ce
state: installed

- name: Editing the config


lineinfile:
path: /etc/gitlab/gitlab.rb
regexp: '^external_url'
line: external_url 'http://localhost'
- name: Reconfiguring gitlab
command: gitlab-ctl reconfigure

 Issue faced today -vagrant remained in stopping state and couldn’t perform any
action on the machine.
Fix- cd C:\Program Files\Oracle\VirtualBox
VBoxManage.exe startvm <vm-uuid> --type emergencystop
Then restart the machine using vagrant up

03/09/2020

Ansible galaxies are used for the playbooks to be used over and over without re writing the
code. In ansible galaxies roles are written

08/09/2020
Integrating Jenkins with ansible to execute code on dev server

28/09/2020
Docker installation run images
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
sudo usermod -aG docker centos

reboot the system and start docker service.


To install images browse docker hub for required image
docker pull nginx

docker container run -d -p 80:80 --name=webserver nginx

docker exec -it dockerid /bin/bash


cat /etc/os-release
To stop server
docker container stop 4a0b4f3937f4

Jenkins image
Docker pull Jenkins
docker container run -d -p 81:8080 --name=jenkinsimg Jenkins
docker exec -it jenkinsimg /bin/bash

29/09/2020
We have 2 modes to run an image interactive terminal mode and daemon mode.
Using daemon mode, session will remain running until unless we terminate it or stop the
image.
Ex- docker container run -d -p 81:8080 --name=jenkinsimg Jenkins

Using Interactive terminal mode, session will be terminate when we exit from -it mode.
Ex- docker container run -it -p 81:80 --name=testcentos centos

Ubuntu image Centos image

docker container run -it -p 80:80 docker container run -it -p 80:80
--name=test ubuntu --name=testcentos centos

apt-get update -y yum update -y


yum install vim -y
apt-get install apache2 -y service/systemctl doesn’t work on centos
service apache2 start cd /usr/sbin/
service apache2 status ./nginx
yum install net-tools
run hostaddress in browser netstat -tulnp

Build own Docker image


mkdir nginx
cd nginx/
vim Dockerfile
FROM centos:7

RUN mkdir /app/

RUN yum update -y &&\


yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-
7.noarch.rpm -y &&\
yum install nginx -y

COPY ./index.html /usr/share/nginx/html


CMD ["nginx", "-g", "daemon off;"]

vim index.html
docker build . -t mywebserver:Vr1
docker image ls
docker run -it -p 80:80 mywebserver:Vr4

docker exec -it mywebserver:Vr4 /bin/bash

Pending to write custom Jenkins image


FROM centos:7
RUN yum update -y && yum install java-1.8.0-openjdk -y
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-
ci.org/redhat/jenkins.repo &&\
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

RUN yum install jenkins -y


CMD java -jar /usr/lib/Jenkins/Jenkins.war

push images to docker hub


docker tag mywebserver:Vr4 harini96/custom_images:v1
docker login
docker push harini96/custom_images:v1

05/09/2020
1.Create 3 VMs,
2.install docker,

curl -fsSL https://get.docker.com -o get-docker.sh


sudo sh get-docker.sh

3.modify user to docker and start docker service.


Sudo usermod -aG docker centos
Sudo service docker start
Sudo chkconfig docker on

4.On the master,


Docker swarm init

Will generate join id using which nodes can be added to master, Add port 2377 to security
group before join on slaves
docker swarm join --token SWMTKN-1-
5g3iv56cya1im4eiwkzuyose9uv00xn5wvveisbxucl0nl7e8p-9xidoh1jobuo68rsvfrp5kphc
172.31.8.173:2377

docker node ls ( for list of nodes connected)


docker service create --replicas=3 -d -p 80:80 nginx
ss (to check if servers are running on all nodes)

enable load balancer to achieve high availability.


Create target groups create exclusive security group for load balancer.
Add security group to docker servers
Wait for load balancers to provision and browse load balancer dns hostname.
Issue- sourcemapkilled while building nodejs project
Due to insufficient RAM.
Move docker image from one server to another server.
docker save img-id -o img1.tar.gz
scp img1.tar.gz
docker load -i img1.tar.gz

How to check image layers – docker history <imageName/ID>

13/10/2020

Mithun Notes
15-02-2020 7:00 PM
==================
Docker

Containarization Platfrom using which we can package our applications(code) & required
softwares to run application in the form of
containers.

Build Once Run AnyWhere(Physical Machine,Virtural Machine,Cloud).

Docker has two editions:

Docker CE --> Community Edition(Open Source)

Docker EE --> Enterprise Edition(Commerical)

Components/Terminology:
=======================
Docker Image
Docker Container
Docker Registry(Repository)
Public --> hub.docker.com (Public Accessable from anywhere)
Private --> Can be accessable with in private network(Nexus,JFrog)
Dockerfile

docker build -t <repoDetails>/<appName>:<version> .

ex:
# docker hub dockerhandson is username of my repo.Replace dockerhandson with your
user name.
docker build -t dockrhandson/java-web-app:1 .

# Image tagged with rivate repo


docker build -t 189.98.72.182:8081/java-web-app:1 .

# Authenticate with Repo


#Public Repo(docker hub)
docker login -u <username> -p <password>

ex:

docker login -u dockerhandson -p password

#Private Repo
docker login -u <username> -p <password> <URL>

docker login -u admin -p password 189.98.72.182:8081

# Docker push

docker push <repoDetails>/<appName>:<version>

# Image tagged with Public Repo


docker push dockrhandson/java-web-app:1

# Image tagged with Privare Repo


docker push 189.98.72.182:8081/java-web-app:1

docker run -d -p <hostPort>:<containerPort> --name <containerName> <imageName>

ex:
docker run -d -p 8080:8080 --name javawebappcontainer dockerhandson/java-web-app:1

16-02-2020 6:00 AM
==================

docker version
docker --version
docker info

Image Commands:
==============
Docker Image: It's package which will have all the required components like application
code & softwares which are required to run application.

Base Images : Software images which contains some software.Like


ubuntu,centos,tomcat,mysql,jenkins ..etc.

Custom Images: We can create our own image using docker file on top of base image
which can have application code + Softwares.

Create a simple docker image from nginx

# Create one index.html


vi index.html

#Create Dockerfile(Default Name)

FROM nginx
COPY index.html /usr/share/nginx/html

docker build -t <repodetails/appName:version> .

Ex:
docker build -t dockerhandson/testnginx .

#Tag image with private Repo

docker build -t 189.81.23.41:8081/testnginx .

# List images in the docker machine


docker images (or) docker image ls

# Authenticat with Repo

# With public repo


docker login -u <userName> -p <password>

ex:
docker login -u username -p password

# With Private Rep


docker login -u <userName>. -p <password>. <RepoURL>

ex:
docker login -u admin -p password 189.81.23.41:8081

# Push image to repo


docker push <repodetails/appName:version>

ex:
docker push dockerhandson/testnginx

# Download Image From Repo


docker pull <ImageId/Name>

#Delete Image from docker machine


docker rmi <ImageName/ImageID>

ex:
docker rmi dockerhandson/testnginx:latest

docker rmi <ImageName/ImageID> <ImageName/ImageID>

Delete all images


=================
docker rmi -f $(docker images -q)

# It will also delete all unsued images


docker image prune

# It will delete all stopped containers ,all unused images,un used networks.
docker system prune

# Get more details about an image


docker image inspect <imageId/ImageName>
docker inspect <imageId/ImageName>

# Get Layers of an image


docker history <imageId/ImageName>

How can we move image from one system to another system with out repo?

1) # Save image as a tar file in source system where u have a image.


docker save <imageName> - o <FileName>.tar

2) SCP image tar file from source system to destination system


scp <FileName>.tar username@DestionationIP

3) In destination server execute docker load to load image from tar file.
docker load -i <FileName>.tar

docker build
docker push
docker login
docker images or docker image ls
docker images -q
docker rmi <imageId/Name>
docker rmi -f <imageId/Name> <imageId/Name>
docker rmi -f $(docker images -q)
docker image prune
docker system prune
docker image inspect <imageId/Name>
docker inspect <imageId/Name>
docker history <imageId/Name>
docker search <imageName>
docker save
docker load

Contanier Commands
==================

# It will create container from the image but it will not start the container.

docker create --name <contaierName> - p <hostPort>:<containerPort> <imageName>

# It will create & Start container from the image.


docker run -d --name <contaierName> - p <hostPort>:<containerPort> <imageName>

ex:
docker run -d -p 80:80 --name nginxcontianer dockerhandson/testnginx

# List Running Containers


docker ps or docker container ls

# List All Containers Running+Stopped


docker ps -a or docker container ls -a
docker ps --all or docker container ls --all

# List Only Stopped Contianers


docker ps -a --filter "status=exited"

# Inspect Container

docker container inspect <containerId/Name>


docker inspect <containerId/Name>

# Start Container
docker start <containerId/Name>

# Stop Container
docker stop <containerId/Name>

# Restart Container
docker restart <containerId/Name>

# Pause Container(It will pause container process)


docker pause <containerId/Name>

# Un Pause Container
docker unpause <containerId/Name>

Q) How can we copy files from HostSystem to Container or Container to HostSystem?

# Container to system
docker cp <containerName>:/<ContainerFilePath> <SystemFileName>

# System to Cotnainer
docker cp <SourceFile/SystemFilePath> <containerName>:/<ContainerFilePath>

How can we troubleshoot or debug applications which are running as a conainers?

# It will disply Standard Out ,Standard Error Of Container terminal.


docker logs <containerName/Id>
docker logs --tail <NoLines> <containerName/Id>
docker logs -f <containerName/Id>

# It will display Memory/CPU Usage of a cotainer.


docker stats <containerName/Id>
# It will process detials which is runing inside a container
docker top <containerName/Id>

# We can execute commands inside container


docker exec <ContainerID/Name> <Command>
ex:
docker exec javawebapp pwd
docker exec javawebapp ps

docker exec javawebapp cat /etc/*releases

Q) How can we login or go inside into to container?


docker exec -it <containerName/ID> /bin/bash

docker exec -it <containerName/ID> bash

docker exec -it <containerName/ID> sh

# It will attach container process(terminal) to the host system


docker attach <containerName/ID>

# We can create a image from the container.


docker commit <containerID/Name> <imageName>

# Remove Containers

# Below command can remove container if container is in stopped/exited state.


docker rm <containerID/Name>

# Below command can remove runinging container(Force Remove).


docker rm -f <containerID/Name>

# How to remove all stopped containers


docker container prune
docker rm $(docker ps -aq --filter "status=exited")

# Remove Paused Containers


docker rm -f $(docker ps -aq --filter "status=paused")

# Remove Paused Containers


docker rm -f $(docker ps -aq --filter "status=paused")

# Remove All Containers


docker rm -f $(docker ps -aq)

# Who can we delete containers which are created from specific image?

docker ps -a|awk '$2=="<imageNameWithVersion>" {print $1}' |xargs docker rm -f

Ex:

docker ps -a|awk '$2=="dockerhandson/java-web-app" {print $1}' |xargs docker rm -f


22-Feb-2020 6:00 AM
===================
Monolothic --> Application Will be having a single code base.

Flipkart(Example)
Features
--> SignUp
--> SignIn
--> CheckOut(AddToCart)
--> Payments
--> Orders
--> MyAccount

It will generate only one build artifact.(war/ear)

If Develepment is following Monolothic Architecture all the features(modules) will be part


of same code base(Git Repo). Entire
application will be build as a single package.

Disadvantages:

1) Maintaince:

2) Deployment --> Eventhough there is smale change in one feature.Complete application


has to rebuild,retest and deploy entire application.

2) Scaling: We will endup scaling entire application (all features) eventhough we want to
scall one few features since all features are part of sample code base hence same build
package.

MicroServcies --> Application Will be developed as collection of mutiple independent


services ,having a multiple code bases.

Flipkart
Features
--> SignUp --> Git Repo --> singup.jar/war --> DockerImage --> Container
--> SignIn --> Git Repo --> signin.jar/war --> DockerImage --> Container
--> CheckOut(AddToCart) --> Git Repo --> checkout.jar/war
--> Payments --> Git Repo --> checkout.jar/war
--> Orders --> Git Repo --> checkout.jar/war
--> MyAccount --> Git Repo --> checkout.jar/war

Dockerfile Keywords
==================
#Sample Dockerfile

FROM tomcat:openjdk-8
COPY target/*war /usr/local/tomcat/webapps/java-web-app.war

Docker file keywords

FROM --> FROM indicates from which base image() you want to create your own image.

ex:
(Only OS no Softwares)
FROM ubuntu
FROM centos
FROM tomcat:openjdk-8 (OS + Java+Tomcat Sofwares)
FROM openjdk:8 (OS + Java8)
FROM nginx (OS + nginx)
FROM node (OS + node software)
FROM python(OS + python)

Note: In one docker file we can have more than one FROM keywords. But 99% In
Dockerfile we will use only one FROM <baseimageName>. Base Image is dependson on
what type of application we want to build&run as part of docker.

MAINTAINER --> It's depricated in latest versions. It's just info about who is
maintaining/created the docker file. It's about author of dockefile.

ex:
MAINTAINER mihtuntechnologies@gmai.com

COPY --> It can copy files from host/local system(Where u are building an image) to image
while creating an image.

ex:

COPY <sourceFilePath> <destinationPath>


<Local/SystemFilePath> <imagePath>

COPY target/java-web-app*.war /usr/local/tomcat/webapps/java-web-app.war

ADD --> It can copy files from host/local system to an image. And also it can download
files from remote http/s locations.

Ex:

ADD <sourceFilePath> <destinationPath>


ADD <sourceFileHTTP/S URL> <destinationPath>

ADD target/java-web-app*.war /usr/local/tomcat/webapps/java-web-app.war

ADD http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.51/bin/apache-tomcat-
8.5.51.tar.gz /opt/tomcat

RUN --> RUN keyword indicates the command that needs to be executed
on the image. RUN keywords will be processed or executed
while creating a image. We can have n number of RUN keyword
in a Dockerfile. All RUN keywords will be executed.

EX:

RUN <command> (shell form)


Inside image your command will be processed like below.
/bin/sh -c command
ex:

RUN mkdir -p /opt/tomcat

RUN ["executable", "param1", "param2"] (Executable form)


Inside image your command will be processed like below.
/bin/executable param1 param2

RUN ['mkdir', '-p', '/opt/tomcat']

RUN apt install git


RUN apt install vim

CMD --> CMD key word will indicate what command has to be executed while creating a
container.We can have more than on CMD in docker file.But only
latest/recent CMD will be executed.

CMD java -jar <jarName>


/bin/sh -c java -jar jarName

CMD ['java' , '-jar' , 'jarName']


/bin/java -jar jarName

ENTRYPOINT --> Entry Point also will get executed while creating a container.

ENTRYPOINT java -jar jarName(ShellForm)


ENTRYPOINT ["java",'-jar' , 'Jarname'] (Executable Form)

22-Feb-2020 7:00 PM
===================

What is difference between CMD & ENTRYPOINT ?


CMD can be overridden while creating a container. ENTRYPOINT can't be overrridden
while creating a contianer.

Can we have both CMD & EntryPoint in a dockerfile?


Yes We can have both in a Dockerfile.If we have both CMD & EntryPoint. CMD will be
passed as an argument for ENTRYPOINT.

We can use both CMD & ENTRYPOINT for below requirement.

Contaienr should run alywas same process(program) with defulat arguments . But
dynamically i should be able to change arguments while creating a container.

ENTRYPOINT["echo" , "FROM Dockerfile EntryPoint"]


CMD["ls" "/usr"]

Shell and Exec forms

All three instructions (RUN, CMD and ENTRYPOINT) can be specified in shell form or exec
form. Let’s get familiar with these forms first.

RUN <command> (shell form)


RUN ["executable", "param1", "param2"] (exec form)

CMD <command> (shell form)


CMD ["executable", "param1", "param2"] (exec form)

ENTRYPOINT <command> (shell form)


ENTRYPOINT ["executable", "param1", "param2"] (exec form)

Shell form
<instruction> <command>

Examples:

RUN apt-get install python3


CMD echo "Hello world"
ENTRYPOINT echo "Hello world"

When instruction is executed in shell form it calls /bin/sh -c <command> under the hood
and normal shell processing happens. For example, the following snippet in Dockerfile

ENV name John Dow


ENTRYPOINT echo "Hello, $name"

when container runs as docker run -it <image> will produce output

Hello, John Dow


Note that variable name is replaced with its value.

Exec form
This is the preferred form for CMD and ENTRYPOINT instructions.

<instruction> ["executable", "param1", "param2", ...]

Examples:

RUN ["apt-get", "install", "python3"]


CMD ["/bin/echo", "Hello world"]
ENTRYPOINT ["/bin/echo", "Hello world"]

When instruction is executed in exec form it calls executable directly, and shell processing
does not happen. For example, the following snippet in Dockerfile

ENV name John Dow


ENTRYPOINT ["/bin/echo", "Hello, $name"]
when container runs as docker run -it <image> will produce output

Hello, $name
Note that variable name is not substituted.

Note:

Whether you're using ENTRYPOINT or CMD (or both) the recommendation is to always
use the exec form so that's it's obvious which command is running as PID 1 inside your
container.

• You may also run into problems with the shell form if you're building a minimal
image which doesn't even include a shell binary. When Docker is constructing the
command to be run it doesn't check to see if the shell is available inside the container -- if
you don't have /bin/sh in your image, the container will simply fail to start.
When using the shell form of either the ENTRYPOINT or CMD instruction.If we need to
send any sort of POSIX(SIGKILL,SIGTERM,SIGSTOP) signals to the container since /bin/sh
won't forward signals to child processes.

Refer Below Links for more details:

https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/
https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/

23-Feb-2020 6:00 AM
===================

EXPOSE

The EXPOSE instruction does not actually publish the port. It functions as a type of
documentation between the person who builds the image and the person who runs the
container, about which ports are intended to be published.

EX:
EXPOSE <port>

EXPOSE 8080

EXPOSE 8081

EXPOSE 3306

WORKDIR --> We can set Working directory using WORKDIR key for image/container.

ex:

WORKDIR /usr/local/tomcat

LABEL --> We can set labels(metadata) for an image using LABEL KeyWord.

Ex:

LABEL <key> <value>

LABEL branch develop

VOLUME --> VOLUME keyword will mount contianer file system to docker host file
system. We will use Volume to take a back up of contianers file system.
EX:

VOLUME <ContianerFolderPath>

VOLUME /var/lib/jenkins
VOLUME /data/db

USER --> It will set USER for a image/container so that container will run as the given
USER.

USER jenkins

USER nexus

ENV --> It will set an environment varibale for an image.

ENV <key> <Value>

ex:

ENV JAVA_HOME /usr/local/java


ENV CATALINA_HOME /usr/local/tomcat

ARG --> We define argumetns so that we can refer any where in Dockefile and also we can
dynamically pass argument values while creating an image.
ARG <key>=<value>

What are the best practices we should follow in Dockerfile?


1) Try to use alpine images where ever is possible.
2) Don't install unneccessary packages/softwares in Dockerimage.We should have
only minimal packages/softwares which are required to be run our application.
3) Try to reduce number of layes as much as possible, by mereging muttiple run
commands
into a signle command whereever is possible.

Docker Networks
One cotnainer can to talk to another container if both contaienrs are in same docker
network.

Docker has 3 types of networks by defualt.

# List Networks
docker network ls

1) dridge(default bridge)
2) host
3) none

While creating a container if we don't mention the docker network name containers will
be created in default brige network. If containers are in defualt bridge network
communication happens using only IP. Cotnainers can't access each other using cotnaienr
name.

bridge
default bridge
custom bridge

host --> Container will be created in host network,we no need to do port


publish/forword.Container will not have ip.Can be accessable using System IP and
port.

none --> Container will be created in none/null network.Container will not have ip.Can't
be accessable.

# Create Network
Syntax: docker network create -d <driver> <networkName>

Ex:
docker network create -d bridge flipkartnetwork

# Inspect network
docker network inspect <networkNameOrId>

# Remove unused networks


docker network prune

# Remove One or More Cotnainers


docker network rm <networkNameOrId>
Container to container communication can happen using Name(HostName)/IP in custom
bridge.

Create containes in custom bridge

docker run -d -p 8080:8080 --name tomcatcotnainer --network flipkartnetwork


dockerhandson/java-web-app
Create another container in same network and try to ping with name & IP from above
container.

docker run -d -p 8080:8080 --name dbcontainer --network flipkartnetwork


dockerhandson/java-web-app

docker inspect dockerhandson (Get IP)

Login to tomcatcotnainer and ping db container using name & IP to test connectivity.

docker login exec -it tomcatcotnainer


ping dockerhandson
ping <IP>

23-Feb-2020 7:00 PM
==================

Volumes:
=======

1) Create docker network using below commond(If it's not created already)

docker network create -d bridge flipkartnetwork

2) Create Spring Application Container in above network & which will talk to mongo data
base container

docker run -d -p 8080:8080 --name springapp --network flipkartnetwork


dockerhandson/spring-boot-mongo:latest

3) Create a mongo contianer with out volume in above network

docker run -d --name mongo --network flipkartnetwork mongo

4) Access Spring application & insert data it will be inserted to mongo db. Delete and
recreate mongo container
what ever you have inserted will no longer be availbale. As once we delete contaienr
data also will be deleted
in container.

To take data backup from container we have to use volunmes


5) Create a volume a Local Volume(Execute docker volume ls to check existing volumes)

docker volume create dbbackup


# To list volumes
docker volume ls

6) Use above volume while creating container.

docker run -d --name mongo -v dbbackup:/data/db --network flipkartnetwork mongo

7) Access Spring application & insert data it will be inserted to mongo db. Delete and
recreate mongo container
with same volume mapping. You can see the data back.

===== Network Volumes Using AWS EBS==========

1) Create IAM User with EC2 Full Access and user access key & Secret Key of the same.
Replace your access key & secret below.

docker plugin install rexray/ebs EBS_ACCESSKEY=AKIAZXQITIEDYDKJRX66


EBS_SECRETKEY=L5oxizF4AMqJ6nmsyErgssLohX4DpXVN/1Zn101k

docker volume create --driver rexray/ebs --name ebsvolume

docker run -d -p 27017:27017 --name mongocontainer --network flipkartnetwork -v


ebsvolume:/data/db mongo

Docker Compose:

It's tool for defining and runing multicontainer applications.It's a yml file.

With Out Compose:

We have to run long docker run commands to deploy multi continer applications.

docker volume create -d <driver> <volumeName>

docker volume create -d local mongobackup

docker network create -d <driver> <networkNAme>

docker network create -d bridge springappnetwork


docker run -d -p 8080:8080 --name springcontaienr --network springappnetwork
dockerhandson/spring-boot-docker

docker run -d --name mongo --network springappnetwork -v mongobackup:/data/db


mongo

With Compose

Install docker compose using below command:

sudo apt install docker-compose

We will define all the serivces(cotainers) details in compose file using compose file we can
deploy multi container applications.

defautl name: docker-compose.yml or docker-compose.yaml

Example 1: (Volumes & Networks also will be created by docker compose)

version: '3.1' # Docker Comopose File Format Version

services:
springapp:
image: dockerhandson/spring-boot-mongo
ports:
- 8080:8080
networks:
- flipkartbridge
container_name: springappcotnaienr

mongo:
image: mongo
container_name: mongo
networks:
- flipkartbridge
volumes:
- mongobkp:/data/db

volumes:
mongobkp:

networks:
flipkartbridge:
driver: bridge

Example 2: (Volumes & Networks will not be created by docker compose.As we set
volumes and networks as external)
==========

version: "3.1"
services:
springboot:
image: dockerhandson/spring-boot-mongo
container_name: springboot
ports:
- 8080:8080

mongo:
image: mongo
container_name: mongo
volumes:
- mongobackup:/data/db

volumes:
mongobackup:
external: true

networks:
default:
external:
name: springappnetwork

docker-compose conig
docker-compose up -d
docker-compose down

Example 3: (Volumes & Networks will be created by docker compose.We can pass
environment varibles to containers while creating if required).
==========

If Custom Name

docker-compose-wordpress.yml

version: '3.1'

services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wordpressnetwork

wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wordpressnetwork

volumes:
db_data:

networks:
wordpressnetwork:
driver: bridge

docker-compose -f docker-compose-wordpress.yml config

docker-compose -f docker-compose-wordpress.yml up -d

docker-compose -f docker-compose-wordpress.yml down

Docker Compose Commands:

config Validate and view the Compose file


create Create services
down Stop and remove containers, networks, images, and volumes
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information

29-Feb-2020 6:00 AM

#!/bin/bash
sudo apt-get update
sudo apt-get install curl -y
sudo curl -fsSL get.docksal.io | bash
sudo usermod -aG docker ubuntu

Logout from the the terminal and login again

Note: Make Sure You Open Required/All Ports in AWS Security Groups.

======================================================================
# Initialize docker swarm cluster by exeuting below command on docker server which you
want make it as Manager

docker swarm init

# Initialyze Docker swarm with Public IP


Note: Don't use below(If restart your systems public ip will change will break your cluster)
use above commond to initilaze cluster.

docker swarm init --listen-addr=eth0 --advertise-addr $(curl


http://169.254.169.254/latest/meta-data/public-ipv4) (Only run in manager node)
docker swarm join-token worker (Get the token in manager & exeute in nodes)

======================================================================

docker run imageName --> It will create/deploy one application in single machine -->
docker service create

docker-compose up --> To create/deploy mutiple applications in single mahcine -->


docker stack deploy --composefile docker-compose.yml

Docker Swarm has two modes

Global --> All the nodes (3 servers 1 Manager + 2 Workers)


Replicas --> It will deploye based on replicated number.

docker service create -p 8080:8080 --name javawebapp --replicas 2 dockerhandson/java-


web-app

# User constriants to create containers in specific docker hosts based on condtion


docker service create -p 8080:8080 --name javawebapp --replicas 2 --constraint
'node.role==worker' dockerhandson/java-web-app

# Create a service with a rolling update policy


docker service create --replicas 2 --name nginxservice --update-delay 10s --update-
parallelism 1 nginx:alpine
docker service update --image nginx:latest nginxservice

# Create a service with Volume mapping


docker service create -p <hostPort>:<containerPort> --name <serviceName> --replicas 1
--mount type=volume,source=<volumeName>,destination=<containerfolderPath>
<imageName>

# List Services
docker service ls
# List Services process
docker service ps <servicenName>

# Scale Services
docker service scale javawebapp=3

docker service rm javawebapp

# Add labels to node


docker node update --label-add key=value <nodeid>
Ex: docker node update --label-add server=nodeone qmdh9tgvdef99sryhbezswfl9

#Use above label in constrainsts


docker service create -p 8080:8080 --name javawebapp --replicas 1 --constraint
'node.labels.server==nodeone' dockerhandson/java-web-app

# Drain Nodes in Cluster(Swarm will not create containers in drained nodes)


docker node update --availability drain <NodeID>

# Stack Deploy

version: '3.1'

services:
springboot:
image: dockerhandson/spring-boot-mongo:latest
restart: always
container_name: springboot
ports:
- 8182:8080
working_dir: /opt/app
depends_on:
- mongo
deploy:
replicas: 2
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure

mongo:
image: mongo
container_name: mongo
# ports: # for demo/debug purpose only
# - 27018:27017
volumes:
- data:/data/db
- data-bkp:/data/bkp
restart: always

volumes:
data:
data-bkp:

=================================================================

docker stack deploy --compose-file docker-compose.yml springmongo

docker stack ls

docker stack rm <stackName>

# To come out of swarm execute below commond in worker node


docker swarm leave

# Remove node from Manager


docker node rm <nodename>

version: '3.1'

services:
springboot:
image: dockerhandson/spring-boot-mongo:latest
restart: always
container_name: springboot
ports:
- 8182:8080
working_dir: /opt/app
depends_on:
- mongo
deploy:
replicas: 2
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure

mongo:
image: mongo
container_name: mongo
volumes:
- data:/data/db
- data-bkp:/data/bkp
restart: always

volumes:
data:
external: true
data-bkp:
external: true

networks:
default:
external:
name: flipkartoverlay

Ravi’s session Notes-


Docker volumes- If any destructive operations are performed on the container all the data
will be lost as the data is non-persistent on Docker containers. We mitigate this issue we use
docker volumes which can used independent of container.

 Docker volumes are persistent as they work separately from container life cycle.
 Using docker volumes, data can be shared across multiple containers. Sharing can be
done between host and container also.
 When we create a container, a volume needs to be initialized. Volumes will not be
garbage collected
 Types of volumes
1. Anonymous volumes-
o Not user friendly
o Hard to maintain
o These volumes are controlled by docker itself ( can be access
using sudoer permissions)
o Command - docker run -dt -v /path_of_data <image>
o Ex- docker run -dt –name myserver -v /dir1 centos
o Docker volume inspect <volume-id> to inspect where the
data volume is located.
2. Named volumes-
o These volumes are controlled by docker itself ( can be access
using sudoer permissions)
o Easy to maintain and mount on multiple containers and are
user friendly
o Command - docker run -dt -v vol-name:/path_of_data
<image>
o docker run -dt –name myserver -v dir-vol:/dir1 centos
o
3. Host volumes/bind mount-
o Not controlled or managed by docker , these are custom and
must be maintained by host itself.
o Docker container inspect <container name> to inspect the
volumes

docker container run -dt –name ecomm -v ~/ecomm:/usr/local/apache2/htdocs -p 80:80


httpd

Docker Compose – Compose is used for defining and running multi container applications.
With compose use YAML files to configure application services.
Install docker compose
It is a 3 step process-
1. Define your app’s environment with a Dockerfile
2. Define the services that make up your app in docker-compose.yml so they
can be run together in an isolated environment.
Version: ‘1’
Services:
Web:
Image: “httpd”
Ports:
- “8090:80”
App:
Image: “nginx”
Ports:
- “9090:80”
nodeApp:
Image: “ravi2krishna/nodeapp”
Ports:
- “8080:80”

docker-compose up -d

3. Run docker-compose up and Compose starts and runs your entire app

Kubernetes
Container helps to reduce software running costs. Consume less CPU, RAM etc. However,
container brings scalability challenges.
Kubernetes contains master node and worker nodes
Adaptation of docker swarm is Kubernetes where user can host 2 application in same
cluster. Namespace are used to differentiate between applications. (kubectl get
namespaces). Services supporting Kubernetes like EKS, AKS
Kubernetes contain lot of objects.

Kubectl get pods -n proj-name


Kubectl delete pods prjecname-id -n namespace name

Pods- smallest unit of Kubernetes, containers run inside pod.


When we replicate application for no. of nodes, even if the user terminates or any node
crashes.

./kubectl.exe get deployments -n projname


./kubectl.exe get nodes
./kubectl.exe init
./kubectl.exe get pods
./kubectl.exe get pods -n kube-system

Kubectl works on api, hence we use gets and puts.

./kubectl.exe get pods -n proj1


./kubectl.exe apply -f pods.yml

All containers in a pod will have same ip address. IP address will be assigned to pod.
DB, webserver, api extc can be hosted on single pod.
The container running in pod will not be exposed.

Ravi’s session Notes on Kubernetes-


KOPS help in creating, destroying, upgrading and maintaining production grade highly
available Kubernetes clusters from CLI
Master node contains API server serving ETCD, controller, scheduler
Deployment- Represents an application which is run on a container

Replica set- manages the number of pods that are supposed to be up and running
Pod- It is a smallest unit, that contains the container specification.
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o
"awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Kubectl installation

curl -LO "https://dl.k8s.io/release/$(curl -L -s


https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client

KOPS installation
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s
https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name
| cut -d '"' -f 4)/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops

kops create cluster --name=test.k8s.local --state=s3://k8-bucket-test --zones=ap-


south-1a,ap-south-1b --node-count=2 --node-size=t2.medium --master-size=t2.medium
–-yes
kops delete cluster --name=test.k8s.local --state=s3://k8-bucket-test –-yes
kops validate cluster
kubectl get nodes
kubectl get all
kubectl get pods
kubectl get deployments
kubectl run web --image=httpd --port 80
kubectl get pods -o wide
kubectl delete pod/web

kubectl create deployment web --image=ngnix --port 9090 --replicas 2


kubectl delete deployment.apps/web

Nana-
Most basic configuration of deployment
kubectl create deployment nginx-depl --image=nginx

kubectl edit deployment nginx-depl


kubectl create deployment mogo-depl --image=mongo
kubectl describe pod mogo-depl-599d9c76fd-wd967
kubectl logs mogo-depl-599d9c76fd-wd967
kubectl exec -it mogo-depl-599d9c76fd-wd967 -- bin/bash
kubectl get namespaces
kubectl create namespace namespace-name
kubectl get all -n my-first-namespace
kubectl apply -f mtsql-configMap.yml --namespace=my-first-namespace
Demo Project-
Setup a Sample application with DB as Backend-

Secret and ConfigMap must be created first as we will reference it in a deployment file
Generate username and password from linux box using below commands
echo -n “username” | base64
echo -n “password” | base64

Ingress controller functionality

Helm release management architecture-

22-oct-2020
Prometheus and Grafana
These are infrastructure monitoring tools.
Below are the installation steps for Prometheus
sudo mkdir -p /etc/prometheus

sudo mkdir -p /var/lib/prometheus

for i in prometheus promtool


do
sudo cp $i /usr/local/bin
done

for i in prometheus.yml consoles console_libraries


do
sudo cp -rf $i /etc/prometheus
done

#setup prometheus permissions to directories


for i in /etc/prometheus /var/lib/prometheus /usr/local/bin/prometheus
/usr/local/bin/promtool
do
sudo chown -R prometheus:prometheus $i
done

sudo cp ~/prometheus.service /etc/systemd/system/


sudo chown root:root /etc/systemd/system/prometheus.service

sudo systemctl(service) daemon-reload


sudo systemctl(service) prometheus start

sudo chkconfig prometheus on


echo "the app is running on port 9090"

Write a service file for Prometheus


[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=mutli-user.target

Prometheus will be running on port 9090.

Install Grafana
wget https://dl.grafana.com/oss/release/grafana-7.2.2-1.x86_64.rpm
sudo yum install grafana-7.2.2-1.x86_64.rpm
sudo service Grafana-server start
Sonarqube installation steps:
Github- github.com/SonarSource/sonarqube
Install java
Wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.7.zip
Unzip sonarqube-6.7.7.zip
~/ sonarqube-6.7.7/bin/linux-x-64/sonar.sh status
~/ sonarqube-6.7.7/bin/linux-x-64/sonar.sh start
~/ sonarqube-6.7.7/bin/linux-x-64/sonar.sh stop
Sonarqube runs on port 9000

Generate token
Copy the maven sonar command
mvn sonar:sonar \
-Dsonar.host.url=http://13.232.51.121:9000 \
-Dsonar.login=8ed15c7cb3517c7c14944e77a8ed45ac108d0796
And enter in project folder, it will check for code leakages and vulnerabilities
Nexus installation steps:\
Binary repo’s are 2 types-
1. Snapshots- These are mutable changing from time to time as code changes are
made. These are basically unstable versions (development).
Identify with <vr-snapshot>

2. Releases- These are stable version that has only single release.
3. Identify using snapshots using pom.xml -<version> directive. Snapshot is not visible
attached to version.
4. Use mvn deploy command to push executable files to repository.
sudo yum install wget unzip git java-1.8* -y
wget https://download.sonatype.com/nexus/oss/nexus-2.14.20-02-bundle.tar.gz
sudo tar -xvf nexus-2.14.20-02-bundle.tar.gz
~/nexus-2.14.20-02/bin/nexus status
~/nexus-2.14.20-02/bin/nexus start
http://65.1.86.133:8081/nexus/

http://localhost:8081/nexus/

update pom.xml with distribution management tag. Remove snapshot in version name for
releases
Add 1.0.0 -SNAPSHOT for snapshots of jar files

<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://13.126.139.209:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>http://13.126.139.209:8081/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
</project>

update sudo vim /usr/share/maven/conf/settings.xml file with the repo credentials

<servers>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
Tomcat webserver setup-
Install tomcat binary
wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.63/bin/apache-tomcat-
8.5.63.tar.gz
tar -xvf apache-tomcat-8.5.63.tar.gz
~/apache-tomcat-8.5.63/bin/startup.sh

Tomcat runs on port 8080


sudo netstat -ntlp | grep 8080

credentials should be updated in apache-tomcat-8.5.63/conf/tomcat-users.xml


in tomcat/webapps/manager/META-INF/context.xml file comment valve section and restart
the tomcat server.

update the war files in apache-tomcat-8.5.63/web-apps directory

use profiles for running the application on different servers


mvn tomcat6:deploy -P qa
mvn tomcat6:deploy -P prod

LAMP stack-
 Install web server, serve an application on the sever.
 For serving php applications install php, similarly install java for java applications and python
for python-built applications.
 For HA, setup a load balancer and register running instance on classic load balancers, wait till
the instances are registered (In service status) query browser with elb dns name.
 If any of the servers are down http request won’t be sent to that sever.

Issue faced-

Registered 2 instance each in different AZ, one of the instances went OutOfService-healthy
threshold check failed.

Solution- index.html not configured

Configuring Database-

Wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

Sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm


Sudo yum install mysql-server -y
Sudo service mysqld start

You might also like