The k8s builder can be used with the nano test network by following the instructions below.
Download the latest builder binaries from the releases page and extract them to a k8s_builder/bin
directory in your home directory.
After installing the nano test network prereqs, the fabric-samples/config/core.yaml
file needs to be updated with the k8s builder configuration.
externalBuilders:
- name: k8s_builder
path: <HOME>/k8s_builder
propagateEnvironment:
- CORE_PEER_ID
- KUBECONFIG_PATH
You can use yq to update the fabric-samples/config/core.yaml
files.
Make sure you are in the fabric-samples
directory before running the following commands.
FABRIC_K8S_BUILDER_PATH=${HOME}/k8s_builder yq -i '.chaincode.externalBuilders += { "name": "k8s_builder", "path": "${FABRIC_K8S_BUILDER_PATH}" | envsubst(ne), "propagateEnvironment": [ "CORE_PEER_ID", "KUBECONFIG_PATH" ] }' config/core.yaml
The k8s builder needs a kubeconfig file to access a Kubernetes cluster to deploy chaincode. Make sure the KUBECONFIG_PATH
environment variable is available on every peer the builder is configured on.
export KUBECONFIG_PATH=$HOME/.kube/config
The sample contracts for Go, Java, and Node.js publish a Docker image which the k8s builder can use and a chaincode package file which can be used with the peer lifecycle chaincode install
command.
Use of a pre-generated chaincode package .tgz greatly simplifies the deployment, aligning with standard industry practices for CI/CD and git-ops workflows.
Download a sample chaincode package, e.g. for the Go contract:
curl -fsSL \
https://github.com/hyperledger-labs/fabric-builder-k8s/releases/download/v0.7.2/go-contract-v0.7.2.tgz \
-o go-contract-v0.7.2.tgz
Deploy the chaincode package as usual, starting by installing the k8s chaincode package.
peer lifecycle chaincode install go-contract-v0.7.2.tgz
Export a PACKAGE_ID
environment variable for use in the following commands.
export PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid go-contract-v0.7.2.tgz) && echo $PACKAGE_ID
Note: the PACKAGE_ID
must match the chaincode code package identifier shown by the peer lifecycle chaincode install
command.
Approve the chaincode.
peer lifecycle chaincode approveformyorg -o 127.0.0.1:6050 --channelID mychannel --name sample-contract --version 1 --package-id $PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt
Commit the chaincode.
peer lifecycle chaincode commit -o 127.0.0.1:6050 --channelID mychannel --name sample-contract --version 1 --sequence 1 --tls --cafile "${PWD}"/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt
Query the chaincode metadata!
peer chaincode query -C mychannel -n sample-contract -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}'