Docker
Docker
===================
This is the process of running multiple OS's parallelly on
a single pice of h/w.
Here we have h/w(bare metal) on top of which we have host os
and on the host os we install an application called as hypervisor
On the hypervisor we can run any no of OS's as guest OS
Containarization
======================
Here we have bare metal on top of whcih we install the host Os
and on the hsot OS we install an application called as Docker Engine
On the docker engine we can run any application in the form of containers
Docker is a technology for creating thse containers
These containers pass through less no of layers to access the h/w resources
also organizations need not spend money on purchasing licenses of different
OS's to maintian various applications
Docker can be used at the the stages of S/W development life cycle
Build---->Ship--->Run
===========================================================================
Docker comes in 2 flavours
Docker CE (Community Edition)
Docker EE (Enterprise Edition)
======================================================================
Day 2
==================================================================
Setup of Docker on Windows
==============================
1 Download docker desktop from
  https://www.docker.com/products/docker-desktop
2 Install it
===============================================================================
Using AWS
================
1 Login in AWS account
sh get-docker.sh
=================================================================
==========================================================
Docker Host: The server where docker is installed is called
               docker host
==========================================================================
===========================================================================
Important docker commands
==============================
Working on docker images
===============================
1 To pull a docker image
  docker pull image_name
===================================================================
11 To create a docker image from a dockerfile
   docker build -t image_name .
15 To start a container
   docker start container_id/container_name
16 To stop a container
   docker stop container_id/container_name
17 To restart a container
   docker restart container_id/container_name
   To restart after 10 seconds
   docker restart -t 10 container_id/container_name
40 To delete a volume
   docker volume rm volume_name/volume_id
========================================================================
Day 3
========================================================================
Docker Host: The server where docker is installed is called
docker host
Docker client: This is CLI of docker which accepts the docker commands
from the users and passes to a background process called docker deamon
Docker deamon: This accepts the commands comming from docker client
and routes them to work on docker images or contaienr or the registry
Docker registry: This is the location where docker images are stored
This if of 2 type
1 Public (hub.docker.com)
2 Private: This is set up on one of our internal servers
================================================
UseCase 1
===============
Create an nginx container in detached mode
docker run --name webserver -p 8888:80 -d nginx
===============================================================
Day 4
===============================================================
UseCase 1
===============
Create an nginx container in detached mode
docker run --name webserver -p 8888:80 -d nginx
=============================================================================
UseCase 3
===================
Create jenkins container and do automtic port mapping
docker run --name jenkins -d -P jenkins
=============================================================
UseCase 4
Start centos as a container and launch interactive terminal on it
docker run --name c1 -it centos
exit
============================================================
Usecase 5
Create an ubuntu container and launch interactive terminal in it
docker run --name u1 -it ubuntu
exit
==============================================================================
UseCase 6
4 Python Scripts
5 Ansible
UseCase 1
=============
Create 2 busybox containers and link them
UseCase 2
=====================
Create a mysql container and a wordpress container and
link them
5 To access tomcat
  public_ip_dockerhost:6060 or 7070
===============================================================================
Day 6
====================================================================
=======================
Setup LAMP architecture
3 Create a php container and link with mysql and apache containers
  docker run --name php -d --link mydb:mysql --link apache:httpd php:7.2-apache
=========================================================================
Docker compose
=======================
The disadvantage of "link" option is it is depricated and
the same individual command have to be given multiple times
to setup similar architectures.
To avaoid this we can use docker compose
Docker compose uses yml files to setup the multu container
architecture and these files can be resused any number of time
Url: https://docs.docker.com/compose/install/
===================================================================
Day 7
==============================================================
 mywordpress:
  image: wordpress
  ports:
    - 8888:80
  links:
    - mydb:mysql
...
==================================================================
==============================================================================
Create a docker compose file to setup CI-CD environemnt where
a jenkins container should be linked with 2 tomcat containers
one for qaserver and other for prodserver
vim docker-compose.yml
---
version: '3.8'
services:
 jenkinsserver:
  image: jenkins
  ports:
    - 5050:8080
 qaserver:
  image: tomee
  ports:
   - 6060:8080
  links:
   - jenkinsserver:jenkins
 prodserver:
  image: tomee
  ports:
   - 7070:8080
  links:
   - jenkinsserver:jenkins
