How to extend the dependencies of code blocks
Standalone Mode
Dependency Library Persistence
The following operations must be performed while the service is running and need to be done only once during the first setup. Subsequent installation of extension libraries does not require this operation.
-
Create a directory to mount the dependency libraries (the actual directory can be customized), such as:
mkdir -p /data/mingdao/script/volume/command/package/python-3.6/
mkdir -p /data/mingdao/script/volume/command/package/nodejs-10.18.0/ -
Retrieve pre-installed dependency libraries:
docker cp $(docker ps | grep command | awk '{print $1}'):/usr/local/lib/python3.6/site-packages/ /data/mingdao/script/volume/command/package/python-3.6/
docker cp $(docker ps | grep command | awk '{print $1}'):/usr/local/node-10.18.0/lib/node_modules/ /data/mingdao/script/volume/command/package/nodejs-10.18.0/ -
Modify
docker-compose.yamlto mount the dependency library directories from the host to the container:services:
command:
volumes:
- ./volume/command/package/python-3.6/site-packages/:/usr/local/lib/python3.6/site-packages/
- ./volume/command/package/nodejs-10.18.0/node_modules/:/usr/local/node-10.18.0/lib/node_modules/ -
Execute the following command in the root directory of the manager to restart the service:
bash ./service.sh restartall
Installing Extension Libraries Online
- Ensure the "Dependency Library Persistence" step is completed first. The container's dependency library directory must be persistently mounted to the host. Otherwise, extension libraries will need to be reinstalled after the container restarts.
- Ensure the server has internet access for online installation of extension libraries.
Python
Using the example of installing the python-dateutil extension library:
-
Enter the
commandcontainer:docker exec -it $(docker ps | grep command | awk '{print $1}') bash -
Install the
python-dateutilextension library inside thecommandcontainer:pip3 install --target=/usr/local/lib/python3.6/site-packages/ python-dateutil
After installation, the newly installed extension library can be found in the default host path /data/mingdao/script/volume/command/package/python-3.6/site-packages/.
JavaScript (based on Nodejs)
Using the example of installing the dayjs extension library:
-
Enter the
commandcontainer:docker exec -it $(docker ps | grep command | awk '{print $1}') bash -
Install the
dayjsextension library inside the container:/usr/local/node-10.18.0/bin/npm -g install dayjs
After installation, the newly installed extension library can be found in the default host path /data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules/.
Installing Extension Libraries Offline
- Ensure the "Dependency Library Persistence" step is completed first. The container's dependency library directory must be persistently mounted to the host. Otherwise, extension libraries will need to be reinstalled after the container restarts.
- This method can be used if the server does not have internet access or if the extension libraries are provided as offline package files.
Python
Using the example of installing the pycryptodome extension library:
-
In an environment with internet access, retrieve the offline package file for the extension library:
pip3 download pycryptodome -d "/package"- The offline package file for the
pycryptodomedependency library will be generated in the/packagedirectory.
- The offline package file for the
-
Upload the offline package file to the server hosting the HAP service.
-
Copy the offline package file into the container and install it:
-
Copy the
pycryptodomedependency library offline file to the/tmpdirectory inside thecommandcontainer:docker cp pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl $(docker ps | grep command | awk '{print $1}'):/tmp -
Enter the
commandcontainer:docker exec -it $(docker ps | grep command | awk '{print $1}') bash -
Install the
pycryptodomeextension library offline package inside the container:pip3 install --target=/usr/local/lib/python3.6/site-packages/ /tmp/pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl
-
JavaScript (based on Nodejs)
Using the example of installing the dayjs extension library:
-
In an environment with internet access, retrieve the offline package file for the extension library:
npm install dayjs -save- A
node_modules/dayjsdependency library directory will be generated in the current directory.
- A
-
Package the
dayjsextension library directory:cd node_modules
tar czf dayjs.tar.gz dayjs -
Upload the packaged file
dayjs.tar.gzto the server hosting the HAP service. -
Extract the offline package file to the persistently mounted path for dependency libraries:
tar xf dayjs.tar.gz -C /data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules- If the default data path for HAP was modified, adjust the extraction path according to the actual environment setup.
Cluster Mode
Dependency Library Persistence
-
Create the dependency library mount directory on the first microservice server:
mkdir -p /data/mingdao/script/volume/command/package/python-3.6/site-packages
mkdir -p /data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules -
Retrieve the prebuilt
Pythondependency libraries to the local directory:kubectl cp $(kubectl get pod|grep command-|awk 'NR==1{print $1}'):/usr/local/lib/python3.6/site-packages /data/mingdao/script/volume/command/package/python-3.6/site-packages -
Retrieve the prebuilt
JavaScriptdependency libraries to the local directory:kubectl cp $(kubectl get pod|grep command-|awk 'NR==1{print $1}'):/usr/local/node-10.18.0/lib/node_modules /data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules -
Package and archive the
/data/mingdao/script/volume/command/directory on the current server, then synchronize and distribute it to all other microservice nodes in the cluster. -
On each microservice node, extract the archived file. Ensure that the following paths are established and the internal file contents are identical on every node:
/data/mingdao/script/volume/command/package/python-3.6/site-packages
/data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules
Configure Dependency Library Mounts
-
Edit the
service.yamlconfiguration file for the microservice (typically located in the/data/mingdao/script/kubernetes/path of the first microservice node).Add mount configurations for the
commandservice under itsdeployment:volumeMounts:
- mountPath: /usr/local/lib/python3.6/site-packages
name: python-volume
- mountPath: /usr/local/node-10.18.0/lib/node_modules
name: node-volume
volumes:
- name: python-volume
hostPath:
path: /data/mingdao/script/volume/command/package/python-3.6/site-packages
type: Directory
- name: node-volume
hostPath:
path: /data/mingdao/script/volume/command/package/nodejs-10.18.0/node_modules
type: Directory- Modify the instance count for the
commandservice:- Adjust the
replicasvalue to match or exceed the number of current microservice nodes. - For example, if there are 3 microservice nodes, set the replica count to 3 or above.
- Adjust the
- Modify the instance count for the
-
Apply the updated configuration file to activate the mounts:
kubectl apply -f service.yaml -
Check the status of the
commandservice:kubectl get pod | grep command- After the update, the
commandservice will perform a rolling restart. Wait 1-2 minutes until allcommandinstances restart and their status displays as2/2, indicating normal operation.
- After the update, the
Online Installation of Extension Libraries
- Ensure the "Dependency Library Persistence" step is completed to persist the container dependency library directories onto the host machine; otherwise, extension libraries will need to be reinstalled after container restarts.
- Online installation of extension libraries requires the server to have internet access.
Python
In a cluster environment, since the command service uses multi-instance (Pod) deployment, you must ensure that the extension library is correctly installed in each Pod instance. The following example script runs in the default namespace by default, executing installation commands across all relevant Pods:
Example: Installing the
python-dateutilextension library
for pod in $(kubectl get pods -l app=command -n default -o jsonpath='{.items[*].metadata.name}'); do
echo "Processing Pod: $pod"
kubectl exec -it $pod -n default -- pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --target=/usr/local/lib/python3.6/site-packages/ python-dateutil
done
JavaScript (Nodejs-based)
In a cluster environment, since the command service uses multi-instance (Pod) deployment, you must ensure that the extension library is correctly installed in each Pod instance. The following example script runs in the default namespace by default, executing installation commands across all relevant Pods:
Example: Installing the
dayjsextension library
for pod in $(kubectl get pods -l app=command -n default -o jsonpath='{.items[*].metadata.name}'); do
echo "Processing Pod: $pod"
kubectl exec -it $pod -n default -- /usr/local/node-10.18.0/bin/npm -g install dayjs --registry=https://registry.npmmirror.com
done
Offline Installation of Extension Libraries
- Ensure the "Dependency Library Persistence" step is completed to persist the container dependency library directories onto the host machine; otherwise, extension libraries will need to be reinstalled after container restarts.
- Use offline installation methods when the server cannot access the internet or if the provided extension libraries come as offline package files.
Python
Example: Installing the
pycryptodomeextension library
-
Retrieve the offline file for the extension library in an environment with internet access:
pip3 download pycryptodome -d "/package" -i https://mirrors.aliyun.com/pypi/simple/- The
/packagedirectory will contain the offline files for thepycryptodomedependency library.
- The
-
Upload the offline library file (e.g.,
pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl) to a temporary directory on the first microservice node. -
Execute the following script on the first microservice node.
Since the
commandservice in the cluster uses multi-instance (Pod) deployment, you must ensure the offline library is distributed and installed across all instances. The following example script runs in thedefaultnamespace by default, distributing and installing the offline library file across all relevant Pods:# Update the FILE_NAME variable value based on the actual offline file name
FILE_NAME="pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl"
for pod in $(kubectl get pods -l app=command -n default -o jsonpath='{.items[*].metadata.name}'); do
echo "Processing Pod: $pod"
kubectl cp ./$FILE_NAME $pod:/tmp/ -n default
kubectl exec -it $pod -n default -- pip3 install --target=/usr/local/lib/python3.6/site-packages/ /tmp/$FILE_NAME
done
JavaScript (Nodejs-based)
Example: Installing the
dayjsextension library
-
Retrieve the offline file for the extension library in an environment with internet access:
npm install dayjs -save --registry=https://registry.npmmirror.com- The
node_modules/dayjsdirectory will be generated containing thedayjsdependency library.
- The
-
Package the
dayjsextension library directory:cd node_modules
tar czf dayjs.tar.gz dayjs -
Upload the packaged file
dayjs.tar.gzto a temporary directory on the first microservice node. -
Execute the following script on the first microservice node.
Since the
commandservice in the cluster uses multi-instance (Pod) deployment, you must ensure the extension library is correctly distributed and installed across all instances. The following example script runs in thedefaultnamespace by default, distributing and extracting the library across all relevant Pods:# Update the NODE_FILE variable value based on the actual offline package file name
NODE_FILE="dayjs.tar.gz"
for pod in $(kubectl get pods -l app=command -n default -o jsonpath='{.items[*].metadata.name}'); do
echo "Processing Pod: $pod"
kubectl cp ./$NODE_FILE $pod:/tmp/ -n default
kubectl exec -it $pod -n default -- tar xf /tmp/$NODE_FILE -C /usr/local/node-10.18.0/lib/node_modules
done