Exemplo n.º 1
0
def reset_kubeadm():
    """Uninstall kubernetes on a node.

    Runs `kubeadm reset` on the specified machine in order to remove the
    kubernetes services and undo all configuration set by `kubeadm init`.

    """
    # Get script from path.
    script = os.path.join(os.path.dirname(__file__), 'mega-reset.sh')
    ctx.download_resource(
        os.path.join('scripts', 'mega-reset.sh'), script
    )

    # Get worker.
    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    ctx.logger.info('Running "kubeadm reset" on %s', machine)

    _add_run_remove_script(
        cloud_id=machine.cloud.id,
        machine_id=machine.id,
        script_path=os.path.abspath(script),
        script_name='kubeadm_reset_%s' % random_string(length=4)
    )
Exemplo n.º 2
0
def configure_kubernetes_master():
    """Configure the kubernetes master.

    Sets up the master node and stores the necessary settings inside the node
    instance's runtime properties, which are required by worker nodes in order
    to join the kubernetes cluster.

    """
    ctx.logger.info('Setting up kubernetes master node')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    # Token for secure master-worker communication.
    token = '%s.%s' % (random_string(length=6), random_string(length=16))
    ctx.instance.runtime_properties['master_token'] = token.lower()

    # Store kubernetes dashboard credentials in runtime properties.
    ctx.instance.runtime_properties.update({
        'auth_user':
        ctx.node.properties['auth_user'],
        'auth_pass':
        ctx.node.properties['auth_pass'] or random_string(10),
    })

    ctx.logger.info('Installing kubernetes on master node')

    # Prepare script parameters.
    params = "-u '%s' " % ctx.instance.runtime_properties['auth_user']
    params += "-p '%s' " % ctx.instance.runtime_properties['auth_pass']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'master'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'],
        su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']
Exemplo n.º 3
0
def configure_kubernetes_master():
    """Configure the kubernetes master.

    Sets up the master node and stores the necessary settings inside the node
    instance's runtime properties, which are required by worker nodes in order
    to join the kubernetes cluster.

    """
    ctx.logger.info('Setting up kubernetes master node')
    prepare_kubernetes_script()

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=ctx.instance.runtime_properties['cloud_id'],
        machine_id=ctx.instance.runtime_properties['machine_id'],
    )

    # Token for secure master-worker communication.
    token = '%s.%s' % (random_string(length=6), random_string(length=16))
    ctx.instance.runtime_properties['master_token'] = token.lower()

    # Store kubernetes dashboard credentials in runtime properties.
    ctx.instance.runtime_properties.update({
        'auth_user': ctx.node.properties['auth_user'],
        'auth_pass': ctx.node.properties['auth_pass'] or random_string(10),
    })

    ctx.logger.info('Installing kubernetes on master node')

    # Prepare script parameters.
    params = "-u '%s' " % ctx.instance.runtime_properties['auth_user']
    params += "-p '%s' " % ctx.instance.runtime_properties['auth_pass']
    params += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    params += "-r 'master'"

    # Run the script.
    script = conn.client.run_script(
        script_id=ctx.instance.runtime_properties['script_id'], su=True,
        machine_id=machine.id,
        cloud_id=machine.cloud.id,
        script_params=params,
    )
    ctx.instance.runtime_properties['job_id'] = script['job_id']
def get_master_init_args():
    """Return the arguments required to install the kubernetes master."""

    ctx.logger.info('Preparing cloud-init for kubernetes master')

    # Token for secure master-worker communication.
    token = '%s.%s' % (random_string(length=6), random_string(length=16))
    ctx.instance.runtime_properties['master_token'] = token.lower()

    # Store kubernetes dashboard credentials in runtime properties.
    ctx.instance.runtime_properties.update({
        'auth_user': ctx.node.properties['auth_user'],
        'auth_pass': ctx.node.properties['auth_pass'] or random_string(10),
    })

    arguments = "-u '%s' " % ctx.instance.runtime_properties['auth_user']
    arguments += "-p '%s' " % ctx.instance.runtime_properties['auth_pass']
    arguments += "-t '%s' " % ctx.instance.runtime_properties['master_token']
    arguments += "-r 'master'"

    return arguments
