<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: nithinalias</title>
    <description>The latest articles on DEV Community by nithinalias (@nithinalias).</description>
    <link>https://dev.to/nithinalias</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813244%2Fed7928ad-8f36-4274-8996-ed1e643a609f.png</url>
      <title>DEV Community: nithinalias</title>
      <link>https://dev.to/nithinalias</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nithinalias"/>
    <language>en</language>
    <item>
      <title>Devops Project - CI/CD Jenkins Ansible Kubernetes</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Tue, 05 Apr 2022 14:13:50 +0000</pubDate>
      <link>https://dev.to/nithinalias/devops-project-cicd-jenkins-ansible-kubernetes-6lj</link>
      <guid>https://dev.to/nithinalias/devops-project-cicd-jenkins-ansible-kubernetes-6lj</guid>
      <description>&lt;p&gt;&lt;strong&gt;Kubernetes on AWS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



                                               Dockerhub 
                                              /        \
                                            /            \                          
                                      Push/Image       Pull\Image        
                                        /                    \
      Pull Code       Copy Artifacts  /     Deploy Container   \
Github---------&amp;gt;Jenkin--------------&amp;gt;Ansible---------------&amp;gt;Kubernetes         |
                    |  
                    | Build Code
                   \|/
                  Maven  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setup Kubernetes on Amazon EKS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can follow same procedure in the official  AWS document &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html"&gt;Getting started with Amazon EKS – eksctl&lt;/a&gt;   &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pre-requisites:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an EC2 Instance &lt;/li&gt;
&lt;li&gt;Install AWSCLI latest verison &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Setup kubectl   &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download kubectl version 1.21
&lt;/li&gt;
&lt;li&gt;Grant execution permissions to kubectl executable
&lt;/li&gt;
&lt;li&gt;Move kubectl onto /usr/local/bin
&lt;/li&gt;
&lt;li&gt;Test that your kubectl installation was successful
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   curl &lt;span class="nt"&gt;-o&lt;/span&gt; kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl
   &lt;span class="nb"&gt;chmod&lt;/span&gt; +x ./kubectl
   &lt;span class="nb"&gt;mv&lt;/span&gt; ./kubectl /usr/local/bin 
   kubectl version &lt;span class="nt"&gt;--short&lt;/span&gt; &lt;span class="nt"&gt;--client&lt;/span&gt;      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setup eksctl &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download and extract the latest release
&lt;/li&gt;
&lt;li&gt;Move the extracted binary to /usr/local/bin
&lt;/li&gt;
&lt;li&gt;Test that your eksclt installation was successful
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   curl &lt;span class="nt"&gt;--silent&lt;/span&gt; &lt;span class="nt"&gt;--location&lt;/span&gt; &lt;span class="s2"&gt;"https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;_amd64.tar.gz"&lt;/span&gt; | &lt;span class="nb"&gt;tar &lt;/span&gt;xz &lt;span class="nt"&gt;-C&lt;/span&gt; /tmp
   &lt;span class="nb"&gt;sudo mv&lt;/span&gt; /tmp/eksctl /usr/local/bin
   eksctl version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an IAM Role and attache it to EC2 instance&lt;br&gt;&lt;br&gt;
   &lt;code&gt;Note: create IAM user with programmatic access if your bootstrap system is outside of AWS&lt;/code&gt;&lt;br&gt;&lt;br&gt;
   IAM user should have access to&lt;br&gt;&lt;br&gt;
   IAM&lt;br&gt;&lt;br&gt;
   EC2&lt;br&gt;&lt;br&gt;
   CloudFormation&lt;br&gt;&lt;br&gt;
   Note: Check eksctl documentaiton for &lt;a href="https://eksctl.io/usage/minimum-iam-policies/"&gt;Minimum IAM policies&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create your cluster and nodes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   eksctl create cluster &lt;span class="nt"&gt;--name&lt;/span&gt; cluster-name  &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--region&lt;/span&gt; region-name &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--node-type&lt;/span&gt; instance-type &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--nodes-min&lt;/span&gt; 2 &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--nodes-max&lt;/span&gt; 2 &lt;span class="se"&gt;\ &lt;/span&gt;
   &lt;span class="nt"&gt;--zones&lt;/span&gt; &amp;lt;AZ-1&amp;gt;,&amp;lt;AZ-2&amp;gt;

   example:
   eksctl create cluster &lt;span class="nt"&gt;--name&lt;/span&gt; valaxy-cluster &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--region&lt;/span&gt; ap-south-1 &lt;span class="se"&gt;\&lt;/span&gt;
   &lt;span class="nt"&gt;--node-type&lt;/span&gt; t2.small &lt;span class="se"&gt;\&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To delete the EKS clsuter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   eksctl delete cluster cluster-name &lt;span class="nt"&gt;--region&lt;/span&gt; ap-south-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Validate your cluster using by creating by checking nodes and by creating a pod&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   kubectl get nodes
   kubectl run tomcat &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;tomcat 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deploying Nginx pods on Kubernetes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Deploying Nginx Container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    kubectl create deployment  demo-nginx &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nginx &lt;span class="nt"&gt;--replicas&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 &lt;span class="nt"&gt;--port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;80
    &lt;span class="c"&gt;# kubectl deployment regapp --image=valaxy/regapp --replicas=2 --port=8080&lt;/span&gt;
    kubectl get all
    kubectl get pod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expose the deployment as service. This will create an ELB in front of those 2 containers and allow us to publicly access them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   kubectl expose deployment demo-nginx &lt;span class="nt"&gt;--port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;80 &lt;span class="nt"&gt;--type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;LoadBalancer
   &lt;span class="c"&gt;# kubectl expose deployment regapp --port=8080 --type=LoadBalancer&lt;/span&gt;
   kubectl get services &lt;span class="nt"&gt;-o&lt;/span&gt; wide
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Pod and Service using manifest file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create pod manifest file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
vim pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
  labels: 
    app: demo-app

spec:
  containers: 
    - name: demo-nginx
      image: nginx
      ports:
        - name: demo-nginx
          containerPort: 80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create service manifest file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim service.yml

apiVersion: v1
kind: Service
metadata:
  name: demo-service
  labels: 
    app: demo-app

spec:
  ports:
  - name: nginx-port
    port: 80
    targetPort: 80
  selector:
    app: demo-app
  type: LoadBalancer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run pod and service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f pod.yml
kubectl get pod

kubectl apply -f service.yml
kubectl get service

kubectl get all
kubectl get pods -o wide
kubectl get services -o wide
kubectl describe pod podname
kubectl describe service servicename
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setup Pod and Service&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                          selector:                labels:
                            app: demo-app            app: demo-app
External network--------------&amp;gt;Service-------------------&amp;gt;Pod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we know there will be large number of pods,When we send a request from External network to Service it looks for selector app: demo-app and forward the requset to the pods having Labels app: demo-app.That is why we use selector in service manifest file and labels in pod manifest file.&lt;/p&gt;

&lt;h4&gt;
  
  
  Integrating Kubernetes in CI/CD pipeline
&lt;/h4&gt;

&lt;p&gt;Go to ansible machine&lt;/p&gt;

&lt;p&gt;create a user and allow password authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
adduser ansadmin
id ansadmin

vim /etc/ssh/sshd_config

PasswordAuthentication yes
#PasswordAuthentication no

systemctl restart ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify visudo for created user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/sudoers

# User privilege specification
root    ALL=(ALL:ALL) ALL
ansadmin ALL=(ALL:ALL) NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User Privilege Lines&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root ALL=(ALL:ALL) ALL The first field indicates the username that the rule will apply to (root).

root ALL=(ALL:ALL) ALL The first “ALL” indicates that this rule applies to all hosts.

root ALL=(ALL:ALL) ALL This “ALL” indicates that the root user can run commands as all users.

root ALL=(ALL:ALL) ALL This “ALL” indicates that the root user can 
run commands as all groups.

root ALL=(ALL:ALL) ALL The last “ALL” indicates these rules apply to all commands.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ssh-key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;su - ansadmin
ssh-keygen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to Kubernetes machine&lt;/p&gt;

&lt;p&gt;Create Deployment and Service using manifest file&lt;/p&gt;

&lt;p&gt;Create deployment manifest file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
vim deployment.yml

apiVersion: v1
kind: Deployment
metadata:                        ---&amp;gt;Deployment name and label
  name: demo-webapp 
  labels: 
    app: webapp

spec:
  replicas: 2
  selector:                      ---&amp;gt;Create 2 pods from pod template
    matchLabels:
       app: webapp

template:                                                    |
    metadata:                    ---&amp;gt; pod definition         |        
      labels:                                                |       
        app: webapp                                          |
                                                             |           
    spec: containers                                         |---&amp;gt; Template to create a pod                                     |
    - name: webapp                                           |
      image: nithinalias/mytomcat ---&amp;gt; container definition  |
      imagePullPolicy: Always                                |
      ports:                                                 |
      - containerPort: 8080                                  |   

strategy:
  type: RollingUpdate
  rollingUpdate:                 ---&amp;gt;make sure only one pod updated at a time
    maxSugre: 1
    maxUnavialable: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create service manifest file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim service.yml

apiVersion: v1
kind: Service                    ---&amp;gt; Resource Type

metadata:
  name: demo-service             ---&amp;gt; service name and label
  labels: 
    app: webapp

spec:
  ports:
  - name: nginx-port
    port: 8080                   ---&amp;gt; port number exposed at cluster level
    targetPort: 8080             ---&amp;gt; port that container listening

  selector:                      ---&amp;gt; To which deployment it can send traffic
    app: webapp
  type: LoadBalancer             ---&amp;gt; service type          
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;labels in deployment manifest file need to match with selector in service manifest file.containerPort in deployment manifest file need to match with targetPort in service manifest file.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
passwd
adduser ansadmin
id ansadmin
docker login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow password authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/ssh/sshd_config

PermitRootLogin yes
PasswordAuthentication yes
#PasswordAuthentication no

systemctl restart ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify visudo for created user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/sudoers

# User privilege specification
root    ALL=(ALL:ALL) ALL
ansadmin ALL=(ALL:ALL) NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to ansible machine and add ip address of kubernetes machine and ansible machine(then only jenkin server can access ansible and kubernetes machine) inside ansible host file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/ansible/hosts

[ansible]
192.168.33.25
[kubernetes]
192.168.33.30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;copy the ssh public key to kubernetes machine and ansible machine itself(localhost).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;su - ansadmin
ssh-copy-id ansadmin@ip-of-ansiblemachine
ssh-copy-id ansadmin@ip-of-kubernetesmachine
ssh-copy-id root@ip-of-kubernetesmachine
ansible all -m ping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now authorized key will be generated inside /home/ansadmin/.ssh/authorized_keys of kubernetes,ansible machine and /root/.ssh/authorized_keys of kubernetes machine.&lt;/p&gt;

