def create(config, plandir, cluster, overrides): data = {'kubetype': 'generic'} data.update(overrides) data['cluster'] = overrides['cluster'] if 'cluster' in overrides else cluster data['kube'] = data['cluster'] masters = data.get('masters', 1) if masters == 0: pprint("Invalid number of masters", color='red') os._exit(1) version = data.get('version') if version is not None and not version.startswith('1.'): pprint("Invalid version %s" % version, color='red') os._exit(1) data['basedir'] = '/workdir' if os.path.exists("/i_am_a_container") else '.' cluster = data.get('cluster') image = data.get('image', 'centos7') domain = data.get('domain', 'karmalabs.com') clusterdir = pwd_path("clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): pprint("Please remove existing directory %s first..." % clusterdir, color='red') sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) k = config.k result = config.plan(cluster, inputfile='%s/masters.yml' % plandir, overrides=data, wait=True) if result['result'] != "success": os._exit(1) source, destination = "/root/join.sh", "%s/join.sh" % clusterdir scpcmd = k.scp(firstmaster, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) source, destination = "/etc/kubernetes/admin.conf", "%s/auth/kubeconfig" % clusterdir scpcmd = k.scp(firstmaster, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers", color='blue') if 'name' in data: del data['name'] config.plan(cluster, inputfile='%s/workers.yml' % plandir, overrides=data) pprint("Kubernetes cluster %s deployed!!!" % cluster) masters = data.get('masters', 1) master_node = "%s-master" % cluster if masters > 1 else "%s-master-0" % cluster master_ip = k.info(master_node)['ip'] info("Create the following /etc/hosts entry if needed") info("%s %s %s.%s" % (master_ip, master_node, master_node, domain)) info("Use The following command to interact with this cluster") info("export KUBECONFIG=clusters/%s/auth/kubeconfig" % cluster) info("export PATH=$PWD:$PATH") prefile = 'pre_ubuntu.sh' if image in UBUNTUS else 'pre_el.sh' predata = config.process_inputfile(cluster, "%s/%s" % (plandir, prefile), overrides=data) with open("%s/pre.sh" % clusterdir, 'w') as f: f.write(predata)
def create(config, plandir, cluster, overrides): platform = config.type data = {'kubetype': 'generic', 'xip': False, 'domain': 'karmalabs.com'} data.update(overrides) data['cluster'] = overrides['cluster'] if 'cluster' in overrides else cluster data['kube'] = data['cluster'] masters = data.get('masters', 1) if masters == 0: pprint("Invalid number of masters", color='red') os._exit(1) network = data.get('network', 'default') xip = data['xip'] api_ip = data.get('api_ip') if masters > 1: if platform in cloudplatforms: domain = data.get('domain', 'karmalabs.com') api_ip = "%s-master.%s" % (cluster, domain) elif api_ip is None: if network == 'default' and platform == 'kvm': pprint("Using 192.168.122.253 as api_ip", color='yellow') data['api_ip'] = "192.168.122.253" api_ip = "192.168.122.253" else: pprint("You need to define api_ip in your parameters file", color='red') os._exit(1) if xip and platform not in cloudplatforms: data['domain'] = "%s.xip.io" % api_ip version = data.get('version') if version is not None and not version.startswith('1.'): pprint("Invalid version %s" % version, color='red') os._exit(1) data['basedir'] = '/workdir' if os.path.exists("/i_am_a_container") else '.' cluster = data.get('cluster') image = data.get('image', 'centos7') data['ubuntu'] = True if image in UBUNTUS or 'ubuntu' in image.lower() else False clusterdir = pwd_path("clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): pprint("Please remove existing directory %s first..." % clusterdir, color='red') sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) k = config.k result = config.plan(cluster, inputfile='%s/masters.yml' % plandir, overrides=data, wait=True) if result['result'] != "success": os._exit(1) source, destination = "/root/join.sh", "%s/join.sh" % clusterdir firstmasterip = k.info(firstmaster)['ip'] scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) source, destination = "/etc/kubernetes/admin.conf", "%s/auth/kubeconfig" % clusterdir scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers", color='blue') if 'name' in data: del data['name'] config.plan(cluster, inputfile='%s/workers.yml' % plandir, overrides=data) pprint("Kubernetes cluster %s deployed!!!" % cluster) masters = data.get('masters', 1) info("export KUBECONFIG=clusters/%s/auth/kubeconfig" % cluster) info("export PATH=$PWD:$PATH") prefile = 'pre_ubuntu.sh' if data['ubuntu'] else 'pre_el.sh' predata = config.process_inputfile(cluster, "%s/%s" % (plandir, prefile), overrides=data) with open("%s/pre.sh" % clusterdir, 'w') as f: f.write(predata) os.environ['KUBECONFIG'] = "%s/%s/auth/kubeconfig" % (os.getcwd(), clusterdir) apps = data.get('apps', []) if apps: for app in apps: appdir = "%s/apps/%s" % (plandir, app) if not os.path.exists(appdir): pprint("Skipping unsupported app %s" % app, color='yellow') else: pprint("Adding app %s" % app, color='blue') if '%s_version' % app not in overrides: data['%s_version' % app] = 'latest' kube_create_app(config, appdir, overrides=data)
def create(config, plandir, cluster, overrides): platform = config.type data = {'kubetype': 'k3s'} data.update(overrides) data['cluster'] = overrides[ 'cluster'] if 'cluster' in overrides else cluster data['kube'] = data['cluster'] masters = data.get('masters', 1) network = data.get('network', 'default') api_ip = data.get('api_ip') if masters > 1: if platform in cloudplatforms: domain = data.get('domain', 'karmalabs.com') api_ip = "%s-master.%s" % (cluster, domain) elif api_ip is None: if network == 'default' and platform == 'kvm': pprint("Using 192.168.122.253 as api_ip", color='yellow') data['api_ip'] = "192.168.122.253" else: pprint("You need to define api_ip in your parameters file", color='red') os._exit(1) version = data.get('version') if version is not None and not version.startswith('1.'): pprint("Invalid version %s" % version, color='red') os._exit(1) data['basedir'] = '/workdir' if os.path.exists( "/i_am_a_container") else '.' cluster = data.get('cluster') clusterdir = pwd_path("clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): pprint("Please remove existing directory %s first..." % clusterdir, color='red') sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) if masters > 1: datastore_endpoint = data.get('datastore_endpoint') if datastore_endpoint is None: result = config.plan(cluster, inputfile='%s/datastore.yml' % plandir, overrides=data, wait=True) if result['result'] != "success": os._exit(1) datastore_type = data['datastore_type'] datastore_user = data['datastore_user'] datastore_password = data['datastore_password'] datastore_ip = config.k.info("%s-datastore" % cluster).get('ip') datastore_name = data['datastore_name'] if datastore_type == 'mysql': datastore_name = "tcp(%s)" % datastore_name datastore_endpoint = "%s://%s:%s@%s/%s" % ( datastore_type, datastore_user, datastore_password, datastore_ip, datastore_name) data['datastore_endpoint'] = datastore_endpoint k = config.k result = config.plan(cluster, inputfile='%s/masters.yml' % plandir, overrides=data, wait=True) if result['result'] != "success": os._exit(1) source, destination = "/root/join.sh", "%s/join.sh" % clusterdir firstmasterip = k.info(firstmaster)['ip'] scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) source, destination = "/root/kubeconfig", "%s/auth/kubeconfig" % clusterdir scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True) os.system(scpcmd) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers", color='blue') if 'name' in data: del data['name'] config.plan(cluster, inputfile='%s/workers.yml' % plandir, overrides=data) pprint("K3s cluster %s deployed!!!" % cluster) info("export KUBECONFIG=clusters/%s/auth/kubeconfig" % cluster) info("export PATH=$PWD:$PATH")
def create(config, plandir, cluster, overrides): platform = config.type data = {'kubetype': 'k3s', 'sdn': 'flannel'} data.update(overrides) data['cluster'] = overrides.get( 'cluster', cluster if cluster is not None else 'testk') plan = cluster if cluster is not None else data['cluster'] data['kube'] = data['cluster'] masters = data.get('masters', 1) network = data.get('network', 'default') sdn = None if 'sdn' in overrides and overrides[ 'sdn'] is None else data.get('sdn') token = data.get('token', 'supersecret') api_ip = data.get('api_ip') if masters > 1: if platform in cloudplatforms: domain = data.get('domain', 'karmalabs.com') api_ip = "%s-master.%s" % (cluster, domain) elif api_ip is None: if network == 'default' and platform == 'kvm': warning("Using 192.168.122.253 as api_ip") data['api_ip'] = "192.168.122.253" api_ip = "192.168.122.253" elif platform == 'kubevirt': selector = {'kcli/plan': plan, 'kcli/role': 'master'} api_ip = config.k.create_service("%s-api" % cluster, config.k.namespace, selector, _type="LoadBalancer", ports=[6443]) if api_ip is None: sys.exit(1) else: pprint("Using api_ip %s" % api_ip) data['api_ip'] = api_ip else: error("You need to define api_ip in your parameters file") sys.exit(1) if data.get('virtual_router_id') is None: data['virtual_router_id'] = hash(data['cluster']) % 254 + 1 pprint("Using keepalived virtual_router_id %s" % data['virtual_router_id']) data['basedir'] = '/workdir' if container_mode() else '.' install_k3s_args = [] for arg in data: if arg.startswith('install_k3s'): install_k3s_args.append("%s=%s" % (arg.upper(), data[arg])) cluster = data.get('cluster') clusterdir = os.path.expanduser("~/.kcli/clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): error("Please remove existing directory %s first..." % clusterdir) sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) with open("%s/kcli_parameters.yml" % clusterdir, 'w') as p: installparam = overrides.copy() installparam['api_ip'] = api_ip installparam['plan'] = plan installparam['kubetype'] = 'k3s' yaml.safe_dump(installparam, p, default_flow_style=False, encoding='utf-8', allow_unicode=True) if os.path.exists("manifests") and os.path.isdir("manifests"): data['files'] = [{ "path": "/root/manifests", "currentdir": True, "origin": "manifests" }] k = config.k bootstrap_overrides = data.copy() bootstrap_install_k3s_args = install_k3s_args.copy() if sdn is None or sdn != 'flannel': bootstrap_install_k3s_args.append( "INSTALL_K3S_EXEC='--flannel-backend=none'") bootstrap_install_k3s_args = ' '.join(bootstrap_install_k3s_args) bootstrap_overrides['install_k3s_args'] = bootstrap_install_k3s_args result = config.plan(plan, inputfile='%s/bootstrap.yml' % plandir, overrides=bootstrap_overrides) if result['result'] != "success": sys.exit(1) nodes_overrides = data.copy() nodes_install_k3s_args = install_k3s_args.copy() if sdn is None or sdn != 'flannel': nodes_install_k3s_args.append( "INSTALL_K3S_EXEC='--flannel-backend=none'") nodes_install_k3s_args = ' '.join(nodes_install_k3s_args) nodes_overrides['install_k3s_args'] = nodes_install_k3s_args if masters > 1: pprint("Deploying extra masters") threaded = data.get('threaded', False) or data.get( 'masters_threaded', False) config.plan(plan, inputfile='%s/masters.yml' % plandir, overrides=nodes_overrides, threaded=threaded) with open("%s/join.sh" % clusterdir, 'w') as f: if api_ip is None: api_ip = k.info(firstmaster)['ip'] joincmd = f"curl -sfL https://get.k3s.io | K3S_URL=https://{api_ip}:6443 K3S_TOKEN={token} " joincmd += ' '.join(install_k3s_args) extra_args = data['extra_worker_args'] if data.get( 'extra_worker_args', []) else data.get('extra_args', []) extra_args = ' '.join(extra_args) f.write("%s sh -s - agent %s \n" % (joincmd, extra_args)) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers") if 'name' in data: del data['name'] os.chdir(os.path.expanduser("~/.kcli")) threaded = data.get('threaded', False) or data.get( 'workers_threaded', False) config.plan(plan, inputfile='%s/workers.yml' % plandir, overrides=data, threaded=threaded) success("K3s cluster %s deployed!!!" % cluster) info2("export KUBECONFIG=$HOME/.kcli/clusters/%s/auth/kubeconfig" % cluster) info2("export PATH=$PWD:$PATH")
def create(config, plandir, cluster, overrides): platform = config.type k = config.k data = {'kubetype': 'generic', 'xip': False, 'domain': 'karmalabs.com'} data.update(overrides) if 'keys' not in overrides and not os.path.exists(os.path.expanduser("~/.ssh/id_rsa.pub"))\ and not os.path.exists(os.path.expanduser("~/.ssh/id_dsa.pub"))\ and not os.path.exists(os.path.expanduser("~/.kcli/id_rsa.pub"))\ and not os.path.exists(os.path.expanduser("~/.kcli/id_dsa.pub")): error( "No usable public key found, which is required for the deployment") os._exit(1) data['cluster'] = overrides.get( 'cluster', cluster if cluster is not None else 'testk') plan = cluster if cluster is not None else data['cluster'] data['kube'] = data['cluster'] masters = data.get('masters', 1) if masters == 0: error("Invalid number of masters") os._exit(1) network = data.get('network', 'default') xip = data['xip'] api_ip = data.get('api_ip') if platform in cloudplatforms: domain = data.get('domain', 'karmalabs.com') api_ip = "%s-master.%s" % (cluster, domain) elif api_ip is None: if network == 'default' and platform == 'kvm': warning("Using 192.168.122.253 as api_ip") data['api_ip'] = "192.168.122.253" api_ip = "192.168.122.253" elif platform == 'kubevirt': selector = {'kcli/plan': plan, 'kcli/role': 'master'} api_ip = config.k.create_service("%s-api" % cluster, config.k.namespace, selector, _type="LoadBalancer", ports=[6443]) if api_ip is None: os._exit(1) else: pprint("Using api_ip %s" % api_ip) data['api_ip'] = api_ip else: error("You need to define api_ip in your parameters file") os._exit(1) if xip and platform not in cloudplatforms: data['domain'] = "%s.xip.io" % api_ip if data.get('virtual_router_id') is None: data['virtual_router_id'] = hash(data['cluster']) % 254 + 1 pprint("Using keepalived virtual_router_id %s" % data['virtual_router_id']) version = data.get('version') if version is not None and not str(version).startswith('1.'): error("Invalid version %s" % version) os._exit(1) data['basedir'] = '/workdir' if os.path.exists( "/i_am_a_container") else '.' cluster = data.get('cluster') image = data.get('image', 'centos7') data['ubuntu'] = True if 'ubuntu' in image.lower() or [ entry for entry in UBUNTUS if entry in image ] else False clusterdir = os.path.expanduser("~/.kcli/clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): error("Please remove existing directory %s first..." % clusterdir) sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) with open("%s/kcli_parameters.yml" % clusterdir, 'w') as p: installparam = overrides.copy() installparam['api_ip'] = api_ip installparam['virtual_router_id'] = data['virtual_router_id'] installparam['plan'] = plan yaml.safe_dump(installparam, p, default_flow_style=False, encoding='utf-8', allow_unicode=True) result = config.plan(plan, inputfile='%s/masters.yml' % plandir, overrides=data) if result['result'] != "success": os._exit(1) source, destination = "/root/join.sh", "%s/join.sh" % clusterdir firstmasterip, firstmastervmport = _ssh_credentials(k, firstmaster)[1:] scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True, vmport=firstmastervmport) os.system(scpcmd) source, destination = "/etc/kubernetes/admin.conf", "%s/auth/kubeconfig" % clusterdir scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True, vmport=firstmastervmport) os.system(scpcmd) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers") if 'name' in data: del data['name'] os.chdir(os.path.expanduser("~/.kcli")) config.plan(plan, inputfile='%s/workers.yml' % plandir, overrides=data) success("Kubernetes cluster %s deployed!!!" % cluster) masters = data.get('masters', 1) info2("export KUBECONFIG=$HOME/.kcli/clusters/%s/auth/kubeconfig" % cluster) info2("export PATH=$PWD:$PATH") prefile = 'pre_ubuntu.sh' if data['ubuntu'] else 'pre_el.sh' predata = config.process_inputfile(plan, "%s/%s" % (plandir, prefile), overrides=data) with open("%s/pre.sh" % clusterdir, 'w') as f: f.write(predata) os.environ['KUBECONFIG'] = "%s/auth/kubeconfig" % clusterdir apps = data.get('apps', []) if apps: os.environ["PATH"] += ":%s" % os.getcwd() for app in apps: appdir = "%s/apps/%s" % (plandir, app) if not os.path.exists(appdir): warning("Skipping unsupported app %s" % app) else: pprint("Adding app %s" % app) if '%s_version' % app not in overrides: data['%s_version' % app] = 'latest' kube_create_app(config, appdir, overrides=data)
def create(config, plandir, cluster, overrides): platform = config.type data = {'kubetype': 'k3s'} data.update(overrides) data['cluster'] = overrides.get('cluster', cluster if cluster is not None else 'testk') plan = cluster if cluster is not None else data['cluster'] data['kube'] = data['cluster'] masters = data.get('masters', 1) network = data.get('network', 'default') token = data.get('token', 'supersecret') api_ip = data.get('api_ip') if masters > 1: if platform in cloudplatforms: domain = data.get('domain', 'karmalabs.com') api_ip = "%s-master.%s" % (cluster, domain) elif api_ip is None: if network == 'default' and platform == 'kvm': pprint("Using 192.168.122.253 as api_ip", color='yellow') data['api_ip'] = "192.168.122.253" elif platform == 'kubevirt': selector = {'kcli/plan': plan, 'kcli/role': 'master'} api_ip = config.k.create_service("%s-api" % cluster, config.k.namespace, selector, _type="LoadBalancer", port=6443) if api_ip is None: os._exit(1) else: pprint("Using api_ip %s" % api_ip, color='blue') data['api_ip'] = api_ip else: pprint("You need to define api_ip in your parameters file", color='red') os._exit(1) version = data.get('version') if version not in ['stable', 'latest', 'testing']: pprint("Invalid version %s" % version, color='red') os._exit(1) data['basedir'] = '/workdir' if os.path.exists("/i_am_a_container") else '.' cluster = data.get('cluster') clusterdir = os.path.expanduser("~/.kcli/clusters/%s" % cluster) firstmaster = "%s-master-0" % cluster if os.path.exists(clusterdir): pprint("Please remove existing directory %s first..." % clusterdir, color='red') sys.exit(1) if find_executable('kubectl') is None: get_kubectl() if not os.path.exists(clusterdir): os.makedirs(clusterdir) os.mkdir("%s/auth" % clusterdir) with open("%s/kcli_parameters.yml" % clusterdir, 'w') as p: installparam = overrides.copy() installparam['plan'] = plan yaml.safe_dump(installparam, p, default_flow_style=False, encoding='utf-8', allow_unicode=True) k = config.k result = config.plan(cluster, inputfile='%s/bootstrap.yml' % plandir, overrides=data) if result['result'] != "success": os._exit(1) if masters > 1: pprint("Deploying extra masters", color='blue') if 'name' in data: del data['name'] config.plan(cluster, inputfile='%s/masters.yml' % plandir, overrides=data) firstmasterip, firstmastervmport = _ssh_credentials(k, firstmaster)[1:] with open("%s/join.sh" % clusterdir, 'w') as f: if api_ip is None: api_ip = k.info(firstmaster)['ip'] f.write("curl -sfL https://get.k3s.io | K3S_URL=https://%s:6443 K3S_TOKEN=%s sh -\n" % (api_ip, token)) source, destination = "/root/kubeconfig", "%s/auth/kubeconfig" % clusterdir scpcmd = scp(firstmaster, ip=firstmasterip, user='******', source=source, destination=destination, tunnel=config.tunnel, tunnelhost=config.tunnelhost, tunnelport=config.tunnelport, tunneluser=config.tunneluser, download=True, insecure=True, vmport=firstmastervmport) os.system(scpcmd) workers = data.get('workers', 0) if workers > 0: pprint("Deploying workers", color='blue') if 'name' in data: del data['name'] os.chdir(os.path.expanduser("~/.kcli")) config.plan(cluster, inputfile='%s/workers.yml' % plandir, overrides=data) pprint("K3s cluster %s deployed!!!" % cluster) info("export KUBECONFIG=$HOME/.kcli/clusters/%s/auth/kubeconfig" % cluster) info("export PATH=$PWD:$PATH")