Devops Full Notes
Devops Full Notes
1. Git --------------------------------------------- 04
2. Git commands, git workflow, git changes -------------------------------------------------------- 04
5. Terraform --------------------------------------------- 09
6. Terraform Commands, Terraform Workspaces, Terraform Import, Terraform Import ------- 11
37. RUN DOCKER FILE DIRECTLY TAKING SOURCE FROM GITHUB -------------------------- 75
2
VEERA NARESHIT
VEERA NARESHIT
3
VEERA NARESHIT
VEERA NARESHIT
--------------------###GIT###--------------------------------
---------------------Git commands---------------------
4
VEERA NARESHIT
VEERA NARESHIT
--------------------git workflow------------
◼ git add -----to add file into staging area from local working directory
◼ git status ----to see the status of the file where it is
◼ git commit -m "comment"
---------------------#git changes#-----------
-------------------------------------------------
---------------------------------------------------------------------------------
------------------------------------------------------------------------------- ##Revert
or
----Note: even you can remove changes manually. But if we have updated multiple files and don’t know
which lines to remove this command really helps`
◼ git restore --staged <file_name> #to revert changes from Staging area to working directory
◼ git reset HEAD~1 # to revert changes from local repo to working directory ◼ git restore <file>
5
VEERA NARESHIT
VEERA NARESHIT
#if The git reset HEAD~2 command moves the current branch backward by two commits
Note: if you can give git status or any git commit command everything clear no untracked file no staging
files
GitHub is an online software development platform. It's used for storing, tracking, and collaborating on
software projects. It makes it easy for developers to share code files and collaborate with fellow developers
on open-source projects.
-----git push-----
## git local to remote sync (how to add that locally created folder into git hub)
after creating repos locally and remote we have to give following commands
◼ git remote set-url origin git@[Link]:User/[Link]
◼ now it will sync we can start pull push commands
-----------------------------------------------------------------------------------------------
BRANCHES:
◼ git checkout -b dev : to create and switch from one branch to another.
7
VEERA NARESHIT
VEERA NARESHIT
Git marge will help to combine the changes from two or more branches into a single branch. Developers
will work on different branches to improve code or to develop the code after completion we can merge
them into a single version of the code.
Rebase is one of two Git utilities designed to integrate changes from one branch onto another.
Rebasing is the process of combining or moving a sequence of commits on top of a new base commit. Git
rebase is the linear process of merging.
With the "cherry-pick" command, we can pick individual commits from any branch into your current HEAD
branch.
Contrast this with the way commit integration normally works in Git: when performing a Merge or Rebase,
all commits from one branch are integrated.
The .gitignore file tells Git which files to ignore when committing your project to the GitHub repository.
gitignore is located in the root directory of your repo.
8
VEERA NARESHIT
VEERA NARESHIT
---------------------------------------------------------------------------------------------------------
TERRAFORM
-------------------------------------------------------------------------------------------------------
Terraform is an IAC tool, used primarily by DevOps teams to automate various infrastructure tasks. The
provisioning of cloud resources, for instance, is one of the main use cases of Terraform. It’s a cloud-
agnostic, open-source provisioning tool written in the Go language and created by HashiCorp.
Terraform allows you to describe your complete infrastructure in the form of code. Even if your servers
come from different providers such as AWS or Azure, Terraform helps you build and manage these
resources in parallel across providers. Think of Terraform as connective tissue and common language that
you can utilize to manage your entire IT stack.
IaC replaces standard operating procedures and manual effort required for IT resource management with
lines of code. Instead of manually configuring cloud nodes or physical hardware, IaC automates the process
infrastructure management through source code.
Here are several of the major key benefits of using an IaC solution like Terraform:
Speed and Simplicity. IaC eliminates manual processes, thereby accelerating the delivery and management
lifecycles. IaC makes it possible to spin up an entire infrastructure architecture by simply running a script.
Team Collaboration. Various team members can collaborate on IaC software in the same way they would
with regular application code through tools like Github. Code can be easily linked to issue tracking systems
for future use and reference.
9
VEERA NARESHIT
VEERA NARESHIT
Error Reduction. IaC minimizes the probability of errors or deviations when provisioning your infrastructure.
The code completely standardizes your setup, allowing applications to run smoothly and error-free without
the constant need for admin oversight.
Disaster Recovery. With IaC you can actually recover from disasters more rapidly. Because manually
constructed infrastructure needs to be manually rebuilt. But with IaC, you can usually just re-run scripts and
have the exact same software provisioned again.
Enhanced Security. IaC relies on automation that removes many security risks associated with human error.
When an IaC-based solution is installed correctly, the overall security of your computing architecture and
associated data improves massively. Install Terraform
Windows
projectname/
|-- [Link]
|-- [Link]
|-- [Link]
|-- [Link]
|-- [Link]
|-- [Link]
|-- [Link]
Terraform tutorials online often demonstrate a directory structure consisting of three files:
The issue with this structure is that most logic is stored in the single [Link] file which therefore becomes
pretty complex and long. Terraform, however, does not mandate this structure, it only requires a directory
of Terraform files. Since the filenames do not matter to Terraform I propose to use a structure that enables
users to quickly understand the code. Personally I prefer the following structure
10
VEERA NARESHIT
VEERA NARESHIT
### Version
Terraform Workspaces
“Terraform must store state about your managed infrastructure and configuration. This state is used by
Terraform to map real world resources to your configuration, keep track of metadata, and to improve
performance for large infrastructures. This state file is extremely important; it maps various resource
metadata to actual resource IDs so that Terraform knows what it is managing. This file must be saved and
distributed to anyone who might run Terraform.”
11
VEERA NARESHIT
VEERA NARESHIT
Remote State:
“By default, Terraform stores state locally in a file named [Link]. When working with Terraform in
a team, use of a local file makes Terraform usage complicated because each user must make sure they
always have the latest state data before running Terraform and make sure that nobody else runs Terraform
at the same time.”
“With remote state, Terraform writes the state data to a remote data store, which can then be shared
between all members of a team.”
State Lock:
“If supported by your backend, Terraform will lock your state for all operations that could write state.
This prevents others from acquiring the lock and potentially corrupting your state.”
“State locking happens automatically on all operations that could write state. You won’t see any message
that it is happening. If state locking fails, Terraform will not continue. You can disable state locking for most
commands with the -lock flag but it is not recommended.”
Copy and paste this configuration in your source code editor in your [Link] file.
region = "us-east-1"
## before that we have to create resource are s3 and dynamodb those resource will call in [Link]
Copy and paste this configuration in your source code editor in your [Link] file.
12
VEERA NARESHIT
VEERA NARESHIT
# S3 resource "aws_s3_bucket"
#Dynamodb
read_capacity = 20 write_capacity = 20
attribute {
name = "LockID"
type = "S"
Data source in terraform relates to resources but only it gives the information about an object rather than
creating one. It provides dynamic information about the entities we define outside of terraform.
Data Sources allow fetching data about the infrastructure components’ configuration. It allows to fetch data
from the cloud provider APIs using terraform scripts.
When we refer to a resource using a data source, it won’t create the resource. Instead, they get information
about that resource so that we can use it in further configuration if required.
For example, we will create an ec2 instance using a vpc and subnet, both of which are created on aws
console that is external to terraform configuration.
Step 1: Create a terraform directory and create a file named [Link] in it. Below code represents the
details of the aws provider that we’re using, like its region, access key and secret key.
13
VEERA NARESHIT
VEERA NARESHIT
#keys no need to configure here it will call from .aws folder from local
Step 2: In that directory, create another file named demo_datasource.tf and use the code given below.
id = vpc_id
= subnet_id
resource "aws_security_group" "sg" { # here we are creating security grop by calling exicting vpc so we can
use data source block name = "sg" vpc_id = data.aws_vpc.[Link]
ingress =[
cidr_blocks = [ "[Link]/0"]
= 22 protocol = "tcp"
security_groups = []
self = false
to_port = 22
egress = [
cidr_blocks = [ "[Link]/0"]
=0 protocol = "-1"
security_groups = []
self = false
to_port =0
[ data.aws_security_group.[Link] ] tags = {
In the above block of code, we are using a vpc and a subnet that is already created on AWS using its
console. Then using data block, which refers to data sources, that is, a vpc and a subnet. By doing this, we
are retrieving the information about the vpc and subnet that are created outside of terraform
configuration. Then creating a security group that uses vpc_id that was fetched using data block. Further
creating the EC2 instance that uses the subnet_id that was also fetched using data block.
So, in this example, data source is being used to get data about the vpc and subnet that were not created
using terraform script and using this data further for creating an EC2.
block
[ "amazon" ]
filter {
ami-hvm-*-gp2" ]
15
VEERA NARESHIT
VEERA NARESHIT
filter {
name = "root-device-type"
values = [ "ebs" ]
filter {
name = "virtualization-type"
values = [ "hvm" ]
filter {
name = "architecture"
values = [ "x86_64" ]
Terraform is a relatively new technology and adopting it to manage an organisation’s cloud resources might
take some time and effort. The lack of human resources and the steep learning curve involved in using
Terraform effectively causes teams to start using cloud infrastructure directly via their respective web
consoles.
For that matter, any kind of IaC method (CloudFormation, Azure ARM templates, Pulumi, etc.) requires
some training and real-time scenario handling experience. Things get especially complicated when dealing
with concepts like states and remote backends. In a worst case scenario, you can lose the [Link]
file. Luckily, you can use the import functionality to rebuild it.
Getting the pre-existing cloud resources under the Terraform management is facilitated by Terraform
import. import is a Terraform CLI command which is used to read real-world infrastructure and update the
state, so that future updates to the same set of infrastructure can be applied via IaC.
16
VEERA NARESHIT
VEERA NARESHIT
The import functionality helps update the state locally and it does not create the corresponding
configuration automatically. However, the Terraform team is working hard to improve this function in
upcoming releases.
Simple Import
With an understanding of why we need to import cloud resources, let us begin by importing a simple
resource – EC2 instance in AWS. I am assuming the Terraform installation and configuration of AWS
credentials in AWS CLI is already done locally. We will not go into the details of that in this tutorial. To
import a simple resource into Terraform, follow the below step-by-step guide.
Assuming the Terraform installation and configuration of AWS credentials in AWS CLI is already done locally,
begin by importing a simple resource—EC2 instance in AWS. For the sake of this tutorial, we will create an
EC2 resource manually to be imported. This could be an optional step if you already have a target resource
to be imported.
Go ahead and provision an EC2 instance in your AWS account. Here are the example details of the EC2
instance thus created:
Name: MyVM
Type: [Link]
The aim of this step is to import this EC2 instance into our Terraform configuration. In your desired path,
create `[Link]` and configure the AWS provider. The file should look like the one below.
// Provider configuration
terraform {
required_providers { aws = {
17
VEERA NARESHIT
VEERA NARESHIT
source = "hashicorp/aws"
= "us-east-1"
Run terraform init to initialize the Terraform modules. Below is the output of a successful initialization.
Terraform has created a lock file .[Link] to record the provider selections it
made above. Include this file in your version control repository so that Terraform can
guarantee to make the same selections by default when you run "terraform init" in the
future.
You may now begin working with Terraform. Try running "terraform plan" to see any
changes that are required for your infrastructure. All Terraform commands should
now work.
18
VEERA NARESHIT
VEERA NARESHIT
If you ever set or change modules or backend configuration for Terraform, rerun
this command to reinitialize your working directory. If you forget, other commands
As discussed earlier, Terraform import does not generate the configuration files by itself. Thus, you need to
create the corresponding configuration for the EC2 instance manually. This doesn’t need many arguments
as we will have to add or modify them when we import the EC2 instance into our state file.
However, if you don’t mind not seeing colorful output on CLI, you can begin adding all the arguments you
know. But this is not a foolproof approach, because normally the infrastructure you may have to import will
not have been created by you. So it is best to skip a few arguments anyway.
In a moment we will take a look at how to adjust our configuration to reflect the exact resource. For now,
append the [Link] file with EC2 config. For example, I have used the below config. The only reason I have
included ami and instance_type attribute, is that they are the required arguments for aws_instance
resource block.
4. Import
Think of it as if the cloud resource (EC2 instance) and its corresponding configuration were available in our
files. All that’s left to do is to map the two into our state file. We do that by running the import command as
follows.
import command:
19
VEERA NARESHIT
VEERA NARESHIT
0b9be609418aa0609]
Import successful!
The resources that were imported are shown above. These resources are now in your
The above command maps the aws_instance.myvm configuration to the EC2 instance using the ID. By
mapping I mean that the state file now “knows” the existence of the EC2 instance with the given ID. The
state file also contains information about each attribute of this EC2 instance, as it has fetched the same
using the import command.
Please notice that the directory now also contains [Link] file. This file was generated after the
import command was successfully run. Take a moment to go through the contents of this file.
Right now our configuration does not reflect all the attributes. Any attempt to plan/apply this configuration
will fail because we have not adjusted the values of its attributes. To close the gap in configuration files and
state files, run terraform plan and observe the output.
- timeouts {}
20
VEERA NARESHIT
VEERA NARESHIT
───────────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly
these actions if you run "terraform apply" now.
The plan indicates that it would attempt to replace the EC2 instance. But this goes completely against our
purpose. We could do it anyway by simply not caring about the existing resources, and creating new
resources using configuration.
The good news is that Terraform has taken note of the existence of an EC2 instance that is associated with
its state.
At this point, it is important to understand that the [Link] file is a vital piece of reference for
Terraform. All of its future operations are performed with consideration for this state file. You need to
investigate the state file and update your configuration accordingly so that there is a minimum difference
between them.
The use of the word “minimum” is intentional here. Right now, you need to focus on not replacing the given
EC2 instance, but rather aligning the configuration so that the replacement can be avoided. Eventually, you
would achieve a state of 0 difference.
Observe the plan output, and find all those attributes which cause the replacement. The plan output will
highlight the same. In our example, the only attribute that causes replacement is the AMI ID. Closing this
gap should avoid the replacement of the EC2 instance.
Change the value of ami from “unknown” to what is highlighted in the plan output, and run terraform plan
again. Notice the output.
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
~ update in-place
21
VEERA NARESHIT
VEERA NARESHIT
id = "i-0b9be609418aa0609"
~ tags ={
~ tags_all ={
- "Name" = "MyVM"
This time the plan does not indicate the replacement of the EC2 instance. If you get the same output, you
are successful in partially importing our cloud resource. You are currently in a state of lowered risk—if we
apply the configuration now, the resource will not be replaced, but a few attributes would change.
If we want to achieve a state of 0 difference, you need to align your resource block even more. The plan
output highlights the attribute changes using ~ sign. It also indicates the difference in the values. For
example, it highlights the change in the instance_type value from “[Link]” to “unknown”.
In other words, if the value of instance_type had been “[Link]”, Terraform would NOT have asked for a
change. Similarly, you can see there are changes to the tags highlighted as well. Let’s change the
configuration accordingly so that we close these gaps. The final aws_instance resource block should look as
follows:
= "ami-00f22f6155d6d92c5"
instance_type = "[Link]"
22
VEERA NARESHIT
VEERA NARESHIT
tags = {
"Name": "MyVM"
Terraform has compared your real infrastructure against your configuration and found no differences, so no
changes are needed.
If you have the same output, congratulations, as you have successfully imported a cloud resource into your
Terraform config. It is now possible to manage this configuration via Terraform directly, without any
surprises.
Meta-arguments in Terraform are special arguments that can be used with resource blocks and modules to
control their behavior or influence the infrastructure provisioning process. They provide additional
configuration options beyond the regular resource-specific arguments.
depends_on: Specifies dependencies between resources. It ensures that one resource is created or
updated before another resource.
count: Controls resource instantiation by setting the number of instances created based on a given
condition or variable.
for_each: Allows creating multiple instances of a resource based on a map or set of strings. Each
instance is created with its unique key-value pair. lifecycle: Defines lifecycle rules for managing
resource updates, replacements, and deletions.
provider: Specifies the provider configuration for a resource. It allows selecting a specific provider or
version for a resource.
provisioner: Specifies actions to be taken on a resource after creation, such as running scripts or executing
commands.
23
VEERA NARESHIT
VEERA NARESHIT
connection: Defines the connection details to a resource, enabling remote execution or file
transfers. variable: Declares input variables that can be provided during Terraform execution.
output: Declares output values that can be displayed after Terraform execution.
locals: Defines local values that can be used within the configuration files.
-----------------------------------------------------------------------
------------------------------------------------------------------------
Terraform has a feature of identifying resource dependency. This means that Terraform internally knows the
sequence in which the dependent resources needs to be created whereas the independent resources are
created parallelly.
But in some scenarios, some dependencies are there that cannot be automatically inferred by Terraform. In
these scenarios, a resource relies on some other resource’s behaviour but it doesn’t access any of the
resource’s data in arguments.
For those dependencies, we’ll use depends_on meta-argument to explicitly define the dependency.
depends_on meta-argument must be a list of references to other resources in the same calling resource.
Example-1
provider "aws" {
= "qwertyuiopasdfg"
24
VEERA NARESHIT
VEERA NARESHIT
Example-2
Version: "2012-10-17",
Statement: [
Action: "ec2:*",
Effect: "Allow",
Resource: "*"
})
"example_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = "examplerole"
25
VEERA NARESHIT
VEERA NARESHIT
Principal = {
Service = "[Link]"
},
})
= "example_policy_attachment" roles =
[aws_iam_role.example_role.name] policy_arn =
aws_iam_policy.example_policy.arn
"example_profile" role =
aws_iam_role.example_role.name
= var.image_id
--------------------------------------------------
----------------------------------------------------
In Terraform, a resource block actually configures only one infrastructure object by default. If we want
multiple resources with same configurations, we can define the count meta-argument. This will reduce the
overhead of duplicating the resource block that number of times.
count require a whole number and will then create that resource that number of times. To identify each of
them, we use the [Link] which is the index number corresponds to each resource. The index ranges
from 0 to count-1.
This argument is specified in resources as well as in modules (Terraform version 0.13+). Also, count meta-
argument cannot be used with for_each.
example:1
ami = "ami-0230bd60aa48260c6"
2 tags = {
# Name = "webec2"
Name = "webec2-${[Link]}"
example:2
variable "ami" {
type = string
default = "ami-0440d3b780d96b29d"
27
VEERA NARESHIT
VEERA NARESHIT
variable "instance_type" {
"[Link]"
variable "sandboxes" {
type = list(string)
= length([Link])
tags = {
Name = [Link][[Link]]
As specified in the count meta-argument, that the default behaviour of a resource is to create a single
infrastructure object which can be overridden by using count, but there is one more flexible way of doing
the same which is by using for_each meta argument.
The for_each meta argument accepts a map or set of strings. Terraform will create one instance of that
resource for each member of that map or set. To identify each member of the for_each block, we have 2
objects:
[Link]: The map key or set member corresponding to each member. [Link]: The
28
VEERA NARESHIT
VEERA NARESHIT
This argument is specified in resources (Terraform version 0.12.6) as well as in modules (Terraform version
0.13+)
## Example for_each
# [Link]
variable "ami" {
type = string
default = "ami-0078ef784b6fa1ba4"
= string
default = "[Link]"
"sandbox" {
ami = [Link]
instance_type = var.instance_type
------------------------------------------------------------------
-----------------------------------------------------------------
29
VEERA NARESHIT
VEERA NARESHIT
provider meta-argument specifies which provider to be used for a resource. This is useful when you are
using multiple providers which is usually used when you are creating multi-region resources. For
differentiating those providers, you use an alias field.
The resource then reference the same alias field of the provider as [Link] to tell which one to use.
Ex:
= "us-east-1" alias =
"america"
= "del-hyd-naresh-it"
= "del-hyd-naresh-it-test2" provider =
[Link]
The Terraform lifecycle is a nested configuration block within a resource block. The lifecycle metaargument
can be used to specify how Terraform should handle the creation, modification, and destruction of
resources. Meta-arguments are arguments used in resource blocks.
30
VEERA NARESHIT
VEERA NARESHIT
ami = "ami-0440d3b780d96b29d"
instance_type = "[Link]"
Name = "test"
lifecycle { create_before_destroy = true #this attribute will create the new object first and then destroy
the old one
# lifecycle {
# prevent_destroy = true #Terraform will error when it attempts to destroy a resource when this is set to
true:
#}
# lifecycle {
# ignore_changes = [tags,] #This means that Terraform will never update the object but will be able to
create or destroy it.
# }
Controlling the flow of Terraform operations is possible using the lifecycle meta-argument. This is useful in
scenarios when you need to protect items from getting changed or destroyed.
31
VEERA NARESHIT
VEERA NARESHIT
A common scenario that requires the use of a lifecycle meta-argument occurs when the Terraform provider
itself does not handle a change correctly and so can be safely ignored, rather than the provider attempting
to update an object necessarily. With the provider version updates, these “bugs” are slowly ironed out, at
which point the lifecycle meta-argument can be removed from the resource.
There are several attributes available for use with the lifecycle meta-argument: create_before_destroy:
When Terraform determines it needs to destroy an object and recreate it, the normal behavior will create
the new object after the existing one is destroyed. Using this attribute will create the new object first and
then destroy the old one. This can help reduce downtime. Some objects have restrictions that the use of
this setting may cause issues with, preventing objects from existing concurrently. Hence, it is important to
understand any resource constraints before using this option.
lifecycle { create_before_destroy
= true
prevent_destroy
This lifecycle option prevents Terraform from accidentally removing critical resources. This is useful to avoid
downtime when a change would result in the destruction and recreation of resource. This block should be
used only when necessary as it will make certain configuration changes impossible.
=========
lifecycle { prevent_destroy
= true
Terraform will error when it attempts to destroy a resource when this is set to true:
details...
Resource [resource_name] has lifecycle.prevent_destroy set, but the plan calls for this resource to be
destroyed. To avoid this error and continue with the plan, either disable lifecycle.prevent_destroy or reduce
the scope of the plan using the -target flag.
===========
##ignore_changes
32
VEERA NARESHIT
VEERA NARESHIT
The Terraform ignore_changes lifecycle option can be useful when attributes of a resource are updated
outside of Terraform.
It can be used, for example, when an Azure Policy automatically applies tags. When Terraform detects the
changes the Azure Policy has applied, it will ignore them and not attempt to modify the tag.
Attributes of the resource that need to be ignored can be specified.
lifecycle { ignore_changes
=[ tags["department"]
If all attributes are to be ignored, then the all keyword can be used. This means that Terraform will never
update the object but will be able to create or destroy it.
lifecycle { ignore_changes
=[
all
A local value assigns a name to an expressions so you can use the name multiple times within a module. It
is helpful to avoid repeating the same values or expressions multiple times in a configuration, but if
overused they can also make a configuration hard to read . Locals values are not set by the user input or
values in terraform files, instead, they are set ‘locally’ to the configuration .
Ex:
hydnaresh"
33
VEERA NARESHIT
VEERA NARESHIT
# bucket = "web-dev-bucket"
# bucket = "${[Link]}-${[Link]}-bucket-hyd"
bucket = [Link]-name
tags = {
# Name = "${[Link]}-${[Link]}-bucket-hyd"
Name = [Link]-name
Environment = [Link]
Terraform includes the concept of provisioners as a measure of pragmatism, knowing that there will always
be certain behaviors that can’t be directly represented in Terraform’s declarative model.
Provisioners can be used to model specific actions on the local machine or on a remote machine in order to
prepare servers or other infrastructure objects for service.
##File Provisioner:
The file provisioner is used to copy files or directories from the machine executing the terraform apply to
the newly created resource. The file provisioner can connect to the resource using either ssh or winrm
connections.
The file provisioner can upload a complete directory to the remote machine.
# ...
34
VEERA NARESHIT
VEERA NARESHIT
"conf/[Link]" destination =
"/etc/[Link]"
##local-exec Provisioner:
The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on
the machine running Terraform, not on the resource.
Basically, this provisioner is used when you want to perform some tasks onto your local machine where you
have installed the terraform. So local-exec provisioner is never used to perform any task on the remote
machine. It will always be used to perform local operations onto your local machine.
# ...
##remote-exec Provisioner:
As the name suggests remote-exec provisioner is always going to work on the remote machine. With the
help of this, you can specify the commands of shell scripts that want to execute on the remote machine.
The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to
run a configuration management tool, bootstrap into a cluster, etc. It requires a connection and supports
both ssh and winrm.
# ...
type = "ssh"
user = "ubuntu" # Replace with the appropriate username for your EC2 instance
35
VEERA NARESHIT
VEERA NARESHIT
# private_key = file("C:/Users/veerababu/.ssh/id_rsa")
= self.public_ip
provisioner "remote-exec" {
inline = [
"touch file200",
It can be used inside the Terraform resource object and in that case, it will be invoked once the resource is
created, or it can be used inside a null resource which is my preferred approach as it separates this non-
terraform behavior from the real terraform behavior.
module?
Terraform modules are reusable and encapsulated collections of Terraform configurations. They simplify
managing resources, making your Terraform code more manageable and scalable. Modules make defining,
configuring, and organizing resources modular and consistent while abstracting away their complexity to
make Terraform code more scalable and maintainable.
Using Terraform modules brings several advantages to your infrastructure provisioning process:
Reusability: Modules allow you to organize infrastructure resources and configurations into containers you
can repurpose across projects and environments. This reuse saves effort and reduces errors significantly.
Abstraction: Modules simplify resource creation and configuration processes for Terraform configuration
files, making them more concise and understandable.
36
VEERA NARESHIT
VEERA NARESHIT
Encapsulation: Modules isolate resources and their dependencies, making it more straightforward for you
to manage or modify individual pieces of your infrastructure without impacting others or hindering
modularity in its codebase. This improves its modularity.
Versioning: Terraform modules can be versioned, making it easier to track changes and update
dependencies in an orderly manner. This ensures that changes made do not cause unintended problems in
your infrastructure.
Collaboration: Modules allow your team and the wider community to work more collaboratively by sharing
them via Terraform Registry or private module repositories - encouraging best practices and standardizing
infrastructure configurations.
Creating an AWS Virtual Private Cloud (VPC) is fundamental for many infrastructure deployments. Instead
of defining the VPC configuration repeatedly, we can create a Terraform module for it.
modules/
vpc/
[Link]
[Link]
= { Name = [Link] }
In this module, we define an AWS VPC resource and allow customization of the VPC and name using input
variables.
37
VEERA NARESHIT
VEERA NARESHIT
38
VEERA NARESHIT
}
The [Link] file declares the input variables that can be set when the module is used.
"./modules/vpc"
= "my-vpc"
In the main Terraform configuration, we use the module block to include the VPC module. We specify the
module's source directory and provide values for the input variables.
Now, you can easily create multiple VPCs with different configurations by reusing this module.
Creating Amazon Elastic Compute Cloud (EC2) instances is another common task in AWS. Let's create a
Terraform module for EC2 instances.
modules/
ec2/
[Link]
[Link]
39
ami = [Link] instance_type
= var.instance_type subnet_id =
var.subnet_id key_name =
var.key_name
tags = {
Name = [Link]
In this module, we define an AWS EC2 instance resource and allow customization of the AMI, instance type,
subnet, key name, and name using input variables.
EC2 instance."
the module.
"./modules/ec2" ami =
"ami-12345678" instance_type =
01234567" key_name =
"my-key-pair" name =
"my-ec2-instance"
In the main Terraform configuration, we use the module block to include the EC2 instance module. We
specify the module's source directory and provide values for the input variables.
With this module, you can easily create EC2 instances with different configurations across your
infrastructure.
These examples demonstrate how Terraform modules promote code reuse, abstraction, and encapsulation.
By following similar patterns, you can create modules for various infrastructure components, including
databases, load balancers, and networking resources.
◼ [Link]
◼ #for my github reference here root_module is source reference
Connection Block
41
You can create one or more connection blocks that describe how to access the remote resource. One use
case for providing multiple connections is to have an initial provisioner connect as the root user to set up
user accounts and then have subsequent provisioners connect as a user with more limited permissions.
Connection blocks don't take a block label and can be nested within either a resource or a provisioner.
A connection block nested directly within a resource affects all of that resource's provisioners.
Example: connection {
type = "ssh"
user = "ubuntu" # Replace with the appropriate username for your EC2 instance
# private_key = file("C:/Users/veerababu/.ssh/id_rsa")
= self.public_ip
Output values make information about your infrastructure available on the command line, and can expose
information for other Terraform configurations to use.
Example:
aws_instance.test.public_ip sensitive
= true
output "instance_id"{
value = aws_instance.[Link]
aws_instance.test.public_dns output
42
}
"instance_arn" { value =
aws_instance.[Link]
pipeline {
agent any
stages {
stage('clone') {
steps {
stage('init') {
steps { sh
'terraform init'
stage('apply') {
steps {
43
MAVEN
What is Maven?
Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and
manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by The
Apache Software Foundation.
############################################### mavnen
advantages
Foundadtion.
################################################
44
We can se folder structure
my-app |--
[Link]
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- [Link]
`-- test
`-- java
`-- com
`-- mycompany
`-- app
|-- target
target folder Note: after run the goals we can see jar/war file in target folder
TYPES OF ARTIFACTS:
JAVA : MAVEN
PYTHON : GRADLE
45
.NET : VS CODE
C, C# : MAKE FILE
Ex:
#######Folder structure########
[Link] FILE:
[Link] - Maven configuration file. Controls the build process for the project
this file will have complete info of the project. Ex: Name, Tools, Version,
Snapshot, Dependencies.
Note: if we want to pass any goals this file must be on project folder.
without this file maven will not pass any goals. each project we need to
have only one [Link] multiple project cant use same [Link].
1. dpendencies
Dependencies : responsible for downloading the required third party drivers libraries from remote to local
46
--mvn archetype:generate # to generate and see the sample templates
[Link]
<html>
<head>
<title>Login Form</title>
</head>
<body>
<tr>
<td>UserName</td>
</tr>
47
<tr>
<td>Password</td>
</tr>
</table>
</body>
</html>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
48
-------############# JENKINS CICD #############---------------
what is CICD?
In software engineering, CI/CD or CICD is the combined practices of continuous integration and continuous
delivery or, less often, continuous deployment. They are sometimes referred to collectively as continuous
development or continuous software development
Why Jenkins?
Jenkins is an open-source automation tool for Continuous Integration (CI) and Continuous Deployment
(CD).
It is easy to install.
It has 1000+ plugins to ease your work. If a plugin does not exist, you can code it and share it with the
community.
It is free of cost.
It is built with Java and hence, it is portable to all the major platforms.
renamed as jenins.
49
jenkins runs on 8080 port
=================================
◼ if wget command not [Link] install the wget software using this command "yum install wget
-y"
◼ sudo systemctl status jenkins --> check the status of jenkins service
TO CONNECT:
◼ public_ip:8080 (browser)
◼ cat /var/lib/jenkins/secrets/initialAdminPassword (server)
---paste password on browser -- > installing plugins --- > user details -- > start
◼ Default: /var/lib/jenkins/workspace
=================================================
50
Process for Amazon linux 2 AMI type
==========================================
◼ command : sudo su –
◼ Provide the password
------------------------------------------------------------- ---------------------------Triggers-------------------------
In Jenkins, a build trigger is a mechanism that initiates the execution of a Jenkins job or pipeline. Triggers
are used to start builds automatically based on various events or conditions. There are several ways to
trigger builds in Jenkins:
Build Now: You can manually trigger a build at any time by clicking the “Build Now” or “Build” button on the
Jenkins dashboard for a specific job. This is useful for ad-hoc or on-demand builds.
Build Periodically: You can schedule builds to run at specific intervals using the “Build periodically” option in
the job configuration. You can use cron syntax to define the schedule. For example, to run a build every
night at 2:00 AM, you can use the cron expression 0 2 * * *.
Poll SCM: If you’re using a version control system (e.g., Git, Subversion), you can configure the job to poll
the repository for changes. When changes are detected, Jenkins will automatically trigger a build.
This is known as the “Poll SCM” build trigger.
Webhooks Trigger:
51
Webhooks: Many version control systems and external services support webhooks, which allow them to
notify Jenkins when code changes occur. Jenkins can listen for these webhook notifications and trigger
builds in response.
Build after other projects are built: You can configure a job to be triggered after one or more upstream jobs
have been built successfully. This is useful for creating build pipelines or ensuring that certain prerequisites
are met before starting a build.
Job1------job2-----job3
downstream is job2
--so whenever jb1 triggers after that job2 will trigeere agter that job3 will trigger
--if any one downstream job fail means necxt upstream job will not success
--Trigger builds with parameters: You can set up parameterized builds where a build is triggered with
specific parameter values. This is useful for customizing builds based on input parameters.
Pipeline Trigger:
Pipeline Trigger: If you’re using Jenkins Pipelines (defined in a Jenkinsfile), you can use various triggers
within the pipeline script itself. For example, you can set up a webhook trigger, a schedule trigger, or a
manual input trigger as part of your pipeline script.
Jenkins has numerous plugins available that can provide additional trigger mechanisms. For example, the
“GitHub Webhook” plugin allows Jenkins to listen to GitHub events and trigger builds accordingly.
The choice of build trigger method depends on your specific use case and requirements.
-----------------pipeline types----------------
1. Declaritive
2. Groovy scripted
------------------------------------------------
52
Declaritive
---------------------------------------------------
Basic Example:
(PASSS:shortcut)
----------------------
single stage:example
-----------------------------------
stages { stage('first') {
first stage'
-------------------------- multi
stage example:
-------------------------
pipeline { agent
any
stages { stage('first') {
first stage'
53
stage('second') { steps {
'[Link]
stage('clean') {
steps { sh
"mvn clean"
stage('install') {
steps {
sh "mvn install"
example-2
54
pipeline {
agent any
'[Link]
package'
---------------------------------------------------------------------------
Groovy
---------------------------------------------------------
stage{"first") {
sh file-1
stage{"second") {
sh file-2
node {
55
stage('git') { git branch: 'main', credentialsId:
'terraform', url:
'[Link]
stage('clean') {
sh "mvn clean"
stage('install') {
sh "mvn install"
yum-config-manager --add-repo
[Link]
------------------------------
----declaritive--------
56
pipeline {
agent any
'[Link]
stage('init') { steps {
stage('plan') {
steps { sh
"terraform plan"
stage('action') {
steps {
------------------------groovy-----------------
'[Link]
57
}
stage('init') { sh "terraform
init -reconfigure"
stage('action') { sh "terraform
${action} --auto-approve"
----------------------------------------------- approach 3:
Note:
#we can run job by calling above jenkins file in git hub
- To Implement
◼ give GITHUB url and in defination section we need to "sleect pipeline script from scm" option and
build the job
#Jenkins file
pipeline {
agent any
stages {
stage('Checkout Code') {
58
steps {
checkout scm
stage ("plan") {
steps { sh
('terraform plan')
steps {
[Link]
59
In ec2instance:
Apply>Save
Build it
----------------------------------------------------------------------------------------
JENKINS BACKUP
Goto Manage Jenkins >Scroll down to Tools and Actions and click on ThinBackup>Settings
Save
ec2instance cd
/var/lib/jenkins/backup
ls
jenkins log in jenkins again and we see that pipeline job is restored
60
----------------------------------------------------------------------------------------
jenkins login jenkins browser again, we see its updated to 2.453 -------------------------
---------CD----------------------------------------------------
◼ After builld the war file we have to deploy in any application serever like "TOMCAT"
◼ Create one Ec2 instance if (amazon linux 2) ----note : if you choose linux 2023 commands
will different)
for java 11
---for java 17
----Install tomcat
61
◼ custom manager app tomcat --------find / -name [Link] ----after finding the file need
to open with vi
<!-- -->
◼ <!--<Valve className="[Link]"
◼ allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
------------------------------------------------------
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
-----------------------------------------------------
-----step-1 need to configure Maven and jenkins paths in global tool configuration
◼ manaje jenkins---> tools---> add path for both java and maven
------step-3:need to configure credentials --username : deployer password deployer :--- reference from (
<user username="deployer" password="deployer" roles="manager-script"/>)
◼ Dashboard ---> Manage Jenkins -- > Credentials --- > System ---> Global credentials (unrestricted) --
> add credentials
-----step-4 create maven job and give git url for source code to build
-------step-6 In postbuild actions select war/ear file to give path of War --path **/*.war
-------step-7 we need to add container (tomcat) we have to give configured credentials of tomcat and give
url of tomcat webhook [Link]
"CI/CD----Maven job---Javaproject"
####pipelinejob:example
pipeline {
agent any
'[Link]
stage('clean') {
steps { sh 'mvn
clean'
63
stage('test') {
steps { sh
'mvn test' }
stage('install') {
steps { sh 'mvn
install'
stage('deployment') {
steps {
-----------------------------------------------------------------------------------------
64
=================================================================================
==============================
=================================================================================
=======================
MICRO SERVICES: Microservices are deployed using VM or Containers. Containers are the preferred
deployment route for microservices as containers are lighter, portable, and modular. The microservice code
is packaged into a container image and deployed as a container service. multiple services are deployed on
multiple servers with multiple databases.
note: if number odf application are more will go with microservices concept (docker and kubernetes)
65
FLEXIBLE
COST
MAINTAINANCE
EASY CONTROL
Docker is an open platform for developing, shipping, and running applications. Docker enables you to
separate your applications from your infrastructure so you can deliver software quickly. With Docker, you
can manage your infrastructure in the same ways you manage your applications. By taking advantage of
Docker's methodologies for shipping, testing, and deploying code, you can significantly reduce the delay
between writing code and running it in production.
DOCKER IMAGE:
Docker images are read-only templates that contain instructions for creating a container. A Docker image is
a snapshot or blueprint of the libraries and dependencies required inside a container for an application to
run
CONTAINERS:
A Docker container image is a lightweight, standalone, executable package of software that includes
everything needed to run an application: code, runtime its same as a server/vm. it will not have any
operating system. (Ec2 server=AMI, CONTAINER=IMAGE) os will be maanged in image
66
sudo yum install docker -y # If linux 20203
Note : By default Docker works with the root user and other users can only access to Docker with sudo
commands. However, we can bypass the sudo commands by creating a new group with the name docker
and add ec2_user.
newgrp docker
sudo chmod 666 /var/run/[Link] # to give access docker demon to run docker server
#If you want to see an extended version of the version details, such as the API version, Go version, and
Engine Version, use the version command without dashes. give below command
docker version
# ----commands:---- #
67
---to pull base images from public docker repository
## To login container ##
[Link] you want to come out from connainer without stop give "ctrl+pq"
ps -ef --to know how many processors runing if it is in vm many process we can see but it is in
container onle few becuase its light weight
ps -ef | wc -l #to know number of processors request running backend #### how to start
container#####
or
or
68
◼ docker inspect cont_name: to get complete info of a container
## danger commands##
=================================================================================
=================================================================================
Dockerfile is a simple text file that consists of instructions to build Docker images.
-----------------------------------
69
◼ docker build -t check .
◼ docker run -p <HOST_PORT>:<CONTAINER:PORT> IMAGE_NAME
◼ docker run -dit --name demo -p 8080:80 check
ex: docker run -dt -p 3000:3000 --name project-2 saturday
FROM httpd:2.4
COPY . /public-html/ /usr/local/apache2/htdocs/ # copy present directory files into destination patha tha is
/usr/local/apache2/htdocs/
RUN apt-get -y install apache2 #by using RUN instrcution install apache2
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] #Run apache2 srever while running container
from image Note:
-D FOREGROUND This is not a docker command this is Apache server argument which is used to run the
web server in the background. If we do not use this argument the server will start and then it will stop
FROM centos:7
EXPOSE 80
#httpdserver
# IMAGE PUSH #
======================================================================
70
######### docker push to Docker private repository #########
================================================================
◼ Retrieve an authentication token and authenticate your Docker client to your registry.
◼ aws ecr get-login-password --region ap-south-1 | docker login --username AWS --passwordstdin
[Link]
---Note: If you receive an error using the AWS CLI, make sure that you have the latest version of the AWS CLI
and Docker installed.
----Build your Docker image using the following command. For information on building a Docker file from
scratch see the instructions here . You can skip this step if your image is already built:
-----After the build completes, tag your image so you can push the image to this repository:
=================================================================================
=================================================================================
# scenario :1 single stage after run mavne manually, deploy war file on tomcat webapp by using docker file
FROM tomcat:latest
# scenario :2 multi satge --run Maven Image by using docker file and deploy on tomcat webapp
71
WORKDIR /app
COPY . .
FROM tomcat:latest
ARG MAVEN_VERSION=3.9.6
mv apache-maven-${MAVEN_VERSION} /usr/lib/maven
ENV PATH=$MAVEN_HOME/bin:$PATH
COPY . /app
WORKDIR /app
FROM tomcat:latest
72
====================================================
====================================================
FROM centos:centos7
------note: condition:1 after run command docker run image ----it will install httpd
------note: condition -2 if we run docker run image yum install -y git it will overwrite new command it will
download git only and ignores httpd
-------but i need only allow required attribute change like httpd, git ex : docker run image httpd or git
instead of docker run image yum install -y httpd or git
##### Entrypoint
FROM centos:centos7
ENTRYPOINT ["yum", "install", "-y", "git"] note: condition:1 after run command
docker run image ----it will install git condition -2 if we run docker run image
FROM centos:centos7
CMD ["git"]
73
######## Docker network ##############
bridge
host
none
◼ docker run -dt --name container1 ubuntu
◼ docker network ls
create two containers
container1
container2
◼ docker inspect <containername>
----container1: [Link]
----container2: [Link]
login to container
◼ docker exec -it <containernmae> /bin/bash
##--- need to install ping libraries by using below command (if ping command will not work)
◼ apt-get update -y
◼ apt install iputils-ping
# host type
Docker network host, also known as Docker host networking, is a networking mode in which a Docker
container shares its network namespace with the host machine. The application inside the container can be
accessed using a port at the host's IP address
#None type none network in docker means when you don't want any network interface for your
container. If you want to completely disable the networking on a container, you can use the --network
none flag when starting the container.
◼ docker network create <networkname> # to maitain private connection one container from
another container
########################################################## ---------------Docker
volumes ----------
Volumes are a mechanism for storing data outside containers. All volumes are managed by Docker and
stored in a dedicated directory on your host, usually /var/lib/docker/volumes for Linux systems.
volume path
cd /var/lib/docker/volumes
- - - - - - - -- - - - - - - - -- - - - - -- - -
docker-compose version
services: mydb:
environment:
MYSQL_ROOT_PASSWORD: test
image: wordpress
links:
- mydb:site ports:
[Link]
75
◼ docker-compose -f [Link] up ###need to give this command for custom file name
◼ docker-compose up -----#to run docker compose
◼ docker-compose ps -----#to see containers status ########### list of commands on Docker
◼ docker-compose config
docker-compose rm
2. Docker installed on Jenkins instance. Click here to for integrating Docker and Jenkins
4. Repo created in ECR, Click here to know how to do that. plugins :: docker ,ecr
76
Jenkins pipeline to automate the following:
- Automating builds
options {
skipStagesAfterUnstable()
stages { stage('Clone
repository') {
steps {
stage('maven install') {
steps {
sh 'mvn install'
stage('Build') { steps {
system prune -a
'''
77
stage('Deploy') {
steps { sh '''
aws ecr get-login-password --region us-east-1 | docker login --username AWS --passwordstdin
[Link] docker tag test:latest
[Link]/test2:latest docker push
[Link]/test2:latest
'''
---------------------------------- Kubernetes----------------------------
Kubernetes is a container orchestration system that was initially designed by Google to help scale
containerized applications in the cloud. Kubernetes can manage the lifecycle of containers, creating and
destroying them depending on the needs of the application, as well as providing a host of other features.
Kubernetes has become one of the most discussed concepts in cloud-based application development, and
the rise of Kubernetes signals a shift in the way that applications are developed and deployed. In general,
Kubernetes is formed by a cluster of servers, called Nodes, each running Kubernetes agent processes and
communicating with one another. The Master Node contains a collection of processes called the control
plane that helps enact and maintain the desired state of the Kubernetes cluster, while
Worker Nodes are responsible for running the containers that form your applications and services.
78
Kubernetes control plane—manages Kubernetes clusters and the workloads running on them. Include
components like the API Server, Scheduler, and Controller Manager.
Kubernetes Workernode that can run containerized workloads. Each node is managed by the kubelet, an
agent that receives commands from the control plane. --
==============================================================
Provides an API that serves as the front end of a Kubernetes control plane. It is responsible for handling
external and internal requests—determining whether a request is valid and then processing it. The API can
be accessed via the kubectl command-line interface or other tools like kubeadm, and via REST calls.
## Scheduler :------
This component is responsible for scheduling pods on specific nodes according to automated workflows
and user defined conditions, which can include resource requests
## etcd :-----
A key-value database that contains data about your cluster state and configuration. Etcd is fault tolerant
and distributed.
## Controller:-----
It receives information about the current state of the cluster and objects within it, and sends instructions to
move the cluster towards the cluster operator’s desired state.
==================================================================== ##
## kubelet: ----
Each node contains a kubelet, which is a small application that can communicate with the Kubernetes
control plane. The kubelet is responsible for ensuring that containers specified in pod configuration are
running on a specific node, and manages their lifecycle.. It executes the actions commanded by your
control plane
79
All compute nodes contain kube-proxy, a network proxy that facilitates Kubernetes networking services. It
handles all network communications outside and inside the cluster, forwarding traffic or replying on the
packet filtering layer of the operating system.
Each node comes with a container runtime engine, which is responsible for running containers. Docker is a
popular container runtime engine, but Kubernetes supports other runtimes that are compliant with Open
Container Initiative, including CRI-O and rkt.
===============================================================================
#Nodes: ----
Nodes are physical or virtual machines that can run pods as part of a Kubernetes cluster. A cluster can scale
up to 5000 nodes. To scale a cluster’s capacity, you can add more nodes.
#POD:--------
Pods—pods are the smallest unit provided by Kubernetes to manage containerized workloads. A pod
typically includes several containers, which together form a functional unit or microservice.
================================================================================= What is
Minikube?
Minikube is a tool that sets up a Kubernetes environment on a local PC or laptop minikube quickly
sets up a local Kubernetes cluster on macOS, Linux, and Windows. We proudly focus on helping
application developers and new Kubernetes users. ---Installation Process Minikube ---
kubectl is the Kubernetes-specific command line tool that lets you communicate and control
Kubernetes clusters. Whether you're creating, managing, or deleting resources on your Kubernetes
platform, kubectl is an essential tool. ----------kubectl Installation-----------
-----EKSCTL installation----
80
"[Link]
s)_amd64.[Link]" | tar xz -C /tmp
◼ sudo mv /tmp/eksctl /usr/local/bin
`Note: create IAM user with programmatic access if your bootstrap system is outside of AWS`
IAM
EC2
VPC
CloudFormation
name \
--region region-name \
--node-type instance-type \
--nodes-min 2 \
--nodes-max 2 \
--zones <AZ-1>,<AZ-2>
example:
--region ap-south-1 \
--node-type [Link] \
81
◼ kubectl run pod --image nginx
---[Link]---
apiVersion: v1 kind:
Pod metadata:
app: webapp
containers:
- name: nginx-container
image: nginx
==================================================================== #Kuberentes
Service#:
-Kubernetes, a Service is a method for exposing a network application that is running as one or more
Pods in your cluster.
As indicated by its name, this is just an address that can be used inside the cluster.
◼ NodePort: A NodePort differs from the ClusterIP in the sense that it exposes a port in each Node.
◼ LoadBlancer :This service type creates load balancers in various Cloud providers like AWS, GCP,
Azure, etc., to expose our application to the Internet.
#Port: the port on which the service is exposed. Other pods can communicate with it via this port.
82
#TargetPort: the actual port on which your container is deployed. The service sends requests to this port
and the pod container must listen to the same port.
#NodePort: exposes a service externally to the cluster. So the application can be accessed via this port
externally. By default, it’s automatically assigned during deployment.
================================================================================
=========================================================================== #
◼ The Kubernetes Metrics Server is an aggregator of resource usage data in your cluster, and it isn't
deployed by default in Amazon EKS clusters.
◼ we need to deploy by following process
83
-----------------------------------ingress------------------------- What is an
Ingress?
In Kubernetes, an Ingress is an object that allows access to your Kubernetes services from outside
the Kubernetes cluster. You configure access by creating a collection of rules that define which
inbound connections reach which services.
------we are able to seec load blancer link and acces by giving path along
# Role
==============================================================
developer-role rules:
84
- apiGroups: [""] # "" indicates the core API group
["get", "list"]
=================================================
==========================================
RoleBinding metadata:
"developer" apiGroup:
[Link] roleRef:
apiGroup: [Link]
- userarn:
arn:aws:iam::992382358200:user/eks username:
=========================================================
85
◼ kubectl get rb
◼ kubectl get rolebinding
◼ kubectl api-resources
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be #
reopened with the relevant failures. ----for example below reference ------ apiVersion:
v1 data:
mapRoles: |
- groups:
- system:bootstrappers - system:nodes
rolearn: arn:aws:iam::992382358200:role/eksctl-naresh-nodegroup-ng-
bbb93edNodeInstanceRole-9GWNpfucPXRt username:
system:node:{{EC2PrivateDNSName}} mapUsers: |
- userarn:
arn:aws:iam::992382358200:user/eks
username: eks
groups:
- developer
creationTimestamp: "2024-03-22T02:22:59Z"
4108-8653-690369d98c4f
==================================================================== ##
schedulers ######################
## schedule ##
86
Scheduling overview
◼ A scheduler watches for newly created Pods that have no Node assigned. For every Pod that the
scheduler discovers, the scheduler becomes responsible for finding the best Node for that Pod to
run on. The scheduler reaches this placement decision taking into account the scheduling principles
described below.
1. Node Selector
2. Nodeaffinity
3. Daemonset
[Link]
NodeSelector is the simplest recommended form of node selection constraint. You can add the
nodeSelector field to your Pod specification and specify the node labels you want the target node to have.
Kubernetes only schedules the Pod onto nodes that have each of the labels you specify.
Example:
# to unlabel
# to list
87
apiVersion: v1
kind: Pod
metadata: name:
myapp labels:
app: webapp
type: front-end
spec: containers:
- name: nginx-container
image: nginx
nodeSelector: size:
Large
→ if my pod label is not matching it will not schedule on any node always trying to schedule on labeld
node only otherwise it will not scheduled
===============================================================================
[Link] affinity
→ Node affinity is conceptually similar to nodeSelector, allowing you to constrain which nodes your
Pod can be scheduled on based on node labels. There are two types of node affinity:
[Link]
***it will schedule if matches the pod and node label only otherwise it will not schedule
affinity: nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
88
- key: disktype
operator: In values:
- ssd containers: -
imagePullPolicy: IfNotPresent
[Link]:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec: affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions: - key:
disktype operator: In
values:
- ssd containers: -
imagePullPolicy: IfNotPresent
======================================================================
3. Daemonset:
◼ A Daemonset is another controller that manages pods like Deployments, ReplicaSets, and
StatefulSets. It was created for one particular purpose: ensuring that the pods it manages to run on
all the cluster [Link] is going to schedule all available nodes
89
ex : if we have three nodes same pod is going to schedule on three nodes
apiVersion: apps/v1
kind: DaemonSet
metadata: name:
nginx spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec: containers:
- name: test-nginx
image: nginx
ports:
- containerPort: 8080
resources: limits:
cpu: 100m
memory: 200Mi
requests: cpu:
50m memory:
100Mi
========================================================
◼ Taints are the opposite – they allow a node to repel a set of pods. Tolerations are applied to pods.
Tolerations allow the scheduler to schedule pods with matching taints.
Two types:
[Link]:
90
[Link]
imagePullPolicy: IfNotPresent
effect: "NoSchedule"
IMP Note:
*Toleration pod only create into specfic tainted node if labels match
*The taint effect defines how a tainted node reacts to a pod without appropriate toleration. It must be one
of the following effects;
*NoSchedule—The pod will not get scheduled to the node without a matching toleration. (willnot schedule
new pods on tainted node but runinng pods will not delete also after enable taint to nodes)
91
*NoExecute—This will immediately evict all the pods without the matching toleration from the node
(no new pods will schedule will and also delete runinng pods also after enable taint to nodes)
===============================================volume============================ What is
a Kubernetes volume?
A Kubernetes volume is a directory containing data accessible to containers in a given pod, the
Within the Kubernetes container orchestration and management platform, volumes provide a
plugin mechanism that connects ephemeral containers with persistent data storage.
When a pod with a unique identification is deleted, the volume associated with it is destroyed.
If a pod is deleted but replaced with an identical pod, a new and identical volume is also created.
Step - 1
b) Install helm in your local machine. Below is the link -> [Link]
1. Static hots path (pv,pvc,deployment) not recomanded if node will delete volumes also delete
2. Static cloud ebs(pv,pvc,deployment) static approach no effect even node will be deleted still volum
persistent but ec2 volume need to create manually
3. Dynamic volume provision create by Storage class (pvc,storage class) here pv is going to create by
Storage class
92
-----helm install-----
◼ [Link]
◼ wget [Link]
◼ mv linux-amd64/helm /usr/local/bin/helm
◼ helm version
#Note: after create cluster we have to give Iam ec2 full access or admin access to node group IAM role then
only ebs volume able to create by node
Step - 2
#ReadWriteMany#
If you need to write to the volume, and you may have multiple Pods needing to write to the volume
where you'd prefer the flexibility of those Pods being scheduled to different nodes, and
ReadWriteMany is an option given the volume plugin for your K8s cluster, use ReadWriteMany.
#ReadWriteOnce#
93
If you need to write to the volume but either you don't have the requirement that multiple pods should be
able to write to it, or ReadWriteMany simply isn't an available option for you, use ReadWriteOnce.
#ReadOnlyMany
If you only need to read from the volume, and you may have multiple Pods needing to read from the
volume where you'd prefer the flexibility of those Pods being scheduled to different nodes, and
ReadOnlyMany is an option given the volume plugin for your K8s cluster, use ReadOnlyMany.
If you only need to read from the volume but either you don't have the requirement that multiple pods
should be able to read from it, or ReadOnlyMany simply isn't an available option for you, use
ReadWriteOnce.
In this case, you want the volume to be read-only but the limitations of your volume plugin have forced you
to choose ReadWriteOnce (there's no ReadOnlyOnce option). As a good practice, consider the
[Link]
=================================================================================
=======================================
#PVC recalim policies reclaim policy : delete # this is defualt one ,which means when ever pvc deleted
pv will delete automatically cretaed by storage class but ebs volume will not delete if you want to
delete delete it manually
=============================================================================
94
For more details:
◼ [Link]
in-eks-cluster-22caee18a872
◼
---------------------------------------HELM Charts--------------------------------------------
Helm is a tool that automates the creation, packaging, configuration, and deployment of Kubernetes
applications by combining your configuration files into a single reusable package.
-----helm install-----
◼ [Link]
◼ wget [Link]
◼ tar -zxvf [Link]
◼ mv linux-amd64/helm /usr/local/bin/helm
◼ chmod 777 /usr/local/bin/helm # give permissions
◼ helm version
1 helloworld
2├── charts
3├── [Link]
4├── templates
5│ ├── [Link]
6│ ├── _helpers.tpl
7│ ├── [Link]
8│ ├── [Link]
95
9│ ├── [Link]
14└── [Link]
◼ [Link]
world40c05fa4ac5a
◼
---------------------------------------------------
-------------ArgoCD--------------------
Argo CD is a declarative continuous delivery tool for Kubernetes. It can be used as a standalone tool or as a
part of your CI/CD workflow to deliver needed resources to your clusters.
-------Install process
#access it nodeIp:Nodeport
◼ kubectl edit secret argocd-initial-admin-secret -n argocd # to get intial credential to login argocd
◼ echo bnFabGx3emtCNjB5dFZQSA== | base64 --decode ##For more details:
◼ [Link]
practicalexample-f4a9a8488cf9
◼
=================================================================
#Statefulset
Manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering
and uniqueness of these Pods.
Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec.
Unlike a Deployment, a StatefulSet maintains a sticky identity for each of its Pods. These pods are
created from the same spec, but are not interchangeable: each has a persistent identifier that it
maintains across any rescheduling.
Using StatefulSets :
StatefulSets are valuable for applications that require one or more of the following.
◼ [Link] ◼
========================================================
97
#Headless service
◼ A Headless Service is a variation of the ClusterIP Service, where the clusterIP field is set to None.
Unlike traditional services, Headless Services do not use a single Service IP to proxy connections to
the Pods. Instead, they allow you to directly connect to Pods without any load balancing
intermediary.
◼ Use Cases for Headless Services
◼ Headless Services are particularly useful in the following scenarios:
◼ Service Discovery: Some service discovery mechanisms, such as Kubernetes DNS-based service
discovery, require direct access to individual Pods. Headless Services provide a convenient way to
achieve this.
◼ Stateful Applications: Applications that require direct access to individual Pods, such as databases
or distributed storage systems, can benefit from using Headless Services.
◼ Custom Load Balancing: If you need to implement custom load balancing logic or use a specific load
balancing mechanism, Headless Services allow you to directly access the Pods without relying on
Kubernetes' built-in load balancing. -----Accessing Pods using Headless Services -------example
through DNS name:
◼ Once you have created a Headless Service, you can access the Pods directly using their DNS names
or IP addresses.
◼ The DNS name for each Pod follows the pattern <pod-ip>.<namespace>.[Link].
##For example, if you have a Pod with the IP address [Link] in the default namespace, you can
access it using the DNS name like below
◼ [Link]. -----dns name Also
Headless Services in Kubernetes offer a unique approach to accessing individual Pods directly,
without relying on a load balancer or a single Service IP. This type of service is invaluable in
scenarios where direct Pod access is required, such as service discovery mechanisms, stateful
applications like databases, or when implementing custom load balancing logic.
By setting the clusterIP field to None, Headless Services bypass the traditional load balancing layer
and instead provide a direct connection to individual Pods. This is achieved through the assignment
of DNS records for each Pod, allowing you to access them by their DNS names or IP addresses.
◼ kubectl run -i --tty --image busybox:1.28 dns-validate # create pod and access servcice from
pod by using nslookup.
◼ example : nslookup<servcie name> nslookup <cluster ip enables servcie name> Server:
[Link]
Address 1: [Link] [Link] you will
98
Name: mysql
◼ [Link]
◼
Note:
pleae click below git hub link for all yml files
◼ [Link]
***************************SonarQube**********************
Code Quality check tool
SonarQube is a very popular code quality management tool that is used widely for code analysis to identify
code smells, possible bugs, and performance enhancements. SonarQube supports many popular
programming languages like Java, JavaScript, C#, Python, Kotlin, Scala etc. It also provides test and code
coverage.
Benfits:
Improve quality.
Reduce risk(vulnerabilities).
Code vulnerabilities: Code vulnerabilities are software flaws that open opportunities for potential
application misuse, exploits, or breaches that result in sensitive information disclosure, data leaks,
ransomware attacks, and other cyber security issues.
-----------------
threecompontes
------>sonarquber server
STEUP:
dependency: java11 or 17
100
# sonarqube has to run with user only
◼ sh /opt/sonarqube-8.9.6.50800/bin/linux-x86-64/[Link] start
◼ sh /opt/sonarqube-8.9.6.50800/bin/linux-x86-64/[Link] status
---------------------------------------------------------------------
sonarQube on Docker
--------------------------------------------------------------------
prerequistes :
101
create token in sonarqube ---->after login go to adminstration---security--user--create token
step-1
--Required plugins--
step-2 open your sonarqube—generate the token go to credential and select secret text mode paste you
token and give name go to system configurations paste url and select key and give name ex: 'SonarQube'
(note: this name only we have to call like withSonarQubeEnv('SonarQube'))
step-3
--create jenkins job and paste below script mavne and sonar
--passing with envvironment varaibles without hards coded values (sonar url and token)
pipeline {
agent any
stages {
stage('scm') {
steps {
stage('clean') {
steps {
sh 'mvn clean
stage('code quality') {
steps {
102
withSonarQubeEnv('SonarQube'){
---------------------------------------- jfrog-----------------------------------------
Why Only Jfrog
it supports:
===========================
- Repository management tools helps development teams create, maintain, and track their software
packages.
Options
-------
1. Jfrog Artifact
2. Nexus
3. Apache Archiva
4. Nuget
5. github
103
6. s3
--------------
maven repo...etc
- JFrog Artifactory is a repository manager that supports all available software package types - Artifactory,
Jfrog Artifactory?
==================
Jfrog Artifactory is a tool used in devops methodology to store artifacts (readily deployable code)
What is Artifact
----------------
The files that contain both compiled code and resources that are used to compile them are know as
artifact.
- source code --> Build Tools --> Compilation --> Binary code --> Dependencys/resources --> Artifact
104
what
is artifact Repository?
----------------------------
- An artifact repository is a repository which can store multiple different versions of [Link] time the
war or [Link] file is created. it stored in a server dedicated for the artifacts.
in real-time in the above process if you have any error in test env, we will rollback to version control to fix it.
instead of that if you store artifacts in repo we can rollback to prevision version.
Author
------
[Link]
- Jfrog Pipelines
- Jfrog x-ray
- Jfrog connect
Written in
------------
Releases
--------
Free vs Pro
-----------
[Link]
105
Type of packages it supports:
-----------------------------
[Link] Note:-
---------------------------------------
1. Pre-requisites:
[Link]
Username as admin
Password as password
106
#Note:we have to change it password after loggedin
=======================================
<distributionManagement>
<repository>
<id>central</id>
<name>NareshIT</name>
<url>[Link]
</repository>
<snapshotRepository>
<id>central</id>
<name>NareshIT</name>
<url>[Link]
</snapshotRepository>
</distributionManagement>
<servers>
<server>
<id>naresh</id>
<username>admin</username>
<password>naresh_123</password>
</server>
</servers>
107
3. Navigate to maven structure where [Link] and src locates, and give below command. - mvn
Deploy
###############APPROACH-2##########
=====================================
pre-requisites
A Artifactory server
A Jenkins Server
Integration Steps
3. Artifactory Servers
Server ID : test
Username : admin
Password : default password is "password" only. (login with default password later you can change)
Approach-1
- Build Environment
- Execute job
Approach-2
- Build Environment(optional)
-> Resolve artifacts from Artifactory : <provide Artifactory server and repository details>
- Deploy Artifacts to Artifactory : <provide Artifactory server and repository details> - Execute job
---------------------------------------------------------------------------------------------------------------------------- Approach-3
(recomended)
pipeline {
agent any
'[Link]
stage('clean') {
steps { sh 'mvn
clean'
stage('test') {
steps { sh
'mvn test' }
stage('install') {
steps { sh 'mvn
install'
110
stage('Push artifacts into artifactory') {
"files": [
"pattern": "*.war",
"target": "JFrog/"
}'''
stage('deployment') {
steps {
Trivy scans local and remote container images, supports multiple container engines, as well as archived and
extracted images. It works on raw filesystem and remote git repositories. With Trivy, you can scan
whenever and wherever you need to.
111
wget rpm -ivh
[Link]
trivy verison
----------------------ansible----------------
What is Ansible?
Ansible is an open source, command-line IT automation software application written in Python. It can
configure systems, deploy software, and orchestrate advanced workflows to support application
deployment, system updates, and more. Ansible's main strengths are simplicity and ease of use.
112
#How Ansible works?
In Ansible, there are two categories of computers: the control node and managed nodes. The control node
is a computer that runs Ansible. There must be at least one control node, although a backup control node
may also exist. A managed node is any device being managed by the control node.
=================================
======================================
=======================================
Inventory defines the managed nodes you automate, with groups so you can run automation tasks on
multiple hosts at the same time
Create our own inventory file vi inventory and add target private ips
or
We can add target private ips into defualt path of inventory fiel
◼ path--- vi /etc/ansible/hosts
===============================
Ad-hoc commands
113
=============================
Ad-hoc commands are commands which can be run individually to perform quick functions
◼ ansible -i inventory all -a "yum install git -y" -b # this command is to define our own path inventory
◼ ansible all -a "yum install git -y" -b # this command is for deaflt path of inventory so no need to
give "-i inventory argument"
'--- all' is apply for all hosts # we can replace with all if we want to call sepcific ip
Example
[web]
[Link]
[test]
[Link]
Exampes:
◼ ansible -i inventory all -a "yum install maven -y" -b # with define inventory
114
◼ ansible -all -a "git --version" -b #default inventory
Ansible modules are units of code that can control system resources or execute system commands.
Ansible provides a module library that you can execute directly on remote hosts or through playbooks. You
can also write custom modules.
"path=/var/www/html"
----------user----------
-----------setup--------
---------------file------------
----------------------
115
(basic state's name =
(httpd install)
state=latest
state=present
state=absent )
-a "name=httpd state=absent" -b
state=stopped state=
started state=restrted
state= absent)
Examples :
ansible -i inventory all -m yum -a "name=httpd state=latest" -b #to install httpd ansible -i inventory all -m
service -a "name=httpd state=started" -b #to start service
[defaults] host_key_checking =
False
116
----------------------Ansible playbook----------------------------
Ansible Playbooks are lists of tasks that automatically execute for your specified inventory or groups of
hosts. One or more Ansible tasks can be combined to make a play—an ordered grouping of tasks mapped
to specific hosts—and tasks are executed in the order in which they are written
---
yum:
name: httpd
state: latest
service:
name: httpd
state: started
[Link]
---
117
- name: install httpd software
yum:
name: httpd
state: latest
state: started
copy:
/var/www/html/[Link] - name:
name: httpd
state: restarted
==============================================
-----------Global Variable-------------
==============================================
---
- b: present
- c: started
- d: restarted
tasks:
yum:
name: "{{a}}"
state: "{{b}}"
118
- name: start web server
state: "{{c}}"
copy:
/var/www/html/[Link]
service:
name: "{{a}}"
state: "{{d}}"
tasks:
name: git
state: present
tags: a
name: maven
name: test
state: present
tags: c
119
SINGLE TAG: ansible-playbook [Link] --tags a
Prerequistes
python3
boto3
----install pip
---install boto3
120
pip show boto3 #to verify boto3
You might already have this collection installed if you are using the ansible package. It is not included in
ansible-core. To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install [Link]. You need further requirements to be able to
use this inventory plugin, see Requirements for details.
sudo vi /etc/ansible/[Link]
[defaults] enable_plugins =
aws_ec2 inventory
=./aws_ec2.yml
host_key_checking = False
sudo vi /etc/ansible/aws_ec2.yml
---
- ap-southeast-1 filters:
tag:Name:
- dev
# for all
121
# if we want to add inventory groups
- ap-southeast-1 keyed_groups:
# add hosts to tag_Name_value groups for each aws_ec2 host's [Link] variable.
# add hosts to the group dev if any of the dictionary's keys or values is the word 'dev'.
filters: tag:Name:
- 'dev'
---
yum:
name: httpd
state: latest
service:
name: httpd
state: started
122
--------sample playbook with inventory group
---
tasks:
yum:
name: httpd
state: latest
service:
name: httpd
state: started
[Link]
123