项目作者: IBM

项目描述 :
A Code Pattern to teach how to run a COBOL program on Kubernetes
高级语言: Dockerfile
项目地址: git://github.com/IBM/kubernetes-cobol.git
创建时间: 2019-05-13T17:07:21Z
项目社区:https://github.com/IBM/kubernetes-cobol

开源协议:Apache License 2.0

下载


Build Status

Running COBOL on a Kubernetes cluster

In this code pattern, we will build a Docker container with a simple hello world COBOL application using Kubernetes and Docker. We will build a Docker container locally, test it, push it to a registry, then pull it down to a remote Kubernetes cluster expecting our output.

When you have completed this code pattern, you will understand how to:

  • Build Docker containers locally
  • Test a basic Docker container
  • Push a Docker container to a remote registry
  • Configure Kubernetes to pull from a remote registry and run a COBOL Hello World Application

Steps

  1. Install Docker Community Edition.
  2. Clone the Repository Locally.
  3. Install IBM Cloud CLI.
  4. Create your namespace.
  5. Build your COBOL container.
  6. Test your COBOL container locally.
  7. Create and connect to IBM Cloud Kubernetes cluster.
  8. Run a job on Kubernetes.

1. Install Docker Community Edition

First install the Docker-CE edition on your local workstation. There are two main editions of Docker, I’d like to take a moment to discuss the different versions here. First there is Docker Community Edition, or docker-ce, and Docker Enterprise Edition, or docker-ee. They both have advantages and are aimed at different use cases. I strongly suggest after walking through the documentation here to verify that Docker-CE is the correct one for your use case.

Some of the further steps require that docker command do NOT use sudo preface, which is the default behavior after installation. To do so, log in as root user or follow the optional installations steps here.

2. Clone the repository locally

Clone down this git repository on your local workstation.

  1. $ git clone https://github.com/IBM/kubernetes-cobol

3. Install IBM Cloud CLI

Before continue, make sure you have an active IBM Cloud account, as well as you have the IBM Cloud CLI already working in your shell. To learn more about IBM Cloud Container Registry and sign up for an IBM Cloud account, visit website. To install the IBM CLoud CLI, follow these instructions.

4. Create your namespace

Next create a namespace in IBM Cloud Container Registry to store your Docker images. Log into IBM Cloud via the CLI, then run the following commands. We are going to call our container registry namespace “docker_cobol” as our example.

NOTE: docker_cobol is a universal namespace, you will need to change it so SOMETHING ELSE for you. I suggest maybe your first name and docker_cobol for instance jj_docker_cobol in my case.

  1. $ ibmcloud login
  2. $ ibmcloud cr namespace-add docker_cobol
  3. $ ibmcloud cr namespace-list | grep docker_cobol # a sanity check to make sure it was created correctly

5. Build your COBOL container

Build the Docker container on your local workstation. This will require a few steps. We will walk you through each. First change the directory of docker/. You’ll want build your container and tag it with a meaningful tag. Using the IBM Cloud registry, you can build the container and also push it to a registry in one command. We are going to use the namespace that we created above, and call the container hello_world and label it with v1.

  1. $ cd docker/
  2. $ ibmcloud cr login
  3. $ ibmcloud cr build --tag us.icr.io/docker_cobol/hello_world:v1 ./
  4. Sending build context to Docker daemon 2.56kB
  5. Step 1/11 : FROM centos:latest
  6. latest: Pulling from library/centos
  7. 8ba884070f61: Pull complete
  8. Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
  9. Status: Downloaded newer image for centos:latest
  10. ---> 9f38484d220f
  11. Step 2/11 : ENV GMP_VERSION=6.0.0
  12. ---> Running in 2fb58cad59c6
  13. Removing intermediate container 2fb58cad59c6
  14. ---> fdbd9fe6be02
  15. Step 3/11 : ENV GNU_COBOL=1.1
  16. ---> Running in 31dc23ee79b3
  17. Removing intermediate container 31dc23ee79b3
  18. ---> 96ad047e4fd5
  19. [-- snip --]
  20. The push refers to repository [us.icr.io/docker_cobol/hello_world]
  21. f3eaacc5c5c1: Pushed
  22. cdf3deb1ef52: Pushed
  23. 98290c1d657b: Pushed
  24. 438a70293769: Pushed
  25. 166cbcd2bc5d: Pushed
  26. cc201d7335e0: Pushed
  27. d69483a6face: Pushed
  28. v1: digest: sha256:9dac5ddf1210b899bf3fd75e263bc5a5854ade2141ec2abb6f3e6bf5c59b3539 size: 1785
  29. $

6. Test your COBOL container locally