=========================================================================
Day 8
=====================================================================
UseCase
==============
Create a docker compose file to setup the LAMP architecture
vim lamp.yml
---
version: '3.8'
services:
 mydb:
  image: mysql
  environment:
   MYSQL_ROOT_PASSWORD: intelliqit
 apache:
  image: httpd
  ports:
   - 8989:80
  links:
   - mydb:mysql
 php:
  image: php:7.2-apache
  links:
   - mydb:mysql
   - apache:httpd
...
===========================================================================
=========================================================================
Create a docker compose file to setup the selenium
testing environment where a hub container is linked with 2 node
containers
vim selenium.yml
---
version: '3.8'
services:
 hub:
  image: selenium/hub
  ports:
   - 4444:4444
 chrome:
  image: selenium/node-chrome-debug
  ports:
   - 5901:5900
  links:
   - hub:selenium
 firefox:
  image: selenium/node-firefox-debug
  ports:
   - 5902:5900
  links:
   - hub:selenium
==========================================================================
Volumes
=============
Containers are ephemeral but the data processed by the container
should be persistent.This can be done using volumes
3 In the ubuntu container go into dat folde and create some files
  cd data
  touch file1 file2
  exit
4 Create another centos container c2 and it should use the volumes used by c1
  docker run --name c2 -it --volumes-from c1 centos
7 Create another centos container c3 and it should use the volume used by c2
  docker run --name c3 -it --volumes-from c2 centos
10 Go into any of the 3 contianers and we will see all the files
   docker attach c1
   cd /data
   ls
   exit
===========================================================================
Day 9
==========================================================================
Docker volume containers
----------------------------
These volumes are bidirectoinal ie the changes done on host
will be reflected into container and changes done by container
will be reflected to host machine
1 Create a volume
  docker volume create myvolume
5 Create an ubuntu container and mount the above volume into the tmp folder
  docker run --name u1 -it -v myvolume:/tmp ubuntu
===========================================================================
UseCase
============
Create a volume "newvolume" and create tomcat-users.xml file in it
Create a tomcat container and mount the above volume into it
Copy the tomcat-users.xml files to the required location
1 Create a volume
  docker volume create newvolume
=========================================================================
Dockerfile
===================
Dockerfile uses predefined keyword to create customsied
docker images.
COPY : Used to copy files from host to the customised image that
       we are creating
ADD : This is similar to copy where it can copy files from host
      to image but ADD can also downlaod files from some remote server
CMD : USed to run the default process of the container from outside
ENTRYPOINT : This is also used to run the default process of the container
LABEL: Used to store data about the docker image in key value pairs
SHELL : Used to specify what shell should be by default used by the image
===================================================================================
===
Day 10
===================================================================================
==
UseCase
===========
Create a dockerfile to use nginx as abse image and specify
the maintainer as intelliqit
FROM nginx
MAINTAINER intelliqit
-----------------------------------------------------------------------
UseCase
==============
Create a dockerfile from ubuntu base image and install
git in it
1 Create dockerfile
  vim dockerfile
  FROM ubuntu
  MAINTAINER intelliqit
  RUN apt-get update
  RUN apt-get install -y git
4 Create a container from the new image and it should have git installed
  docker run --name u1 -it myubuntu
  git --version
=========================================================
Cache Busting
===================
When we create an image from a dockerfile docker stores all the
executed isntructions in a its cache.Next time if we edit the
same docker file and add few new instructions and build an image
out of it docker will not execute the previously executed statements
Instead it will read them from the cache
This is a time saving mechanism
The disadvantage is if the docker file is edited with a huge time
gap then we might end up installing s/w's that are outdated
Eg:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y git
To avoid this problem and make docker execute all the instructions
once more time without reading from cache we use "cache busting"
docker build --no-cache -t myubuntu .
=========================================================================
Create a shell script to install multiple s/w's and copy this
into the docker image and execute it a the the time os creating the image
6 Check if the script.sh is present in / and also see if tree and git are installed
  ls /
  git --version
  tree
=================================================================
Create a dockerfile from base ubuntu image and make
/data as the default volume
1 Create a dockerfile
  vim dockerfile
  FROM ubuntu
  MAINTAINER intelliqit
  VOLUME /data
3 Create a container from the above image and check for the volume
  docker run --name u1 -it myubuntu
  ls (we should see the data folder)
5 Check for the mount section and copy the source path
======================================================================
Create a dcoker file from nginx base image and expose 80 port
1 vim dockerfile
  FROM nginx
  MAINTAINER intelliqit
  EXPOSE 90
2 Create an image
  docker build -t mynginx .
