python3⾃动运维_Python3⾃动化运维
前⾔本⽂选⽤的Python服务器操作系统,是最常⽤于开发和⽣产环境的Linux系统:Ubuntu和Centos。
为了测试⽅便和快速部署,本⽂将使⽤Docker + Rancher来部署环境。 Docker和Rancher的使⽤可参看本博的其他⽂章。本⽂只记录实际操作过程中的必要步骤,并⾮完全步骤。
制作服务端Linux OS基镜像
制作Centos并启⽤SSH的基镜像docker pull centos:7.5.1804
docker tag centos:7.5.1804 10.240.4.159/os/centos:7.5.1804
docker push 10.240.4.159/os/centos:7.5.1804
mkdir centos-ssh
cd centos-ssh
cp /usr/share/zoneinfo/Asia/Shanghai .
wget mirrors.aliyun/po
po
vi Dockerfile
#----------------------------------------------------------------------
FROM 10.240.4.159/os/centos:7.5.1804
ADD Shanghai /etc/localtime
po /pos.po
RUN yum install openssl openssh-server -y
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
&& ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
&& ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
composition翻译成中文&& sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
&& echo "ydgw" | passwd --stdin root
ENTRYPOINT ["/usr/sbin/sshd"]
CMD ["-D"]
#----------------------------------------------------------------------
docker build -t 10.240.4.159/os/centos:7.5.1804-ssh .
docker push 10.240.4.159/os/centos:7.5.1804-ssh
# 在rancher下新建并启动容器(以下都为在rancher⾥操作 SSH端⼝16022)
# docker镜像中不启动SSH,也没有设定时区,需要在rancher⾥设置以下两项se是常量元素还是微量元素
命令:/usr/sbin/sshd -D
添加卷:/etc/localtime:/etc/localtime:ro
# 其他命令参考
centos和ubuntu
# docker ps -a | grep ubuntu
# docker commit -m 'install openssh vi' bcdc3c988539 10.240.4.159/os/ubuntu:18.04-ssh
制作ubuntu并启⽤SSH的基镜像docker pull ubuntu:18.04
docker tag ubuntu:18.04 10.240.4.159/os/ubuntu:18.04
docker push 10.240.4.159/os/ubuntu:18.04
mkdir ubuntu-ssh
cd ubutntu-ssh
vi sources.list
#----------------------------------------------------------------------
deb mirrors.aliyun/ubuntu/ bionic main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ bionic-security main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ bionic-updates main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ bionic-proposed main restricted universe multiverse
deb mirrors.aliyun/ubuntu/ bionic-backports main restricted universe multiverse deb-src mirrors.aliyun/ubuntu/ bionic main restricted universe multiverse
deb-src mirrors.aliyun/ubuntu/ bionic-security main restricted universe multiverse deb-src mirrors.aliyun/ubuntu/ bionic-updates main restricted universe multiverse deb-src mirrors.aliyun/ubuntu/ bionic-proposed main restricted universe multiverse deb-src mirrors.aliyun/ubuntu/ bionic-backports main restricted universe multiverse #----------------------------------------------------------------------
cp /usr/share/zoneinfo/Asia/Shanghai .
vi Dockerfile
#----------------------------------------------------------------------
FROM 10.240.4.159/os/ubuntu:18.04
html语义化的理解
ADD sources.list /etc/apt
ADD Shanghai /etc/localtime
RUN apt update
&& apt -y upgrade
&& apt -y install vim openssh-server net-tools
&& apt clean
&& echo 'root:ydgw' | chpasswd
&& sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
&& mkdir /var/run/sshd
ENTRYPOINT ["/usr/sbin/sshd"]
CMD ["-D"]
#----------------------------------------------------------------------
docker build -t 10.240.4.159/os/ubuntu:18.04-ssh .
docker push 10.240.4.159/os/ubuntu:18.04-ssh
# 在linux下启动容器
# docker run -d --name ubuntu-1 10.240.4.159/os/ubuntu:18.04-ssh
# 在rancher下启动容器(以下都为在rancher⾥操作 SSH端⼝16021)
制作含python3和pip3的镜像
Centos安装python3和pip3# 为了⽅便⽇后全新部署,下⾯再制作含python3和pip3的系统镜像
# pip python package manager
mkdir centos-python36
cd centos-python36
rz Python-3.
vi Dockerfile
#----------------------------------------------------------------------
FROM 10.240.4.159/os/centos:7.5.1804-ssh
COPY Python-3. /tmp/Python-3.
RUN cd /tmp
&& tar zxvpf Python-3.
&& cd Python-3.6.6
&& yum -y install gcc zlib zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel sqlite-devel && ./configure
get什么意思网络用语&& make
&& make install
&& mkdir ~/.pip
&& echo "[global]" >> ~/.f
&& echo "index-url = mirrors.aliyun/pypi/simple/" >> ~/.f
&& echo "[install]" >> ~/.f
&& echo "trusted-host=mirrors.aliyun" >> ~/.f
&& rm -rf /tmp/*
&& echo "export LANG=en_US.utf8" >> /etc/profile
#----------------------------------------------------------------------
docker build -t 10.240.4.159/os/centos:7.5.1804-ssh-python36-sqlite .
docker push 10.240.4.159/os/centos:7.5.1804-ssh-python36-sqlite
# ⽤rancher启动⽤centos:7.5.1804-ssh-python36镜像创建的容器,安装Django
pip3 install Django==2.0.6
Ubuntu安装pip3
Python3.6.5在ubuntu:18.04-ssh的基镜像中已默认安装,所以下⾯只安装pip3mkdir ubuntu-python
curse是什么意思cd ubuntu-python
vi Dockerfile
#----------------------------------------------------------------------
FROM 10.240.4.159/os/ubuntu:18.04-ssh
RUN apt update
&& apt -y upgrade
&& apt -y install python3-pip
&& apt clean
&& mkdir ~/.pip
&& echo "[global]" >> ~/.f
&& echo "index-url = mirrors.aliyun/pypi/simple/" >> ~/.f
&& echo "[install]" >> ~/.f
&& echo "trusted-host=mirrors.aliyun" >> ~/.f
#----------------------------------------------------------------------
docker build -t 10.240.4.159/os/ubuntu:18.04-ssh-python36 .
docker push 10.240.4.159/os/ubuntu:18.04-ssh-python36
# ⽤rancher启动⽤ubuntu:18.04-ssh-python36镜像创建的容器,然后测试安装Django
pip3 install Django==2.0.6
## 参考命令
# pip3 install --index-url mirrors.aliyun/pypi/simple/ Image
# pip3 show django
# pip3 list --outdated
Windows客户端配置
相关软件Sublime3
Atom: atom.io/ (本课程使⽤版本1.28.0)
⽤Sublime搭建Python开发环境
python安装时指定路径如D:PythonPython36,若安装后没有⾃动添加环境变更,⼿动将以下内容添加到PATH中D:PythonPython36;D:PythonPython36Scripts;
print('hello world')
# 保存成1.py ctrl + b 运⾏,能成功显⽰表⽰配置成功
Atom安装使⽤
windows下安装使⽤默认设置(下⼀步下⼀步),若安装后没有⾃动添加环境变更,⼿动将以下内容添加到PATH中
C:UsersliuyajunAppDataLocalatombin
更换atom apm 国内源(加快package安装速度),修改C:Usersliuyajun.atom.apm.apmrc⽂件,添加以下内容:
registry=registry./
strict-ssl=false
安装后检测是否成功,windows cmd中执⾏apm install --check
Checking for native build tools done # done 代表检测正常
打开ATMO安装常⽤包,setting -> Install -> 搜索chineses和ftp,选择以下两个包安装
simplified-chinese-menu Atom 的简体中⽂语⾔包,完整,兼容所有已发布版本 Atom
remote-ftp Enable browsing remote FTP/FTPS/SFTP just like the built-in Tree View. Requires a proje
ct.
重启atom,atom如果报错"Cannot load the system dictionary for zh-CN",解决⽅法是打开Atom-Files-Settings-Packages搜索spell check,发现了这个spell-check的插件,
取消掉Use Locales前⾯的勾选或者⼿动填写en-US设置⽂件名称。
atom常⽤快捷键ctrl-alt-c remote-ftp:connect Remote Ftp atom-workspace
ctrl-alt-d remote-ftp:disconnect Remote Ftp atom-workspace
ctrl-alt-o remote-ftp:toggle Remote Ftp atom-workspace
新建Atom项⽬
WINDOWS在⼯作⽬录下新建⼀个空⽬录,如centos-16022,将这个⽬录拖拽到Atom中,然后扩展->Remote FTP->Create SFTP config file
修改 host/port/user/pass⼏项内容后保存{
"protocol": "sftp",
"host": "10.240.4.160",
"port": 16022,
"user": "root",
"pass": "ydgw",
"promptForPass": false,
"remote": "/",
"local": "",
"agent": "",
"privatekey": "",
"passphrase": "",
"hosthash": "",
"ignorehost": true,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。