All Code Cheat Sheet
29. Redis – 15 – install, HA, put get https://www.tutorialspoint.com/redis/redis_environment.htm
28. MongoDB – 15 – install, HA, put get https://www.tutorialspoint.com/mongodb/mongodb_environment.htm
27. Hadoop – 15 – install of cloudera hadoop – https://www.youtube.com/watch?v=AZovvBgRLIY and https://www.youtube.com/watch?v=itcAf3lICrk&t=1s and https://www.youtube.com/watch?v=xYnS9PQRXTg
20. Packer – 20 – simple coding for docker, ami – https://github.com/gruntwork-io/infrastructure-as-code-training/tree/master/packer-example
25. VXLAN and OpenFlow – 10 – defintion only https://www.youtube.com/watch?v=j7on2iLk5ls and https://www.youtube.com/watch?v=CMtuAsm5ApA
23. Container Networking – 10 – understand, notedown https://www.youtube.com/watch?v=Yr6-2ddhLVo
22. Container Storage – 10 – understand, notedown https://www.youtube.com/watch?v=4o0WYS32x_E
19. ELK – 20 – installation, notedown https://www.youtube.com/watch?v=ge8uHdmtb1M
18. Prometheus – 20 – install, notedown https://www.youtube.com/watch?v=WUkNnY65htQ
All Codes and Commands
Kubernetes Coding
——————
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: nginxha
spec:
replicas: 2
template:
metadata:
label:
app: nginxapp
tier: frontend
spec:
containers:
– name: nginx1
image: nginx
ports:
containerPort: 80
imagePullPolicy: Always
– name: nginx2
image: nginx
ports:
containerPort: 80
imagePullPolicy: Always
Docker Commands
——————
docker run -d ubuntu tail -f /dev/null
docker exec containerid bash
# Volume Container Storage
docker create -v /tmp –name datacontainer ubuntu
docker run -it –volumes-from datacontainer ubuntu bash
# Host Volume Storage
docker run -d -v ~/sharedfolder:/tmp ubuntu
# Docker Image by Snapshot
docker commit containerid nameofimage
# Dockerfile
FROM ubuntu
MAINTAINER username@emaiid.commit
COPY somefile.txt /opt/
ADD . /code
WORKDIR /code
RUN apt-get update
RUN apt-get install nginx
CMD [“/bin/sh”, “-c”, “service nginx start && tail -f /dev/null”]
# Docker Compose
# docker-compose.yml
version: ‘3’
services:
web:
build: .
ports:
– “5000:5000”
volumes:
– .:/code
redis:
image: “redis:alpine”
# docker-compose up
# docker-compose stop
# docker-compose down –volumes
Python
———
#!/usr/bin/python
a = 21
b = 10
c = 0
counter = 100
var = 100
count = 0
# Print Example
name=input(“Whats your name? “)
print(“Hello, ” + name)
# Variable Example
print counter
# Operator example
c = a + b
print “Line 1 – Value of c is “, c
# Conditional Example
if var == 200:
print(“1 – Got a true expression value” + str(var))
elif var == 150:
print(“2 – Got a true expression value” + str(var))
elif var == 100:
print(“3 – Got a true expression value” + str(var))
else:
print(“4 – Got a false expression value” + str(var))
# Loop Example
while (count < 9):
print(“The count is: ” + str(count))
count = count + 1
Ruby
———–
#!/usr/bin/ruby -w
# variables
a = 20
x = [“Blue”, “Red”, “Green”, “Yellow”, “White”]
$i = 0
$num = 5
# Operators
puts(10 + 20)
puts “Hello, Ruby!”;
# Conditional
if a >= 18
puts “You are eligible to vote.”
else
puts “You are not eligible to vote.”
end
# Loop
for i in x do
puts i
end
def test(a1 = “Ruby”, a2 = “Perl”)
puts “The programming language is #{a1}”
puts “The programming language is #{a2}”
end
while $i < $num do
puts(“Inside the loop i = #$i” )
$i +=1
end
test “C”, “C++”
test
Git
———–
git clone https://github.com/studynine/samplejava.git
cd samplejava
cat README.md
echo “test 123” > test.txt
git add test.txt
git commit -m “comment”
git push
#BRANCHING
git branch newfeature
git checkout newfeature
git branch
echo “new feature” > feature.txt
git add feature.txt
git commit -m “featurecomment”
git push origin newfeature
#MERGING
git checkout master
git merge newfeature
git push
#HISTORY
#simplehistory
git log
#last two changes
git log -2
#last two changes detail diff
git log -p -2
#short diff history
git log –stat
#uncommitted changes since the last commit
git diff
echo “anotherline” >> feature.txt
git diff feature.txt
git log –prety=oneline
git diff commitid1 commitid2
Puppet
———–
user {“steve”:
ensure => present,
}
package {“ntp”:
ensure => present,
}
service {“ntp”:
ensure => stopped,
}
file {“/tmp/file1.txt”:
ensure => present,
}
file {“/tmp/file1.txt”:
ensure => present,
content => “Hello world”,
}
$x = “some content\n”
file { ‘/tmp/testing’:
content => $x
}
exec {“wget google.com”:
path => “/usr/bin/”,
cwd => “/tmp/”,
}
$x = “Hello”
if $x == “Hello” {
file {“/tmp/hello”:
ensure => present,
}}
else {
file {“/tmp/nothello”:
ensure => present,
}}
$x = “bye”
if $x == “Hello” {
file {“/tmp/hello”:
ensure => present,
}}
elsif $x == “bye” {
file {“/tmp/bye”:
ensure => present,
}}
else {
file {“/tmp/invalid”:
ensure => present,
}}
case $operatingsystem {
centos: { $apache = “httpd” }
redhat: { $apache = “httpd” }
debian: { $apache = “apache2” }
ubuntu: { $apache = “apache2” }
default: { notify{“Unrecognized operating system for webserver”: } }
}
package {‘apache’:
name => $apache,
ensure => latest,
}
if $osfamily == “RedHat” {
$pkg = “httpd”
notify { “greeting\n”: message => “Setting the value of pkg variable as httpd as the operating system is RedHat based\n” }
}
if $osfamily == “Debian” {
$pkg = “apache2”
notify { “greeting”: message => “Setting the value of pkg variable as apache2 as the operating system is Debian based” }
}
package {“$pkg”:
ensure => present,
}
puppet module generate username-modulename
file { “/home/ubuntu/test.txt”:
mode => “0644”,
owner => ‘ubuntu’,
group => ‘ubuntu’,
source => ‘puppet:///modules/mymodule/test.txt’,
}
Puppet Templates
————————-
# .pp file
$servers_real=[ “0.debian.pool.ntp.org iburst”,
“1.debian.pool.ntp.org iburst”,
“2.debian.pool.ntp.org iburst”,
“3.debian.pool.ntp.org iburst”, ]
file { ‘ntp.conf’:
path => ‘/etc/ntp.conf’,
ensure => file,
require => Package[‘ntp’],
content => template(“ntp/${conf_file}.erb”),
}
# .erb file
<% @servers_real.each do |this_server| -%>
server <%= this_server %>
<% end -%>
Chef
——
# Attributes can be defined in attributes/default.rb file
default[“starter_name”] = “Sam Doe”
# In recipes/default.rb file, the attributes can be called in as
log “Welcome to Chef, #{node[“starter_name”]}!” do
level :info
end
execute “update packagerepos” do
command “apt-get update”
end
package “apache2” do
action :install
end
service “apache2” do
action :start
end
file “/tmp/file1” do
content “Hello world”
end
remote_file “wordpress_latest” do
source ‘http://wordpress.org/latest.tar.gz’
path “/tmp/latest.tar.gz”
end
cookbook_file “/tmp/sample.txt” do
source “sample.txt”
end
cookbook_file “/var/www/html/index.html” do
source “index.html”
end
pkgname = “vim”
package pkgname do
action :install
end
package “php5” do
action :install
not_if { node[‘platform’] == ‘centos’ }
end
if node[‘platform’] == ‘ubuntu’
execute “apt-get update” do
command “apt-get update”
end
end
execute “test” do
command “apt-get update && touch /tmp/updatedone”
not_if {File.exists?(“/tmp/updatedone”)}
end
execute “test” do
command “apt-get update && rm -f /tmp/updateneeded”
only_if {File.exists?(“/tmp/updateneeded”)}
end
packages = [‘vim’, ‘git’, ‘curl’]
packages.each do |package|
apt_package package do
action :install
end
end
knife cookbook create nameofcookbook
chef cookbook generate nameofcookbook
cookbook_file “/var/www/html/index.html” do
source “index.html”
end
Chef Templates
——————–
#deafult.rb
template ‘/tmp/message’ do
source ‘Test.erb’
variables(
hi: ‘Tesing’,
world: ‘Welt’,
from: node[‘fqdn’]
servers: [“alpha”, “bravo”, “charlie”]
)
end
# .erb file
<%- 4.times do %>
<%= @hi %>, <%= @world %> from <%= @from %>!
<%- end %>
<% @servers.each do |name| %>
ServerAlias <%= name %>;
<% end %>
Ansible
———
# Playbooks
# package.yml
—
– hosts: all
become: true
tasks:
– name: Install Git
apt: name=git state=present update_cache=true
# service.yml
—
– hosts: all
become: true
tasks:
– name: Install NTP
apt: name=ntpd state=installed update_cache=true
– name: Stop ntp Service
service: name=ntpd state=stopped
# file.yml
—
– hosts: all
become: true
tasks:
– name: Hello World
copy:
content: ‘Hello World’
dest: /var/hello.txt
# execute.yml
—
– hosts: web1
become: true
tasks:
– name: Execute links installation
command: apt-get install links
# handlers.yml
—
– hosts: db1
become: true
tasks:
– name: Install nginx
apt: pkg=nginx state=installed update_cache=true
– name: coping using contnet
copy:
content: “Hello nginx”
dest: /usr/share/nginx/html/index.html
notify: start nginx service
handlers:
– name: start nginx service
service: name=nginx state=restarted
# variable.yml
—
– hosts: all
become: true
vars:
package: firefox
tasks:
– name: Install Package
apt: name={{ package }} state=latest
# conditional.yml
—
– hosts: web1
become: true
vars:
run: “yes”
tasks:
– name: Install apache on Redhat
command: yum install -y httpd
when: ansible_os_family == “RedHat”
– name: Execute when yes
command: apt-get install links
when: run == “yes”
# loop.yml
—
– hosts: all
become: true
tasks:
– name: Install Packages
apt: name={{ item }} state=latest
with_items:
– leafpad
– mousepad
– geany
# ansible-galaxy init example
# /roles/example/{default, vars, tasks, handlers}/main.yml
#tasks/main.yml
—-
– name: Install nginx
apt: pkg={{ package }} state=installed update_cache=true
– name: coping using contnet
template:
src: index.j2
dest: /usr/share/nginx/html/index.html
notify: start nginx service
# vars/main.yml
—
package: nginx
rolename: myrole
# templates/index.j2
Hello World from {{ rolename }}
ansible-galaxy install geerlingguy.ntp
—
– hosts: all
become: true
roles:
– geerlingguy.ntp
Ansible Templates
———————–
# playbook.yml
—
– hosts: all
vars:
variable1: ‘Hello…!!!’
variable2: ‘My first playbook using template’
list1: [‘Apple’,’Banana’,’Cat’, ‘Dog’]
tasks:
– name: Basic Template Example
template:
src: example1.j2
dest: /home/knoldus/ Documents/Ansible/output.txt
– name: Template Loop example.
template:
src: example2.j2
dest: /home/knoldus/Documents/Ansible/output.txt
# example.j2 file
{{ variable1 }}
This is
{{ variable2 }}
Example of template module loop with a list.
{% for item in list1 %}
{{ item }}
{% endfor %}
Terraform
—————–
# EC22 Example
# terraform.tfvars
access_key = “YOURACCESSKEY
secret_key = “YOURSECRETKEY”
# variables.tf
variable “access_key”{}
variable “secret_key”{}
variable “key_name”{}
variable “key_path”{}
variable “aws_region”{
description=”Region to Launch Server’s”
default=”us-east-1″
}
variable “aws_amis”{
default = {
us-east-1=”ami-9eaa1cf6”
}
}
variable “subnet_id” {
default = “subnet-f5460aaf”
description = “Your Subnet”
}
variable “vpc_id” {
default = “vpc-b1a182c8”
description = “Your VPC ID”
}
variable “sg_name” {
default = “terraform-sg”
description = “Tag Name for sg”
}
# main.tf
provider “aws”{
region=”${var.aws_region}”
access_key=”${var.access_key}”
secret_key=”${var.secret_key}”
}
resource “aws_security_group” “default”{
name=”terraform-sg”
description=”Created by terraform”
vpc_id = “${var.vpc_id}”
ingress{
from_port = 22
to_port = 22
protocol= “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress{
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
egress {
from_port = “0”
to_port = “0”
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags {
Name = “${var.sg_name}”
}
}
resource “aws_instance” “web”{
connection={
user=”ubuntu”
private_key=file(“${path.module}/mykey.pem”)
}
instance_type=”t2.medium”
ami=”${lookup(var.aws_amis, var.aws_region)}”
key_name = “${var.key_name}”
security_groups= [“${aws_security_group.default.name}”]
tags{
Name=”Terraform-EC2-test”
}
subnet_id = “${var.subnet_id}”
}
resource “aws_eip” “ip” {
instance = “${aws_instance.web.id}”
depends_on = [“aws_instance.web”]
}
# output.tf
output “address”{
value=”${aws_instance.web.public_dns}”
}
output “ip” {
value = “${aws_eip.ip.public_ip}”
}
# RDS example
# variables.tf
variable “aws_access_key” {
default = “AKIAIOSFODNN7EXAMPLE ”
description = “AWS ACCESS KEY”
}
variable “aws_secret_key” {
default = “wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY”
description = “AWS SECRET KEY”
}
variable “identifier” {
default = “mydb-rds”
description = “Identifier for your DB”
}
variable “storage” {
default = “10”
description = “Storage size in GB”
}
variable “engine” {
default = “postgres”
description = “Engine type, example values mysql, postgres”
}
variable “engine_version” {
description = “Engine version”
default = {
mysql = “5.6.22”
postgres = “9.4.1”
}
}
variable “instance_class” {
default = “db.t2.micro”
description = “Instance class”
}
variable “db_name” {
default = “mydb”
description = “db name”
}
variable “username” {
default = “myuser”
description = “User name”
}
variable “password” {
default = “12345678”
description = “password”
}
variable “subnet_1_cidr” {
default = “10.0.1.0/24”
description = “Your AZ”
}
variable “subnet_2_cidr” {
default = “10.0.2.0/24”
description = “Your AZ”
}
variable “az_1” {
default = “us-east-1b”
description = “Your Az1, use AWS CLI to find your account specific”
}
variable “az_2” {
default = “us-east-1c”
description = “Your Az2, use AWS CLI to find your account specific”
}
variable “vpc_id” {
default = “vpc-b1a182c8”
description = “Your VPC ID”
}
variable “cidr_blocks” {
default = “0.0.0.0/0”
description = “CIDR for sg”
}
variable “sg_name” {
default = “rds_sg”
description = “Tag Name for sg”
}
# main.tf
provider “aws” {
access_key = “${var.aws_access_key}”
secret_key = “${var.aws_secret_key}”
region = “us-east-1”
}
resource “aws_security_group” “default” {
name = “main_rds_sg”
description = “Allow all inbound traffic”
vpc_id = “${var.vpc_id}”
ingress {
from_port = 0
to_port = 65535
protocol = “TCP”
cidr_blocks = [“${var.cidr_blocks}”]
}
egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags {
Name = “${var.sg_name}”
}
}
resource “aws_subnet” “subnet_1” {
vpc_id = “${var.vpc_id}”
cidr_block = “${var.subnet_1_cidr}”
availability_zone = “${var.az_1}”
tags {
Name = “main_subnet1”
}
}
resource “aws_subnet” “subnet_2” {
vpc_id = “${var.vpc_id}”
cidr_block = “${var.subnet_2_cidr}”
availability_zone = “${var.az_2}”
tags {
Name = “main_subnet2”
}
}
resource “aws_db_subnet_group” “default” {
name = “main_subnet_group”
description = “Our main group of subnets”
subnet_ids = [“${aws_subnet.subnet_1.id}”, “${aws_subnet.subnet_2.id}”]
}
resource “aws_db_instance” “default” {
depends_on = [“aws_security_group.default”]
identifier = “${var.identifier}”
allocated_storage = “${var.storage}”
engine = “${var.engine}”
engine_version = “${lookup(var.engine_version, var.engine)}”
instance_class = “${var.instance_class}”
name = “${var.db_name}”
username = “${var.username}”
password = “${var.password}”
vpc_security_group_ids = [“${aws_security_group.default.id}”]
db_subnet_group_name = “${aws_db_subnet_group.default.id}”
}
# Outputs.tf
output “subnet_group” {
value = “${aws_db_subnet_group.default.name}”
}
output “db_instance_id” {
value = “${aws_db_instance.default.id}”
}
output “db_instance_address” {
value = “${aws_db_instance.default.address}”
}
packer
——————–
{
“variables”: {
“region”: “us-east-1”
},
“builders”: [{
“ami_name”: “abhradip-sample-{{isotime | clean_ami_name}}”,
“ami_description”: “An Ubuntu AMI that with some customization”,
“instance_type”: “t2.micro”,
“region”: “{{user `region`}}”, # refers to variable section
“type”: “amazon-ebs”,
# now starts the section to select the base ami from amazon
“source_ami_filter”: {
“filters”: {
“virtualization-type”: “hvm”,
“architecture”: “x86_64”,
“name”: “*ubuntu-xenial-16.04-amd64-server-*”,
“block-device-mapping.volume-type”: “gp2”,
“root-device-type”: “ebs”
},
“owners”: [“099720109477”],
“most_recent”: true
},
# these above informations should be selecting the ubuntu ami in us-east-1 zone
“ssh_username”: “ubuntu”
}],
“provisioners”: [{
“type”: “shell”,
“inline”: [
“echo ‘Sleeping for 30 seconds to give Ubuntu enough time to initialize (otherwise, packages may fail to install).'”,
“sleep 30”
“echo hello > /opt/test.txt”
]
}]
}
Helm
——————-
# Charts.yaml
apiVersion: v1
appVersion: “1.0”
description: A Helm Chart for Kubernetes
name: mychart
version: 0.1.0
# values.yaml
service:
name: someservice
type: ClusterIP
internalPort: 80
# templates/service.yml
apiVersion: v1
kind: Service
metadata:
name: {{ template “fullname” . }}
labels:
chart: “{{ .Chart.Name }}-{{ .Chart.Version | replace “+” “_” }}”
spec:
type: {{ .Values.service.type }}
ports:
– port: {{ .Values.service.externalPort }}
targetPort: {{ .Values.service.internalPort }}
protocol: TCP
name: {{ .Values.service.name }}
selector:
app: {{ template “fullname” . }}