===================================================================================
=
Day 11
===================================================================================
Create a dockerfile from jenkins base image and make the default user as root
1 vim dockerfile
  FROM jenkins/jenkins
  MAINTAINER intelliqit
  USER root
4 Go into the interactive shell and check if the default user is root
  docker exec -it j1 bash
  whoami
=========================================================================
Create a dockerfile from ubunt base image and downlaod jenkins.war
into it
1 Create a dockerfile
  vim dockerfile
  FROM ubuntu
  MAINTIANER intelliqit
  ADD https://get.jenkins.io/war-stable/2.263.4/jenkins.war   /
1 vim dockerfile
  FROM ubuntu
  MAINTAINER intelliqit
  RUN apt-get update
  RUN apt-get install -y openjdk-8-jdk
  ADD https://get.jenkins.io/war-stable/2.263.4/jenkins.war /
  ENTRYPOINT ["java","-jar","jenkins.war"]
3 Create a container from the above image and we will see that
  it behaves like a jenkins container
  docker run --name u1 -it myubuntu
======================================================================
==============================================================================
UseCase
=============
Create a dockerfile from ubuntu base image and make it behave
like nginx
1 Create a dockerfile
  vim dockerfile
  FROM ubuntu
  MAINTAINER intelliqit
  RUN apt-get update
  RUN apt-get install -y nginx
  ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]
  EXPOSE 80
========================================================================
CMD and ENTRYPOIT
------------------------
Bothe of them are used to specify the default process that should be
triggered when the container starts but the CMD instruction can be
overridden with some other process passed at the docker run command
Eg:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nginx
CMD ["/usr/sbin/nginx","-g","daemon off;"]
EXPOSE 80
===============================================================================
Docker Networking
=========================
Docker supports 4types of networks
1 Bridge
2 Host
3 null
4 Overlay
=============================================================================
Day 13
============================================================================
UseCase
===============
Create 2 bridge networks intelliq1 and intelliq2
Create 2 busybox containers c1,c2 and c3
c1 and c2 should run on intelliq1 network and shoul ping each other
c3 should run on intelliq2 network and it should not be able to ping c1 or c2
Now put c2 on intelliq2 network,since c2 is on both intelliq1 and intelliq2
networks it should be able to ping to both c1 and c3
but c1 and c3 should not ping each other directly
==================================================================================
=========================================================================
Create a dockerfile and use it directly in docker-compsoe
vim dockerfile
FROM jenkins/jenkins
MAINTAINER intelliqit
RUN apt-get update
RUN apt-get install -y git
vim docker-compose.yml
version: '3.8'
services:
 jenkins:
  build: .
  ports:
   - 7070:8080
  container_name: jenkins
 mytomcat:
  image: tomee
  ports:
    - 6060:8080
  container_name: mytomcat
...
================================================================================
Day 14
=================================================================================
UseCase
=============
Create a custom bridge network and create a docker compose file
to start postgres and adminer container on the above created
network
services:
 db:
  image: postgres
  environment:
   POSTGRES_PASSWORD: intelliqit
   POSTGRES_USER: myuser
   POSTGRES_DB: mydb
 adminer:
  image: adminer
  ports:
   - 8888:8080
networks:
 default:
  external:
    name: intelliqit
...
==========================================================================
Docker compose file to create 2 networks and run containers on different network
vim docker-compose.yml
---
version: '3.8'
services:
 mydb:
  image: jenkins/jenkins
  ports:
   - 5050:8080
  networks:
   - abc
 qaserver:
  image: tomee
  ports:
   - 6060:8080
  networks:
   - xyz
 prodserver:
  image: tomee
  ports:
   - 7070:8080
  networks:
   - xyz
networks:
 abc: {}
 xyz: {}
...
==============================================================================
==========================================================================
Docker compose file to create 2 containers and also create 2 volumes for both the
containers
---
version: '3.8'
services:
 db:
  image: mysql:5
  environment:
   MYSQL_ROOT_PASSWORD: intelliqit
 volumes:
   mydb:/var/lib/mysql
 wordpress:
  image: wordpress
  ports:
   - 9999:80
  volumes:
   wordpress:/var/www/html
volumes:
  mydb:
  wordpress
