1. ansible#
ansible
What is it? What does it consist of?
What is its purpose?
paramiko --> This library/module enables ssh functionality in ansible
What is it?
ansible is the name of an automation operations tool
What does it consist of?
Components of ansible:
-
Host inventory -- defines the clients and categorizes them: db, web, etc.
-
Playbook -- a script that instructs the hosts to perform certain tasks according to the given script
-
Module -- a program that implements individual functions
-
Plugin -- implements additional small functions
What is its purpose?
Installing software, uninstalling software, modifying configurations, applying patches, creating backups, running scripts (operations work)
ansible is written in Python and uses ssh as the underlying protocol
paramiko --> This library/module enables ssh functionality in ansible
sshpass module --> Linux command that enables ssh login
[root@cPen_A ~]# ansible --version # Note: Check version
ansible 2.9.15
2. grep#
# grep related commands
[root@cPen_B network-scripts]# cat /etc/passwd|grep -B 5 mail # Note: View the previous 5 lines
[root@cPen_B network-scripts]# cat /etc/passwd|grep -A 5 mail # Note: View the next 5 lines
[root@cPen_B network-scripts]# cat /etc/passwd|grep -C 5 mail # Note: View the previous and next 5 lines
[root@cPen_A ~]# vim /etc/ansible/ansible.cfg # Note: ansible configuration file
forks = 5 # Note: Start 5 processes to handle -f option
IP address not displayed
service NetworkManager stop # Note: This service affects it
service network restart
template template --> Most of it is fixed, with a few variables based on values retrieved from ansible
[root@cPen_A ansible]# ansible web -m setup # Note: View information of the node server in dictionary format
192.168.0.79 | SUCCESS => {
"ansible_facts": {
……
}
[root@cPen_A ansible]# ansible web -m setup -a "filter=ansible_all_ipv4_addresses"
# Note: Filter to obtain specific information
# Note: Get the IPV4 address of the ansible-demo3 host
192.168.0.79 | SUCCESS => {
"ansible_facts": { # Note: ansible facts
"ansible_all_ipv4_addresses": [
"192.168.0.79"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
==========================================================
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("IP address", 22, "username", "password")
==========================================================
Solving the problem of needing to enter "yes" when using password connection for the first ssh
[root@C ansible]# vim hosts
[web]
192.168.0.77
[db]
192.168.0.35 ansible_ssh_user=root ansible_ssh_pass=123456
[root@C ansible]# vim ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False
api interface --> application interface
# Note: Write interface
json What is it used for? Lightweight data exchange format
[root@cPen_A ansible]# ansible-doc -l # Note: View ansible modules
# Note: doc document, l list
latest means installation, not necessarily the latest
Project
ansible
Project Name: Deploying nginx cluster and mysql cluster in batches using ansible
Project Environment: centos8.2, ansible2.9.15, nginx1.19.5, mysql5.7.31
Project Description:
Use ansible to deploy 6 nginx servers, 1 NFS file server, and 3 MySQL servers in batches. Nginx is installed by compiling the source code, MySQL is installed using binary installation, and NFS is installed using the yum installation method. Scripts for compiling and installing nginx and MySQL are prepared in advance and executed on nginx and MySQL servers using ansible, achieving batch automated deployment using playbooks.
Project Steps:
1. Deploy 11 Linux virtual machines with CentOS 8.2 installed and configured with 1 core and 2GB of memory.
2. Install and deploy ansible on one of the Linux systems using yum.
3. Prepare a script for one-click installation of nginx, along with the nginx.conf configuration file, for later use in playbooks.
4. Prepare a script for one-click installation of MySQL, along with the my.cnf configuration file, for later use in playbooks.
5. Establish ssh passwordless communication between the ansible server and all nginx, MySQL, and NFS servers.
6. Finally, write ansible playbook files to deploy nginx, MySQL, and NFS servers.
7. Test the success of the entire installation and deployment.
Project Insights:
1. Gained some understanding of the use of ansible in large-scale clusters and experienced the convenience of batch deployment.
2. Developed a certain understanding of clusters.
3. Improved scripting skills.
4. Enhanced troubleshooting ability through troubleshooting various issues.
============================================================================================
When writing a project
1. Present the data
nginx
What is it? What is it used for?
nginx is a web server
web refers to websites
dns
domain name system
www.aliyun.com --> ip
Why do we need DNS?
To easily remember a website name
IP addresses are difficult to remember
Forward lookup: domain name --> ip
Reverse lookup: ip --> domain name
What happens when you enter www.baidu.com in the browser?
-
Browser cache
-
Hosts file
-
Local DNS server
-
Forwarding server
-
Root domain server
-
Iterative process
-
Recursive return
-
Direct access
CDN Content Delivery Network: Acceleration (nearest resolution)
Note: There is a delay in caching#
Note: CDN is heavily used in video services#
Record types
A record --> address record
www --> 120.77.248.190
What are the steps (tasks to be done) to launch your own website?
-
Purchase a cloud server and deploy nginx, mysql, Python, and other environments
-
Purchase a domain name www.chenpeng.com and bind the domain name to the cloud server --> A record --> Record the server in China (required for domestic servers)
-
Develop the website --> Write the website
-
Test the website
-
Upload it to the nginx web root directory on the cloud server --> Go live
[root@cPen_C lianxi]# yum provides nslookup
# Note: Check which package provides the nslookup command
NFS
Network File System
What is it used for?
Sharing files --> Multiple servers use the same content
Note: NFS is the cheapest liberation solution, but it is affected by network, low server hardware configuration, disk speed, CPU, memory, and network bandwidth are relatively poor#
Note: More expensive solutions: professional storage#
Note: Better solutions are SAN fiber devices#
SAN -->
NAS --> Network Attached Storage
SAN generally refers to Storage Area Network. Storage Area Network (SAN) uses Fibre Channel (FC) technology to connect storage arrays and server hosts through FC switches to establish a dedicated network for data storage.
NAS (Network Attached Storage) is a device connected to the network with data storage capabilities, so it is also called "network storage".
ansible
Project
dns
nginx one-click deployment
Website launch
NFS
SAN
NAS
nginx one-click installation script
#!/bin/bash
#author:cali
#time:2020-49
#company:sanchuang
#email:695811769@qq.com
mkdir -p /nginx2
cd /nginx2
#download nginx
curl -O http://nginx.org/download/nginx-1.17.9.tar.gz
#create user
useradd lihu
#unzip
tar xf nginx-1.17.9.tar.gz
#enter the unzipped directory
cd nginx-1.17.9
#resolve dependencies
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
#configure
./configure --prefix=/usr/local/nginx9 --user=lihu --group=lihu --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module --with-stream
#compile and install
make -j 2 ; make install
#start on boot
echo "/usr/local/nginx9/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
#modify PATH variable
echo "PATH=/usr/local/nginx9/sbin:$PATH" >>/etc/profile
PATH=/usr/local/nginx9/sbin:$PATH
#start
nginx