webapp: This image is used as a webserver container which runs on apache. There is an index.php file hosted on this server in this directory (/var/www/html/index.php) and it is a simple webform to enter details in the database container.
db: This image is used as database container which runs on mysql and stores the data recorded/collected/received from the webserver - webapp. This database container's details should be a part of webapp's connection string.
For webapp container to be connected and functional, we need to create a database called company and a table in the company database called employee which has name, mobile fields IN the db container.
When no IP address is assigned to the container you can run the container in none network. It is mostly used for applications that need to be tested in an isolated environment. After testing, we can disconnect the container from the network and connect it to another network.
Step 1: Create a centos container on none or null network
docker container run -it --name c1 --network none -d centos
Step 2: Inspect the container and verify that it is running on none networks
docker inspect c1
Step 3: Once the testing is done then remove none network from n1 container and attach bridge network
docker network disconnect none c1
docker network connect bridge c1
Step 4: Verify the n1 container is having bridge network
Try This Consideration:- You have 3 nodes ( 1 master and 2 worker nodes) Cluster.
Create a Pod called nginx-pod1 with image nginx (it runs on Port 80) kubectl run nginx-pod1 --image=nginx OR #nginx-pod1.yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod1 spec: containers: - name: c1 image: nginx
Create another Pod called tomcat-pod1 with image tomcat (it runs on Port 8080) kubectl run tomcat-pod1 --image=tomcat OR #tomcat-pod1.yaml apiVersion: v1 kind: Pod metadata: name: tomcat-pod1 spec: containers: - name: c1 image: tomcat
Access these pods on worker nodes (use curl command ). Are you able to Access ? If not then checkout the reason. kubectl get pods -o wide curl <podIP> yes able to. for nginx is ok but for tomcat need to put port number (8080)
Go inside tomcat pod ( use kubectl exec ....command) and try to access nginx service Pod by using nginx-pod1 IP address and pod name. Are you able to access the nginx website ? if no then figure out the reason. kubectl exec -it tomcat-pod1 -- bash curl <nginx-pod1 IP:port> can cause in same localhost (calico network layer)
Go inside nginx pod (use kubectl exec ....command) and try to access tomcat service with IP address and pod name ot tomcat-pod1. Are you able to access the nginx website? if no then figure out the reason. kubectl exec -it nginx-pod1 -- bash curl <tomcat-pod1 IP:port> can cause in same localhost (calico network layer)
Change docker image of nginx-pod1 from nginx to tomcat. Is it possible to do on a running pod? No. Error from server (AlreadyExists): pods "nginx-pod1" already exists kubectl edit pod nginx-pod1 -o yaml (then edit the image to change to tomcat).
Check the restart value of nginx-pod1 (it must change to 1) kubectl get pod nginx-pod1 -o wide
check all the events of tomcat-pod1 and also find out in which namespace tomcat-pod1 is created. kubectl describe pod tomcat-pod1 Name: tomcat-pod1 Namespace: default
Display the tomcat-pod1 properties in json format. kubectl get pod tomcat-pod1 -o json
Step 2: Create a webapp container with network as host and DO NOT DO a port forwarding on port number 80 as per custom network cause this assignment is run on host network.
docker container run -it --name web --network host -d ramansharma95/webapp
Go to browser and check that you are able to see the default web page. localhost:80
Step 3: Go inside the container (web) and check the code for index.php cause you will need to edit it a little.
docker exec -it web bash
vi /var/www/html/index.php
edit the username from db to host ip address (192.168.33.10).
# you will find that in the index.php, it has connection configs to connect to a server named 'db' but need to edit to host ip address cause its on the same network and also the required username and password to enter that server.
Step 4:- Create a db container with network defined (note this does not require port forwarding)
docker container run -it --name db --network host -d ramansharma95/mysql
Step 5:- Go inside the container (db).
docker exec -it db bash
5.2 connect to mysql with username root and password whizlabs
mysql -uroot -pwhizlabs
5.3 Create a database company
show databases;
create database company;
show databases;
5.4 Create a table employee with name and mobile field.
use company;
create table employee ( name varchar(30), mobile varchar(30) );
5.5 Show all the records in this table
select * from employee;
Step 6: Go to browser and add some employees details in the webpage and check again records in employee table, it should have those records added.
5. echo "<h1> Welcome to test.html page! </h1>" > /var/lib/docker/volumes/deploy-vol/_data/test.html
6. curl localhost/test.html
7. docker exec -it assignment1 bash
7.2 in container:
echo "<h1> This is newtest page and NOT test page! </h1>" > /var/www/html/newtest.html
7.3 exit container:
exit
7.4 curl localhost/newtest.html
ASSIGNMENT 2:-
1. Attach a docker volume to multiple containers and add or modify the files in any of these containers and check that the changes are getting reflected in all the containers or not.
2. Point #1 is to be repeated for Bind Mount
3. Delete the Bind mount directory which is mapped to a container's destination directory and check the destination behavior means try to create some file or list the files on the destination folder.
4. Continue to Point 3, you may get some error that the source directory does not exist. Create source directory again and check the destination folder and find out whether you are able to create new files in this folder or not.
ANSWERS:
1. yes
2. yes
3. for bind mount:
touch: cannot touch '/app1/devops3fromc3.txt': No such file or directory
3.2 for volume (means prune the docker volume):
4. for bind mount:
touch: cannot touch '/app1/devops3fromc3.txt': No such file or directory (but if you create a container after that, it can sync the files) this means that by using bind, once your source is changed, your target will never find the source again even if its the same name.
4.2 for volume (means create a docker volume with same name after pruning it):
5. Restart the container which you are using for #3 and #4 and again check the destination folder to store some data and find out whether you are able to see it in the source folder or not.
Create a setup of 3 ubuntu Servers where one of the servers is the Master server where the Ansible is installed whereas the other 2 webservers are managed nodes.
1. Create a playbook to install apache on the webservers
2. Create a playbook to start an apache service on webservers
3. Create a playbook to create a file /tmp/status with dynamic contents on dbservers.(vars_prompt)
4. Create a playbook to copy /tmp/status.txt file to web servers /tmp/status.txt
5. Create a playbook to Stop the apache service and delete /tmp/status.txt and also uninstall the apache service ( create a separate play for uninstalling the apache service).
6. Create a playbook where you define a variable called testvar and the value of the variable should be entered at run time if the value of a variable is Testing then create a file /tmp/testfile.txt and write the content that "Testing is in progress" on webservers.
7. A series of tasks in one playbook:
7.1 Create a playbook to install apache (apache2 for Ubuntu and httpd for CentOs) on webservers ( consider webservers are either CentOs or Ubuntu System).
7.2. Start httpd service on webservers which are CentOs.
7.3. Remove apache from Webserver if the OS family is Debian and OS is Ubuntu.
7.4. Create a user John on Debian based OS webservers. (use user module) and verify that user created.(cat /etc/passwd | grep John| wc -l)
ANSWER:
1. install apache 2. start apache --- - name: Play for webservers (node2, node3) hosts: webservers tasks: - name: installing apache2 on webservers apt:
name:apache2
state:present
- name: starting apache2 service on webservers service: name: apache2 state: started
3. create file and dynamic value --- - name: Play for dbservers (node1) hosts: dbservers vars_prompt: name: data prompt: Enter a value tasks: - name: create a .txt file in /tmp command: touch /tmp/status.txt
- name: please add a dynamic text in the .txt file copy: content: "{{ data }}" dest: /tmp/status.txt
4. copy file --- - name: Play for pushing dbservers file to webservers hosts: webservers tasks: - name: copying .txt file from dbservers to webservers copy: src: /tmp/status.txt dest: /tmp/status.txt
5.1. stop apache and delete .txt file --- - name: Play for webservers (node2, node3) hosts: webservers tasks: - name: stopping apache2 service from webservice service: name: apache2 state: stopped - name: deleting /tmp/status.txt from webservice shell: rm /tmp/status.txt
5.2. uninstall apache --- - name: Play for webservers(node2, node3) hosts: webservers tasks: - name: uninstall apache2 service from webservers apt:
name:apache2
state:absent
purge:yes
6. prompt in runtime
--- - name: play for assignment playbook 6 hosts: webservers vars_prompt: name: testvar prompt: enter a value tasks: - name: creating testfile.txt in /tmp if testvar is Testing command: touch /tmp/testfile.txt when: testvar == "Testing"
- name: write content in the .txt file shell: echo 'Testing is in progress' >> /tmp/testfile.txt when: testvar == 'Testing'
7. A series of tasks in a playbook
--- - name: play for assignment playbook 7 hosts: webservers tasks:
Assignment: Create a setup of 3 ubuntu Servers where one of the servers is the Master server where Ansible is installed whereas the other 2 webservers are managed nodes.
Perform the below tasks by using Ansible Adhoc commands:
1. Install Apache server on webservers using shell module 2. Stop Apache service on webservers using service module 3. Restart Apache service on webservers using service module. 4. Uninstall Apache service using apt module 5. Run date command on webservers. 6. Create a file call app.java under /tmp/java folder 7. Add some content to app.java file. 8. Read app.java file using shell module. 9. Delete app.java file. 10. Install tree command using apt module.
Answer:
ansible webservers -m shell -a "apt
install apache2 -y"
ansible webservers -m service -a "name=apache2 state=stopped"
ansible webservers -m service -a "name=apache2 state=restarted"
ansible webservers -m apt -a "name=apache2 state=absent purge=yes"
ansible webservers -m shell -a "date"
Answer for 6:
ansible webservers -m shell -a "mkdir /tmp/java/"
ansible webservers -m shell -a "touch /tmp/java/app.java"
ansible webservers -m copy -a "content='Hello World' dest=/tmp/java/app.java"
ansible webservers -m shell -a "cat /tmp/java/app.java"
ansible webservers -m shell -a "rm -ifr /tmp/java/app.java"
ansible webservers -m apt -a "name=tree state=present"
Run the below ansible adhoc command with service module (apache2 is required to be installed first):
ansible webservers -m service -a "name=apache2 state=started"
ansible webservers -m service -a "name=apache2 state=stopped"
ansible webservers -m service -a "name=apache2 state=restarted"
ansible webservers -m service -a "name=apache2 state=reloaded"
Challenge Part 5:
create a file /tmp/status.txt in master system and copy it to agent system using copy module with the destination having the same name.
create a file /tmp/status.txt in master system and copy it to agent system using copy module with the destination having a different name (newstatus.txt).
create a file with text: "Hello World" in agent system by using copy module from master system.
ASSIGNMENT: Git Release Preparation Steps with SonarQube (CLI method)
1. Create a branch release (dev branch --> build branch-->release branch) from the build branch of your code.
2. Install and Configure SonarQube.
3. Add/Modify the changes in the dev branch and update the build and release branch.
4. Analyze the code which is in the release branch with SonarQube (here we need to use Maven Integration with sonarqube).
5. Once Analysis is done then merge the release branch to the master branch.
6. For CICD pipeline point of view use Gitlab/Jenkins/Bamboo integration with SonarQube.
7. Once the Analysis is done then it Git Release closure.
ANSWER TO ASSIGNMENT:
// Create the below steps on a local system, result will be local repo.
1. Create a branch release (dev branch --> build branch-->release branch) from the build branch of your code.
a. go to any folder and cmd.
b. type in mvn archetype:generate to create a maven project. go through the procedures (1865, groupID, artifactID, etc, etc..)
mvn archetype:generate
-> a folder (named with artifactID) will be created.
c. git init (this will initialize git in the folder, it will create some required files)
-> to check, git status
d. prepare to add the files into git local repo
git add .
git commit -m "maven proj added for sonar in gitlab"
e. create a dev branch (as by default, you are now in the main/master branch) and checkout into that branch.
git branch dev
git checkout dev
f. IN THE DEV BRANCH, create a build branch and checkout into that branch.
git branch build
git checkout build
g. IN THE BUILD BRANCH, create a release branch.
git branch release
2. Install and Configure SonarQube.
a. install SonarQube.
www.sonarqube.com
b. configure in pom.xml by runing maven package and sonar:sonar. Then check in sonarqube web portal.
mvn package
mvn clean package sonar:sonar
c. check what is modified.
git status
d. you dont want the target/ folder so you need to create a .gitignore file and put in ".gitignore" and "target/".
echo ".gitignore" >> .gitignore
echo "target/" >> .gitignore
(this will result in an error cause in the .gitignore file, there are still double quotes. so open it using notepad and delete the double quotes).
e. add and commit all changes.
git add .
git commit -m "sonar is installed with maven"
3. Add/Modify the changes in the dev branch and update the build and release branch.
a. Make the changes in dev branch code and merge the changes to build branch and merge build branch to release branch (only edit pom.xml in release branch later).
???
b. add sonar dependencies in release branch code by opening Eclipse IDE and search for the pom.xml file and add in properties, dependencies, profile. Don't forget to put in a new token from SonarQube web portal also.
4. Analyze the code which is in the release branch with SonarQube (here we need to use Maven Integration with sonarqube).
a. go to SonarQube web portal (localhost:9000) and check if the release branch code is in the sonarqube project list.
localhost:9000
5. Once Analysis is done then merge the release branch to the master branch.
a. merge release code into master branch
git checkout master
git merge release
b. now master branch will have the same pom.xml config as per release (where the config has the sonarqube dependencies). Thus you will have to ensure that dependency, profiles is not present in the pom.xml in master.
type pom.xml (to check)
edit in notepad or something..lol
// Now this is for pushing from local repo into remote repo
6. For CICD pipeline point of view use Gitlab/Jenkins/Bamboo integration with SonarQube.
//if for example, you used SSH, and want to change to HTTPS, in the .git folder, click on config file and change from there. (p/s: .git folder is hidden btw)
git push origin main
c. check in GitLab if the files have been added.
d. go to sonarcloud
https://sonarcloud.io/login
e. create a new organization
enter key
choose free plan
create
f. create a new project MANUALLY
add in project key
g. click on Analyze on GitLab CICD. There will be several steps:
steps 6.1: -
Add environment variables
a. Define the SonarCloud Token environment variable
In GitLab, go to Settings > CI/CD > Variables to add the following variable and make sure it is available for your project:
In the Key field, enter SONAR_TOKEN
In the Value field, enter 853728a0ead4599ab5509eb6df0f325954556e8e
Make sure that the Protect variable checkbox is unticked
Make sure that the Mask variable checkbox is ticked
b. Define the SonarCloud URL environment variable
Still in Settings > CI/CD > Variables add a new variable and make sure it is available for your project:
In the Key field, enter SONAR_HOST_URL
In the Value field, enter https://sonarcloud.io
Make sure that the Protect variable checkbox is unticked
No need to tick the Mask variable checkbox this time
Step 6.2:-
Create or update a .gitlab-ci.yml file
What option best describes your build?
so what you have to do is create a new .gitlab-ci.yml file in GitLab.
Step 6.3:- click on maven and this will appear:
Update your pom.xml file with the following properties:
Create or update your .gitlab-ci.yml yaml file with the following content:
variables:SONAR_USER_HOME:"${CI_PROJECT_DIR}/.sonar"# Defines the location of the analysis task cacheGIT_DEPTH:"0"# Tells git to fetch all the branches of the project, required by the analysis tasksonarcloud-check:image:maven:3.6.3-jdk-11cache:key:"${CI_JOB_NAME}"paths:-.sonar/cachescript:-mvnverifysonar:sonar-Dsonar.projectKey=newkeysonarcloudonly:-merge_requests-master-develop
Note that this is a minimal base configuration to just run a SonarCloud analysis on your master branch and merge requests.
If you already have a pipeline configured and running you might want to just add this new step to your existing yaml file.
so basically 2 things:
1. edit pom.xml
2. edit .gitlab-ci.yml (p/s in the script, update to mvn clean package sonar:sonar -Dsonar.projectKey=newkeysonarcloud)
7. Once the Analysis is done then it Git Release closure.