=========================================================================
Day 15
=========================================================================
Container Orchestration
==============================
This is the process of handling docker containers running
on multiple linux servers in a distributed environment
Advantages
=================
1 Load Balancing
2 Scalling
3 Rolling update
4 High Availability and Disaster recovery(DR)
LoadBalancing
==================
Each container is capable of sustaining a specific user load
We can increase this capacity by running the same application
on multiple containers(replicas)
Scalling
==============
We should be able to increase or decrease the number of containers
on which our applications are running without the end user
exepriencing any downtime.
Rolling update
=======================
Application running in a live environment should be upgraded or
downgraded to a different version without the end user having any
downtime
Disaster Recovery
======================
In case of network failuers or server crashes still the container
orchestration tools maintain the desired count of containers
and thereby provide the same service to the end user
===========================================================================
Setup of Docker Swarm
============================
1 Create 3 AWS ubuntu instances
2 Name them as Manager,Worker1,Worker2
3 Install docker on all of them
4 Change the hostname
  vim /etc/hostname
  Delete the content and replace it with Manager or Worker1 or Worker2
5 Restart
  init 6
6 To initilise the docker swarm
  Connect to Manager AWS instance
  docker swarm init
  This command will create a docker swarm and it will also generate
  a tokenid
7 Copy and paste the token id in Worker1 and Worker2
===============================================================================
TCP port 2376 for secure Docker client communication. This port is required for
Docker Machine to work. Docker Machine is used to orchestrate Docker hosts.
TCP port 2377. This port is used for communication between the nodes of a Docker
Swarm or cluster. It only needs to be opened on manager nodes.
TCP and UDP port 7946 for communication among nodes (container network discovery).
UDP port 4789 for overlay network traffic (container ingress networking).
=========================================================================
Day 16
=========================================================================
Load Balancing:
Each docker containers has a capability to sustain a specific
user load.To increase this capability we can increase the
number of replicas(containers) on which a service can run
UseCase
------------
Create nginx with 5 replicas and check where these replicas are
running
=======================================================================
Scalling
============
This is the process of increasing the number of replicas or decreasing
the replicas count based on requirement without the end user experiencing
any down time.
UseCase
============
Create tomcat with 4 replicas and scale it to 8 and scale it
down to 2
=======================================================================
Rolling updates
======================
Services running in docker swarm should be updated from once
version to other without the end user downtime
UseCase
===========
Create redis:3 with 5 replicas and later update it to redis:4
also rollback to redis:3
4 Check redis:3 replcias are shut down and in tis palce redis:4 replicas are
running
  docker service ps myredis
6 Check if redis:4 replicas are shut down and in its place redis:3 is running
  docker service ps myredis
===================================================================================
================================================================================
To remove a worker from swarm cluster
docker node update --availability drain Worker1
====================================================================
FailOver Scenarios of Workers
================================
Create httpd with 6 replicas and delete one replica running on the manager
Check if all 6 replicas are still running
Drain Worker1 from the docker swarm and check if all 6 replicas are running
on Manager and Worker2,make Worker1 rejoin the swarm
Make Worker2 leave the swarm and check if all the 6 replicas are
running on Manager and Worker1
4 Delete a replica
  docker rm -f container_id_from_step3
If one manager node goes down other manager becomes the Leader
Quorum is resonsible for doing this activity and if uses a RAFT
algorithm for handling the failovers of managers.Quorum also
is responsible for mainting the min number of manager
Min count of manager required for docker swarm should be always
more than half of the total count of Managers
=================================================================================
Day 14
=================================================================================
=============================================================================
Overlay Networking
=========================
This is the deafult network used by swarm
and this network perfrom network load balancin
ie even if a service is running on a specicfic worker we can
access if from orther slave
UseCase
=============
Start nginx with 2 repliacs and check if we can acces it from
browser from manager and all workers
1 Create nginx
  docker service create   --name webserver -p 8888:80 --replicas 2 nginx
3 Check if we can access nginx from the third node where it is not present
  public_ip_of_thirdnode:8888
=============================================================================
Day 18
==============================================================================
UseCase
===========
Create 2 overlay networks intelliqit1 and intelliqit2
Create httpd with 5 replacs on intelliqit1 network
Create tomcat with 5 replicas on default overlay "ingres" network
and later perform rolling network update to intelliqit2 network
4 To delete a stack
  docker stack rm stack_name
=====================================================================
UseCase
================
Create a docker stack file to start 3 replicas of wordpress
and one replica of mysql
vim stack1.yml
---
version: '3.8'
services:
 db:
  image: "mysql:5"
  environment:
   MYSQL_ROOT_PASSWORD: intelliqit
 wordpress:
  image: wordpress
  ports:
   - "8989:80"
  deploy:
   replicas: 3
