Kubernetes deployment code

The following code will generate a Docker image with the Kubernetes deployment file in YAML format. It is very well organized by the Ballerina source code folder tree structure, referenced here:

// --------------------------------------
@kubernetes:Deployment {
image: "demo/ballerina-packt-image",
name: "ballerina-packt-image"
}

// secretes.toml is file in which we defined our secure or other variables
@kubernetes:ConfigMap {
ballerinaConf: "secretes.toml"
}
@http:ServiceConfig {
basePath: "/"
}
// --------------------------------------

//Following is just same code that we given in above twitter example to give you reference about where you can place your deployment code
service<http:Service> hello bind listener {
@http:ResourceConfig {
path: "/",
methods: ["POST"]
}
hi (endpoint caller, http:Request request, Person person) {
string body = check request.getTextPayload();
var status = check tweeter ->tweet("hello" + body + "PacktPub #ballerina example" );
int id = status.id;
string createdAt = status.CreatedAt;
json js = {
twitterID: id,
createdAt: createdAt,
key: "value"
};

}

In the preceding code block, the Deployment and ConfigMap lines will convert your code into easily deployable code on a Kubernetes cluster. After writing those lines, you should be in a position to run the rest of the commands.

If you build and compile your newly modified code, you will see some Kubernetes related messages. It will also generate a Kubernetes folder structure in which you can find your deployment configuration in YAML. This can be directly deployed on Kubernetes or on an OpenShift cluster by using the kubctl command on the generated ConfigMap files under the Kubernetes folder. An example of this is as follows:

@kubernetes:ConfigMap - complete 1/1 - Referring that it has successfully generated Kubernetes config map files
$ ballerina build packt-twitter-example.bal
@kubernetes:Service                      - complete 1/1
@kubernetes:ConfigMap                    - complete 1/1
@kubernetes:Docker                       - complete 3/3
@kubernetes:Deployment                   - complete 1/1

The following tree command shows the file and folder structure generated after the build command:

$ tree
.
├── packt-twitter-example.bal
├── packt-twitter-example.balx
├── kubernetes
│   ├── packt-twitter-example_config_map.yaml
│   ├── packt-twitter-example_deployment.yaml
│   ├── packt-twitter-example_svc.yaml
│   └── docker
│       └── Dockerfile
└── secretes.toml

Once you run kubectl apply  on the generated configuration map YAML file or on the folder structure, it will deploy your Twitter code as a running pod on which you can redirect network traffic:


# Following kubectl command will deploy YAML configs on your kubernetes cluster

$ kubectl apply -f kubernetes/ configmap "packt-twitter-example_config_map" created deployment "packt-twitter-example" created service "packt-twitter-example" created

#Run following commands to see your deployment status

$ kubectl get pods
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset