#在部署之前,一定要修改hosts的配置文件,在脚本中并不能修改配置文件内容
import os
import pexpect
import paramiko
import re

vip = ‘192.168.5.80’
rs1_ip = ‘192.168.5.81’
rs2_ip = ‘192.168.5.82’
rs3_ip = ‘192.168.5.83’

def hostname():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.80’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(
‘hostnamectl set-hostname master’
‘cd /root/;’
)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.81’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(
‘hostnamectl set-hostname node1’
)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.82’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(
‘hostnamectl set-hostname node2’
)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.83’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(
‘hostnamectl set-hostname node3’
)
print(‘集群名字修改成功’)

def yum_already():
res1 = os.system(‘yum -y install ansible &> /dev/null’)
res2 = os.system(‘wget http://download2.yunwei.edu/shell/kubernetes.tar.gz &> /dev/null’)
if res1 == 0 and res2 == 0:
print(‘ansible配置正常’)
print(‘k8s配置文件下载成功’)
else:
print(‘ansible配置出错’)
print(‘k8s配置文件下载失败’)

def ready():
back = pexpect.spawn(‘ssh-keygen’)
back.expect(‘save the key:’)
back.sendline(’’)
back.sendline(’’)
back.sendline(’’)
back.sendline(’’)
print(‘密钥生成成功’)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.181’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(

    'echo "192.168.5.80 master" >> /etc/hosts;'
    'echo "192.168.5.81 node1" >> /etc/hosts;'
    'echo "192.168.5.82 node2" >> /etc/hosts;'
    'echo "192.168.5.83 node3" >> /etc/hosts;'
    
    'echo "[srv]" >> /etc/ansible/hosts;'
    'echo "192.168.5.80" >> /etc/ansible/hosts;'
    'echo "192.168.5.81" >> /etc/ansible/hosts;'
    'echo "192.168.5.82" >> /etc/ansible/hosts;'
    'echo "192.168.5.83" >> /etc/ansible/hosts;'
    
    'ssh-copy-id master;'
    'ssh-copy-id node1'
    'ssh-copy-id node2'
    'ssh-copy-id node3'
)
print('免密登陆和ansible配置完成')

def text():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.80’, 22, ‘root’, ‘123ece’, timeout=5)
stdin, stdout, stderr = ssh.exec_command(
‘ansible srv -m ping’
)
print(stdout.read().decode(‘utf-8’))
print(stderr.read().decode(‘utf-8’))

def devide():
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.80’, 22, ‘root’, ‘123ece’, timeout=5)
ssh.exec_command(
‘cd /root/;’
‘tar -zxvf kubernetes.tar.gz;’
‘cd kubernetes;’
‘tar -xxvf kube-yunwei-197.tar.gz;’
‘tar -zxvf k8s197.tar.gz;’
‘mv bin /etc/ansible/;’
‘cd kube-yunwei-197;’
)
except Exception as e:
print(e)

back1 = pexpect.spawn('mv * /etc/ansible/')
back1.expect('')
back1.sendline('y')
back1.sendline('n')

res3 = os.system('cd /etc/ansible/example')
if res3 == 0:
    print(' 配置文件')
else:
    print('找不到改配置文件')

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.5.80', 22, 'root', '123ece', timeout=5)
stdin, stdout, stderr = ssh.exec_command(
    'mv /etc/ansible/example/hosts.s-master.example /etc/ansible/hosts;'
    'cd /etc/ansible/;'
    'ansible-playbook 01.prepare.yml;'
    'ansible-playbook 02.etcd.yml;'
    'ansible-playbool 03.docker.yml'
    'cd /root/kubernetes;'
    'tar -zxvf image.tar.gz;'
    'cd /root/kubernetes/image;'
    '(mv bash-completion-2.1-6.el7.noarch.rpm coredns-1.0.6.tar.gz heapster-v1.5.1.tar kubernetes-dashboard-amd64-v1.8.3.tar.gz grafana-v4.4.3.tar influxdb-v1.3.3.tar pause-amd64-3.1.tar calico;'
    'scp -r calico node1:/root/;'
    'scp -r calico node2:/root/;'
    'scp -r calico node3:/root/;'
)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))

def images():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.81’, 22, ‘root’, ‘123ece’, timeout=5)
stdin, stdout, stderr = ssh.exec_command(
‘cd /root/image/calico && for im in ls ; do docker load -i $im ; done;’
)
print(stdout.read().decode(‘utf-8’))
print(stderr.read().decode(‘utf-8’))

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.5.82', 22, 'root', '123ece', timeout=5)
stdin, stdout, stderr = ssh.exec_command(
    'cd /root/image/calico && for im in `ls` ; do docker load -i $im ; done;'
)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.5.83', 22, 'root', '123ece', timeout=5)
stdin, stdout, stderr = ssh.exec_command(
    'cd /root/image/calico && for im in `ls` ; do docker load -i $im ; done;'
)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))

def devide1():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘192.168.5.80’, 22, ‘root’, ‘123ece’, timeout=5)
stdin, stdout, stderr = ssh.exec_command(
‘cd /etc/ansible/;’
‘ansible-playbook 04.kube-master.yml;’
‘ansible-playbook 05.kube-node.yml;’
‘ansible-playbook 06.network.yml;’
‘cd /etc/ansible/manifests/coredns;’
‘kubectl create -f coredns.yaml;’
‘cd /etc/ansible/manifests/dashboard;’
‘kubectl create -f .;’
‘cd /etc/ansible/manifests/heapster;’
‘kubectl create -f .;’
)
print(stdout.read().decode(‘utf-8’))
print(stderr.read().decode(‘utf-8’))
print(‘k8s集群部署成功’)

if name == ‘main’:
hostname()
yum_already()
ready()
text()
devide()
images()
devide1()

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