=====================================================================
UseCase
==============
Create a stack file to setup CI-cd architecture where a jenkins
container is linked with tomcats for qa and prod environments
The jenkins contianers should run only on Manager
the qaserver tomcat should run only on Worker1 and prodserver
tomcat should run only on worker2
vim stack2.yml
---
version: '3.8'
services:
 myjenkins:
  image: jenkins/jenkins
  ports:
   - 5050:8080
  deploy:
   replicas: 2
   placement:
    constraints:
     - node.hostname == Manager
 qaserver:
  image: tomcat
  ports:
   - 6060:8080
  deploy:
   replicas: 3
   placement:
    constraints:
     - node.hostname == Worker1
 prodserver:
  image: tomcat
  ports:
   - 7070:8080
  deploy:
   replicas: 4
   placement:
      constraints:
       - node.hostname == Worker2
...
==============================================================================
Day 19
==============================================================================
UseCase
Create a stack file to setup the selenium hub and nodes architecture
but also specify a upper limit on the h/w
vim stack3.yml
---
version: '3.8'
services:
 hub:
  image: selenium/hub
  ports:
   - 4444:4444
  deploy:
   replicas: 2
   resources:
    limits:
      cpus: "0.1"
      memory: "300M"
 chrome:
  image: selenium/node-chrome-debug
  ports:
   - 5901:5900
  deploy:
   replicas: 3
   resources:
    limits:
     cpus: "0.01"
     memory: "100M"
 firefox:
  image: selenium/node-firefox-debug
  ports:
   - 5902:5900
  deploy:
   replicas: 3
   resources:
    limits:
     cpus: "0.01"
     memory: "100M"
===========================================================================
Docker secrets
===========================
This is a feature of docker swarm using which we can pass secret data
to the services running in swarm cluster
These secrets are created on the host machine and they will be
availbale from all the replicas in the swarm cluster
==============================================================================
Create a secret for mysql password and setup mysql wordpress architrcture
Create a secret
echo "Intelliqit" | docker secret create mysql -
vim stack5.yml
---
version: '3.8'
services:
 db:
  image: "mysql:5"
  environment:
   MYSQL_ROOT_PASSWORD_FILE: /run/secrets/mysql
  secrets:
   - mysql:
 wordpress:
  image: wordpress
  ports:
   - "8989:80"
  deploy:
   replicas: 3
secrets:
 mysql:
  external: true
==============================================================================
Create 3 secrets for postgres user,password and db
and pass them to the stack file
1 Create secrets
  echo "intelliqit" | docker secret create pg_password -
  echo "myuser" | docker secret create pg_user -
  echo "mydb" | docker secret create pg_db -
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
    deploy:
     replicas: 2
secrets:
    pg_password:
     external: true
    pg_user:
     external: true
    pg_db:
     external: true
...
==================================================================================
Day 20
==================================================================================
================================================================================
Kubernetes
======================
Slaves are the nodes that accept the work load from the master
and handle activites load balancing,autoscalling,high availability etc
2 Service: This is used for port mapping and network load balancing
7 Deployment: This used for perfroming all activites that a Replicaset can do
  it can also handle rolling update
8 Volume: Used to preserve the data even when the pods are deleted
9 Statefulsets: These are used to handle stateful application like data bases
  where consistency in read write operations has to be maintained.
Kubernetes Architecture
=============================
Master Componentes
=======================
Container runtime: This can be docker or anyother container technology
apiServer: Users interact with the apiServer using some clinet like ui,command line
tool like kubelet.It is the apiServer which is the gateway to the cluster
It works as a gatekeeper for authentication and it validates if a specific
user is having permissions to execute a specific command.Example if we want to
deploy a pod or a deployment first apiServers validates if the user is authorised
to perform that action and if so it passes to the next process
ie the "Scheduler"
Scheduler: This process accepts the instructions from apiServer after validation
and starts an application on a sepcific node or set of nodes.It estimates
how much amount of h/w is required for an application and then checks which
slave have the necessary h/w resources and instructs the kubelet to deploy
the application
kubelet: This is the actual process that takes the orders from scheduler and
deploy an application on a slave.This kubelet is present on both master and slave
controller manager: This check if the desired state of the cluster is always
maintained.If a pod dies it recreates that pod to maintain the desired state
kubelet: This process interacts with container run time and the node
and it start a pod with a container in it
=========================================================================
Day 21
==========================================================================
UseCase
===========
Create nginx as a pod and name it webserver
kubectl run --image nginx webserver
To see more info about the pods like their ip and slave where they are running
kubectl get pods -o wide
============================================================================
UseCase
=========
Create mysql pod and name it mydb and go into its interactive terminal and create
few tables
=========================================================================
Kuberentes Defintion files
==============================
Objects in Kubernetes cluster are deployed using these
defintion files
They are created using yml and they generally these 4 top level
fields.
apiVersion:
kind:
metadata:
spec:
metadata: Here we can give additional info about the Pod like
the name of the Pod,some labels etc
spec: This is where exact info about the object that is created is
specified like containers info port mapping,no of replicas etc
================================================================
kind                      apiVersions
===================================================
Pod                v1
Service                   v1
Secret                    v1
Namespace          v1
ReplicationController     v1
ReplicaSet                apps/v1
Deployment            apps/v1
StatefuleSet              apps/v1
DeamonSet                 apps/v1
==================================================================
Create a pod defintion file to start nginx pod with a name webserver
1 vim pod-defintion1.yml
---
apiVersion: v1
kind: Pod
metadata:
 name: nginx-pod
 labels:
  type: proxy
  author: intelliqit