&lt;p&gt;create a directory,give ownership of ansadmin,add ansadmin to docker group and docker.sock permissions to all users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
mkdir /opt/docker
chown ansadmin:ansadmin /opt/docker
usermod -aG docker ansadmin
id ansadmin
chmod 777 /var/run/docker.sock
systemctl restart docker
su - ansadmin
docker login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create dockerfile for tomcat image creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/dockerfile

FROM tomcat
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ansible-playbook for building tomcat image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/build_tomcatimage.yml

---

- hosts: ansible

  tasks:
  - name: create docker image
    command: docker build -t mytomcat .
    args:
     chdir: /opt/docker

  - name: create tag to push image onto dockerhub
    command: docker tag mytomcat nithinalias/mytomcat

  - name: push docker image
    command: docker push nithinalias/mytomcat

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ansible-playbook to run deployment and service manifest file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/run_kube_manifestfile.yml

---

- hosts: kubernetes
  user: root

  tasks:
  - name: run deployment manifestfile on kubernetes
    command: kubectl apply -f deployment.yml

  - name: run service manifestfile on kubernetes
    command: kubectl apply -f service.yml

  - name: update deployment with new pods if image updated in dockerhub
    command: kubectl rollout restart deployment.apps/demo-webapp

# we don't want to mention exact path like /root/deployment.yml or /root/service.yml, since we are performing the task as root user.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Integrate Ansible with Jenkins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we need to add ansadmin ssh connection of ansible machine.&lt;/p&gt;

&lt;p&gt;Manage Jenkins  - Configure System - SSH servers - Add - Add name(ansible-server),Hostname(ip-of-ansible machine),username&lt;br&gt;
(ansadmin) - Advanced - Enable Use password authentication or use a different key - Add password of user ansadmin(instead you can use ssh-key/path of ssh-key if present) - Test configuration - Apply - save.&lt;/p&gt;

&lt;p&gt;Continuous Integration using Jenkins &lt;/p&gt;

