def create_rc_mysql(nombre_mysql, image_mysql, nombre_odoo, pass_mysql, \ nom_dom_ps, dic_argv, api): rc_yaml = load_file_yaml("kuberntetes_yaml/ps-mysqlmysql-rc-vol.yaml") rc_yaml["metadata"]["name"] = nombre_mysql rc_yaml["metadata"]["labels"]["app"] = nombre_mysql rc_yaml["metadata"]["labels"]["provider"] = nombre_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]["image"] = image_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]["name"] = nombre_mysql rc_yaml["spec"]["template"]["metadata"]["labels"]["app"] = nombre_mysql rc_yaml["spec"]["template"]["metadata"]["labels"]["name"] = nombre_mysql rc_yaml["spec"]["template"]["metadata"]["labels"][ "provider"] = nombre_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]["env"][0]\ ["value"] = pass_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]["env"][1]\ ["value"] = dic_argv["PS_THEME"] rc_yaml = add_limit_resources(rc_yaml, "1", dic_argv["limit_cpu_db"], \ dic_argv["limit_mem_db"]) rc_yaml["spec"]["template"]["spec"]["containers"][0]["volumeMounts"][0]\ ["subPath"] = "%s/mysql" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["volumes"][0]\ ["persistentVolumeClaim"]["claimName"] = nombre_odoo if not dic_argv["debug"]: print "Creando rc con %s" % (nombre_mysql) pykube.ReplicationController(api, rc_yaml).create() else: pass
def terminate(self, name): """Terminate a service. It will terminate Service, then ReplicationController and Pods have the same labels.""" del_obj = { 'apiVersion': 'v1', 'kind': '', 'metadata': { 'name': name, 'namespace': get_conf().kube_namespace } } try: del_obj['kind'] = 'Service' pykube.Service(self.api, del_obj).delete() del_obj['kind'] = 'ReplicationController' pykube.ReplicationController(self.api, del_obj).delete() del_obj['kind'] = 'Pod' pod_selector = ZOE_LABELS pod_selector['service_name'] = name pods = pykube.Pod.objects( self.api).filter(namespace=get_conf().kube_namespace, selector=pod_selector).iterator() for pod in pods: del_obj['metadata']['name'] = str(pod) pykube.Pod(self.api, del_obj).delete() log.info('Service deleted on Kubernetes cluster') except Exception as ex: log.error(ex)
def k8s_update(self, args): api = pykube.HTTPClient( pykube.KubeConfig.from_file("/home/micros/.kube/config")) name = args.name for service in args.services: obj = self.k8s_parse_obj(name, service) pykube.ReplicationController(api, obj).update()
def deleteReplicationController(NamespaceName, appname, deployId): rcname = appname + "-rc" obj = { "kind": "ReplicationController", "apiVersion": "v1", "metadata": { "name": rcname, "namespace": NamespaceName, "labels": { "deployId": deployId } } } pykube.ReplicationController(api, obj).delete()
def spawn_replication_controller(self, service_instance: ServiceInstance): """Create and start a new replication controller.""" config = KubernetesReplicationControllerConf() config.set_name(service_instance.name) config.set_labels(ZOE_LABELS) config.set_labels({'service_name': service_instance.name}) config.set_replicas(1) config.set_spec_selector(ZOE_LABELS) config.set_spec_selector({'service_name': service_instance.name}) config.set_temp_meta_labels(ZOE_LABELS) config.set_temp_meta_labels({'service_name': service_instance.name}) config.set_spec_container_image(service_instance.image_name) config.set_spec_container_name(service_instance.name) if len(service_instance.environment) > 0: envs = {e[0]: str(e[1]) for e in service_instance.environment} config.set_spec_container_env(envs) if len(service_instance.ports) > 0: config.set_spec_container_ports(service_instance.ports) if service_instance.memory_limit is not None: config.set_spec_container_mem_limit( service_instance.memory_limit.min) if service_instance.core_limit is not None: config.set_spec_container_core_limit( service_instance.core_limit.min) if len(service_instance.volumes) > 0: config.set_spec_container_volumes(service_instance.volumes, service_instance.name) if service_instance.command is not None: config.set_spec_container_command(service_instance.command) info = {} try: pykube.ReplicationController(self.api, config.get_json()).create() log.info('Created ReplicationController on Kubernetes cluster') info = self.inspect_replication_controller(service_instance.name) except Exception as ex: log.exception(ex) return info
def create_rc_ps(key_api_ps, nombre_ps, image_ps, nombre_odoo, nombre_mysql, \ pass_mysql, nom_dom_ps, dic_argv, api): rc_yaml = load_file_yaml("kuberntetes_yaml/ps-mysqlps-rc-vol.yaml") rc_yaml["metadata"]["name"] = nombre_ps rc_yaml["metadata"]["labels"]["app"] = nombre_ps rc_yaml["metadata"]["labels"]["provider"] = nombre_ps rc_yaml["spec"]["template"]["spec"]["containers"][0]["image"] = image_ps rc_yaml["spec"]["template"]["spec"]["containers"][0]["name"] = nombre_ps rc_yaml["spec"]["template"]["metadata"]["labels"]["app"] = nombre_ps rc_yaml["spec"]["template"]["metadata"]["labels"]["provider"] = nombre_ps rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][0]["value"] = nom_dom_ps rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][1]["value"] = nombre_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][3]["value"] = pass_mysql rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][4]["value"] = "http://" + nombre_odoo rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][7]["value"] = dic_argv["ODOO_USER"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][8]["value"] = dic_argv["ODOO_PASS"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][12]["value"] = key_api_ps rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][13]["value"] = dic_argv["ADMIN_MAIL"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][14]["value"] = dic_argv["ADMIN_PASSWD"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][15]["value"] = dic_argv["PS_NOM_USER"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][16]["value"] = dic_argv["PS_APELLIDO_USER"] rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][17]["value"] = dic_argv["PS_THEME"] rc_yaml = add_limit_resources(rc_yaml, dic_argv["ps_replicas"],\ dic_argv["limit_cpu"], dic_argv["limit_mem"]) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["volumeMounts"][0]["subPath"] = "%s/ps/html" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["volumes"][0]\ ["persistentVolumeClaim"]["claimName"] = nombre_odoo if not dic_argv["debug"]: print "Creando rc con %s" % (nombre_ps) pykube.ReplicationController(api, rc_yaml).create() else: pass
def _create_game_rc(self, id, environment_variables): environment_variables['SOCKETIO_RESOURCE'] = "game/%s/socket.io" % id environment_variables['GAME_ID'] = id environment_variables['GAME_URL'] = "http://game-%s" % id environment_variables['PYKUBE_KUBERNETES_SERVICE_HOST'] = 'kubernetes' environment_variables['IMAGE_SUFFIX'] = os.environ.get( 'IMAGE_SUFFIX', 'latest') rc = pykube.ReplicationController( self._api, { 'kind': 'ReplicationController', 'apiVersion': 'v1', 'metadata': { 'name': "game-%s" % id, 'namespace': 'default', 'labels': { 'app': 'aimmo-game', 'game_id': id, }, }, 'spec': { 'replicas': 1, 'selector': { 'app': 'aimmo-game', 'game_id': id, }, 'template': { 'metadata': { 'labels': { 'app': 'aimmo-game', 'game_id': id, }, }, 'spec': { 'containers': [ { 'env': [{ 'name': env_name, 'value': env_value, } for env_name, env_value in environment_variables.items()], 'image': 'ocadotechnology/aimmo-game:%s' % os.environ.get('IMAGE_SUFFIX', 'latest'), 'ports': [ { 'containerPort': 5000, }, ], 'name': 'aimmo-game', 'resources': { 'limits': { 'cpu': '1000m', 'memory': '128Mi', }, 'requests': { 'cpu': '100m', 'memory': '64Mi', }, }, }, ], }, }, }, }, ) rc.create()
def createReplicationController(action, NamespaceName, appname, replicas, hostPath, containerPath, containerImage, cpu, mem, containerPort, deployId, envname, envalue): # cpu = str(cpu) + 'm' mem = str(mem) + 'Gi' withLB_1 = "withLB" + deployId volumesName = appname + "volumes" rcname = appname + "-rc" obj = { "kind": "ReplicationController", "apiVersion": "v1", "metadata": { "name": rcname, "namespace": NamespaceName, "labels": { "deployId": deployId } }, "spec": { "replicas": replicas, "selector": { "deployId": deployId }, "template": { "metadata": { "deletionGracePeriodSeconds": 0, "labels": { "deployId": deployId, "version": "1", withLB_1: "TRUE" }, "annotations": { "deployName": appname } }, "spec": { "volumes": [{ "name": volumesName, "hostPath": { "path": hostPath } }], "containers": [{ "name": appname, "image": containerImage, "env": [{ "name": "DOMEOS_SERVER_ADDR", "value": "10.23.74.210:8080" }, { "name": "CLUSTER_NAME", "value": "domeos" }, { "name": "NETWORK_MODE", "value": "DEFAULT" }, { "name": "MY_POD_NAME", "valueFrom": { "fieldRef": { "apiVersion": "v1", "fieldPath": "metadata.name" } } }, { "name": "MY_POD_NAMESPACE", "valueFrom": { "fieldRef": { "apiVersion": "v1", "fieldPath": "metadata.namespace" } } }, { "name": "MY_POD_IP", "valueFrom": { "fieldRef": { "apiVersion": "v1", "fieldPath": "status.podIP" } } }, { "name": "envname", "value": "envalue" }], "resources": { "limits": { "cpu": cpu, "memory": mem } }, "volumeMounts": [{ "name": volumesName, "mountPath": containerPath }], "livenessProbe": { "tcpSocket": { "port": containerPort }, "initialDelaySeconds": 1, "timeoutSeconds": 3, "periodSeconds": 10, "successThreshold": 1, "failureThreshold": 3 }, "terminationMessagePath": "/dev/termination-log", "imagePullPolicy": "Always" }], "restartPolicy": "Always", "terminationGracePeriodSeconds": 30, "dnsPolicy": "ClusterFirst", "nodeSelector": { "TESTENV": "HOSTENVTYPE" }, "securityContext": {} } } } } if envname != "null" and envalue != "null": obj["spec"]["template"]["spec"]["containers"][0]["env"][ len(obj["spec"]["template"]["spec"]["containers"][0]["env"]) - 1]["name"] = envname obj["spec"]["template"]["spec"]["containers"][0]["env"][ len(obj["spec"]["template"]["spec"]["containers"][0]["env"]) - 1]["value"] = envalue if action == "create": pykube.ReplicationController(api, obj).create() elif action == "update": pykube.ReplicationController(api, obj).update() else: return 0
def create_rc_odoo(nombre_odoo, master_pass, image_odoo, nombre_db, \ pass_db, image_db, dic_argv, api, metodo, nombre_mysql, pass_mysql, \ key_api_ps, kminion_ip): for i in ["db-rc-vol.yaml", "odoo-rc-vol.yaml"]: rc_yaml = load_file_yaml("kuberntetes_yaml/odoo-pg/%s" % (i)) if i == "db-rc-vol.yaml": nombre = nombre_db rc_yaml["metadata"]["labels"]["provider"] = nombre rc_yaml["spec"]["template"]["metadata"]["labels"][ "provider"] = nombre rc_yaml["spec"]["template"]["spec"]["containers"][0][ "image"] = image_db cont = 0 for e in rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"]: if e["name"] == "POSTGRES_PASSWORD": rc_yaml["spec"]["template"]["spec"]["containers"][0]["env"][cont]\ ["value"] = pass_db cont += 1 rc_yaml["spec"]["template"]["spec"]["containers"][0]["volumeMounts"][0]\ ["subPath"] = "%s/postgresql" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["volumes"][0]["persistentVolumeClaim"]\ ["claimName"] = nombre_odoo if metodo == "odoo-simple" or metodo == "odoo-prestashop": try: rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_NOMBRE', 'value': dic_argv["CONF_NOMBRE"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_CALLE', 'value': dic_argv["CONF_CALLE"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_CIUDAD', 'value': dic_argv["CONF_CIUDAD"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_COD_ZIP', 'value': dic_argv["CONF_COD_ZIP"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_WEB', 'value': dic_argv["CONF_WEB"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_TELF', 'value': dic_argv["CONF_TELF"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_TELF_MOVIL', 'value': dic_argv["CONF_TELF_MOVIL"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_MAIL', 'value': dic_argv["CONF_MAIL"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'CONF_DNI', 'value': dic_argv["CONF_DNI"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'ODOO_USER', 'value': dic_argv["ODOO_USER"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'ODOO_PASS', 'value': dic_argv["ODOO_PASS"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'USER_MOBILE', 'value': dic_argv["USER_MOBILE"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'USER_MOBILE_PASS', 'value': dic_argv["USER_MOBILE_PASS"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'NUM_CUENTA', 'value': dic_argv["NUM_CUENTA"]}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'LOGO_BASE64', 'value': dic_argv["LOGO_BASE64"]}) if metodo == "odoo-prestashop": rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"].append({ 'name': 'MYSQL_SERVER', 'value': nombre_mysql }) rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"].append({ 'name': 'MYSQL_PASSWD', 'value': pass_mysql }) rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"].append({ 'name': 'PS_WEBSERVICE_KEY', 'value': key_api_ps }) rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"].append({ 'name': 'PS_DOMAIN', 'value': dic_argv["ps_dominio"] }) rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"].append({ 'name': 'ODOO_TYPE', 'value': metodo }) except KeyError: print "[ERROR 1006] Falta parametros necesarios." sys.exit(0) rc_yaml = add_limit_resources(rc_yaml, "1", dic_argv["limit_cpu_db"], \ dic_argv["limit_mem_db"]) if i == "odoo-rc-vol.yaml": nombre = nombre_odoo rc_yaml["metadata"]["labels"]["app"] = nombre rc_yaml["spec"]["template"]["spec"]["containers"][0][ "image"] = image_odoo if metodo == "odoo-pg" or metodo == "odoo-simple" or \ metodo == "odoo-prestashop": cont = 0 for e in rc_yaml["spec"]["template"]["spec"]["containers"][0][ "env"]: if e["name"] == "HOST": rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][cont]["value"] = nombre_db if e["name"] == "PASSWORD": rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"][cont]["value"] = pass_db cont += 1 rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'MASTER_PASS', 'value': master_pass}) if metodo == "odoo-prestashop": rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'ODOO_TYPE', 'value': metodo}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'KMINION', 'value': kminion_ip}) rc_yaml["spec"]["template"]["spec"]["containers"][0]\ ["env"].append({'name': 'PS_DOMAIN', 'value': dic_argv["ps_dominio"]}) rc_yaml = add_limit_resources(rc_yaml, dic_argv["odoo_replicas"], \ dic_argv["limit_cpu"], dic_argv["limit_mem"]) rc_yaml["spec"]["template"]["spec"]["containers"][0]["volumeMounts"][0]\ ["subPath"] = "%s/odoo/odoo-lib" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["containers"][0]["volumeMounts"][1]\ ["subPath"] = "%s/odoo/external-addons" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["containers"][0]["volumeMounts"][2]\ ["subPath"] = "%s/odoo/etc-odoo" % (nombre_odoo) rc_yaml["spec"]["template"]["spec"]["volumes"][0]["persistentVolumeClaim"]\ ["claimName"] = nombre_odoo rc_yaml["metadata"]["name"] = nombre rc_yaml["spec"]["template"]["metadata"]["labels"]["app"] = nombre rc_yaml["spec"]["template"]["spec"]["containers"][0]["name"] = nombre if not dic_argv["debug"]: print "Creando rc con %s" % (i) pykube.ReplicationController(api, rc_yaml).create() else: pass
def _create_game_rc(self, id, environment_variables): environment_variables[ "SOCKETIO_RESOURCE"] = KubernetesGameManager._create_game_name(id) environment_variables["GAME_ID"] = id environment_variables["GAME_URL"] = "http://game-{}".format(id) environment_variables["PYKUBE_KUBERNETES_SERVICE_HOST"] = "kubernetes" environment_variables["IMAGE_SUFFIX"] = os.environ.get( "IMAGE_SUFFIX", "latest") rc = pykube.ReplicationController( self._api, { "kind": "ReplicationController", "apiVersion": "v1", "metadata": { "name": KubernetesGameManager._create_game_name(id), "namespace": "default", "labels": { "app": "aimmo-game", "game_id": id, }, }, "spec": { "replicas": 1, "selector": { "app": "aimmo-game", "game_id": id, }, "template": { "metadata": { "labels": { "app": "aimmo-game", "game_id": id, }, }, "spec": { "containers": [ { "env": [{ "name": env_name, "value": env_value, } for env_name, env_value in environment_variables.items()], "image": "ocadotechnology/aimmo-game:{}".format( os.environ.get("IMAGE_SUFFIX", "latest")), "ports": [ { "containerPort": 5000, }, ], "name": "aimmo-game", "resources": { "limits": { "cpu": "300m", "memory": "128Mi", }, "requests": { "cpu": "10m", "memory": "64Mi", }, }, }, ], }, }, }, }, ) rc.create()