spec:
 containers:
  - name: webserver
    image: nginx
...
========================================================================
UseCase
================
Create a mysql-pod and give the labels as author=intelliqit
and type=db,also pass the necessay environment variables
1 vim pod-definition2.yml
apiVersion: v1
kind: Pod
metadata:
 name: mysql-pod
 labels:
  author: intelliqit
  type: db
spec:
 containers:
  - name: mydb
    image: mysql:5
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: intelliqit
...
=======================================================================
Day 22
========================================================================
Kops : This is an automated way of setting up unmanaged/self managed Kubernetes
cluster
======================================================================
Kubernetes on AWS using Kops
1. Launch Linux EC2 instance in AWS (Kubernetes Client)
2. Create and attach IAM role to EC2 Instance.
Kops need permissions to access
      S3
      EC2
      VPC
      Route53
      Autoscaling
      etc..
3. Install Kops on EC2
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s
https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut
-d '"' -f 4)/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops
4. Install kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s
https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/
amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
5. Create S3 bucket in AWS
S3 bucket is used by kubernetes to persist cluster state, lets create s3 bucket
using aws cli Note: Make sure you choose bucket name that is uniqe accross all aws
accounts
aws s3 mb s3://sai.in.k8s --region ap-south-1
6. Create private hosted zone in AWS Route53
Head over to aws Route53 and create hostedzone
Choose name for example (sai.in)
Choose type as privated hosted zone for VPC
Select default vpc in the region you are setting up your cluster
Hit create
7 Configure environment variables.
Open .bashrc file
      vi ~/.bashrc
Add following content into .bashrc, you can choose any arbitary name for cluster
and make sure buck name matches the one you created in previous step.
export KOPS_CLUSTER_NAME=sai.in
export KOPS_STATE_STORE=s3://sai.in.k8s
Then running command to reflect variables added to .bashrc
      source ~/.bashrc
8. Create ssh key pair
This keypair is used for ssh into kubernetes cluster
ssh-keygen
9. Create a Kubernetes cluster definition.
kops create cluster \
--state=${KOPS_STATE_STORE} \
--node-count=2 \
--master-size=t3.medium \
--node-size=t3.medium \
--zones=us-east-1a \
--name=${KOPS_CLUSTER_NAME} \
--dns private \
--master-count 1
10. Create kubernetes cluster
kops update cluster --yes --admin
Above command may take some time to create the required infrastructure resources on
AWS. Execute the validate command to check its status and wait until the cluster
becomes ready
============================================================================
UseCase 3
Create a pod defintion file to start a jenkins container in a pod
called jenkins-pod,also perform port mapping to access the jenkins
from a browser
vim pod-definition3.yml
---
apiVersion: v1
kind: Pod
metadata:
 name: jenkins-pod
 labels:
  author: intelliqit
  type: ci-cd
spec:
 containers:
  - name: myjenkins
    image: jenkins
    ports:
      - containerPort: 8080
        hostPort: 8080
...
To see the list of pods along with nodes where they are running
kubectl get nodes -o wide
=========================================================================
Day 23
===========================================================================
Create a pod-defintion file to setup a ghost pod
vim pod-defintion4.yml
apiVersion: v1
kind: Pod
metadata:
 name: ghost-pod
 labels:
  author: intelliqit
  type: cms
spec:
 containers:
  - name: ghost
    image: ghost
    ports:
      - containerPort: 2368
        hostPort: 8080
