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 http://mirrors.aliyun.com/repo/Centos-7.repo

vi Centos-7.repo

vi Dockerfile

#----------------------------------------------------------------------

FROM 10.240.4.159/os/centos:7.5.1804

ADD Shanghai /etc/localtime

ADD Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo

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 ""

&& sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config

&& echo "ydgw.cn" | 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里设置以下两项

命令:/usr/sbin/sshd -D

添加卷:/etc/localtime:/etc/localtime:ro

# 其他命令参考

# 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 http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

#----------------------------------------------------------------------

cp /usr/share/zoneinfo/Asia/Shanghai .

vi Dockerfile

#----------------------------------------------------------------------

FROM 10.240.4.159/os/ubuntu:18.04

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.cn' | 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.6.6.tgz

vi Dockerfile

#----------------------------------------------------------------------

FROM 10.240.4.159/os/centos:7.5.1804-ssh

COPY Python-3.6.6.tgz /tmp/Python-3.6.6.tgz

RUN cd /tmp

&& tar zxvpf Python-3.6.6.tgz

&& cd Python-3.6.6

&& yum -y install gcc zlib zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel sqlite-devel

&& ./configure

&& make

&& make install

&& mkdir ~/.pip

&& echo "[global]" >> ~/.pip/pip.conf

&& echo "index-url = https://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf

&& echo "[install]" >> ~/.pip/pip.conf

&& echo "trusted-host=mirrors.aliyun.com" >> ~/.pip/pip.conf

&& 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

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]" >> ~/.pip/pip.conf

&& echo "index-url = https://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf

&& echo "[install]" >> ~/.pip/pip.conf

&& echo "trusted-host=mirrors.aliyun.com" >> ~/.pip/pip.conf

#----------------------------------------------------------------------

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 https://mirrors.aliyun.com/pypi/simple/ Image

# pip3 show django

# pip3 list --outdated

Windows客户端配置

相关软件Sublime3

Atom: https://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=https://registry.npm.taobao.org/

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 project.

重启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.cn",

"promptForPass": false,

"remote": "/",

"local": "",

"agent": "",

"privatekey": "",

"passphrase": "",

"hosthash": "",

"ignorehost": true,

"connTimeout": 10000,

"keepalive": 10000,

"keyboardInteractive": false,

"keyboardInteractiveForPass": false,

"remoteCommand": "",

"remoteShell": "",

"watch": [],

"watchTimeout": 500

}

ctrl + alt + c连接远程服务器,成功显示如下

ctrl + alt + o 打开Remote标签,可以查看远程目录,并修改内容

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值