&lt;p&gt;New Item - Add item name(CI webapp),select Maven project - OK - Add description - Select Git and add Repositories URL,Branch - Build Triggers(Poll SCM=* * * * * means every minute check the repository and if there any update it will trigger build - Build periodically means it will trigger build periodically even if repository not updated) - Build(Add Root POM=pom.xml,Goals and options=clean install) - Add post-build Action - Send build artifacts over SSH - Select SSH server name(ansible-server),Add sourcefile(webapp/target/*.war),Add Remove prefix(webapp/target),Add Remote directory="//opt//docker"(if it is blank webapp.war will be in /home/ansadmin),&lt;br&gt;
Exec command ="ansible-playbook /opt/docker/build_tomcatimage.yml" - Add post-build action - Build other projects - Go to post-build action(top) - Add Projects to build="CD webapp" - Enable Trigger only if build is stable - Apply - Save - Build Now - Console output.You can see all build outcomes in /var/lib/jenkins/workspace/CI webapp/webapp/target.surefire contain the reports of build and webapp.war is called artifacts.This webapp.war will be in ansible machine /opt/docker directory and copy this to tomcat image.The image created inside ansible machine is pushed to dockerhub.&lt;/p&gt;

&lt;p&gt;Continuous Deployment using Jenkins&lt;/p&gt;

&lt;p&gt;New Item - Add item name(CD webapp) - OK - Add description - Add post-build Action - Send build artifacts over SSH - Select SSH server name(ansible-server),Exec command =&lt;br&gt;
"ansible-playbook /opt/docker/run_kube_manifestfile.yml" - Apply - Save.&lt;/p&gt;

&lt;p&gt;Go to Kubernetes machine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;copy the EXTERNAL-IP of your service and paste it in browser.Now you get the webapp.&lt;br&gt;
                            OR&lt;/p&gt;

&lt;p&gt;You can go to LoadBalancer in AWS - Description - Copy DNS name - paste it in browser.Now you get the webapp.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Source:&lt;/code&gt; &lt;a href="https://github.com/yankils"&gt;https://github.com/yankils&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Devops Project - CI/CD Jenkins Ansible Docker</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Wed, 30 Mar 2022 04:10:33 +0000</pubDate>
      <link>https://dev.to/nithinalias/devops-project-1ig6</link>
      <guid>https://dev.to/nithinalias/devops-project-1ig6</guid>
      <description>&lt;p&gt;&lt;strong&gt;CI/CD pipeline using Git,Jenkins and Maven&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install jenkins using vagrant file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/focal64"

   config.vm.network "private_network", ip: "192.168.33.10"


  config.vm.provider "virtualbox" do |vb|
  vb.memory = "1536"
  end

   config.vm.provision "shell", inline: &amp;lt;&amp;lt;-SHELL
   apt update
   apt install -y openjdk-11-jre
   wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | apt-key add -
   sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ &amp;gt; /etc/apt/sources.list.d/jenkins.list'
   apt update
   apt install -y jenkins
   SHELL
   end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now login to jenkin &lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; and enter the password&lt;br&gt;
that you get from /var/lib/jenkins/secrets/initialAdminPassword.You can change the password from Manage jenkins - Manage users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrate Git with Jenkins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Manage Jenkins - Manage plugins - Available - Select Github - Install without restart - Go back to the top page - Manage Jenkins  - Global Tool Configuration - Add git name,Add Path to Git executable(This we get from linux terminal by using command 'whereis git' and you get ouput '/usr/bin/git'.If it won't work you can use 'git'.) - Apply - save&lt;/p&gt;

&lt;p&gt;New Item - Add item name(PullcodefromGithub),select Freestyle - OK - Add description - Select Git and add Repositories URL,Branch - Apply - Save - Build Now - Console output.You can see the output &lt;br&gt;
/var/lib/jenkins/workspace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrate Maven with Jenkins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install maven&lt;/p&gt;

&lt;p&gt;Download the Binary tar.gz archive from &lt;a href="https://maven.apache.org/download.cgi"&gt;https://maven.apache.org/download.cgi&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
tar -xvzf apache-maven-3.8.5-bin.tar.gz
cd /opt/apache-maven-3.8.5/bin
./mvn -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Outside this directory 'mvn -v' won't work.we need to set up the environment variables(path of maven,java).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;find / -name jvm
export PATH=$PATH:/opt/apache-maven-3.8.5:/opt/apache-maven-3.8.5/bin:/usr/lib/jvm/java-11-openjdk-amd64
echo $PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To change enviornment variables permanentely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /root/.profile

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

M2_HOME=/opt/apache-maven-3.8.5
M2=/opt/apache-maven-3.8.5/bin
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2
export PATH
mesg n 2&amp;gt; /dev/null || true

echo $PATH
source .profile
echo $PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Manage Jenkins - Manage plugins - Available - Select Maven Integration - Install without restart - Go back to the top page - Manage Jenkins  - Global Tool Configuration - Add JDK name,JAVA_HOME(/usr/lib/jvm/java-11-openjdk-amd64),Maven name,MAVEN_HOME(/opt/apache-maven-3.8.5) - Apply - save&lt;/p&gt;

&lt;p&gt;New Item - Add item name(Mavenproject),select Maven project - OK - Add description - Select Git and add Repositories URL,Branch - Build(Add Root POM=pom.xml,Goals and options=clean install) - Apply - Save - Build Now - Console output.You can see all build outcomes in /var/lib/jenkins/workspace/Mavenproject/webapp/target/.surefire contain the reports of build and webapp.war is called artifacts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrating Tomcat server in CI/CD pipeline&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


       Pull Code        Copy Artifacts &amp;amp; Deploy Code
Github----------&amp;gt;Jenkin-----------------------------&amp;gt;Tomcat server
                    |  
                    | Build Code
                   \|/
                  Maven  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setup a Tomcat server&lt;/strong&gt;&lt;br&gt;
create a virtual machine and install JDK,Tomcat server.&lt;br&gt;
Download tar.gz tomcat packages from &lt;a href="https://tomcat.apache.org/download-80.cgi"&gt;https://tomcat.apache.org/download-80.cgi&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
apt update
apt install -y openjdk-11-jre
cd /opt
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.77/bin/apache-tomcat-8.5.77.tar.gz
tar -xvzf apache-tomcat-8.5.77.tar.gz
cd /opt/apache-tomcat-8.5.77/bin
./startup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now login to tomcat &lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; - Manage App - 403 Access Denied&lt;br&gt;
By default the Manager is only accessible from a browser running on the same machine as Tomcat. If you wish to modify this restriction, you'll need to edit the Manager's context.xml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;find / -name context.xml
vim /opt/apache-tomcat-8.5.77/webapps/host-manager/META-INF/context.xml
vim /opt/apache-tomcat-8.5.77/webapps/manager/META-INF/context.xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Disable the line shown below by using &amp;lt;!-- --&amp;gt; in both context.xml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--  &amp;lt;Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /&amp;gt;  --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now restart tomcat server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /opt/apache-tomcat-8.5.77/bin
./shutdown.sh
./startup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to tomcat &lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; - Manage App.Now it will ask for credentials.&lt;/p&gt;

&lt;p&gt;create link files for tomcat startup.sh and shutdown.sh&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ln -s /opt/apache-tomcat-8.5.77/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/apache-tomcat-8.5.77/bin/shutdown.sh /usr/local/bin/tomcatdown
tomcatup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update users information in the tomcat-users.xml file goto tomcat home directory and Add below users to /opt/apache-tomcat-8.5.77/conf/tomcat-users.xml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;role rolename="manager-gui"/&amp;gt;
&amp;lt;role rolename="manager-script"/&amp;gt;
&amp;lt;role rolename="manager-jmx"/&amp;gt;
&amp;lt;role rolename="manager-status"/&amp;gt;
&amp;lt;user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/&amp;gt;
&amp;lt;user username="deployer" password="deployer" roles="manager-script"/&amp;gt;
&amp;lt;user username="tomcat" password="s3cret" roles="manager-gui"/&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now restart tomcat server and using credential login to tomcat &lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; - Manage App&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tomcatdown
tomcatup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Integrate Tomcat with jenkin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Manage Jenkins - Manage plugins - Available - Select Deploy to container(This plugin allows you to deploy a war to a container after a successful build) - Install without restart - Go back to the top page - Manage Jenkins - Manage Credentials - Jenkins - Global credentials - Add credentials - kind(username with password) - username="deployer",password="deployer"(If one system want to access another system we need roles="manager-script"),ID="tomcat_deployer",Description="tomcat_deployer" - Apply - save&lt;/p&gt;

&lt;p&gt;New Item - Add item name(BuildAndDeploy),select Maven project - OK - Add description - Select Git and add Repositories URL,Branch - Build Triggers(Poll SCM=* * * * * means every minute check the repository and if there any update it will trigger build - Build periodically means it will trigger build periodically even if repository not updated) - Build(Add Root POM=pom.xml,Goals and options=clean install) - Add post-build Action - Deploy war/ear to a container - Add WAR/EAR files=*&lt;em&gt;/&lt;/em&gt;.war,Add Containers=Tomcat 8.x Remote,select the deployer credentail that we created,Tomcat URL=&lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; - Apply - Save - Build Now - Console output.You can see all build outcomes in /var/lib/jenkins/workspace/BuildAndDeploy/webapp/target.&lt;br&gt;
surefire contain the reports of build and webapp.war is called artifacts.This webapp.war is deployed inside /opt/apache-tomcat-8.5.77/webapps of tomcat server by authencating the credentials. &lt;/p&gt;

&lt;p&gt;Using credential login to tomcat &lt;a href="http://ip-address:8080"&gt;http://ip-address:8080&lt;/a&gt; - Manage App - select the path webapp - Now you get the required output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrate Docker in CI/CD Pipeline&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

       Pull Code        Copy Artifacts &amp;amp; Deploy Code
Github----------&amp;gt;Jenkin-----------------------------&amp;gt; Docker
                    |  
                    | Build Code
                   \|/
                  Maven  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a docker virtual machine using Vagrantfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/focal64"

  config.vm.network "private_network", ip: "192.168.33.20"

  config.vm.provider "virtualbox" do |vb|
  vb.memory = "1024"
  end

  config.vm.provision "shell", inline: &amp;lt;&amp;lt;-SHELL
     apt update -y
     apt install apt-transport-https ca-certificates -y
     curl software-properties-common
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
     add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
     apt-cache policy docker-ce
     apt install docker-ce -y
  SHELL
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pull and run tomcat image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
docker pull tomcat
docker images
docker run -d --name tomcat-container 8081:8080 tomcat
docker ps -a 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to tomcat &lt;a href="http://ip-address:8081.It"&gt;http://ip-address:8081.It&lt;/a&gt; will show an error like HTTP Status 404 – Not Found.This is because when we browse &lt;a href="http://ip-address:8081"&gt;http://ip-address:8081&lt;/a&gt; it will look into /usr/local/tomcat/webapps&lt;br&gt;
directory.By default this directory will be empty and contents will be present inside /usr/local/tomcat/webapps.dist.so we need to copy this contents to webapp directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it tomcat-container /bin/bash
cd /usr/local/tomcat/webapps
ls
cd /usr/local/tomcat/webapps.dist
ls
cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to tomcat &lt;a href="http://ip-address:8081"&gt;http://ip-address:8081&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create tomcat using Dockerfile&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
vim Dockerfile

FROM ubuntu
RUN apt-get -y update
RUN apt-get -y install openjdk-11-jre
RUN mkdir /opt/tomcat
WORKDIR /opt/tomcat
ADD https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.60/bin/apache-tomcat-9.0.60.tar.gz /opt/tomcat
RUN tar -xvzf apache-tomcat-9.0.60.tar.gz
RUN mv apache-tomcat-9.0.60/* /opt/tomcat
EXPOSE 8080
CMD [ "/opt/tomcat/bin/catalina.sh", "run"]


docker build -t mytomcatserver .
docker run -d --name mytomcat-server -p 8083:8080 mytomcatserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to tomcat &lt;a href="http://ip-address:8083"&gt;http://ip-address:8083&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create customized Dockerfile for tomcat&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
vim Dockerfile

FROM tomcat
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps


docker build -t mytomcat .
docker run -d --name mytomcat-container -p 8085:8080 mytomcat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to tomcat &lt;a href="http://ip-address:8085"&gt;http://ip-address:8085&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrate docker with Jenkins and Update tomcat dockerfile to automate deployment process&lt;/strong&gt;&lt;br&gt;
Enable password authentication in docker machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
vim /etc/ssh/sshd_config

PasswordAuthentication yes
#PasswordAuthentication no

systemctl restart ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a user and add to group 'docker'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adduser dockeradmin
cat /etc/group
usermod -aG docker dockeradmin
id dockeradmin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create tomcat dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir /opt/docker
vim /opt/docker/Dockerfile

FROM tomcat
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we need to give ownership of Dockerfile to dockeradmin.Then only we can automate the deployment using jenkin server.After building, webapp.war inside jenkin server workspace(/var/lib/jenkins/workspace/BuildAndDeploy/webapp/target) will be move to /opt/docker of docker machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chown -R dockeradmin:dockeradmin /opt/docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to Jenkin Server &lt;/p&gt;

&lt;p&gt;Manage Jenkins - Manage plugins - Available - Select Publish Over SSH(Send build artifacts over SSH) - Install without restart - Go back to the top page - Manage Jenkins  - Configure System - SSH servers - Add - Add name(dockerhost),Hostname(ip-of-dockermachine),username(dockeradmin) - Advanced - Enable Use password authentication or use a different key - Add password of user dockeradmin(instead you can use ssh-key/path of ssh-key if present) - Test configuration - Apply - save.&lt;/p&gt;

&lt;p&gt;New Item - Add item name(BuildAndDeploy),select Maven project - OK - Add description - Select Git and add Repositories URL,Branch - Build Triggers(Poll SCM=* * * * * means every minute check the repository and if there any update it will trigger build - Build periodically means it will trigger build periodically even if repository not updated) - Build(Add Root POM=pom.xml,Goals and options=clean install) - Add post-build Action - Send build artifacts over SSH - Select SSH server name(dockerhost),Add sourcefile(webapp/target/*.war),Add Remove prefix(webapp/target),Add Remote directory="//opt//docker"(if it is blank webapp.war will be in /home/dockeradmin),&lt;br&gt;
Exec command ="cd /opt/docker;&lt;br&gt;
  docker build -t mytomcat .;&lt;br&gt;
  docker container stop tomcat-container;&lt;br&gt;
  docker rm tomcat-container;&lt;br&gt;
  docker run -d --name tomcat-container -p 8081:8080 mytomcat" - Apply - Save - Build Now - Console output.You can see all build outcomes in /var/lib/jenkins/workspace/BuildAndDeploy/webapp/target.&lt;br&gt;
surefire contain the reports of build and webapp.war is called &lt;br&gt;
artifacts.This webapp.war will be in docker machine /opt/docker directory and copy this to tomcat image for tomcat container creation.&lt;/p&gt;

&lt;p&gt;Now you can browse &lt;a href="http://ip-adress:8081/webapp/"&gt;http://ip-adress:8081/webapp/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrating ansible in CI/CD pipeline&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;



                                               Dockerhub 
                                              /        \
                                            /            \                          
                                      Push/Image       Pull\Image        
                                        /                    \
      Pull Code       Copy Artifacts  /     Deploy Container   \
Github---------&amp;gt;Jenkin--------------&amp;gt;Ansible---------------&amp;gt;Docker
                    |  
                    | Build Code
                   \|/
                  Maven  


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a ansible machine having docker using vagrant file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/focal64"

  config.vm.network "private_network", ip: "192.168.33.25"

  config.vm.provider "virtualbox" do |vb|
  vb.memory = "1536"
  end

  config.vm.provision "shell", inline: &amp;lt;&amp;lt;-SHELL
     apt update -y
     apt install ansible -y
     apt install apt-transport-https ca-certificates -y
     curl software-properties-common
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
     add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
     apt-cache policy docker-ce
     apt install docker-ce -y
  SHELL
end


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create a user and allow password authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
adduser ansadmin
id ansadmin

vim /etc/ssh/sshd_config

PasswordAuthentication yes
#PasswordAuthentication no

systemctl restart ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify visudo for created user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/sudoers

# User privilege specification
root    ALL=(ALL:ALL) ALL
ansadmin ALL=(ALL:ALL) NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User Privilege Lines&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root ALL=(ALL:ALL) ALL The first field indicates the username that the rule will apply to (root).

root ALL=(ALL:ALL) ALL The first “ALL” indicates that this rule applies to all hosts.

root ALL=(ALL:ALL) ALL This “ALL” indicates that the root user can run commands as all users.

root ALL=(ALL:ALL) ALL This “ALL” indicates that the root user can 
run commands as all groups.

root ALL=(ALL:ALL) ALL The last “ALL” indicates these rules apply to all commands.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ssh-key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;su - ansadmin
ssh-keygen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to docker machine and create user,add to group docker,docker.sock permissions to all users&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
adduser ansadmin
usermod -aG docker ansadmin
id ansadmin
chmod 777 /var/run/docker.sock
systemctl restart docker
docker login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow password authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/ssh/sshd_config

PasswordAuthentication yes
#PasswordAuthentication no

systemctl restart ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify visudo for created user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/sudoers

# User privilege specification
root    ALL=(ALL:ALL) ALL
ansadmin ALL=(ALL:ALL) NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to ansible machine and add ip address of docker machine and ansible machine(then only jenkin server can access ansible and docker machine) inside ansible host file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /etc/ansible/hosts

[docker]
192.168.33.20
[ansible]
192.168.33.25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;copy the ssh public key to docker machine and ansible machine itself(localhost).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;su - ansadmin
ssh-copy-id ansadmin@ip-of-ansiblemachine
ssh-copy-id ansadmin@ip-of-dockermachine
ansible all -m ping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now authorized key will be generated inside /home/ansadmin/.ssh/authorized_keys of docker and ansible machine.&lt;/p&gt;

&lt;p&gt;create a directory,give ownership of ansadmin,add ansadmin to docker group and docker.sock permissions to all users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su -
mkdir /opt/docker
chown ansadmin:ansadmin /opt/docker
usermod -aG docker ansadmin
id ansadmin
chmod 777 /var/run/docker.sock
systemctl restart docker
su - ansadmin
docker login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create dockerfile for tomcat image creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/dockerfile

FROM tomcat
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ansible-playbook for building tomcat image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/build_tomcatimage.yml

---

- hosts: ansible

  tasks:
  - name: create docker image
    command: docker build -t mytomcat .
    args:
     chdir: /opt/docker

  - name: create tag to push image onto dockerhub
    command: docker tag mytomcat nithinalias/mytomcat

  - name: push docker image
    command: docker push nithinalias/mytomcat

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate ansible-playbook for deploy tomcat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim /opt/docker/deploy_tomcat.yml

---
- hosts: docker

  tasks:
  - name: stop existing container
    command: docker stop tomcat-container
    ignore_errors: yes

  - name: remove the container
    command: docker rm tomcat-container
    ignore_errors: yes

  - name: remove image
    command: docker rmi nitinalias/mytomcat
    ignore_errors: yes

  - name: create container
    command: docker run -d --name tomcat-container -p 8082:8080 nithinalias/mytomcat


# If there is no container it will show error.To avoid this we use ignore errors: yes
~

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Integrate Ansible with Jenkins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Already we installed Publish Over SSH plugin and added dockeradmin&lt;br&gt;
ssh connection of docker machine.Now we need to add ansadmin&lt;br&gt;
ssh connection of ansible machine.&lt;/p&gt;

&lt;p&gt;Manage Jenkins  - Configure System - SSH servers - Add - Add name(ansible-server),Hostname(ip-of-ansible machine),username&lt;br&gt;
(ansadmin) - Advanced - Enable Use password authentication or use a different key - Add password of user ansadmin(instead you can use ssh-key/path of ssh-key if present) - Test configuration - Apply - save.&lt;/p&gt;

&lt;p&gt;New Item - Add item name(BuildAndDeployUsingAnsible),select Maven project - OK - Add description - Select Git and add Repositories URL,Branch - Build Triggers(Poll SCM=* * * * * means every minute check the repository and if there any update it will trigger build - Build periodically means it will trigger build periodically even if repository not updated) - Build(Add Root POM=pom.xml,Goals and options=clean install) - Add post-build Action - Send build artifacts over SSH - Select SSH server name(ansible-server),Add sourcefile(webapp/target/*.war),Add Remove prefix(webapp/target),Add Remote directory="//opt//docker"(if it is blank webapp.war will be in /home/ansadmin),&lt;br&gt;
Exec command ="ansible-playbook /opt/docker/build_tomcatimage.yml;&lt;br&gt;
sleep 10;&lt;br&gt;
ansible-playbook /opt/docker/deploy_tomcat.yml" - Apply - Save - Build Now - Console output.You can see all build outcomes in /var/lib/jenkins/workspace/BuildAndDeployUsingAnsible/&lt;br&gt;
webapp/target.surefire contain the reports of build and webapp.war is called artifacts.This webapp.war will be in ansible machine /opt/docker directory and copy this to tomcat image.The image created inside ansible machine is pushed to dockerhub.Pull this image from dockerhub to create tomcat-container inside docker &lt;br&gt;
machine. &lt;/p&gt;

&lt;p&gt;Now you can browse &lt;a href="http://ip-adress:8082/webapp/"&gt;http://ip-adress:8082/webapp/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Note: To check ansible-playbook and run ansible-playbook on particular host you can use the command shown below.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-playbook /opt/docker/deploy_tomcat.yml --check
ansible-playbook /opt/docker/deploy_tomcat.yml --limit ip-adress/groupname 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Source:&lt;/code&gt; &lt;a href="https://github.com/yankils"&gt;https://github.com/yankils&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to Kubernetes</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Mon, 21 Mar 2022 06:00:06 +0000</pubDate>
      <link>https://dev.to/nithinalias/introduction-to-kubernetes-7m6</link>
      <guid>https://dev.to/nithinalias/introduction-to-kubernetes-7m6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction to MINIKUBE&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MINIKUBE

- Open source tool
- To run K8s locally on your system
- Runs a single node K8s cluster inside a VM on your system.
 ___________________                    _____________________
|                   |                  |                     |
|    Minikube       |                  |      MASTER         |
|       /|\         |                  |      ______         |
|        |          |                  |      ______         |
|        |          |______________|\  |                     |
|       VM          |              | \ |      WORKER         |
|       /|\         |______________| / |      ______         |
|        |          |              |/  |      ______         |
|        |          |                  |                     |
|        |          |                  |DOCKER PRE-INSTALLED |
|     HOST OS       |                  |                     |
|___________________|                  |_____________________|
  HOST MACHINE                                 NODE

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install minikube&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt-get update
apt-get install curl
apt-get install apt-transport-https
apt install virtualbox virtualbox-ext-pack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install kubectl&lt;br&gt;
&lt;a href="https://kubernetes.io/docs/tasks/tools/"&gt;https://kubernetes.io/docs/tasks/tools/&lt;/a&gt;&lt;br&gt;
Install Docker&lt;br&gt;
&lt;a href="https://docs.docker.com/engine/install/ubuntu/"&gt;https://docs.docker.com/engine/install/ubuntu/&lt;/a&gt;&lt;br&gt;
Install minikube&lt;br&gt;
&lt;a href="https://minikube.sigs.k8s.io/docs/start/"&gt;https://minikube.sigs.k8s.io/docs/start/&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;minikube start
kubectl get po -A
minikube dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;K8s Commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get nodes
kubectl get pods -o wide
kubectl cluster-info
kubectl config view
kubectl config get-clusters
kubectl config delete-clusters
kubectl config get-contexts
kubectl config current-context
kubectl config set-context context1 --cluster=cluster1 --user=user1 --namespace=namespace1
kubectl config use-context context1
kubectl config current-context
kubectl config get-contexts
kubectl config delete-context context1
kubectl get namespace
kubectl config set-context --current --namespace=namespace1
kubectl create namespace namespace2
kubectl get namespace
kubectl delete namespace namespace2
kubectl get pods --namespace=default
kubectl api-versions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Introduction to KUBECTL&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KUBECTL

- Kubernetes command-line tool
- Allows you to run commands against K8s clusters.
- use kubectl to deploy applications, inspect and manage cluster 
  resources, and view logs
- KUBECTL is the most powerful of three clients(UI,API,KUBECTL).
- create pods,create services,destroy pods....

                                                       UI
                                 ______________________/_    
                                |       ____________  /  |
                                |      |            |/   |
   MASTER PROCESSES--------------------| API SERVER |----- API
                                |      |____________|\   |           
                                |                     \  |
                                |                    CLI (KUBECTL)
                                |                        |
                                | /  POD       SERVICE   | 
   WORKER PROCESSES------------------POD       SECRET    |
                                | \  POD       CONFIGMAP |
                                |                        | 
                                |________________________|
                                           NODE
Install Kubectl
https://kubernetes.io/docs/tasks/tools/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To Create a Kubernetes Cluster Using Kubeadm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use the two links shown below for Kubernetes Cluster creation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
https://github.com/kodekloudhub/certified-kubernetes-administrator-course

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

https://www.cloudsigma.com/how-to-install-and-use-kubernetes-on-ubuntu-20-04/

https://alibaba-cloud.medium.com/how-to-install-and-deploy-kubernetes-on-ubuntu-16-04-6769fd1646db

https://www.digitalocean.com/community/tutorials/how-to-create-a-kubernetes-cluster-using-kubeadm-on-ubuntu-20-04
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ___________________                    _____________________
|                   |                  |                     |
|Container Platform |                  |  Container Platform |
|___________________|                  |_____________________|
| Kubeadm,Kubeclet, |                  |  Kubeadm,Kubeclet,  |
|     Kubectl       |                  |      Kubectl        | 
|___________________|                  |_____________________|
|Designate to become|                  |                     |
|      Master       |         _________|  Join the Cluster   |
|___________________|         |        |_____________________|
|  CN Installation  |         |                 SLAVE
|___________________|         |                                 
|                   |         |
|   Cluster Ready   |         | 
|                   |/|_______| 
|___________________|\|                  
      MASTER                                   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kubeadm Commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubeadm init
kubeadm join
kubeadm reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deploy a webapplication using kubernetes&lt;/strong&gt;&lt;br&gt;
In Master create directory and make deployment file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir demo
cd demo
vim webappdeploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp1
  labels:
    app: webapp-sql
    tier: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp-sql
      tier: frontend
  template:
    metadata:
      labels:
        app: webapp-sql
        tier: frontend
    spec:
      containers:
      - name: webapp1
        image: hshar/webapp
        ports:
        - containerPort: 8081

vim mysqldeploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqldb
  labels:
    app: webapp-sql
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp-sql
      tier: backend
  template:
    metadata:
      labels:
        app: webapp-sql
        tier: backend
    spec:
      containers:
      - name: mysql
        image: hshar/mysql:5.5
        ports:
        - containerPort: 3306

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the deployment commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f webappdeploymentfile.yml
kubectl apply -f mysqldeploymentfile.yml
kubectl get deployment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make service file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim webappservice.yml

apiVersion: v1
kind: Service
metadata:
  name: webapp-sql
spec:
  selector:
    app: webapp-sql
    tier: frontend
  ports:
  - port: 80
  type: NodePort

vim mysqlservice.yml

apiVersion: v1
kind: Service
metadata:
  name: webapp-sql1
spec:
  selector:
    app: webapp-sql
    tier: backend
  ports:
  - port: 3306
  clusterIP: None

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the service commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f webappservice.yml
kubectl apply -f mysqlservice.yml
kubectl get services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter into webapp container and modify it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pods -o wide
kubectl exec -it &amp;lt;webapppodname&amp;gt; bash
kubectl exec -it &amp;lt;mysqlpodname&amp;gt; bash
kubectl get services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open the webapplication in the browser &lt;a href="http://ip-adress:port"&gt;http://ip-adress:port&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to create single pod&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;use the below commands in master node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get nodes
kubectl run pod1 --image nginx
kubectl get pods -o wide
kubectl describe pod pod1
kubectl describe pod pod1 | less
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to create multiple pods and update them using yaml file&lt;/strong&gt;&lt;br&gt;
Use the deployment yaml file given below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim deploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mynginx
spec:
  replicas: 3
  selector:
    matchLabels:
      type: webserver
  template:
    metadata:
      name: mypod
      labels:
        type: webserver
    spec:
      containers:
        - name: c1
          image: nginx:1.7.9

kubectl get all
kubectl describe deployment mynginx
kubectl set image deploy mynginx c1=nginx:1.9.1
kubectl get all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we start updating nginx:1.7.9 to nginx:1.9.1,replicaset of nginx:1.7.9 scaledown and replicaset of nginx:1.9.1 scaleup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ingress&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim Ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 8080


kubectl apply -f Ingress.yml
kubectl get ingress

sudo vim /etc/hosts

10.0.2.15  hello-world.info
#10.0.2.15 is the ADDRESS that we got from 'kubectl get ingress' command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can check the traffic using the command 'curl hello-world.info'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Statefulset&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim statefulset.yml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
     matchLabels:
       app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

kubectl apply -f statefulset.yml
kubectl get all
kubectl get pods -w -l app=nginx
kubectl delete pod -l app=nginx
kubectl get pod -l app=nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see pods are terminating and if you use 'kubectl get pod -l app=nginx' command after sometime, it began container creating.Deleting or Scaling a statefulset down will not delete the volume associated with the statefulset.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitoring Kubernetes cluster&lt;/strong&gt;&lt;br&gt;
Installing and configuring helm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl https://baltocdn.com/helm/signing.asc | sudo apt-key add -
sudo apt-get install apt-transport-https --yes
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

kubectl get ns
kubectl create namespace prometheus
kubectl get ns
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack --namespace prometheus
kubectl get pods -n prometheus
kubectl port-forward -n prometheus prometheus-kube-prometheus-stack-prometheus-0 9090
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you browse prometheus by &lt;a href="http://ip-addresss:9090.Here"&gt;http://ip-addresss:9090.Here&lt;/a&gt; you can check memory,pod,namespace,alert,graph,status....&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl port-forward -n prometheus kube-prometheus-stack-grafana-6c74f5565b-vfbxq 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you browse grafana by &lt;a href="http://ip-addresss:3000.For"&gt;http://ip-addresss:3000.For&lt;/a&gt; getting username and password use the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get secret --namespace prometheus prometheus-grafana -o yaml

echo "admin-password" | base64 --decode
echo "admin-user" | base64 --decode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can login grafana and monitor kubernetes clusters.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Docker Swarm services</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Wed, 16 Mar 2022 10:57:16 +0000</pubDate>
      <link>https://dev.to/nithinalias/docker-swarm-230p</link>
      <guid>https://dev.to/nithinalias/docker-swarm-230p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Docker Swarm Commands&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker swarm init --advertise-addr &amp;lt;ip-addr&amp;gt;
docker service ls
docker service ps &amp;lt;name&amp;gt;
docker service create &amp;lt;name&amp;gt; &amp;lt;image-name&amp;gt;
docker service rm &amp;lt;name&amp;gt;
docker service scale &amp;lt;name&amp;gt;=5
docker swarm leave --force
docker node ls
docker node ps
docker node rm &amp;lt;id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create three virtual machines using vagrant(managenode,node1,node2).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# This defines the version of vagrant
Vagrant.configure("2") do |config|

MACHINE = ["managenode","node1","node2"]
N = 2

(0..N).each do |i|

  config.vm.define "#{MACHINE[i]}" do |node|
  node.vm.hostname = MACHINE[i]
  node.vm.box = "ubuntu/focal64"
  node.vm.network :private_network, ip: "192.168.33.#{10+i}"

  node.vm.provider "virtualbox" do |vb|
  vb.memory = "1500"
  end

  node.vm.provision "shell", inline: &amp;lt;&amp;lt;-SHELL
     apt update -y
     apt install apt-transport-https ca-certificates -y
     curl software-properties-common
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
     add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
     apt-cache policy docker-ce
     apt install docker-ce -y
     SHELL


end
end
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login managenode and initialize docker swarm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker swarm init --advertise-addr &amp;lt;ip-addr of server&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you will receive docker swarm join token address.Copy and paste it node1 and node2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker swarm join --token &amp;lt;join-token&amp;gt; &amp;lt;ipaddr:port number&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can see the created nodes from managenode using the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker node ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;create an image&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a dockerfile for Apache webserver and Copy the Index.html&lt;/p&gt;

&lt;p&gt;Make a directory&lt;br&gt;
mkdir /test&lt;br&gt;
cd /test&lt;br&gt;
Create a sample web page with name index.html&lt;br&gt;
vim /test/index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;

&amp;lt;p&amp;gt;My Personal website&amp;lt;/p&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a file dockerfile&lt;br&gt;
vim /test/dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
COPY index.html /var/www/html/
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to build the docker file by using the docker build command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker build -t myapachewebserver .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Service&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service create --name "Apachewebserver" -p 8080:80 myapachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To list services&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To list task of services&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service ps Apachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can browse the webserver &lt;/p&gt;

&lt;p&gt;&lt;a href="http://ipaddress-of-managenode:8080"&gt;http://ipaddress-of-managenode:8080&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node1:8080"&gt;http://ipaddress-of-node1:8080&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node2:8080"&gt;http://ipaddress-of-node2:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here if any of the node terminated, the service would disturb in that node.If node1 is down then we wouldnot get the webserver through &lt;a href="http://ipaddress-of-node1:8080"&gt;http://ipaddress-of-node1:8080&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To remove service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service rm Apachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If any of the node terminated, the service wouldnot disturbed if you use "--mode global"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service create --name "Apachewebserver" -p 8080:80 --mode global myapachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to make replica you can use command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service rm Apachewebserver

docker service create --name "Apachewebserver" -p 8080:80 --replica 2 myapachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here managenode will select the best performing node from the node1 and node2 along with managenode.&lt;/p&gt;

&lt;p&gt;Now you can browse the webserver &lt;/p&gt;

&lt;p&gt;&lt;a href="http://ipaddress-of-managenode:8080"&gt;http://ipaddress-of-managenode:8080&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node1:8080"&gt;http://ipaddress-of-node1:8080&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node2:8080"&gt;http://ipaddress-of-node2:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can check the services by using the command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker node ps
docker service ps Apachewebserver
docker service ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can check whether the containers present or not  by using the command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here we could see the Apachewebserver container only in managenode and any one of the nodes(node1 and node2).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scaling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can scale the service by using the command shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service scale Apachewebserver=5
docker service ps Apachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you could see 5 copies of Apachewebserver.Managenode will disrtibute the service according to the best performing nodes like  managenode,2 node1 and 2 node2.&lt;/p&gt;

&lt;p&gt;If you want to drain the service you can use the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker node update --availability drain managenode
docker service ps Apachewebserver
docker node ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your managenode availability got drained and an additional node will be created.Now managenode has no power to scale up and down the services.Use the below command to retain the power of managenode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker node update --availability active managenode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Docker Swarm Service using overlay network demo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First remove the service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service rm Apachewebserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create an apache service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service create --name Apache2 --mode global -d -p 8003:80 httpd

docker service ls
docker service ps Apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can browse the webserver &lt;/p&gt;

&lt;p&gt;&lt;a href="http://ipaddress-of-managenode:8003"&gt;http://ipaddress-of-managenode:8003&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node1:8003"&gt;http://ipaddress-of-node1:8003&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node2:8003"&gt;http://ipaddress-of-node2:8003&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a overlay network&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create -d overlay myoverlay1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets create a web application using php and mysql.we can pull a sample application from hshar/webapp and create a service out of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service create --name webapp1 -d --network myoverlay1 -p 8001:80 hshar/webapp

docker service ls
docker service ps Apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create a service for mysql&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker service create --name mysql -d --network myoverlay1 -p 3306:3306 hshar/mysql:5.5

docker service ls
docker service ps mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Managenode will distribute the webapp1 and myql in different nodes&lt;br&gt;
as part of load balancing.&lt;/p&gt;

&lt;p&gt;Login to the node that containing webapp1 and enter into container to modify the webapp1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
docker exec -it &amp;lt;containerID&amp;gt; bash
nano /var/www/html/index.php

&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;Docker Sample App&amp;lt;/title&amp;gt;

&amp;lt;?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$servername = "mysql";
$username = "root";
$password = "edureka";
$dbname = "docker";
$name=$_POST["name"];
$phone=$_POST["phone"];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn-&amp;gt;connect_error) {
    die("Connection failed: " . $conn-&amp;gt;connect_error);
}

$sql = "INSERT INTO emp (name, phone)
VALUES ('".$name."', '".$phone."')";

if ($conn-&amp;gt;query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "&amp;lt;br&amp;gt;" . $conn-&amp;gt;error;
}

$conn-&amp;gt;close();
}
?&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
        &amp;lt;form action="index.php" method="POST"&amp;gt;
                &amp;lt;input type="text" name="name"&amp;gt;
                &amp;lt;input type="text" name="phone"&amp;gt;
                &amp;lt;input type="submit" name="submit"&amp;gt;
        &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login to the node that containing mysql and enter into container to modify the mysql.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
docker exec -it &amp;lt;containerID&amp;gt; bash

echo $MYSQL_ROOT_PASSWORD
mysql -u root -pedureka
CREATE DATABASE docker;
USE docker;
CREATE TABLE emp(name VARCHAR(15),phone VARCHAR(12));
exit

exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can browse the webserver &lt;/p&gt;

&lt;p&gt;&lt;a href="http://ipaddress-of-managenode:8001"&gt;http://ipaddress-of-managenode:8001&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node1:8001"&gt;http://ipaddress-of-node1:8001&lt;/a&gt;&lt;br&gt;
&lt;a href="http://ipaddress-of-node2:8001"&gt;http://ipaddress-of-node2:8001&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the name and phone.Now you can see the name and phone in table "emp" of database "docker" in mysql.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it &amp;lt;containerID&amp;gt; bash

mysql -u root -pedureka
USE docker;
show tables;
select * from emp;
exit

exit

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To leave node from docker swarm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker swarm leave
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a node leaves the swarm, you can run the docker node rm command on a manager node to remove the node from the node list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker node rm &amp;lt;nodename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Docker Compose deploy MERN Stack</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Tue, 15 Mar 2022 12:30:23 +0000</pubDate>
      <link>https://dev.to/nithinalias/docker-compose-deploy-mern-stack-2g2e</link>
      <guid>https://dev.to/nithinalias/docker-compose-deploy-mern-stack-2g2e</guid>
      <description>&lt;p&gt;Clone the MERN stack directory from github&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://github.com/sidpalas/devops-directive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can make MERN stack using the blog link shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://medium.com/swlh/how-to-create-your-first-mern-mongodb-express-js-react-js-and-node-js-stack-7e8b20463e66
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit the database file inside the MERN stack directory as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd mern-docker-compose/
vim server/db/index.js


const mongoose = require('mongoose')

mongoose
    .connect('mongodb://mongo:27017/cinema', { useNewUrlParser: true, useUnifiedTopology: true })
    .catch(e =&amp;gt; {
        console.error('Connection error', e.message)
    })

const db = mongoose.connection

module.exports = db

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are deploying MERN stack in Virtual Machine, you need to change localhost to ipaddress of virtual machine (baseURL: '&lt;a href="http://localhost:3000/api"&gt;http://localhost:3000/api&lt;/a&gt;' to baseURL:'&lt;a href="http://ipaddress:3000/api'"&gt;http://ipaddress:3000/api'&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd mern-docker-compose/
vim client/src/api/index.js


import axios from 'axios'

const api = axios.create({
    baseURL: 'http://localhost:3000/api',
})

export const insertMovie = payload =&amp;gt; api.post(`/movie`, payload)
export const getAllMovies = () =&amp;gt; api.get(`/movies`)
export const updateMovieById = (id, payload) =&amp;gt; api.put(`/movie/${id}`, payload)
export const deleteMovieById = id =&amp;gt; api.delete(`/movie/${id}`)
export const getMovieById = id =&amp;gt; api.get(`/movie/${id}`)

const apis = {
    insertMovie,
    getAllMovies,
    updateMovieById,
    deleteMovieById,
    getMovieById,
}

export default apis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then build and run docker-compose file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make build
make run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Docker compose Install Wordpress using Ansible</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Sun, 13 Mar 2022 12:27:39 +0000</pubDate>
      <link>https://dev.to/nithinalias/docker-compose-install-wordpress-using-ansible-2lnh</link>
      <guid>https://dev.to/nithinalias/docker-compose-install-wordpress-using-ansible-2lnh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Create ansible role&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;roles
├── files
│   └── docker-compose.yml
│   
├── vars
│    └── main.yml
├── dockerwordpressplaybook.yml
└── readme.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add docker-compose.yml in files directory&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'

services:
  # Database
  db:
    container_name: mysql_container
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    container_name: phpmyadmin_container
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    container_name: wordpress_container
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add main.yml in vars directory&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
admin_user: dev
docker_required_packages:
  - "apt-transport-https"
  - "ca-certificates"
  - "curl"
  - "gnupg-agent"
  - "software-properties-common"
  - "python3-pip"
  - "python3-setuptools"
docker_gpg_url: https://download.docker.com/linux/ubuntu/gpg
docker_repo: deb https://download.docker.com/linux/ubuntu focal stable
docker_packges:
  - "docker-ce"
  - "docker-ce-cli"
  - "containerd.io"
docker_compose_url: https://github.com/docker/compose/releases/download/1.28.2/docker-compose-Linux-x86_64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add dockerwordpressplaybook.yml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: docker
  vars_files:
    - vars/main.yml

  tasks:
  - name: Install aptitude using apt
    apt: name=aptitude state=latest update_cache=yes force_apt_get=yes

  - name: Install required system packages for Docker
    apt: name={{ docker_required_packages }} state=latest update_cache=yes

  - name: Add Docker GPG key
    apt_key:
      url: "{{ docker_gpg_url }}"
      state: present

  - name: Add Docker repository
    apt_repository:
      repo: "{{ docker_repo }}"
      state: present

  - name: Install Docker
    apt: name={{ docker_packges }} state=latest update_cache=yes

  - name: Install Python Docker module
    pip:
      name: docker

  - name: Add adminstrator to docker group
    user:
      name: "{{ admin_user }}"
      groups: docker
      append: yes
      createhome: yes

  - name: Install Docker Compose
    get_url:
      url: "{{ docker_compose_url }}"
      dest: /usr/local/bin/docker-compose
      mode: u+x,g+x,o+x

  - name: Set up docker-compose file
    template:
      src: "files/docker-compose.yml"
      dest: "/home/{{ admin_user }}/wordpress/"

  - name: Set permission for docker daemon
    file:
      path: /var/run/docker.sock
      mode: '0770'

  - name: docker compose up
    shell:
      chdir: "/home/{{ admin_user }}/wordpress/"
      cmd: docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the below commands to see images,containers,volumes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
docker ps -a
docker volume list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To shutdown the container use the command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose down 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we use below command in docker-compose.yml file you get the access to edit the wordpress files.If we remove the containers volume will not removed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;volumes: ['./:/var/www/html']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example if we try to add media in wordpress, the default size would be 2 MB.we can change the size by editing .htaccess file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# BEGIN WordPress
php_value memory_limit 100M
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
# END WordPress
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove containers along with volumes use the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose down --volumes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Ansible to install docker and create docker images</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Sun, 13 Mar 2022 08:24:36 +0000</pubDate>
      <link>https://dev.to/nithinalias/ansible-to-install-docker-and-create-docker-images-3589</link>
      <guid>https://dev.to/nithinalias/ansible-to-install-docker-and-create-docker-images-3589</guid>
      <description>&lt;p&gt;&lt;strong&gt;create an ansible file to install docker and create docker images&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: docker
  become: true
  vars:
    container_count: 1
    default_container_name: docker
    default_container_image: ubuntu
    default_container_command: sleep 1d

  tasks:
    - name: Install aptitude
      apt:
        name: aptitude
        state: latest
        update_cache: true

    - name: Install required system packages
      apt:
        pkg:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
          - python3-pip
          - virtualenv
          - python3-setuptools
        state: latest
        update_cache: true

    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker Repdoository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: Update apt and install docker-ce
      apt:
        name: docker-ce
        state: latest
        update_cache: true
      notify: Restart docker

    - name: Install Docker Module for Python
      pip:
        name: docker

    - name: Pull default Docker image
      docker_image:
        name: "{{ default_container_image }}"
        source: pull

    - name: Create default containers
      docker_container:
        name: "{{ default_container_name }}{{ item }}"
        image: "{{ default_container_image }}"
        command: "{{ default_container_command }}"
        state: present
      with_sequence: count={{ container_count }}

  handlers:
    - name: Restart docker
      service:
        name: docker
        state: restarted



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Docker Commands and Dockerfile Creation</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Thu, 10 Mar 2022 18:04:19 +0000</pubDate>
      <link>https://dev.to/nithinalias/docker-commands-and-dockerfile-89g</link>
      <guid>https://dev.to/nithinalias/docker-commands-and-dockerfile-89g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Docker Commands&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker --version
docker --help
docker images
docker pull ubuntu
docker run -it -d ubuntu
docker ps
docker ps -a
docker exec -it containerID bash
exit
docker stop containerID
docker login
docker commit containerID Zulaikha/ubuntu
docker tag Imagename Zulaikha/ubuntu
docker push Zulaikha/ubuntu
docker rm containerID
docker rmi ImageID
docker container logs containerID
docker container rm containerID
docker container kill containerID
docker container run containerID
docker container start containerID
docker export --output="latest.tar" containerID
docker import /home/username/Downloads/demo.tgz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a dockerfile for Apache webserver&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make a directory to build our Docker file for which you can use vim editor. &lt;/p&gt;

&lt;p&gt;mkdir /test&lt;br&gt;
cd /test&lt;br&gt;
sudo vim dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description of the above commands&lt;/p&gt;

&lt;p&gt;Ubuntu is our base image in which we are launching our server.&lt;br&gt;
In the second line, is to set a non-interactive environment.&lt;br&gt;
In the third line, the apt-get update command is used to update all the packages on Ubuntu.&lt;br&gt;
In the fourth line, we are installing apache2 on our image.&lt;br&gt;
In the fifth line, we are installing all the necessary utility Apache packages.&lt;br&gt;
In the sixth line, the apt-get clean command cleans all the unnecessary files from the system.&lt;br&gt;
In the seventh line, the EXPOSE command is used to expose the port 80 of Apache in the container.&lt;br&gt;
The last command is used to run apache2 in the background.&lt;/p&gt;

&lt;p&gt;Next step is to build the docker file by using the docker build command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker build -t myapachewebserver .

Command:
-t: this option is to tag the image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The web server file has built, the next step is to create a container from the image for that we use the docker run command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 8080:80 myapachewebserver
                 OR
docker run -it -p 8080:80 myapachewebserver
Commands:
-d: This option is used to run the container in detached mode i.e the container can run in the background.

-p: This option is used to map our port number with 5000 port numbers on our localhost.
-it: This argument is used to allocate a bash shell and take standard input.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you go to your web browser and write &lt;a href="http://host_ip:8080"&gt;http://host_ip:8080&lt;/a&gt; your Apache server is up and running on that port.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a dockerfile for Apache webserver and Copy the Index.html&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Method 1
&lt;/h1&gt;

&lt;p&gt;Make a directory&lt;br&gt;
mkdir /test&lt;br&gt;
cd /test&lt;br&gt;
Create a sample web page with name index.html&lt;br&gt;
vim /test/index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;

&amp;lt;p&amp;gt;My Personal website&amp;lt;/p&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a file dockerfile&lt;br&gt;
vim /test/dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
COPY index.html /var/www/html/
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to build the docker file by using the docker build command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker build -t myapachewebserver .

Command:
-t: this option is to tag the image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The web server file has built, the next step is to create a container from the image for that we use the docker run command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 8080:80 myapachewebserver
                 OR
docker run -it -p 8080:80 myapachewebserver
Commands:
-d: This option is used to run the container in detached mode i.e the container can run in the background.

-p: This option is used to map our port number with 5000 port numbers on our localhost.
-it: This argument is used to allocate a bash shell and take standard input.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you go to your web browser and write &lt;a href="http://host_ip:8080"&gt;http://host_ip:8080&lt;/a&gt; your &lt;br&gt;
Apache server is up and running on that port.&lt;/p&gt;
&lt;h1&gt;
  
  
  Method 2
&lt;/h1&gt;

&lt;p&gt;Another way to create apache web server image is using manual commands.&lt;br&gt;
Step 1: Get the latest ubuntu Docker image by using docker pull command. Docker pull command is used to download or pull latest image from Docker Hub repositories.&lt;/p&gt;
&lt;h1&gt;
  
  
  docker pull ubuntu:latest
&lt;/h1&gt;

&lt;p&gt;Step 2: To check and list all docker images&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: To run docker image we use following command&lt;br&gt;
-it : This argument is used to allocate a bash shell and take standard input.&lt;br&gt;
-- name : This argument is used to tag a name to the running container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --name webserver ubuntu:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Now install Apache webserver and it’s all dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[root@Docker_Id]#apt-get update -y 
[root@Docker_Id]#apt-get install apache2 -y 
[root@Docker_Id]#apt-get install nano -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Now create a create a webpage at location /var/www/html/index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[root@Docker_Id]# nano /var/www/html/index.html

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;

&amp;lt;p&amp;gt;My Personal website&amp;lt;/p&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 6: Exit from the running container using exit command. Exit command will stop the container. Exit command of docker same as power off or shut down of our computer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[root@Docker_Id]# exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 7: Now we have container in which apache webserver is installed and our webpage is configured. We can make a new customized docker image from the stopped docker image using docker commit command. Docker commit command will build our own image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#docker commit &amp;lt;container_id or name of container will launching&amp;gt; &amp;lt;Name of new image&amp;gt;:&amp;lt;version name&amp;gt;

docker commit webserver webserver:v1
docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 8: Launching a webserver container using our customized image.&lt;br&gt;
-p: This argument is used to port forwarding. Which means anybody from outside who comes for 8080 its request is forwarded to port 80. Port 80 is default port number where apache webserver runs.&lt;br&gt;
/usr/sbin/apache2 –D FOREGROUND: This argument is command which will run when container is launched this command will start the web server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8080:80 webserver:v1 /usr/sbin/apache2 -D FOREGROUND
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 9: To See the Result on web browser&lt;/p&gt;

&lt;p&gt;http://:8080/&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a dockerfile for Nginx webserver and Copy the Index.html&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we need to change the docker file as below.Rest of the steps are same as above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ubuntu
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>AWS Automation</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Tue, 08 Mar 2022 17:11:01 +0000</pubDate>
      <link>https://dev.to/nithinalias/aws-automation-24p0</link>
      <guid>https://dev.to/nithinalias/aws-automation-24p0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction CloudFormation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Template snippets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/CHAP_TemplateQuickRef.html

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Management Tool - cloudFormation - create new stack - Upload a template to Amazon S3 - choose file(file is shown below) - your region should be exact as ImageId - Next - add stack,keyname - Next - Next - create - click on stack name - you could see all in detail - Go to EC2 you could see your Instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0de53d8956e8dcf80
      KeyName: !Ref KeyName
      SecurityGroups:
        - !Ref InstanceSecurityGroup
      Tags:
        - Key: Name
          Value: My CF Instance
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupName: MyDMZSecurityGroup
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0

Outputs: 
  InstanceID:
    Description: The Instance ID
    Value: !Ref MyEC2Instance

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Management Tool - cloudFormation - select stack - Action - Delete stack - yes Delete - Go to S3 - click on the bucket containing template - click on the template file - make public - click on the link you could see the the template - After cloudformation delete finishes you can delete this bucket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction Beanstalk&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Compute - Elastic Beanstalk - Get started - add application name,platform = PHP,Enable upload your code - upload(choose index.zip file contain a __MACOSX folder and index file) - upload - create application - Now a dashboard creates - configuration contain software,Instance,Load Balancer etc.... - you will get a Url, copy and paste in webbrowser to get that application.&lt;/p&gt;

&lt;p&gt;index file content shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Hello Cloud Gurus&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;&amp;lt;h1 align="center"&amp;gt;Hello Cloud Gurus! This is webpage was provisioned using Elastic Beanstalk!&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Action - Delete Application - Delete&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS OpsWorks&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>AWS VPC</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Mon, 07 Mar 2022 16:34:06 +0000</pubDate>
      <link>https://dev.to/nithinalias/aws-vpc-1k5b</link>
      <guid>https://dev.to/nithinalias/aws-vpc-1k5b</guid>
      <description>&lt;p&gt;&lt;strong&gt;VPC Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build a Custom VPC&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Network and Content Delivery - VPC - Your VPCs - create VPC - add name,IPv4 CIDR block = 10.0.0.0/16,Enable Amazon provided IPV6 CIDR block,Tenancy = default - yes,create - Now automatically new Route Table,Network ACLs,Security Groups creates.&lt;/p&gt;

&lt;p&gt;Go to Subnets - create Subnet - add name tag,select the VPC you created,select availability zone,IPv4 CIDR block = 10.0.1.0/24 - &lt;br&gt;
yes create - Go to Subnets - create Subnet - add name tag,select the VPC you created,select another availability zone,IPv4 CIDR block = 10.0.2.0/24 - yes create&lt;/p&gt;

&lt;p&gt;Go to Internet Gateway - create Internet Gateway - add name tag - yes create - Attach to VPC - yes attach &lt;/p&gt;

&lt;p&gt;Go to Route Tables - select the Route table - Subnet Association - There will be no subnet association - Create Route Table - add name tag,select VPC - yes create - Routes(To enable internet) - Edit - Add another route - Destination(0.0.0.0/0),Target = select the internet gateway - save - Edit(To add ipv6) - Add another route - Destination(::/0),Target = select the internet gateway - save - subnet Association - Edit - select subnet(IPv4 CIDR block = 10.0.1.0/24) - save(10.0.1.0/24 = public network,10.0.2.0/24 = private network) - Go to subnets - select 10.0.1.0/24 subnet - subnet Actions - Modify auto-assign IP settings - Enable auto-assign public ipv4 address - save - Launch an EC2 Instance - configure Instance - Network(select the created VPC),select subnets(10.0.1.0/24) - Security Group(Add ssh,http,https rule) - Launch(Public Instance) - Launch another EC2 Instance - configure Instance - Network(select the created VPC),select subnets(10.0.2.0/24) - Launch(Private Instance)&lt;/p&gt;

&lt;p&gt;Network and Security - Security Groups - create security Group - add name,select created VPC - Inbound(Add rule - SSH,MYSQL/AURORA,HTTP,HTTPS,ALL ICMP - Source = 10.0.1.0/24 for all rules) - create - Add this Security Group to created Private Instance - Action - Networking - Change Security Group - select and Assign Security Groups &lt;/p&gt;

&lt;p&gt;Now login to Public Instance that we created - check Internet is available or not(Internet will be available) - Ping private Instance IP - you will get pinging output - Now copy the login-key of Private Instance to Public Instance - Then login to private Instance from Public Instance - Here Internet will not available&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network Address Translation(NAT)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EC2 - Launch an Instance - choose AMI - community AMIs - amazon-ami-vpc-nat-hvm-.... - select - Configure Instance - Network(select the created VPC),select subnets(10.0.1.0/24) - Security Group(Add ssh,http,https rule) - Launch - Select the NAT instance - choose Actions - Networking - Change source/destination check - choose Stop - save&lt;/p&gt;

&lt;p&gt;VPC - Route tables - select main VPC Route table - Routes - Edit - Add another route - Destination(0.0.0.0/0),Target = select the NAT Instance gateway - save&lt;/p&gt;

&lt;p&gt;Now log in private instance from public instance and check internet.Now you will get internet.Now Terminate the nat instance and then we loose internet connection.&lt;/p&gt;

&lt;p&gt;VPC - NAT gateway - create Nat gateway - select subnet(Public subnet 10.0.1.0/24 ) - Create new EIP(Elastic ip) - create a NAT Gateway - Go to Route Table - select main VPC Route table - Routes - Remove old route - Add another route -Destination(0.0.0.0/0),Target = select the NAT gateway - save&lt;/p&gt;

&lt;p&gt;Now log in private instance from public instance and check internet.Now you will get internet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access Control Lists (ACLs)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VPC - Network ACLs - create Network ACLs - add name,select the created VPC - yes,create &lt;/p&gt;

&lt;p&gt;In created Public Instance do the below codes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
echo "&amp;lt;html&amp;gt;&amp;lt;h1&amp;gt;Hello Cloud Gurus!&amp;lt;/h1&amp;gt;&amp;lt;/html&amp;gt;" &amp;gt; /var/www/html/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get the website in web-browser &lt;a href="http://ipaddress"&gt;http://ipaddress&lt;/a&gt; of Public Instance&lt;/p&gt;

&lt;p&gt;VPC - Network ACLs - select the newly created ACL - Inbound Rule - Edit - Add another rule - Rule = 100,Type = HTTP(80),source = 0.0.0.0/0 - Rule = 200,Type = HTTPS(443),source = 0.0.0.0/0 - Rule = 300,Type = SSH(22),source = 0.0.0.0/0 - save - Outbound Rule - Edit - Add another rule - Rule = 100,Type = HTTP(80),source = 0.0.0.0/0 - Rule = 200,Type = HTTPS(443),source = 0.0.0.0/0 - Rule = 300,Type = Custom TCP Rule,Port Range = 1024 - 65535,source = 0.0.0.0/0 - save - subnet Association - select public subnet(10.0.1.0/24) - save &lt;/p&gt;

&lt;p&gt;Now if we add Inbound Rule - Edit - Add another rule - Rule = 101,Type = HTTP(80),source = myipaddress/subnet,Allow/Deny = Deny - Now we get the website in web-browser &lt;a href="http://ipaddress"&gt;http://ipaddress&lt;/a&gt; of Public Instance&lt;/p&gt;

&lt;p&gt;Now if we add Inbound Rule - Edit - Add another rule - Rule = 99,Type = HTTP(80),source = myipaddress/subnet,Allow/Deny = Deny - Now we donot get the website in web-browser &lt;a href="http://ipaddress"&gt;http://ipaddress&lt;/a&gt; of Public Instance &lt;/p&gt;

&lt;p&gt;Here Rule = 99 which comes first in Inbound Rule and that rule get preference first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC End Points&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;IAM - Role - create role - EC2 - EC2 - Next - AmazonS3FullAccess - Next - Add role-name - create role - Add the role to Private Instance - Action - Instance settings - Attach/Replace IAM Role - select the role - Apply &lt;/p&gt;

&lt;p&gt;VPC - Network ACLs - select newly created default Network ACL - subnet association - Edit - Select Public subnet (10.0.1.0/24) - save&lt;/p&gt;

&lt;p&gt;Now login to private Instance from Public Instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su 
aws s3 ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you get S3 bucket list.&lt;/p&gt;

&lt;p&gt;Go to VPC - Route Tables - select newly created main Route Table - Routes - Edit - Remove(0.0.0.0/0) - save&lt;/p&gt;

&lt;p&gt;Now if you use the command shown below you cannot see S3 bucket list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to Endpoint - create Endpoint - select s3 gateway(com.amazonaws.us-east-1.s3) - select created VPC - select main private route table - create Endpoints &lt;/p&gt;

&lt;p&gt;Now using "aws s3 ls" you get S3 bucket list.&lt;/p&gt;

&lt;p&gt;Go to VPC - Route Tables - select newly created main Route Table - Routes - Now you can see VPC Endpoints(This is not behind NAT gateway this is VPC Endpoints)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom VPCs and Application Load Balancers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EC2 - Load Balancer - create Load Balancer - create Application Load Balancer - select the created VPC - Here you need atleast 2 Availabilty Zone of Public Instance &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC Flow Logs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to VPC - Select the created VPC - Action - Create Flow Log - click on Set Up Permission link - view Policy Document - Allow - Go back select the role now created - create Flow Log - But this will show an error because we need to create Destination Log Group - Go to Cloudwatch - Logs - create Log group - add a name - create Log Group - Go to Flow Log - add log group - create flow log&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NAT's Vs Bastions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC Clean Up&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EC2 - Select Public and Private Instance - Actions - Instance state - Terminate - Yes Terminate&lt;/p&gt;

&lt;p&gt;VPC - NAT Gateways - select nat gateway - Action - Delete NAT Gateway - EndPoints - select Endpoints - Action - Delete Enpoints - Internet Gateway - select Internet Gateway - Detach from VPC -  yes,Delete - your VPC - select VPC - Action - Delete &lt;/p&gt;

</description>
    </item>
    <item>
      <title>AWS Network and Route53</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Mon, 07 Mar 2022 07:55:53 +0000</pubDate>
      <link>https://dev.to/nithinalias/aws-network-and-route53-15gd</link>
      <guid>https://dev.to/nithinalias/aws-network-and-route53-15gd</guid>
      <description>&lt;p&gt;&lt;strong&gt;DNS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register Your Domain Name&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Networking and Content Delivery - Route53 - DNS management - Get started now - Registered domains - Register Domain - Add domain name - check(check the price and all) - Add to cart - continue - Add details and Register domain name &lt;/p&gt;

&lt;p&gt;Create 4 EC2 instance in different region.Add this bootstrap script in configure Instance - Advance Details&lt;br&gt;
Webserver -1 in Northern Virginia&lt;br&gt;
Webserver -1 in Northern Virginia&lt;br&gt;
Webserver -2 in Sydney&lt;br&gt;
Webserver -3 in south America sauo paulo&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
yum update -y
yum install httpd -y
cd /var/www/html
echo "webserver -1 - Northern Virginia " &amp;gt; index.html
service httpd start
chkconfig httpd on 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Simple Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - create a Record set - add TTL second = 60 and value as 4 webservers ip address like shown below,Routing policy = simple - create(wait for sometime to service come into effect) - Now type the domain name in webbrowser - you will get the website and region from you get website might be random.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Public ip of Northern Virginia1
Public ip of Northern Virginia2
Public ip of Sydney
Public ip of south America sauo paulo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Weighted Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - Delete simple Routing policy created earlier - create a Record set - add TTL second = 60 and value as Northern Virginia 1 webserver ip address,Routing policy = weighted,weight = 25,setID = webserver1 - create - Do the same for other 3 also(total =25%*4=100%)- wait for sometime to service come into effect - Now type the domain name in webbrowser - you will get the website according to weighted. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Latency Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - Delete Weighted Routing policy created earlier - create a Record set - add TTL second = 60 and value as Northern Virginia 1 webserver ip address and Northern Virginia 2 webserver ip address,Routing policy = Latency,Region automatically will come,setID = webserver1&amp;amp;2 - create(Do this for other two webservers and total there will be three types) - wait for sometime to service come into effect - Now type the domain name in webbrowser - you will get the website with low latency region(use VPN client to change your current network location and test the latency.If you connect to Australia in VPN and type your domain in webbrowser you get the response from sydney).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failover Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - Delete Latency Routing policy created earlier - Go to Health checks - create health check - Add name,what to monitor = endpoint,specify enpoint by = IP address,protocol = HTTP, IP address = public ip of Northern Virginia 1,Host name = domain name,port = 80,path = /index.html - Next - create alarm = no - create health check - wait sometime to service come into effect - Route53 - click on Hosted Zones - click on created DNS name - create a Record set - add TTL second = 60 and value as Northern Virginia 1 webserver ip address,Routing policy = Failover,Failover Record Type = Primary,setID = primary,Enable Associated with health check and add the created health check - create(Do this for Sydney and set Failover Record Type = Secondary,setID = Secondary  ) - wait for sometime to service come into effect - Now type the domain name in webbrowser - you will get the website of Northern Virginia1.If we stop the Northern Virginia1 EC2 Instance(Go to health checks status will be unhealthy) you will get the website of Sydney(secondary)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Geolocation Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - Delete Failover Routing policy created earlier - create a Record set - add TTL second = 60 and value as Northern Virginia 1 webserver ip address,Routing policy = Geolocation,Location = North America,setID = North America set - create(Do this for sydney add TTL second = 60 and value as Sydney webserver ip address,Routing policy = Geolocation,Location = Oceana,setID = Sydney) - wait for sometime to service come into effect - Use VPN client to change your current network location to United states and Now type the domain name in webbrowser you get website of Northern Virginia1.If you connect to Australia in VPN and type your domain in webbrowser you get the website from sydney.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multivalue Routing Policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Route53 - click on Hosted Zones - click on created DNS name - Delete Geolocation Routing policy created earlier - create a Record set - add TTL second = 60 and value as Northern Virginia 1 webserver ip address,Routing policy = Multivalue,setID = web01USEAST-1 - create(Do this Northern Virginia 2 - add TTL second = 60 and value as Northern Virginia 2 webserver ip address,Routing policy = Multivalue,setID = web02USEAST-1) - wait for sometime to service come into effect - Now type the domain name in webbrowser - you will get the website random and if one EC2 Instance stopped you get website second one.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>AWS Security</title>
      <dc:creator>nithinalias</dc:creator>
      <pubDate>Sat, 05 Mar 2022 17:31:55 +0000</pubDate>
      <link>https://dev.to/nithinalias/aws-security-387i</link>
      <guid>https://dev.to/nithinalias/aws-security-387i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Compliance on AWS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DDOS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS marketplace security products&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IAM Custom Policies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;IAM - Policies - create policy - visual editor - choose a service - S3 - In access level list,read - click on Resources - Enable All resources - Review Policy - Add name - create policy&lt;/p&gt;

&lt;p&gt;IAM - Roles - create role - select EC2 - Next Permission - select the created policy - Next - Add role name - create role &lt;/p&gt;

&lt;p&gt;Go to S3(create 2 buckets in different region) - create bucket - Add name,region - create&lt;/p&gt;

&lt;p&gt;Go to EC2 - Launch Instance - Then access Instance from putty or terminal - switch to root(sudo su) - aws s3 ls - unable to locate credentials &lt;/p&gt;

&lt;p&gt;Go to IAM - users - add user - add a username - enable Programatic access - Next - Enable Group Administrator - Next - create user - Now you will get access keyid,secret access key - Go to EC2 Instance - Actions Instance settings - Attach Replace IAM Role - select the created role - Apply - Now type the command 'aws s3 ls' inside the Instance - Now it will show the S3 bucket we created earlier&lt;/p&gt;

&lt;p&gt;create a testfile and copy to S3 bucket&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "Hello World" &amp;gt; test.txt
ls
test.txt
aws s3 cp /home/ec2-user/test.txt s3://bucketname

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will show upload failed since we didnt give write access.&lt;br&gt;
Go to IAM - policies - select the policy that we created - Edit policy - Enable write in Access level - Review policy - save changes - Now you can upload file using above command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 cp /home/ec2-user/test.txt s3://bucketname
aws s3 ls s3://bucketname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same you can do in second bucket in other region&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MFA AND REPORTING WITH IAM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to IAM - Activate MFA on your root account - manage MFA - Enable virtual MFA device - Next step - Next step - scan the barcode using Google authenticator in mobile - add the generated authentication codes - Activate virtual MFA&lt;/p&gt;

&lt;p&gt;This for root account.Now we can do for user account.&lt;/p&gt;

&lt;p&gt;IAM - users - Add user - Add username,Enable programmatic Access - Next - create group - select AdminstrationAccess,give groupname - create group - select the created group - create user(Now you will get access keyid,secret access key) - Download.csv - Go to users - select the created user - security credentials - click on Assigned MFA device - Enable virtual MFA device - Next step - Next step - scan the barcode using Google authenticator in mobile - add the generated authentication codes - Activate virtual MFA&lt;/p&gt;

&lt;p&gt;Go to Instance and remove the role that we created early - select Instance - Actions - Instance settings - Attach Replace IAM Role - No role - Apply - yes,detach - Login using putty or terminal - switch to root(sudo su) - aws s3 ls - unable to locate credentials&lt;/p&gt;

&lt;p&gt;configure user using access keyid,secret access key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws configure
Add access keyid
Add secret access key
Add region name
ENTER
ENTER
aws s3 ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can see the bucket created.Now use the command below for MFA&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws iam create-virtual-mfa-device --virtual-mfa-device-name EC2-User --outfile /home/ec2-user/QRCode.png --bootstrap-method QRCodePNG
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now using 'ls' command you can see QRCode.png and copy the file to s3 bucket&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 cp /home/ec2-user/QRCode.png s3://bucketname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to s3 in amazon service - select the QRCode.png - Actions - make public - make public - click on QRCode.png - click on Link - Now you get the QRCode - scan the barcode using Google authenticator in mobile&lt;/p&gt;

&lt;p&gt;Login to EC2-Instance and use the command shown below for MFA&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws iam enable-mfa-device --user-name EC2-User --serial-number arn:aws:iam::"USERNUMBERHERE":mfa/EC2-User --authentication-code-1 "CODE1HERE" --authentication-code-2 "CODE2HERE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"USERNUMBERHERE" = we get from IAM - users - select user - user ARN(copy the numbers)&lt;/p&gt;

&lt;p&gt;"CODE1HERE" and "CODE2HERE" = we get from google authenticator&lt;/p&gt;

&lt;p&gt;Go to IAM - users - select user - security credential - you can see Assigned MFA device&lt;/p&gt;

&lt;p&gt;Go to IAM - credential report - Download Report - Give details of &lt;br&gt;
users credential&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security Token Service&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security and Logging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS WAF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS Hypervisors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dedicated Instances vs Dedicated Hosts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EC2 - Instances - Dedicated Hosts - Allocate a Host - Add Instance type,availability zone - Allocate host&lt;br&gt;
                          OR&lt;br&gt;
EC2 - Launch Instance - configure Instance(Tenancy = Dedicated Instance or Dedicated Host) - Launch&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS system manager EC2  Run Command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to IAM - Roles - create role - EC2 - EC2 Role for Simple Systems Manager - Next - Next - Add role name - EC2 - Launch Instance - configure Instance(Add created role) - Launch&lt;/p&gt;

&lt;p&gt;Go to System Manager - Run command - select Aws configure Cloudwatch - select created Instance in Target Instance - Run &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS system manager Parameter store&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;EC2 - system manager shared resources(bottom of the page) - parameter store - Get started now - Add name,Enable secure string - create parameter - we can pass this to cloud formation,lamda etc&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS config with s3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Management tools - config - Rules - Add rule - select s3 - you can see rules related to s3 like s3-bucket-public-read-prohibited,s3-bucket-public-write-prohibited etc..&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Presigned URLs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to IAM - Roles - create role - EC2 - Next - select Amazons3FullAccess - Next - Add rolename - create role - Launch Instance - Add created role in configure Instance - Launch - Login to created Instance &lt;/p&gt;

&lt;p&gt;make new s3 bucket and copy a testfile to it using the command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 ls
aws s3 mb s3://bucketname
echo "Hello World" &amp;gt; test.txt
aws s3 cp test.txt s3://bucketname
aws s3 ls s3://bucketname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we go to S3(management console) and access the test.txt by clicking on link we cannot open it.&lt;br&gt;
To access the file for 300 seconds we need to use the command shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 presign s3://bucketname/test.txt --expires-in 300 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generate a https link.copy and paste this link in browser to access the file for 300 seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inspector vs Trusted Advicer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ec2 - Launch an Instance &lt;br&gt;
Security,Identity and Compliance - Inspector - Get started - choose or create role - view details - IAM role = create a new IAM role - Add role name - Allow - Tag your Ec2 Instance - Manage Tags - select Instance and add key and tag - Go back - Install AWS agent - open the link 'To install the Amazon Inspector Agent on a Linux based EC2 Instance' - Login to EC2 instance and run the commands to install AWS agent&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo su 
wget https://inspector-agent.amazonaws.com/linux/latest/install
curl -O https://inspector-agent.amazonaws.com/linux/latest/install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to Inspector window - Next - add name,key,value created earlier - Next - Add name,Rule packages = common vulnerabilities and exposures1.1,Duration = 1hr - Next - create - select the created Inspector - Run - After 1 hr it will give the result - Assesment runs - Download Report - Full Report - Generate Report&lt;/p&gt;

&lt;p&gt;Go to Assessment Templates - create - add name = master template,target name, rule packages(add all the rules),duration=24hr - create and run - After 24 hrs we can download the report&lt;/p&gt;

&lt;p&gt;Go to Management Tools - Trusted Advisor - Give details of Cost optimization,performance,security,Fault Tolerance - we need business or enterprise subscription to unlock these.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared Responsibility&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other Security Aspects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CloudTrail - Turning It On and Validating Logs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;IAM - Groups - create New Group - Add Group name - Next - AWS CloudTrailFullAccess -  &lt;/p&gt;

&lt;p&gt;Go to S3 - select a created bucket - Management - Add Lifecycle rule - Add Rule name - Next - Current Version - Next - click current version - Add expire days - Next - save &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