...
============================================================================
ReplicationController
=======================
This is a high level Kubernets object that can be used for handling
multiple replicas of a Pod.Here we can perfrom Load Balancing
and Scalling
=============================================================================
ReplicaSet
===================
This is also similar to ReplicationController but it is more
advanced and it can also handle load balancing and scalling
It has an additional field in spec section called as "selector"
This selector uses a child element "matchLabels" where the
it will search for Pod based on a specific label name and try to add
them to the cluster
Create a replicaset file to start 4 tomcat replicas   and then perform scalling
vim replica-set.yml
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
 name: tomcat-rs
 labels:
  type: webserver
  author: intelliqit
spec:
 replicas: 6
 selector:
  matchLabels:
   type: webserver
 template:
  metadata:
   name: tomcat-pod
   labels:
    type: webserver
  spec:
   containers:
    - name: mywebserver
      image: tomcat
      ports:
        - containerPort: 8080
          hostPort: 9090
b) Scale from the coomand prompt withbout updating the defintion file
   kubectl scale --replicas=2 -f replica-set.yml
Deployment
================
This is also a high level Kubernetes object which can be used for
scalling and load balancing and it can also perfrom rolling update
vim deployment1.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
 labels:
  author: intelliqit
  type: proxyserver
spec:
 replicas: 3
 selector:
  matchLabels:
   type: proxyserver
 template:
  metadata:
   name: nginx-pod
   labels:
    type: proxyserver
  spec:
   containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80
          hostPort: 8080
==============================================================================
Day 24
==============================================================================
Create a mysql deployment
vim deployment2.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    type: db
    author: intelliqit
spec:
  replicas: 3
  selector:
    matchLabels:
       type: db
  template:
    metadata:
       name: mysql-pod
       labels:
         type: db
    spec:
       containers:
         - name: mydb
           image: mysql
           ports:
             - containerPort: 3306
                hostPort: 8080
           env:
             - name: MYSQL_ROOT_PASSWORD
              value: intelliqit
=============================================================================
Service Object
=====================
Use Case
=================
Create a service defintion file for port mapping an nginx pod
vim pod-defintion1.yml
---
apiVersion: v1
kind: Pod
metadata:
 name: nginx-pod
 labels:
  author: intellqit
  type: reverse-proxy
spec:
 containers:
  - name: appserver
    image: nginx
=========================================================
vim service1.yml
---
apiVersion: v1
kind: Service
metadata:
 name: nginx-service
spec:
 type: NodePort
 ports:
  - targetPort: 80
    port: 80
    nodePort: 30008
 selector:
  author: intellqit
  type: reverse-proxy
==================================================================================
=============================================================================
Create a service object of the type LoadBalancer for a tomcat pods
vim servcie2.yml
---
apiVersion: v1
kind: Service
metadata:
 name: tomcat-service
spec:
 type: LoadBalancer
 ports:
  - targetPort: 80
    port: 80
 selector:
  author: intellqit
  type: appserver
vim pod0defintion5.yml
vim pod-definition2.yml
---
apiVersion: v1
kind: Pod
metadata:
 name: tomcat-pod
 labels:
  type: appserver
  author: intelliqit
spec:
 containers:
  - name: tomcat
    image: tomee
...
================================================================================
Create a service object of the type load balancer for postgres pod
vim service3.yml
---
apiVersion: v1
kind: Service
metadata:
 name: postgres-service
spec:
 type: ClusterIp
 ports:
  - targetPort: 5432
    port: 5432
 selector:
  author: intellqit
  type: db
vim pod-defintion6.yml
apiVersion: v1
kind: Pod
metadata:
 name: mysql-pod
 labels:
  type: db
  author: intelliqit
spec:
 containers:
  - name: mydb
    image: mysql
    env:
      name: MYSQL_ROOT_PASSWORD
      value: intelliqit
================================================================================
Day 25
=================================================================================
Secrets
============
This is used to send encrypted data to the definiton files
Generally passwords for Databases can be encrypted using this
Create a pod defintion file to start a mysql pod and pass the environment
varible using the above secret
vim pod-defitintion5.yml
---
apiVersion: v1
kind: Pod
metadata:
 name: mysql-pod
 labels:
  author: intelliqit
  type: db
spec:
 containers:
  - name: mydb
    image: mysql:5
    env:
      - name: MYSQL_ROOT_PASSWORD
        valueFrom:
         secretKeyRef:
          name: mysql-pass
          key: password