Exemplo n.º 5
0
def drain_and_remove():
    """Mark the node as unschedulable, evict all pods, and remove it.

    Runs `kubectl drain` and `kubectl delete nodes` on the kubernetes
    master in order to drain and afterwards remove the specified node
    from the cluster.

    """
    if ctx.node.properties['master']:  # FIXME Is this necessary?
        return

    # Get master instance.
    master = ctx.instance.relationships[0]._target.instance

    # Render script.
    script = os.path.join(os.path.dirname(__file__), 'drain-node.sh')
    ctx.download_resource_and_render(
        os.path.join('scripts', 'drain-node.sh'), script,
        template_variables={
            'server_ip': master.runtime_properties.get('server_ip',''),
            'auth_user': master.runtime_properties['auth_user'],
            'auth_pass': master.runtime_properties['auth_pass'],
            'hostname': ctx.instance.runtime_properties.get('machine_name', '').lower()
        },
    )

    conn = MistConnectionClient()
    machine = conn.get_machine(
        cloud_id=master.runtime_properties['cloud_id'],
        machine_id=master.runtime_properties['machine_id'],
    )

    ctx.logger.info('Running "kubectl drain && kubectl delete" on %s', machine)

    _add_run_remove_script(
        cloud_id=machine.cloud.id,
        machine_id=machine.id,
        script_path=os.path.abspath(script),
        script_name='kubectl_drain_%s' % random_string(length=4)
    )
Exemplo n.º 6
0
def prepare_kubernetes_script():
    """Upload kubernetes installation script, if missing.

    This method is executed at the very beginning, in a pre-configuration
    phase, to make sure that the kubernetes installation script has been
    uploaded to mist.io.

    This method is meant to be invoked early on by:

        configure_kubernetes_master()
        configure_kubernetes_worker()

    The script_id inside each instance's runtime properties is used later
    on in order to configure kubernetes on the provisioned machines.

    """
    if ctx.instance.runtime_properties.get('script_id'):
        ctx.logger.info('Kubernetes installation script already exists')
    else:
        ctx.logger.info('Uploading fresh kubernetes installation script')
        # If a script_id does not exist in the node instance's runtime
        # properties, perhaps because this is the first node that is being
        # configured, load the script from file, upload it to mist.io, and
        # run it over ssh.
        client = MistConnectionClient().client
        script = os.path.join(os.path.dirname(__file__), 'mega-deploy.sh')
        ctx.download_resource(
            os.path.join('scripts', 'mega-deploy.sh'), script
        )
        with open(os.path.abspath(script)) as fobj:
            script = fobj.read()
        script = client.add_script(
            name='install_kubernetes_%s' % random_string(length=4),
            script=script, location_type='inline', exec_type='executable'
        )
        ctx.instance.runtime_properties['script_id'] = script['id']
Exemplo n.º 7
0
def prepare_kubernetes_script():
    """Upload kubernetes installation script, if missing.

    This method is executed at the very beginning, in a pre-configuration
    phase, to make sure that the kubernetes installation script has been
    uploaded to mist.io.

    This method is meant to be invoked early on by:

        configure_kubernetes_master()
        configure_kubernetes_worker()

    The script_id inside each instance's runtime properties is used later
    on in order to configure kubernetes on the provisioned machines.

    """
    if ctx.instance.runtime_properties.get('script_id'):
        ctx.logger.info('Kubernetes installation script already exists')
    else:
        ctx.logger.info('Uploading fresh kubernetes installation script')
        # If a script_id does not exist in the node instance's runtime
        # properties, perhaps because this is the first node that is being
        # configured, load the script from file, upload it to mist.io, and
        # run it over ssh.
        client = MistConnectionClient().client
        script = os.path.join(os.path.dirname(__file__), 'mega-deploy.sh')
        ctx.download_resource(os.path.join('scripts', 'mega-deploy.sh'),
                              script)
        with open(os.path.abspath(script)) as fobj:
            script = fobj.read()
        script = client.add_script(name='install_kubernetes_%s' %
                                   random_string(length=4),
                                   script=script,
                                   location_type='inline',
                                   exec_type='executable')
        ctx.instance.runtime_properties['script_id'] = script['id']