After a successful build, lets test it out on our local machine. Go ahead and run the following command to pull from your local namespace and run it on your local workstation.

  1. $ ibmcloud cr login # incase you haven't already
  2. $ docker run us.icr.io/docker_cobol/hello_world:v1 # remember to type in the correct namespace previously created
  3. Unable to find image 'us.icr.io/docker_cobol/hello_world' locally
  4. v1: Pulling from docker_cobol/hello_world
  5. 8ba884070f61: Pull complete
  6. e48b6497387f: Pull complete
  7. 28037863ba49: Pull complete
  8. 94e07aff83de: Pull complete
  9. be35421f2508: Pull complete
  10. 70ced04e9e75: Pull complete
  11. 92cce9b2c928: Pull complete
  12. Digest: sha256:9dac5ddf1210b899bf3fd75e263bc5a5854ade2141ec2abb6f3e6bf5c59b3539
  13. Status: Downloaded newer image for us.icr.io/docker_cobol/hello_world:v1
  14. Hello world!

Now that we have the container on our local workstation, lets run some tests against it. We’ll be using some software called InSpec. Go ahead and install the software from the official page. After that, change directory into the inspec/ directory and edit 01-docker.rb file in order to replace “docker_cobol” by the correct namespace previously created.

  1. $ cd inspec/
  2. $ nano 01-docker.rb # inside the file, replace "docker_cobol" example namespace by the namespace previously created
  3. $ inspec exec 01-docker.rb
  4. Profile: tests from 01-docker.rb (tests from 01-docker.rb)
  5. Version: (not specified)
  6. Target: local://
  7. #<Inspec::Resources::DockerImageFilter:0x0000000004c47cd8> with repository == "ubuntu" tag == "12.04"
  8. should not exist
  9. #<Inspec::Resources::DockerImageFilter:0x0000000004c5ccf0> with repository == "us.icr.io/docker_cobol/hello_world" tag == "v1"
  10. should exist
  11. Command: `docker run us.icr.io/docker_cobol/hello_world:v1`
  12. stdout should eq "Hello world!\n"
  13. stderr should eq ""
  14. exit_status should eq 0
  15. Test Summary: 5 successful, 0 failures, 0 skipped
  16. $

There are a many tests you can write and very your Docker containers here, I suggest taking a look at the official documentation here. We have a few tests in the 01-docker.rb file I’d check it to see the a few options for some sanity checks.

7. Create and connect to IBM Cloud Kubernetes cluster

If we now have some successful building on the IBM Cloud, running the output to see Hello World! We need to request a IBM Cloud Kubernetes cluster. Run the following commands to request it. You can run a “free tier” cluster here for this code pattern. This will take a few minutes, and check the status by the second command.

  1. $ ibmcloud ks cluster create classic --name cobol_docker
  2. $ ibmcloud ks clusters | grep cobol_docker

When the cluster is complete and in normal state, we can connect to it. You can connect to in via the following commands, and run the second command as a sanity check.

  1. $ ibmcloud ks cluster config --cluster cobol_docker
  2. OK
  3. The configuration for cobol_docker was downloaded successfully.
  4. Added context for cobol_docker to the current kubeconfig file.
  5. You can now execute 'kubectl' commands against your cluster. For example, run 'kubectl get nodes'.
  6. $ kubectl get nodes
  7. NAME STATUS ROLES AGE VERSION
  8. 10.186.59.93 Ready <none> 1d v1.16.9+IKS

Now that you have your cluster and your container build on the IBM Cloud Container Registry we should create some keys to allow for our Kubernetes cluster to call into the Registry and request our build.

  1. $ ibmcloud iam service-id-create docker_cobol-service-id --description "COBOL Kubernetes service"
  2. $ ibmcloud iam service-policy-create docker_cobol-service-id --roles Manager --service-name container-registry
  3. $ ibmcloud iam service-api-key-create docker_cobol-api-key docker_cobol-service-id --description "API key for COBOL Kubernetes service"
  4. #
  5. # Take note of the API Key created and put it as <API_Key> in the following command
  6. #
  7. $ kubectl create secret docker-registry docker-cobol-registry-secret \
  8. --docker-server=us.icr.io \
  9. --docker-username=iamapikey \
  10. --docker-password=<API_Key> \
  11. --docker-email=null

8. Run a job on Kubernetes

Next change directory to the job directory, and open up the job.yml. You should see the docker_cobol and you need to edit it to your namespace. After this you will want to apply your batch job.

  1. $ cd job/
  2. $ nano job.yml # remember to replace docker_cobol by the namespace previously created
  3. $ kubectl apply -f job.yml
  4. job.batch/cobol-docker-job created

Finally to verify your deployment you will want to run the following to see the Hello World!. The easiest way would be to run the following commands to see the pod Completed then run the logs command to verify that it has the out put we are expecting.

  1. $ kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. cobol-docker-job-mlggk 0/1 Completed 0 40s
  4. $ kubectl logs cobol-docker-job-mlggk
  5. Hello World!

Troubleshooting

I had an error message saying that it is unable to find resource ‘us.icr.io/docker_cobol/hello_world’

Make sure you replace “docker_cobol” by your own namespace as discussed in the section Create your namespace.
If the error persists, it may be related to version divergence (e.g.Unable to find image 'us.icr.io/docker_cobol/hello_world:latest'). In the last case, try to explicitly mention “:v1” after the resource name:

  1. $ docker run us.icr.io/docker_cobol/hello_world:v1

License

This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.

Apache License FAQ