Setting InitScript
    • PDF

    Setting InitScript

    • PDF

    Article Summary

    Available in VPC

    With NAVER Cloud Platform's Ncloud Kubernetes Service, you can manage initial node environments by running a script that has been written in advance with InitScript. InitScript is created with DaemonSet and scheduled on each node.

    Caution
    • Since init script works with DaemonSet, it shouldn't interfere with Container Runtime Interface (CRI) or Kubelet settings.
    • Init script works in Kubernetes, which is similar to a server creation script, but different.
    • The script content must not include content that may create security breaches.

    For more information on the characteristics of DaemonSet, see DaemonSet official document.

    Create and delete init script

    Create init script

    You can run the command corresponding to the regions below to create an init script. When you run the command, the init script is created with DaemonSet on the kube-system namespace.

    • Korea
    $ kubectl --kubeconfig=$KUBE_CONFIG apply -f https://raw.githubusercontent.com/NaverCloudPlatform/nks-examples/main/examples/initscript/pub/kr.yml
    
    • Singapore
    $ kubectl --kubeconfig=$KUBE_CONFIG apply -f https://raw.githubusercontent.com/NaverCloudPlatform/nks-examples/main/examples/initscript/pub/sgn.yml
    
    • Japan
    $ kubectl --kubeconfig=$KUBE_CONFIG apply -f https://raw.githubusercontent.com/NaverCloudPlatform/nks-examples/main/examples/initscript/pub/jpn.yml
    

    Confirm init script creation

    You can run the command below to check the creation and status of the working init script in the cluster. Init script works with DaemonSet and is scheduled on all nodes. It will be created on a new node in case new nodes are added, so creating one separately is not necessary.

    $ kubectl --kubeconfig=$KUBE_CONFIG get daemonset init-script -n kube-system
    

    You can run the command below to confirm if the init script has been created and its early operations.

    $ kubectl --kubeconfig=$KUBE_CONFIG logs  -n kube-system -l name=init-script
    

    Delete init script

    Run the command below to delete the init script created in the cluster.

    $ kubectl --kubeconfig=$KUBE_CONFIG delete daemonset init-script -n kube-system
    

    Set init script and check operations

    In order to run a specific script through init script, the script content encoded in Base64 should be put in the environment variable value of the init script DaemonSet.

    Set init script

    You can set a script as init script by using a script file created in advance, or by editing the init script Daemonset created.

    You can run the command below to set a script that has been created in advance as init script.

    $ kubectl --kubeconfig=$KUBE_CONFIG set env daemonset/init-script -n kube-system STARTUP_SCRIPT=$(base64 {init-script-filename} -w 0)
    

    You can run the command below to set the init script by editing its content yourself.

    $ kubectl --kubeconfig=$KUBE_CONFIG edit daemonset/init-script -n kube-system
    

    Confirm init script operation

    You can run the command below to view the init script's pod log. You can also see the script execution result through the pod log.

    • If you want to view logs for the entire init script pod
    $ kubectl --kubeconfig=$KUBE_CONFIG -n kube-system -l name=init-script
    
    • If you want to view logs for a specific init script pod
    $ kubectl --kubeconfig=$KUBE_CONFIG -n kube-system [init-script-pod-name]
    

    Init script can be edited with the same command as one used to set it. After editing it, a new init script pod will be created. If the script content hasn't changed, the script will not run even if the pod restarts.

    Init script example

    The following is an example of setting a password for each node through init script.

    (1) Check script

    Check the script you are going to register as an init script.

    #!/bin/bash
    
    # change-passwd.sh
    
    echo "Password Init" 
    echo -e '[new password]' | passwd root
    

    (2) Set script to run

    Encode the script file and add it to the init script's environment variable.

    $ kubectl --kubeconfig=$KUBE_CONFIG set env daemonset/init-script -n kube-system STARTUP_SCRIPT=$(base64 change-passwd.sh -w 0)
    

    (3) Confirm operation through pod log

    Check the script operation log through the command below.

    $ kubectl --kubeconfig=$KUBE_CONFIG get logs -n kube-system [init-script-pod-name]
    

    The result will be shown as below if the operation was successful.

    $ kubectl --kubeconfig=$KUBE_CONFIG logs -n kube-system init-script-mzwl7
    Password Init
    New password: Retype new password: passwd: password updated successfully
    !!! startup-script succeeded!
    

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.