def checkRemotePath(c, path, nodename): # 初始化远程工具对象 robj = Remote(getEnv(nodename)) # 下载文件到本地 result = robj.checkpath(path) if result == False: print("Remote Path not exists.") if not confirm("Path not exists in remote machine, Continue[Y/N]?"): return True else: print("Remote Path exists.") return True
def installKbsKubelet(self, nodename): node_env = self.getEnv(nodename) # 初始化远程工具对象 robj = Remote(node_env) print("node ip: " + node_env.host) node_env1 = self.getEnv("master1") # 初始化远程工具对象 robj_master1 = Remote(node_env1) admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) print("master ip: " + node_env1.host) remoteSslPath = self.getRemotePath('remoteSslPath') remoteCfgPath = self.getRemotePath('remoteCfgPath') remoteBinPath = self.getRemotePath('remoteBinPath') remoteSystemdPath = self.getRemotePath('remoteSystemdPath') tmpPath = self.getLocalPath('tmpPath') kbsBinPath = self.getLocalPath('kbsBinPath') kbsConfigPath = self.getLocalPath('kbsConfigPath') #download ca files from master1 to local robj_master1.download(remoteSslPath + "/ca.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/ca.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/ca-key.pem", tmpPath + "/") #download etcd ca files from master1 to local robj_master1.download(remoteSslPath + "/etcd.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/etcd.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/etcd-key.pem", tmpPath + "/") robj_master1.download(remoteCfgPath + "/token.csv", tmpPath + "/") #download kubeconfig files from admin to local robj_admin.download(remoteCfgPath + "/bootstrap.kubeconfig", tmpPath + "/") #stop relative service if the service is running cmdRemote = "systemctl stop kube-kubelet" robj.sudo(cmdRemote) #upload binary file for kube processes if robj.checkpath(remoteBinPath + "/kubelet") == False: robj.upload(kbsBinPath + "/kubelet", remoteBinPath + "/") #upload ca files to node1 robj.upload(tmpPath + "/ca.csr", remoteSslPath + "/") robj.upload(tmpPath + "/ca.pem", remoteSslPath + "/") robj.upload(tmpPath + "/ca-key.pem", remoteSslPath + "/") #upload etcd ca files to node1 robj.upload(tmpPath + "/etcd.csr", remoteSslPath + "/") robj.upload(tmpPath + "/etcd.pem", remoteSslPath + "/") robj.upload(tmpPath + "/etcd-key.pem", remoteSslPath + "/") #upload config files to node1 robj.upload(tmpPath + "/bootstrap.kubeconfig", remoteCfgPath + "/") robj.upload(tmpPath + "/token.csv", remoteCfgPath + "/") sedRegex = "sed \"s/{serviceClusterDnsIp}/%s/g\"" % self.serviceClusterDnsIp file_tmp = tmpPath + "/kubelet.config" file_config = kbsConfigPath + "/kubelet.config" cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) robj.local(cmd_local) # 上传文件到远程主机 robj.upload(file_tmp, remoteCfgPath + "/") #upload systemd service files to node robj.upload(kbsConfigPath + "/kube-kubelet.service", remoteSystemdPath + "/") print("node ip: " + node_env.host) #upload kubelet service config file file_config = kbsConfigPath + "/kube-kubelet.conf" file_tmp = tmpPath + "/kube-kubelet.conf" sedRegex = "sed \"s/^--hostname-override={nodeName}/--hostname-override=%s/g\"" % node_env.host sedRegex = sedRegex + " | sed \"s/^--network-plugin={pluginName}//g\"" sedRegex = sedRegex + " | sed \"s/^--cni-conf-dir={confDir}//g\"" sedRegex = sedRegex + " | sed \"s/^--cni-bin-dir={binDir}//g\"" sedRegex = sedRegex + " | sed \"s/{serviceClusterDnsIp}/%s/g\"" % self.serviceClusterDnsIp cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) robj.local(cmd_local) # 上传文件到远程主机 robj.upload(file_tmp, remoteCfgPath, True) print("systemd daemon reload ...") cmdRemote = "systemctl daemon-reload" robj.sudo(cmdRemote) print("enable kubelet service ...") cmdRemote = "systemctl enable kube-kubelet.service" robj.sudo(cmdRemote) print("restart kubelet service ...") cmdRemote = "systemctl restart kube-kubelet.service" robj.sudo(cmdRemote) print("check kubelet service ...") cmdRemote = "systemctl status kube-kubelet.service" robj.sudo(cmdRemote) print("sleep 5 seconds ...") cmdRemote = "sleep 5" robj.sudo(cmdRemote) print("restart kube apiserver ...") cmdRemote = "systemctl restart kube-apiserver" robj_master1.sudo(cmdRemote) print("show csr info: ") cmdRemote = remoteBinPath + "/kubectl get csr" robj_admin.sudo(cmdRemote) print("pass tls request: ") cmdRemote = remoteBinPath + "/kubectl get csr | grep 'Pending'" cmdRemote = cmdRemote + " | awk 'NR>0 {print $1}'" cmdRemote = cmdRemote + " | xargs " + remoteBinPath + "/kubectl certificate approve" robj_admin.sudo(cmdRemote)
def installKbsProxy(self, nodename): node_env1 = self.getEnv("master1") # 初始化远程工具对象 robj_master1 = Remote(node_env1) admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) node_env = self.getEnv(nodename) # 初始化远程工具对象 robj = Remote(node_env) remoteSslPath = self.getRemotePath('remoteSslPath') remoteCfgPath = self.getRemotePath('remoteCfgPath') remoteBinPath = self.getRemotePath('remoteBinPath') remoteSystemdPath = self.getRemotePath('remoteSystemdPath') tmpPath = self.getLocalPath('tmpPath') kbsBinPath = self.getLocalPath('kbsBinPath') kbsConfigPath = self.getLocalPath('kbsConfigPath') #download proxy cert files from master1 robj_master1.download(remoteSslPath + "/kube-proxy.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/kube-proxy.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/kube-proxy-key.pem", tmpPath + "/") #download proxy kubeconfig files from admin node robj_admin.download(remoteCfgPath + "/kube-proxy.kubeconfig", tmpPath + "/") robj_master1.download(remoteSslPath + "/front-proxy-ca.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/front-proxy-ca-key.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/front-proxy-client.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/front-proxy-client.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/front-proxy-client-key.pem", tmpPath + "/") robj.upload(tmpPath + "/front-proxy-ca.pem", remoteSslPath + "/") robj.upload(tmpPath + "/front-proxy-ca-key.pem", remoteSslPath + "/") robj.upload(tmpPath + "/front-proxy-client.csr", remoteSslPath + "/") robj.upload(tmpPath + "/front-proxy-client.pem", remoteSslPath + "/") robj.upload(tmpPath + "/front-proxy-client-key.pem", remoteSslPath + "/") #stop relative service if the service is running cmdRemote = "systemctl stop kube-proxy" robj.sudo(cmdRemote) #install some packages for proxy cmdRemote = "yum install -y ipvsadm ipset conntrack" robj.sudo(cmdRemote) #upload binary file for kube processes if robj.checkpath(remoteBinPath + "/kube-proxy") == False: robj.upload(kbsBinPath + "/kube-proxy", remoteBinPath + "/") #upload proxy cert files to node1 robj.upload(tmpPath + "/kube-proxy.pem", remoteSslPath + "/") robj.upload(tmpPath + "/kube-proxy-key.pem", remoteSslPath + "/") #upload proxy kubeconfig files to node1 robj.upload(tmpPath + "/kube-proxy.kubeconfig", remoteCfgPath + "/") #upload systemd service files to node1 robj.upload(kbsConfigPath + "/kube-proxy.service", remoteSystemdPath + "/") #upload proxy service config file sedRegex = "sed \"s/^--hostname-override={nodeName}/--hostname-override=%s/g\"" % node_env.host sedRegex = sedRegex + " | sed \"s/{podClusterIpRange}/%s/g\"" % self.podClusterIpRange file_tmp = tmpPath + "/kube-proxy.conf" file_config = kbsConfigPath + "/kube-proxy.conf" cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) robj.local(cmd_local) # 上传文件到远程主机 robj.upload(file_tmp, remoteCfgPath + "/", True) print("systemd daemon reload ...") cmdRemote = "systemctl daemon-reload" robj.sudo(cmdRemote) print("enable proxy service ...") cmdRemote = "systemctl enable kube-proxy.service" robj.sudo(cmdRemote) print("restart proxy service ...") cmdRemote = "systemctl restart kube-proxy.service" robj.sudo(cmdRemote) print("check proxy service ...") cmdRemote = "systemctl status kube-proxy.service" robj.sudo(cmdRemote)
def installKubectl(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) master1_env = self.getEnv("master1") # 初始化远程工具对象 robj_master1 = Remote(master1_env) remoteBinPath = self.getRemotePath('remoteBinPath') remoteSslPath = self.getRemotePath('remoteSslPath') tmpPath = self.getLocalPath('tmpPath') kbsBinPath = self.getLocalPath('kbsBinPath') kbsConfigPath = self.getLocalPath('kbsConfigPath') robj_master1.upload(kbsConfigPath + "/admin-csr.json", remoteSslPath + "/") #create kubectl cert files in master1 cmdRemote = "/usr/local/bin/cfssl gencert" cmdRemote = cmdRemote + " -ca=" + remoteSslPath + "/ca.pem" cmdRemote = cmdRemote + " -ca-key=" + remoteSslPath + "/ca-key.pem" cmdRemote = cmdRemote + " -config=" + remoteSslPath + "/ca-config.json" cmdRemote = cmdRemote + " -profile=kubernetes " + remoteSslPath + "/admin-csr.json" cmdRemote = cmdRemote + " | /usr/local/bin/cfssljson -bare " + remoteSslPath + "/admin" robj_master1.sudo(cmdRemote) #download binary files to local # robj_master1.download(remoteBinPath+"/kubectl", tmpPath+"/") #download ca files from master1 to local robj_master1.download(remoteSslPath + "/ca.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/ca.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/ca-key.pem", tmpPath + "/") #download kubectl ca files from master1 to local robj_master1.download(remoteSslPath + "/admin.csr", tmpPath + "/") robj_master1.download(remoteSslPath + "/admin.pem", tmpPath + "/") robj_master1.download(remoteSslPath + "/admin-key.pem", tmpPath + "/") # upload kubectl file if robj_admin.checkpath(remoteBinPath + "/kubectl") == False: robj_admin.upload(kbsBinPath + "/kubectl", remoteBinPath + "/") #upload cert files to master node robj_admin.upload(tmpPath + "/ca.pem", remoteSslPath + "/") robj_admin.upload(tmpPath + "/admin.pem", remoteSslPath + "/") robj_admin.upload(tmpPath + "/admin-key.pem", remoteSslPath + "/") #set cluster parameters cmdRemote = remoteBinPath + "/kubectl config set-cluster kubernetes" cmdRemote = cmdRemote + " --certificate-authority=" + remoteSslPath + "/ca.pem" cmdRemote = cmdRemote + " --embed-certs=true" cmdRemote = cmdRemote + " --server=https://" + self.proxy_host + ":" + self.proxy_port robj_admin.sudo(cmdRemote) #set client authority parameters cmdRemote = remoteBinPath + "/kubectl config set-credentials admin" cmdRemote = cmdRemote + " --client-certificate=" + remoteSslPath + "/admin.pem" cmdRemote = cmdRemote + " --embed-certs=true" cmdRemote = cmdRemote + " --client-key=" + remoteSslPath + "/admin-key.pem" robj_admin.sudo(cmdRemote) #set context parameters cmdRemote = remoteBinPath + "/kubectl config set-context kubernetes" cmdRemote = cmdRemote + " --cluster=kubernetes" cmdRemote = cmdRemote + " --user=admin" robj_admin.sudo(cmdRemote) #set default context cmdRemote = remoteBinPath + "/kubectl config use-context kubernetes" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl get nodes -o wide" robj_admin.sudo(cmdRemote)
def installNodeNormal(self, nodename): # 初始化远程工具对象 robj = Remote(self.getEnv(nodename)) cfsslBinPath = self.getLocalPath('cfsslBinPath') configPath = self.getLocalPath('configPath') remoteSysctlPath = self.getRemotePath('remoteSysctlPath') remoteLocalBinPath = self.getRemotePath('remoteLocalBinPath') # remoteSystemdPath = self.getRemotePath('remoteSystemdPath') # tmpPath = self.getLocalPath('tmpPath') # dockerConfigPath = self.getLocalPath('dockerConfigPath') #install some packages for proxy # cmdRemote = "yum install -y ipvsadm ipset conntrack" # cmdRemote = "yum install -y conntrack" # robj.sudo(cmdRemote) #upload cfssl file robj.upload(cfsslBinPath + "/cfssl", remoteLocalBinPath + "/") robj.upload(cfsslBinPath + "/cfssljson", remoteLocalBinPath + "/") robj.upload(cfsslBinPath + "/cfssl-certinfo", remoteLocalBinPath + "/") #cfssl tool if robj.checkpath(remoteLocalBinPath + "/cfssl") == True: print("ready to chmod cfssl ...") cmdRemote = "chmod +x /usr/local/bin/cfssl*" robj.sudo(cmdRemote) #close selinux print("ready to close selinux ...") robj.sudo("setenforce 0") robj.sudo( "sed -i \"s/^SELINUX=enforcing/SELINUX=disabled/g\" /etc/sysconfig/selinux" ) robj.sudo( "sed -i \"s/^SELINUX=enforcing/SELINUX=disabled/g\" /etc/selinux/config" ) robj.sudo( "sed -i \"s/^SELINUX=permissive/SELINUX=disabled/g\" /etc/sysconfig/selinux" ) robj.sudo( "sed -i \"s/^SELINUX=permissive/SELINUX=disabled/g\" /etc/selinux/config" ) robj.sudo("getenforce") #close swap print("ready to close swap ...") robj.sudo("swapoff -a") #comment swap line robj.sudo("sed -i 's/.*swap.*/#&/' /etc/fstab") robj.sudo("cat /etc/fstab") #config kernel print("ready to config kernel ...") robj.upload(configPath + "/sysctl/k8s.conf", remoteSysctlPath + "/") robj.sudo("modprobe br_netfilter") #reload system config print("reload system config ...") robj.sudo("sysctl -p " + remoteSysctlPath + "/k8s.conf") #close firewall print("ready to stop firewall ...") robj.sudo("systemctl stop firewalld") robj.sudo("systemctl disable firewalld") robj.sudo("systemctl status firewalld") #remove old version of docker robj.sudo("systemctl stop docker") robj.sudo( "yum remove -y docker docker-client docker-client-latest docker-common" ) robj.sudo( "yum remove -y docker-latest docker-latest-logrotate docker-logrotate docker-engine" ) robj.sudo("systemctl status docker") # yum priorities plugin print("ready to install yum priorities plugin ...") robj.sudo("yum install -y yum-plugin-priorities") #epel-release print("ready to install epel repository ...") robj.sudo("yum install -y epel-release") robj.sudo("yum repolist") #ntpdate if self.mode == "dev": print("ready to install ntpdate ...") robj.sudo("yum -y install ntpdate") robj.sudo("systemctl enable ntpdate") robj.sudo("systemctl restart ntpdate") #set prioritiy for epel if robj.checkpath("/etc/yum.repos.d/epel.repo") == True: cmdRemote = "grep -c \"priority=\" /etc/yum.repos.d/epel.repo" robj.sudo(cmdRemote) priorityExists = robj.getResult().stdout.rstrip() if priorityExists == '0': # insert priority field under the 'enabled=' line cmdRemote = "sed -i '/enabled=/a\priority=1' /etc/yum.repos.d/epel.repo" robj.sudo(cmdRemote) # update yum cmdRemote = "yum makecache" robj.sudo(cmdRemote) # cmdRemote = "yum update -y" # robj.sudo(cmdRemote) cmdRemote = "yum install -y deltarpm" robj.sudo(cmdRemote) # cmdRemote = "yum update -y" # robj.sudo(cmdRemote) #wget tool print("ready to install wget ...") cmdRemote = "yum install -y wget" robj.sudo(cmdRemote) #net tool print("ready to install net-tools ...") cmdRemote = "yum install -y net-tools" robj.sudo(cmdRemote) #install docker component if robj.checkpath("/usr/bin/docker") == False: #Set up repository print("ready to install docker-ce ...") #install required packages. yum-utils provides the yum-config-manager utility, # and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver cmdRemote = "yum install -y yum-utils device-mapper-persistent-data lvm2" robj.sudo(cmdRemote) #set up the stable repository # sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo cmdRemote = "yum-config-manager --add-repo" cmdRemote = cmdRemote + " http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo" robj.sudo(cmdRemote) # update yum cmdRemote = "yum makecache" robj.sudo(cmdRemote) # cmdRemote = "yum update -y" # robj.sudo(cmdRemote) #List and sort the versions available in your repo. This example sorts results by version number, highest to lowest, and is truncated print("list docker versions ...") cmdRemote = "yum list docker-ce --showduplicates | sort -r" robj.sudo(cmdRemote) robj.sudo("sleep 5") # if not confirm("Ready to install docker-ce-18.06.3.ce, Continue[Y/N]?"): # return True #Install a specific version by its fully qualified package name print("ready to install docker-ce-18.06.3.ce ...") cmdRemote = "yum install -y docker-ce-18.06.3.ce docker-ce-cli-18.06.3.ce containerd.io" robj.sudo(cmdRemote) #set docker to start when the system is booted cmdRemote = "systemctl enable docker" robj.sudo(cmdRemote) #set subnet for docker # upload docker service file # sedRegex = "sed \"s/{dockerSubnetIpRange}/%s/g\"" % self.dockerSubnetIpRange # file_tmp = tmpPath + "/docker.service" # file_config = dockerConfigPath + "/docker.service" # cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) # robj.local(cmd_local) # 上传文件到远程主机 # robj.upload(file_tmp, remoteSystemdPath+"/", True) #reload daemon cmdRemote = "systemctl daemon-reload" robj.sudo(cmdRemote) #start docker cmdRemote = "systemctl restart docker" robj.sudo(cmdRemote) # check docker status print("check docker-ce status ...") cmdRemote = "systemctl status docker" robj.sudo(cmdRemote) # check subnet of docker print("check subnet of docker0 ...") cmdRemote = "ip addr show docker0" robj.sudo(cmdRemote) #install socat cmdRemote = "yum install -y socat" robj.sudo(cmdRemote) #prepare deployment directory cmdRemote = "mkdir -p /opt/kubernetes/{bin,cfg,ssl,log}" robj.sudo(cmdRemote) #set environment variable robj.upload(configPath + "/sysctl/k8s.sh", "/etc/profile.d/", True) # cmdRemote = "echo 'export PATH=/opt/kubernetes/bin:$PATH' > /etc/profile.d/k8s.sh" # robj.sudo(cmdRemote) robj.sudo("chmod 755 /etc/profile.d/k8s.sh") cmdRemote = "source /etc/profile.d/k8s.sh" robj.run(cmdRemote)
def installEtcdNodeNormal(self, nodename): node_env = self.getEnv(nodename) # 初始化远程工具对象 robj = Remote(node_env) remoteSslPath = self.getRemotePath('remoteSslPath') remoteBinPath = self.getRemotePath('remoteBinPath') remoteCfgPath = self.getRemotePath('remoteCfgPath') remoteSystemdPath = self.getRemotePath('remoteSystemdPath') remoteEtcdDataPath = self.getRemotePath('remoteEtcdDataPath') etcdBinPath = self.getLocalPath('etcdBinPath') etcdConfigPath = self.getLocalPath('etcdConfigPath') tmpPath = self.getLocalPath('tmpPath') #upload ca files robj.upload(tmpPath + "/ca.csr", remoteSslPath + "/") robj.upload(tmpPath + "/ca.pem", remoteSslPath + "/") robj.upload(tmpPath + "/ca-key.pem", remoteSslPath + "/") #upload etcd ca files robj.upload(tmpPath + "/etcd.csr", remoteSslPath + "/") robj.upload(tmpPath + "/etcd.pem", remoteSslPath + "/") robj.upload(tmpPath + "/etcd-key.pem", remoteSslPath + "/") #upload binary file for etcd processes if robj.checkpath((remoteBinPath + "/etcd")) == False: robj.upload(etcdBinPath + "/etcd", remoteBinPath + "/") if robj.checkpath((remoteBinPath + "/etcdctl")) == False: robj.upload(etcdBinPath + "/etcdctl", remoteBinPath + "/") #upload etcd config file file_config = etcdConfigPath + "/etcd.conf" file_tmp = tmpPath + "/etcd.conf" sedRegex = "sed \"s/{etcdNodeName}/%s/g\"" % ("etcd-" + nodename) sedRegex = sedRegex + " | sed \"s/{etcdNodeIp}/%s/g\"" % node_env.host sedRegex = sedRegex + " | sed \"s/{etcdNodeName1}/%s/g\"" % ("etcd-" + "master1") node1_env = self.getEnv("master1") etcdNodeIp1 = node1_env.host sedRegex = sedRegex + " | sed \"s/{etcdNodeIp1}/%s/g\"" % etcdNodeIp1 sedRegex = sedRegex + " | sed \"s/{etcdNodeName2}/%s/g\"" % ("etcd-" + "master2") node2_env = self.getEnv("master2") etcdNodeIp2 = node2_env.host sedRegex = sedRegex + " | sed \"s/{etcdNodeIp2}/%s/g\"" % etcdNodeIp2 sedRegex = sedRegex + " | sed \"s/{etcdNodeName3}/%s/g\"" % ("etcd-" + "master3") node3_env = self.getEnv("master3") etcdNodeIp3 = node3_env.host sedRegex = sedRegex + " | sed \"s/{etcdNodeIp3}/%s/g\"" % etcdNodeIp3 cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) robj.local(cmd_local) robj.upload(file_tmp, remoteCfgPath + "/") robj.upload(etcdConfigPath + "/etcd.service", remoteSystemdPath + "/") if (remoteEtcdDataPath != "" and remoteEtcdDataPath != "/" and robj.checkpath(remoteEtcdDataPath)): robj.rmfile(remoteEtcdDataPath + "/*") print("cache files of etcd is removed ...") robj.sudo("ls -l " + remoteEtcdDataPath + "/") else: robj.mkdir(remoteEtcdDataPath) #systemd for etcd process robj.sudo("systemctl daemon-reload") robj.sudo("systemctl enable etcd.service") cmdRemote = "nohup systemctl restart etcd.service &> /dev/null &" robj.sudo(cmdRemote)