...
============================================================================
==============================================================================
Create secrets for postgres password
vim secret2.yml
---
apiVersion: v1
kind: Secret
metadata:
 name: postgres-secrets
type: Opaque
stringData:
 password: intelliqit
 user: myuser
 db: mydb
Create a pod defitnition file that start starts postgres db using the above secrets
---
apiVersion: v1
kind: Pod
metadata:
 name: postgres-pod
 labels:
  author: intelliqit
  type: database
spec:
 containers:
  - name: mydb
    image: postgres
    env:
      - name: POSTGRES_PASSWORD
        valueFrom:
         secretKeyRef:
          name: postgres-secrets
          key: password
      - name: POSTGRES_USER
        valueFrom:
         secretKeyRef:
         name: postgres-secrets
         key:
     - name: POSTGRES_DB
       value: mydb
==============================================================================
===================================================================================
Day 26
==================================================================================
Node affinity
=================================
kubectl get nodes --show-labels
================================================================
Taints and toleration
========================
Taints and Tolerations
Node affinity, is a property of Pods that attracts them to a set of nodes (either
as a preference or a hard requirement). Taints are the opposite -- they allow a
node to repel a set of pods.
Tolerations are applied to pods, and allow (but do not require) the pods to
schedule onto nodes with matching taints.
Taints and tolerations work together to ensure that pods are not scheduled onto
inappropriate nodes. One or more taints are applied to a node; this marks that the
node should not accept any pods that do not tolerate the taints.
===================================================================
Volumes
==================
---
apiVersion: v1
kind: Pod
metadata:
 name: redis-pod
 labels:
  author: intelliqit
spec:
 containers:
  - name: redis
    image: redis
    volumeMounts:
      - name: redis-volume
        mountPath: /data/redis
 volumes:
  - name: redis-volume
    emptyDir: {}
We will see the data but not the s/w's (procps) we installed
cd redis
ls
===================================================================================
Day 27
===================================================================================
Helm Chart is a very feature-rich framework when you are working with complex
Kubernetes cluster and deployment. Helm chart provides a very convenient way to
pass values.yaml and use it inside your Helm Chart
tree helloworld
=========================================
How to ADD upstream Helm chart repository
------------------------------------------
helm repo add <REPOSITORY_NAME> <REPOSITORY_URL>
To add any chart repository you should know the name and repository url.
------------------------------------------
helm repo add bitnami https://charts.bitnami.com/bitnami
Verify the repository
---------------------------------
helm search repo bitnami
Removong a repository
-----------------------------
helm repo remove bitnami
===========================================
Demo
===============
In this tutorial, we are going to install WordPress with MariaDB using the Helm
Chart on Kubernetes cluster. With this installation, we are going to see - How we
can upgrade as well as rollback the Helm Chart release of WordPress. This complete
setup inherited the benefits of the Kubernetes .i.e. scalability and availability.
Since we are installing WordPress, so we need to have a database running behind the
WordPress application. From the database standpoint, we are going to use MariaDB.
Helm chart ships all these components in a single package, so that we need not
worry about installing each component separately.
Readme.md
=================
This Readme.md contains the installation instructions and it can be viewed using
the following command
To remove
kubect uninstall wordpress
===============================================================================
Day 28
===============================================================================
=======================================================================
Install prometheus and grafana
======================================
Username is admin
password: prom-operator
==============================================================================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  selector:
    matchLabels:
      run: php-apache
  replicas: 1
  template:
    metadata:
      labels:
         run: php-apache
    spec:
      containers:
      - name: php-apache
         image: intelliqit/mynew
         ports:
         - containerPort: 80
         resources:
           limits:
             cpu: 500m
           requests:
             cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
  labels:
    run: php-apache
spec:
  ports:
  - port: 80
  selector:
    run: php-apache
...
==================================================================================
Persistent volume is the storage that is used by Kubernetes for Volumes
Persistent volume claim is the amount of storage from the persistent volume that
will be allocatted to a Pod.It is alos a PVC that is attached to a Pod
=================================================================
Statefulsets
======================
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - name: tcp
      protocol: TCP
      port: 3306
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  replicas: 1
  serviceName: mysql
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: mysql
          image: mysql:5.6
          ports:
            - name: tpc
               protocol: TCP
               containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
               value: intelliqit
          volumeMounts:
            - name: task-pv-storage
              mountPath: /var/lib/mysql
=============================================================================
To drain nodes from the kubernetes cluster
kubectl drain <node name>
To add them back to the cluster
kubectl uncordon <node name>