赞
踩
运维技术不成熟早期,通过⼿⼯执⾏命令管理硬件、软件资源,运维⼈员需要执⾏⼤量的重复命令完成
⽇常运维⼯作。让⼯具或系统替代⼈⼯来⾃动完成具体的运维⼯作,提⾼效率降低运维成本执在必⾏。
1.⼿⼯运维缺点多
2. 传统⼈⼯运维难以管理⼤量的软、硬件资源
3. 业务需要的频繁变量
4. ⾃动化运维的技术已经成熟
5. ⼯具已经到位
1.需要有⽀持混合云的配置管理数据库
2. 有完备的监控和应⽤性能分析系统
3. 需要具备批量运维⼯具
4. 需要有⽇志分析⼯具
5. 需要有持续集成和版本控制⼯具
6. 还要有漏洞扫描⼯具
安装下载包:python-3.9.4-amd64.exe
为计算机添加安装⽬录搭到环境变量,如图把python的安装⽬录添加到pth系统变量中即可
测试python安装是否成功,cmd打开命令⾏输⼊ python 命令
下载安装包
[root@canoe opt]# wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
解压
[root@canoe opt]# tar xf Python-3.9.2.tgz
将解压后的文件迁移至/usr/local
[root@canoe opt]# mv Python-3.9.2 /usr/local
进入python目录
[root@canoe opt]# cd /usr/local/Python-3.9.2/
[root@canoe Python-3.9.2]#
执行配置文件
[root@canoe Python-3.9.2]# ./configure
安装gcc++
[root@canoe Python-3.9.2]# yum install gcc gcc-c++
编译安装
[root@canoe Python-3.9.2]# make
[root@canoe Python-3.9.2]# make install
在/usr/bin路径下生成python3的软链接
[root@canoe local]# ln -s /usr/local/Python-3.9.2/python /usr/bin/python3
测试进入
[root@canoe bin]# python3
Python 3.9.2 (default, Mar 3 2021, 13:36:26)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
去官网下载https://www.jetbrains.com/pycharm/download/#section=windows
双击安装包,按照next一直进行下去,直至安装完成。
安装
[root@canoe ~]# sudo yum install vim
>>> 2 + 2 4 >>> 50 - 5 * 6 #先乘除后加减 20 >>> (50 - 5 * 6) / 4 #()运算符优先级最⼤ 5.0 >>> 8 / 5 #相除结果不会取整 1.6 >>> 19 / 3 6.333333333333333 >>> 19 // 3 #整数除法返回向下取整后的结果 6 >>> 17 % 3 #求余 2 >>> 5 * 3 + 2.0 17.0 >>> 5 ** 2 #5的平⽅ 25 >>> 2 ** 7 #2的7次⽅ 128 >>>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Python数据类型的转换:
int(x)将x转换成⼀个整数
float(x)将x转换成浮点数
complex(x)将x转换为⼀个复数,实数为x虚数为0
complex(x, y)将x和y转换为复数,x为实数部,y为虚数部
常用的数学函数
认识字符串
>>> 'abc' 'abc' >>> "abc" 'abc' #专⽤明带特殊字符的字符串 >>> 'a\'b\'c' #通过\的⽅式 "a'b'c" >>> "a'b'c" #通过双引号的⽅式 "a'b'c"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
字符串索引
>>> s="abcdefg" >>> s[0] 'a' >>> s[1] 'b' >>> s[:] 'abcdefg' >>> s[2:3] 'c' >>> s[2:] 'cdefg' >>> s[-1] 'g' >>> s[-2:] 'fg' >>> s[0]='f'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
字符串的遍历
>>> s = "abcd" >>> for i, a in enumerate(s): #采用enumerate()的方式进行遍历 ... print(i,a) ... 0 a 1 b 2 c 3 d >>> for a in s: #直接进行遍历字符串 ... print(a) ... a b c d >>>for i in range(len(s)): #采用range()函数里面放置字符串的长度,使用len()函数获取 ... print(i, s[i]) ... 0 a 1 b 2 c 3 d
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
字符串的格式化
与C语言的字符串的格式化类似
不同的类型采用不同的格式进行输出,python也可采用{}进行控制参数类型的输出
>>> print("我叫%s今年%d岁!" %('⼩明', 10))
我叫⼩明今年10岁!
>>> "我叫%s今年%d岁!" %('⼩明', 10)
'我叫⼩明今年10岁!'
>>> print("我叫{}今年{}岁!".format('⼩明', 10))
我叫⼩明今年10岁!
>>> print("我叫{0}今年{1}岁!".format('⼩明', 10))
我叫⼩明今年10岁!
字符串的内建函数
可以通过索引修改列表中的元素
不能对元组中的元素进⾏修改
>>> li = [1, 1, 2, 2, 3] #列表中可以包含重复元素 >>> li [1, 1, 2, 2, 3] >>> li = ['a', 1, 1] #列表中元素类型可以不⼀样 >>> li ['a', 1, 1] >>> li[0] = 'b' #可以通过索引修改列表中的元素 >>> li ['b', 1, 1] >>> tup = (1, 1, 2, 2, 3) #元组中的元素可以重复 >>> tup (1, 1, 2, 2, 3) >>> >>> tup = ('a', 1, 1) #元组中的元素类型可以不同 >>> tup ('a', 1, 1) >>> tup[0] = 'b' #不能对元组中的元素进⾏修改 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
>>> zs = {'name': 'zs', 'age': 15} #定义字典
>>> zs
{'name': 'zs', 'age': 15}
>>>
>>> zs['sex'] = '男' #添加值
>>> zs
{'name': 'zs', 'age': 15, 'sex': '男'}
>>>
>>> zs['name'] = 'ls' #对值进⾏修改
>>> zs
{'name': 'ls', 'age': 15, 'sex': '男'}
>>> s = {1, 1, 2, 3, 4}
>>> s
{1, 2, 3, 4}
>>> def hello():
... print("hello world")
>>> hello()
hello world
条件控制
score = input("请输⼊分数:")
score = int(score)
if score < 0 or score > 100:
print("错误")
elif score >= 90:
print("优")
elif score >=80 :
print("良")
elif score >= 70:
print("中")
elif score >= 60:
print("及格")
else:
print("不及格")
循环语句
continue终止当前循环
>>> for i in range(10):
... print(i, end='\t')
0 1 2 3 4 5 6 7 8 9
>>> for i in range(10): #continue会终⽌当次循环
... if i == 5:
... continue
... print(i, end='\t')
0 1 2 3 4 6 7 8 9
break会终止循环
>>> for i in range(10): #break会终⽌循环
... if i == 5:
... break
... print(i, end='\t')
0 1 2 3 4
得到当前工作目录
>>> import os
>>> os.getcwd()
'/root'
>>>
返回指定⽬录下的所有⽂件和⽬录名
>>> os.listdir('/usr/local/bin')
['pip', '2to3-3.9', 'pydoc3', 'pip3', 'python3-config',
'python3.9-config', 'python3.9', 'idle3.9', 'python3',
'pydoc3.9', 'idle3', 'pip3.9', 'easy_install-3.9', '2to3']
>>>
删除⼀个⽂件
>>> os.remove('/usr/local/bin/hello.py')
>>>
删除多个⽬录
>>> os.removedirs('/usr/local/bin/hello')
>>>
检验给出的路径是否是⼀个⽂件
>>> os.path.isfile('/usr/local/bin/hello.txt')
True
>>>
检验给出的路径是否是⼀个⽬录
>>> os.path.isdir('/usr/local/bin/hello.txt')
False
>>>
判断是否是绝对路径
>>> os.path.isabs('/usr/local/bin/hello.txt')
True
>>>
检验给出的路径是否真实存在
>>> os.path.exists('/usr/local/bin/hello.txt')
True
>>>
返回⼀个路径的⽬录名和⽂件名
>>> os.path.split('/usr/local/bin/hello.txt')
('/usr/local/bin', 'hello.txt')
>>>
分离扩展名
>>> os.path.splitext("hello.txt")
('hello', '.txt')
>>>
获取路径名
>>> os.path.dirname('/usr/local/bin/hello.txt')
'/usr/local/bin'
>>>
运⾏shell命令
>>> os.system("touch /usr/local/bin/hello1.txt")
0
>>>
重命名
>>> os.rename('hello1.txt','hello2.txt')
>>>
创建多集⽬录
>>> os.makedirs("/hello/hello.txt")
>>>
创建单个⽬录
>>> os.mkdir("test")
>>>
获取⽂件属性
>>> os.stat('hello.txt')
os.stat_result(st_mode=33188, st_ino=154259, st_dev=64769, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1617787778, st_mtime=1617787778, st_ctime=1617787778)
>>>
获取⽂件⼤⼩
>>> os.path.getsize('hello.txt')
0
>>>
终⽌当前进程
>>> os.exit()
创建空⽂件
>>> os.mknod("test.txt")
>>>
复制文件
>>> import shutil
>>> shutil.copyfile("hello.txt","hello3.txt")
'hello3.txt'
>>> shutil.copy("hello.txt","test")
'test/hello.txt'
>>>
复制文件夹
>>> shutil.copytree("test","test2")
'test2'
>>>
重命名⽂件(⽬录)
>>> os.rename("test","test1")
>>>
移动⽂件(⽬录)
>>> shutil.move("hello.txt","test3")
'test3'
>>>
>>> f = open("/usr/local/bin/hello.txt","r") >>> str = f.read() >>> f.close() >>> print(str) helsiisjfsfifjsfjis >>>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
>>> f = open("/usr/local/bin/hello.txt","r") >>> lines = f.readlines() >>> for line in lines: ... print(line,end=" ") ... helsiisjfsfifjsfji sjisf sji sji n nsif nsi s >>> f.close() >>>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
>>> with open('hello.txt', 'w') as f:
... f.write("大家好")
...
3
>>> open("/usr/local/bin/hello.txt","r").read()
'大家好'
class Test:
def __enter__(self):
print('enter method executing')
return self
def __exit__(self, type, value, traceback):
print('exit method executing')
def sayHello(self):
print('hello world')
with Test() as test: #会执⾏__enter__⽅法并返回Test对象
test.sayHello()
#执⾏完毕后会调⽤__exit__⽅法
旧模块的使用
- os.system()
- os.popen()
subprocess模块
- subprocess.run()
- subprocess.call()
- subprocess.check_call()
- subprocess.getstatusoutput()
- subprocess.getoutput()
- subprocess.check_output()
subprocess.Popen()
- stdout
- stderr
poll()- wait()
- terminate()
- pid
subprocess最早在2.4版本引⼊。⽤来⽣成⼦进程,并可以通过管道连接他们的输⼊/输出/错误,以及获得
他们的返回值。
subprocess⽤来替换多个旧模块和函数:
- os.system
- os.spawn*
- os.popen*
- popen2.*
commands.*运⾏python的时候,我们都是在创建并运⾏⼀个进程,linux中⼀个进程可以fork⼀个⼦进程,并让这个⼦
进程exec另外⼀个程序。在python中,我们通过标准库中的subprocess包来fork⼀个⼦进程,并且运⾏⼀
个外部的程序。subprocess包中定义有数个创建⼦进程的函数,这些函数分别以不同的⽅式创建⼦进程,所
以我们可以根据需要来从中选取⼀个使⽤。另外subprocess还提供了⼀些管理标准流(standard stream)和
管道(pipe)的⼯具,从⽽在进程间使⽤⽂本通信。
执⾏操作系统的命令,将结果输出到屏幕,只返回命令执⾏状态(0:成功,⾮ 0 : 失败)
>>> import os >>> os.system('df -Th') Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 1000M 0 1000M 0% /dev/shm tmpfs tmpfs 1000M 1008K 999M 1% /run tmpfs tmpfs 1000M 0 1000M 0% /sys/fs/cgroup /dev/vda1 ext4 40G 7.2G 31G 20% / overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/8af005e938f71d7fe40e70ef2e0557688cc54880b71bd3b0bab2472971492427/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1c50d87e3af800afe89dbc4fbf57ebe75ad68a68bbec46f05ccf6ed24fda2a95/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1e5b4cbaedc146648b3de7f8a9fba3acd68a93a5a5bc675286aa150333d52c9a/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1bae9d89d5a26ab8a54a8d7cf0f25dca0f6117ee27ac3c4d09d8b97e52671eb0/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/0b249a8003ebe413c1886c422099daf638ce99a7ca96230edc3b59f9c0554a1e/merged tmpfs tmpfs 200M 0 200M 0% /run/user/0 0 >>>
执⾏操作系统的命令,会将结果保存在内存当中,可以⽤read() ⽅法读取出来
>>> import os >>> res = os.popen("ls -l") >>> print(res) <os._wrap_close object at 0x7f755460f3d0> >>> print(res.read()) total 80 drwxr-xr-x 9 root root 4096 Mar 22 08:56 apache-tomcat-7.0.100 drwxr-xr-x 2 root root 4096 Mar 17 08:39 docker -rw-r--r-- 1 root root 12964 Mar 17 09:40 index.html drwxr-xr-x 8 10143 10143 4096 Dec 9 20:50 jdk1.8.0_281 -rw-r--r-- 1 root root 25548 Apr 7 2017 mysql57-community-release-el7-10.noarch.rpm -rw-r--r-- 1 root root 6140 Nov 12 2015 mysql-community-release-el7-5.noarch.rpm drwxr-xr-x 2 root root 4096 Mar 24 11:38 test drwxr-xr-x 2 root root 4096 Mar 24 11:51 test_1 drwxr-xr-x 2 root root 4096 Mar 22 09:54 tomcat drwxr-xr-x 2 root root 4096 Mar 24 10:36 tomcat1 >>>
>>> import subprocess as sub >>> sub.run(["df","-Th"]) Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 1000M 0 1000M 0% /dev/shm tmpfs tmpfs 1000M 1008K 999M 1% /run tmpfs tmpfs 1000M 0 1000M 0% /sys/fs/cgroup /dev/vda1 ext4 40G 7.2G 31G 20% / overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/8af005e938f71d7fe40e70ef2e0557688cc54880b71bd3b0bab2472971492427/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1c50d87e3af800afe89dbc4fbf57ebe75ad68a68bbec46f05ccf6ed24fda2a95/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1e5b4cbaedc146648b3de7f8a9fba3acd68a93a5a5bc675286aa150333d52c9a/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/1bae9d89d5a26ab8a54a8d7cf0f25dca0f6117ee27ac3c4d09d8b97e52671eb0/merged overlay overlay 40G 7.2G 31G 20% /var/lib/docker/overlay2/0b249a8003ebe413c1886c422099daf638ce99a7ca96230edc3b59f9c0554a1e/merged tmpfs tmpfs 200M 0 200M 0% /run/user/0 CompletedProcess(args=['df', '-Th'], returncode=0) >>>
执⾏命令,返回命令的结果和执⾏状态,0或者⾮0
>>> res = subprocess.call(["ls","-l"])
total 80
drwxr-xr-x 9 root root 4096 Mar 22 08:56 apache-tomcat-7.0.100
drwxr-xr-x 2 root root 4096 Mar 17 08:39 docker
-rw-r--r-- 1 root root 12964 Mar 17 09:40 index.html
drwxr-xr-x 8 10143 10143 4096 Dec 9 20:50 jdk1.8.0_281
-rw-r--r-- 1 root root 25548 Apr 7 2017 mysql57-community-release-el7-10.noarch.rpm
-rw-r--r-- 1 root root 6140 Nov 12 2015 mysql-community-release-el7-5.noarch.rpm
drwxr-xr-x 2 root root 4096 Mar 24 11:38 test
drwxr-xr-x 2 root root 4096 Mar 24 11:51 test_1
drwxr-xr-x 2 root root 4096 Mar 22 09:54 tomcat
drwxr-xr-x 2 root root 4096 Mar 24 10:36 tomcat1
>>> res
0
>>>
执⾏命令,返回结果和状态,正常为0 ,执⾏错误则抛出异常
接受字符串形式的命令,返回 ⼀个元组形式的结果,第⼀个元素是命令执⾏状态,第⼆个为执⾏结果
接受字符串形式的命令,放回执⾏结果
执⾏命令,返回执⾏的结果,⽽不是打印
paramiko是基于Python实现的SSH2远程安全连接,⽀持认证及密钥⽅式。可以实现远程命令执⾏、⽂件传
输、中间SSH代理等功能,相对于Pexpect,封装的层次更⾼,更贴近SSH协议的功能
官⽹地址:http://www.paramiko.org/installing.html
http://docs.paramiko.org/en/2.4/
https://pypi.org/project/paramiko/
[root@canoe ~]# pip3 install paramiko
import paramiko
hostname = '' //linux服务器的ip地址
username = 'root'
password = '**********' //root账户的密码
paramiko.util.log_to_file('syslogin.log') #发送paramiko⽇志到syslogin.log⽂件
ssh = paramiko.SSHClient() #创建⼀个SSH客户端client对象
ssh.load_system_host_keys() #获取客户端host_keys,默认~/.ssh/known_hosts,⾮默
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname,username=username,password=password) #创建SSH连接
stdin,stdout,stderr = ssh.exec_command('ls /') #调⽤远程执⾏命令⽅法
print(stdout.read().decode('utf-8')) #打印命令执⾏结果,得到Python列表形式,可以
ssh.close()
-
paramiko包含两个核⼼组件,⼀个为SSHClient类,另⼀个为SFTPClient类。
SSHClient类是SSH服务会话的⾼级表示,该类封装了传输(transport)、通道(channel)及SFTPClient的校
验、建⽴的⽅法,通常⽤于执⾏远程命令。
client = SSHClient()
client.load_system_host_keys()
client.connect('ssh.example.com')
stdin, stdout,stderr = client.exec_command('ls -l')
SFTPClient作为⼀个SFTP客户端对象,根据SSH传输协议的sftp会话,实现远程⽂件操作,⽐如⽂件上传、
下载、权限、状态等操作。
import paramiko
# 实例化SSHClient
client = paramiko.SSHClient()
# ⾃动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不再本地know_hosts⽂件中记录的主机
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接SSH服务端,以⽤户名和密码进⾏认证
client.connect(hostname='', port=22, username='root', password='*********')
# 打开⼀个Channel并执⾏命令
stdin, stdout, stderr = client.exec_command('ls -al /') # stdout 为正确输出,stderr
# 打印执⾏结果
print(stdout.read().decode('utf-8'))
# 关闭SSHClient
client.close()
import paramiko transport = paramiko.Transport(("", 22)) transport.connect(username='root', password='**********') sftp = paramiko.SFTPClient.from_transport(transport) sftp.put('C:\\Users\\canoe\\Desktop\\taobao.zip', '/opt/tamp/taobao.zip') sftp.close()
- 1
- 2
- 3
- 4
- 5
- 6
import paramiko transport = paramiko.Transport(("", 22)) transport.connect(username='root', password='*********') sftp = paramiko.SFTPClient.from_transport(transport) sftp.get('/opt/tamp/taobao.zip', 'C:\\Users\\canoe\\Desktop\\taobao.zip') sftp.close()
- 1
- 2
- 3
- 4
- 5
- 6
下载地址:https://www.sourcetreeapp.com/
双击下载的.exe文件,进行安装
登录www.gitee.com创建一个仓库
通过创建的仓库生成的HTTP/SSH链接,在本地远程访问仓库,进行拉取,提交推送
使用sourcetree克隆远程仓库
创建一个文件并提交
推送本地仓库到远程仓库里面去
查看远程仓库
tar -zxvf 包
[root@canoe ~]# java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
[root@canoe ~]#
[root@canoe bin]# ./startup.sh
Using CATALINA_BASE: /root/tomcat10
Using CATALINA_HOME: /root/tomcat10
Using CATALINA_TMPDIR: /root/tomcat10/temp
Using JRE_HOME: /root/jdk1.8.0_281
Using CLASSPATH: /root/tomcat10/bin/bootstrap.jar:/root/tomcat10/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@canoe bin]# ./shutdown.sh
Using CATALINA_BASE: /root/tomcat10
Using CATALINA_HOME: /root/tomcat10
Using CATALINA_TMPDIR: /root/tomcat10/temp
Using JRE_HOME: /root/jdk1.8.0_281
Using CLASSPATH: /root/tomcat10/bin/bootstrap.jar:/root/tomcat10/bin/tomcat-juli.jar
Using CATALINA_OPTS:
[root@canoe bin]#
[root@canoe ~]# yum update
[root@canoe ~]# systemctl stop firewalld #关闭防火墙
[root@canoe ~]# systemctl disable firewalld #默认开机不启动防火墙
[root@canoe ~]# yum install -y iptables-services #安装iptables
[root@canoe ~]# systemctl start iptables #启动iptables
[root@canoe ~]# systemctl enable iptables #开机启动iptables
[root@canoe ~]# iptables -F #清空iptables设置规则
[root@canoe ~]# service iptables save
下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
需要下载的软件包
创建一个docker目录,将上述下载的文件赋值过去
[root@canoe ~]# mkdir ~/docker
复制下载的文件到此目录
[root@canoe docker]# yum -y install * #安装
[root@canoe docker]# systemctl start docker #启动
[root@canoe docker]# systemctl enable docker #设置开机启动
[root@canoe docker]# docker run hello-world #测试是否安装成功:
将docker配置国内的阿里云的镜像上,以便于后期进行下载与访问速度快
在/etc/下新建一个docker目录
[root@canoe ~]# mkdir -p /etc/docker
新建一个daemon.json文件
[root@canoe docker]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://jvxivkc1.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://jvxivkc1.mirror.aliyuncs.com"]
}
[root@canoe docker]# sudo systemctl daemon-reload
[root@canoe docker]# sudo systemctl restart docker
下载wordpress镜像
[root@canoe ~]# docker pull wordpress
下载mariadb镜像
[root@canoe ~]# docker pull mariadb
查看镜像
[root@canoe ~]# docker images
启动mariadb
[root@canoe ~]# docker run --name db --env MYSQL_ROOT_PASSWORD=example -d mariadb
启动wordpress
[root@canoe ~]# docker run --name MyWordPress2 --link db:mysql -p 8080:80 -d wordpress
查看docker中启动的进程
[root@canoe ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11e4919c8d9f wordpress "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp MyWordPress2
c7a80f0f3528 mariadb "docker-entrypoint.s…" 3 hours ago Up 3 hours 3306/tcp db
访问wordpress
- 浏览器访问:http://106.13.30.87:8080/
2.
3.
4.
容器创建时需要指定镜像,每个镜像都由唯一的标示 Image ID ,和容器的 Container ID 一样,默认 128 位,可以使用前 16 为缩略形式,也可以使用镜像名与版本号两部分组合唯一标示,如果省略版本号,默认使用最新版本标签 ( latesr )
镜像的分层:Docker 的镜像通过联合文件系统 ( union filesystem ) 将各层文件系统叠加在一起
docker commit CID xx.xx.xx
转换命令
docker build -t wangyang/jdk-tomcat
新建一个目录
[root@canoe ~]# mkdir test
[root@canoe ~]# cd test
新建一个Dockerfile文件与新建一个hello.txt
[root@canoe test]# touch Dockerfile
[root@canoe test]# touch hello.txt
向Dockerfile文件写入
[root@canoe test]# vim Dockerfile
-
容器转为镜像
[root@canoe test]# docker build -t self_tomcat:V1.0 .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM tomcat:latest
---> 040bdb29ab37
Step 2/4 : MAINTAINER 指定作者的名字
---> Using cache
---> 7a4403b55301
Step 3/4 : RUN mkdir /usr/local/tomcat/webapps/test
---> Using cache
---> e29f8cbb4a99
Step 4/4 : ADD ./hello.txt /usr/local/tomcat/webapps/test
---> 9b25feb11939
Successfully built 9b25feb11939
Successfully tagged self_tomcat:V1.0
[root@canoe test]#
启动镜像
[root@canoe test]# docker run --name tomcat_self1 -d -p 8080:8088 self_tomcat:V1.0
cf59691df26cd06a51454af5cd26af19b914c0041fc03b230fdd1a617784d968
-
浏览器访问
架构组成:ansible核心、ansible模块
工作原理:
yum安装:
配置文件
定义远控主机文件:
/etc/ansible/hosts
[servers]
192.168.31.107
192.168.31.108
192.168.31.109
生成密钥
# ssh-keygen
使用ssh-copy-id命令来复制ansible公钥到各个节点
[root@canoe ~]# ssh-copy-id root@192.168.31.107
[root@canoe ~]# ssh-copy-id root@192.168.31.108
[root@canoe ~]# ssh-copy-id root@192.168.31.109
执行ping命令
[root@canoe ~]# ansible servers -m ping
192.168.31.108 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.31.109 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.31.107 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
指定用户,执行ping命令
[root@canoe ~]# ssh-copy-id chen@192.168.31.107
[root@canoe ~]# ssh-copy-id chen@192.168.31.108
[root@canoe ~]# ssh-copy-id chen@192.168.31.109
[root@canoe ~]# ansible all -m ping -u chen
192.168.31.109 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.31.108 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.31.107 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
执行命令echo hello
[root@canoe ~]# ansible -a "echo hello" servers
192.168.31.109 | CHANGED | rc=0 >>
hello
192.168.31.107 | CHANGED | rc=0 >>
hello
192.168.31.108 | CHANGED | rc=0 >>
hello
执行命令date -R
[root@canoe ~]# ansible -m command -a "date -R" servers
192.168.31.108 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:55 +0800
192.168.31.109 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:54 +0800
192.168.31.107 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:56 +0800
执行命令uptime
[root@canoe ~]# ansible -m command -a "uptime" servers
192.168.31.108 | CHANGED | rc=0 >>
12:17:21 up 23 min, 2 users, load average: 0.00, 0.01, 0.05
192.168.31.109 | CHANGED | rc=0 >>
12:17:21 up 22 min, 2 users, load average: 0.24, 0.06, 0.07
192.168.31.107 | CHANGED | rc=0 >>
12:17:22 up 11 min, 2 users, load average: 0.08, 0.09, 0.07
ping": “pong”
}
192.168.31.108 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
192.168.31.107 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
```
执行命令echo hello
[root@canoe ~]# ansible -a "echo hello" servers
192.168.31.109 | CHANGED | rc=0 >>
hello
192.168.31.107 | CHANGED | rc=0 >>
hello
192.168.31.108 | CHANGED | rc=0 >>
hello
执行命令date -R
[root@canoe ~]# ansible -m command -a "date -R" servers
192.168.31.108 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:55 +0800
192.168.31.109 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:54 +0800
192.168.31.107 | CHANGED | rc=0 >>
Sun, 28 Mar 2021 12:15:56 +0800
执行命令uptime
[root@canoe ~]# ansible -m command -a "uptime" servers
192.168.31.108 | CHANGED | rc=0 >>
12:17:21 up 23 min, 2 users, load average: 0.00, 0.01, 0.05
192.168.31.109 | CHANGED | rc=0 >>
12:17:21 up 22 min, 2 users, load average: 0.24, 0.06, 0.07
192.168.31.107 | CHANGED | rc=0 >>
12:17:22 up 11 min, 2 users, load average: 0.08, 0.09, 0.07
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。