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.

  1. Clone the GitHub directory in the hyperledger/fabric-samples repository that contains fabcar/external. The directory contains the external version of the FabCar chaincode in Go, fabcar.go, and a Dockerfile file. Ignore the metadata.json and chaincode.env.example files.
  2. 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.

  3. 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 .
  4. 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 is repository.example.org. The Kubernetes secret name that is used to pull images is ocirsecret. The CHAINCODE_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 the CHAINCODE_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 called CORE_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
  5. Run the following command to generate a Kubernetes secret in the chaincode namespace. For name_of_secret, specify the secret from the deployment.yaml file. In this example, the name of the secret is ocirsecret.
    kubectl create secret docker-registry name_of_secret -n chaincode --docker-server='repository.example.org/blockchain' --docker-username='your_username' --docker-password='auth_token'
  6. 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.