Scenario: Run Chaincode as a Service
The following example illustrates running the FabCar chaincode as an external service (a pod in the Kubernetes cluster) which you can then deploy to Oracle Blockchain Platform.
Before you complete the following steps, deploy the chaincode as an external package in Oracle Blockchain Platform.
- Clone the GitHub directory in the
hyperledger/fabric-samples
repository that containsfabcar/external
. The directory contains the external version of the FabCar chaincode in Go,fabcar.go
, and aDockerfile
file. Ignore themetadata.json
andchaincode.env.example
files. - Modify the
Dockerfile
file as shown in the following example:# Use the Go image from Docker Hub with specified versions for Go and Alpine Linux FROM golang:1.13.8-alpine3.10 # Create a directory named "chaincode" in the image RUN mkdir /chaincode # Set "/chaincode" as the working directory within the image WORKDIR /chaincode # Copy the contents of the local directory into "/chaincode" in the image COPY . . # Fetch and install Go dependencies for the project RUN go get -d -v ./... # Install Go dependencies RUN go install -v ./... # Compile the Go code in the current directory, creating an executable binary named "fabcar" RUN go build -o fabcar . # Expose port 9999, indicating that the application within the container will listen on this port EXPOSE 9999 # Set the default command to run the "fabcar" executable when the container starts CMD ["/chaincode/fabcar"]
This sequence of commands sets the base image for building the new image to the Go image with specific versions for Go and Alpine Linux, creates a directory, sets that directory as the working directory, copies the local contents into the image, fetchs and installs Go dependencies, compiles the Go code, configures the container to listen on port 9999, and sets the default command to run the
fabcar
executable. - Run the following command to build the Docker image, which creates a Docker image
named
fabcar-sample
, which can then be pushed to a repository.docker build -t fabcar-sample .
- Create a Kubernetes deployment YAML file to run the Docker image that
contains the chaincode. The following example shows a
deployment.yaml
file. The repository where the Docker image is pulled from isrepository.example.org
. The Kubernetes secret name that is used to pull images isocirsecret
. TheCHAINCODE_SERVER_ADDRESS
value is 0.0.0.0:9999, which indicates that the server is configured to accept incoming connections on port 9999 from any IP address that is associated with the computer. Get theCHAINCODE_ID
value from the chaincode package ID that is displayed when you deploy on the peer node in Oracle Blockchain Platform by selecting the external chaincode type and uploading a.zip
file. In the Oracle Blockchain Platform UI, the chaincode ID is displayed as chaincode_name:package_ID. For Java chaincode, the chaincode ID variable is calledCORE_CHAINCODE_ID_NAME
.apiVersion: apps/v1 kind: Deployment metadata: name: external-fabcar-node namespace: chaincode labels: chaincode-type: external chaincode-name: external-fabcar-node spec: replicas: 1 selector: matchLabels: chaincode-type: external chaincode-name: external-fabcar-node template: metadata: labels: chaincode-type: external chaincode-name: external-fabcar-node spec: imagePullSecrets: - name: ocirsecret containers: - env: - name: CHAINCODE_SERVER_ADDRESS value: "0.0.0.0:9999" - name: CHAINCODE_ID value: "ext-fabcar-node:7f953544f26124914c29467d5079f601ddefa53375fffec830ecd70c13547877" image: repository.example.org/blockchain/fabcar-sample:latest imagePullPolicy: Always name: assettransfer ports: - containerPort: 9999 name: grpc protocol: TCP --- apiVersion: v1 kind: Service metadata: labels: chaincode-name: external-fabcar-node chaincode-type: external name: external-fabcar-node namespace: chaincode spec: ports: - name: grpc port: 9999 protocol: TCP targetPort: 9999 selector: chaincode-name: external-fabcar-node chaincode-type: external type: ClusterIP
- Run the following command to generate a Kubernetes secret in the
chaincode
namespace. For name_of_secret, specify the secret from thedeployment.yaml
file. In this example, the name of the secret isocirsecret
.kubectl create secret docker-registry name_of_secret -n chaincode --docker-server='repository.example.org/blockchain' --docker-username='your_username' --docker-password='auth_token'
- Run the following command to deploy the Kubernetes pod and
service.
kubectl apply -f deployment.yaml
You can now run transactions that use the external chaincode from Oracle Blockchain Platform.