Page History
...
Follow the
...
Create an API with the following details:
...
/hello/v1
...
instructions below to deploy WSO2 API Microgateway in Kubernetes.
Table of Contents |
---|
Step 1 - Follow the installation prerequisites
Make sure to do the following:
- Carry out the installation prerequisites for WSO2 API Microgateway Toolkit.
- Install and setup kubectl in your client machine.
- Setup a Kubernetes cluster.
- Make sure that kubectl points to your Kubernetes cluster.
Step 2 - Create a Microgateway project
- Navigate to a preferred workspace folder using the command line to set the location that is used to store the Microgateway project.
Create a project.
Let's create a project namedk8s_project
by running the following command. This will create the folder structure for the artifacts to be included.Code Block title Format micro-gw init <project_name>
Code Block title Example micro-gw init k8s_project Project 'k8s_project' is initialized successfully.
Step 3 - Build the Microgateway project
Add the API to the project.
Navigate to the
/petstore/api_definitions
directory and add the OpenAPI definition(s) to this directory. Let's use the Petstore sample OpenAPI definition in this scenario.Info The latter mentioned instructions uses the developer first approach. However, if you wish to work with APIs that you have published in WSO2 API Manager, you can either import a single API or you can import the APIs as a group.
Create the input for WSO2 API Microgateway Toolkit.
Create adeployment.toml
file enabling Kubernetes deployment, service and config map resources. The config map is used to copy themicro-gw.conf
file.that you will use as the input when creating the microgateway project. This TOML file should contain the relevant deployment configurations as shown below. For more information on each of the above parameters, see deployment.toml for Kubernetes.Code Block [kubernetes] [kubernetes.kubernetesDeployment] enable = true #name = '' #labels = '' #replicas = '' #enableLiveness = '' #initialDelaySeconds = '' #periodSeconds = '' #livenessPort = '' #imagePullPolicy = '' #image = '' #env = '' #buildImage = '' #copyFiles = '' #dockerHost = '' #dockerCertPath = '' #push = '' #username = '' #password = '' #baseImage = '' #singleYAML = '' [kubernetes.kubernetesService] enable = true #name = '' #labels = '' serviceType = 'NodePort' #port = '' [kubernetes.kubernetesConfigMap] enable = true ballerinaConf = '<MICROGW_TOOLKIT_HOME>/resources/conf/micro-gw.conf' #[[kubernetes.kubernetesConfigMap.configMaps]] # name = '' # mountPath = '' # readOnly = false # data = ['']
Info Specifytitle deployment-config.toml Note Make sure to specify the complete path for the
ballerinaConf
, by replacing the<MICROGW_TOOLKIT_HOME>
placeholder with
the
full
path.
exExample:
home/users/wso2am-micro-gw-toolkit-3.x.x/resources/conf/micro-gw.conf
Let’s create a project called
k8s_project
and provide thedeployment.toml
file as an input.Expand title Click here to see the description for each parameter... Table of Contents maxLevel 4 minLevel 4 kubernetes.kubernetesDeployment
Parameter Description Default value name Name of the deployment. <outputfilename>-deployment namespace Namespace of the deployment. null labels The labels related to the particular deployment. "app: <outputfilename>" replicas The number of replicas. 1 dependsOn The endpoints that the deployment depends on. null enableLiveness Enable or disable the liveness probe. false initialDelaySeconds The initial delay in seconds before performing the first probe. 10s periodSeconds The time interval between liveness probes in seconds. 5s livenessPort The port checked by the liveness probe. <ServicePort> imagePullPolicy Docker image pull policy. IfNotPresent image Docker image with tag. <output file name>:latest env List of environment variables. null buildImage Enable or disable building the Docker image. true copyFiles Copy external files for the Docker image. null dockerHost Docker host IP and docker PORT.
E.g. tcp://192.168.99.100:2376null dockerCertPath Docker cert path. null push Enable or disable pushing the Docker image to the registry.
This can only be enabled ifbuildImage
parameter is set to true.false username Username for the docker registry. null password Password for the docker registry. null baseImage Base image to create the docker image. ballerina/ballerina:latest imagePullSecrets Image pull secrets value. null singleYAML Enable or disable generating a single yaml file for all k8s resources. false kubernetes.kubernetesService
Parameter Description Default value name Name of the Service. <service name>-service labels The labels for the service. "app: <outputfilename>" serviceType The type of service. ClusterIP port The port used by the service. Port number kubernetes.kubernetesIngress
Parameter
Description
Default value
name
Name of the Ingress.
<service name>-ingress
labels
The labels for the service.
"app: <outputfilename>"
hostname
Host name of the Ingress.
<service name>.com
annotations
Map of additional annotations.
null
path
Resource path.
/
targetPath
This URL will be used to rewrite.
null
ingressClass
Ingress class.
nginx
enableTLS
Enable or disable Ingress TLS.
false
kubernetes.kubernetesHPA
Parameter
Description
Default value
name Name of the Horizontal Pod Autoscaler <service name>-hpa labels The labels for the service. "app: <outputfilename>" minReplicas The minimum number of replicas in the deployment. No of replicas in deployment maxReplicas The maximum number of replicas in the deployment. minReplicas+1 cpuPrecentage CPU percentage to start scaling. 50 kubernetes.kubernetesSecret
Parameter
Description
Default value
name Name of the secret mount. <service_name>-secret mountPath Path to the mount on the container. null readOnly Denotes whether the mount is read-only or not. true data Paths to the data files. null kubernetes.kubernetesConfigMap
Parameter
Description
Default value
name Name of the config map mount <service_name>-config-map mountPath Path to the mount on the container null readOnly Denotes whether the mount is read-only or not. true ballerinaConf Location of the ballerina conf file. null data Paths to the data files. null kubernetes.kubernetesPersistentVolumeClaim
Parameter
Description
Default value
name Name of the secret mount. null annotations The metadata annotations in the map. null mountPath Path to mount on the container. null readOnly Denotes whether the mount is read-only or not. false accessMode Access mode. ReadWriteOnce volumeClaimSize Size of the volume claim. null kubernetes.kubernetesJob
Parameter Description Default value name Name of the job. <output file name>-job namespace Namespace for the job. default labels The labels for the job. "app: <outputfilename>" restartPolicy Restart policy for the job. Never backoffLimit Backoff limit for the job. 3 activeDeadlineSeconds Active deadline given in seconds. 20 schedule Schedule for cron jobs. none imagePullPolicy Docker image pull policy. IfNotPresent image Docker image with tag. <output file name>:latest env List of environment variables. null buildImage Enable or disable building the Docker image. true dockerHost Docker host IP and docker PORT.
E.g.tcp://192.168.99.100:2376
null dockerCertPath Docker cert path. null push Enable or disable pushing the Docker image to the registry.
This can only be enabled ifbuildImage
parameter is set to true.false username Username for the docker registry. null password Password for the docker registry. null baseImage Base image to create the docker image. ballerina/ballerina:latest Lets create a microgateway project/
- Navigate to a preferred workspace folder using the command line. (This location is used to run the Microgateway commands and to generate Microgateway artifacts.)
Create a project using the command given below,(We need to add the <MGW_TOOLKIT_HOME>/bin folder to PATH variable in order to run the command from any location)
Code Block micro-gw init k8s_project
Now lets add the API(open API definition) to the project. Navigate to the
/k8s_project/api_definitions
directory. Add the API definition(s) to this
directory. A sample open API definition can be found here.Build the project using the following command,
codeBuild the microgateway project.
Use your command line tool to navigate to where the project directory (
k8s_project
) was created and execute the following command to build the project.Code Block title Format micro-gw build <project_name> --deployment-config deployment.toml
Code Block title Example micro-gw build k8s_project --deployment-config deployment.toml
This generates the following Kubernetes resources.
├── k8s_project
│ └── docker
│ └── Dockerfile
├── k8s_project_config_map.yaml
├── k8s_project_deployment.yaml
└── k8s_project_svc.yaml
The docker Docker image to be deployed in Kubernetes is created in your local registry. You can find the image
k8s_project:latest
when you execute the docker Docker images command.Deploy the dockerStep 4 - Deploy the Docker image in a Kubernetes environment
.
You can also SCP the image to the Kubernetes nodes as follows:Info You can push the docker image to the docker hub or to a private docker registry. If you change the docker image name, you need to change the image name in the k8s_project_deployment.yaml
file.Save the docker image to a
tar
file. For example,Code Block Let's SCP the image to the Kubernetes nodes to deploy the Docker image in a K8s environment.Info For more options on deploying the Docker image in a Kubernetes environment, see the Deployment related FAQs.
Save the Docker image to a
tar
file.Code Block title Format docker save <MGW-project-name>:latest > <Docker-image-name>.tar
Code Block title Example docker save k8s_project:latest > image.tar
SCP the image to the Kubernetes nodes. For example,
Code Block title Format scp -i <identity -file> image.tar [email protected][email protected]<K8s_NODE_IP:IP>:
Identity file
- This refers to the public key of the Kubernetes node. For example you can get a google_compute_engine.pub for GCE. You have toscp
the Docker image for each and every Kubernetes node.Note When using minikube, the username is
docker
and you can find the IP address by using theminikube ip
command.
Load the docker Docker image in the Kubernetes nodes. This needs to be executed
You need to execute the following command in the Kubernetes nodes. For example,Code Block Code Block title Format docker load < [Docker-image-name].tar
Code Block title Example docker load < image.tar
Step 5 - Start the Kubernetes cluster
Deploy
...
WSO2 API Microgateway in Kubernetes by deploying the Kubernetes resources
...
(artifacts) and starting WSO2 API Microgateway.
Code Block | ||
---|---|---|
| ||
kubectl create -f <MGW-project-name>/target/kubernetes/ |
Code Block | ||
---|---|---|
| ||
kubectl create -f k8s_project/target/kubernetes/ |
Access the API in HTTPS using the following details:
Info |
---|
The
You can use the |
Node port - 32616
...
Step 6 - Invoke the sample API
Step 6.1 - Obtain token
After the APIs are exposed via WSO2 API Microgateway, which you deployed in a Kubernetes cluster, you can invoke an API with a valid JWT token or an opaque access token.
In order to use JWT tokens, WSO2 API Microgateway should be presented with a JWT signed by a trusted OAuth2 service.
Let's use the following sample JWT token that never expires, which was generated using WSO2 API Manager for testing purposes.
Code Block | ||
---|---|---|
| ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UQXhabU14TkRNeVpEZzNNVFUxWkdNME16RXpPREpoWldJNE5ETmxaRFUxT0dGa05qRmlNUSJ9.eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV3YXkiLCJzdWIiOiJhZG1pbiIsImFwcGxpY2F0aW9uIjp7ImlkIjoyLCJuYW1lIjoiSldUX0FQUCIsInRpZXIiOiJVbmxpbWl0ZWQiLCJvd25lciI6ImFkbWluIn0sInNjb3BlIjoiYW1fYXBwbGljYXRpb25fc2NvcGUgZGVmYXVsdCIsImlzcyI6Imh0dHBzOlwvXC9sb2NhbGhvc3Q6OTQ0M1wvb2F1dGgyXC90b2tlbiIsImtleXR5cGUiOiJQUk9EVUNUSU9OIiwic3Vic2NyaWJlZEFQSXMiOltdLCJjb25zdW1lcktleSI6Ilg5TGJ1bm9oODNLcDhLUFAxbFNfcXF5QnRjY2EiLCJleHAiOjM3MDMzOTIzNTMsImlhdCI6MTU1NTkwODcwNjk2MSwianRpIjoiMjI0MTMxYzQtM2Q2MS00MjZkLTgyNzktOWYyYzg5MWI4MmEzIn0=.b_0E0ohoWpmX5C-M1fSYTkT9X4FN--_n7-bEdhC3YoEEk6v8So6gVsTe3gxC0VjdkwVyNPSFX6FFvJavsUvzTkq528mserS3ch-TFLYiquuzeaKAPrnsFMh0Hop6CFMOOiYGInWKSKPgI-VOBtKb1pJLEa3HvIxT-69X9CyAkwajJVssmo0rvn95IJLoiNiqzH8r7PRRgV_iu305WAT3cymtejVWH9dhaXqENwu879EVNFF9udMRlG4l57qa2AaeyrEguAyVtibAsO0Hd-DFy5MW14S6XSkZsis8aHHYBlcBhpy2RqcP51xRog12zOb-WcROy6uvhuCsv-hje_41WQ== |
Info | ||||
---|---|---|---|---|
| ||||
For more information, see the FAQs on Working with Tokens. |
Step 6.2 - Invoke the API
Invoke the API using the JWT token using the following command.
...
Execute the command below to set a self-contained OAuth2.0 access token in the JWT format as a variable on your terminal session.
Code Block title Format TOKEN=<JWT-token>
Code Block title Example TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UQXhabU14TkRNeVpEZzNNVFUxWkdNME16RXpPREpoWldJNE5ETmxaRFUxT0dGa05qRmlNUSJ9.eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV3YXkiLCJzdWIiOiJhZG1pbiIsImFwcGxpY2F0aW9uIjp7ImlkIjoyLCJuYW1lIjoiSldUX0FQUCIsInRpZXIiOiJVbmxpbWl0ZWQiLCJvd25lciI6ImFkbWluIn0sInNjb3BlIjoiYW1fYXBwbGljYXRpb25fc2NvcGUgZGVmYXVsdCIsImlzcyI6Imh0dHBzOlwvXC9sb2NhbGhvc3Q6OTQ0M1wvb2F1dGgyXC90b2tlbiIsImtleXR5cGUiOiJQUk9EVUNUSU9OIiwic3Vic2NyaWJlZEFQSXMiOltdLCJjb25zdW1lcktleSI6Ilg5TGJ1bm9oODNLcDhLUFAxbFNfcXF5QnRjY2EiLCJleHAiOjM3MDMzOTIzNTMsImlhdCI6MTU1NTkwODcwNjk2MSwianRpIjoiMjI0MTMxYzQtM2Q2MS00MjZkLTgyNzktOWYyYzg5MWI4MmEzIn0=.b_0E0ohoWpmX5C-M1fSYTkT9X4FN--_n7-bEdhC3YoEEk6v8So6gVsTe3gxC0VjdkwVyNPSFX6FFvJavsUvzTkq528mserS3ch-TFLYiquuzeaKAPrnsFMh0Hop6CFMOOiYGInWKSKPgI-VOBtKb1pJLEa3HvIxT-69X9CyAkwajJVssmo0rvn95IJLoiNiqzH8r7PRRgV_iu305WAT3cymtejVWH9dhaXqENwu879EVNFF9udMRlG4l57qa2AaeyrEguAyVtibAsO0Hd-DFy5MW14S6XSkZsis8aHHYBlcBhpy2RqcP51xRog12zOb-WcROy6uvhuCsv-hje_41WQ==
Note When you are deploying the Microgateway in production, make sure to change its default certificates.
You can now invoke the API running on the Microgateway using the following cURL command.
32616Code Block title Format curl -X GET "<Any_Kubernetes_Node_IP>:
hello/v1/check<NodePort>/
Headers -
Authorization Bearer <JWT_TOKEN>
Method -
GET
...
As JWT is a self-contained access token, the Microgateway does not need to connect to the Key Manager. However, if you are using an Oauth2 access token, point the Microgateway to the Key Manager using the Key Manager details in the micro-gw.conf
configuration file of the Microgateway. If you are running Key Manager in Kubernetes, you can provide the Key Manager serverUrl
as shown below. The serverUrl
has to be accessible from the Microgateway pods.
<API-context>/<API-resource>" -H "accept: application/xml" -H "Authorization:Bearer <JWT_TOKEN>" -k
Info As this example uses
NodePort
as the service typein Kubernetes, you can access the API using any of the Kubernetes node IP addresses and node ports when using the latter mentioned service type.
In addition, you can use thekubectl get svc
command to list down the services that run on Kubernetes in order to get the port. However, to identify an external IP of the Kubernetes cluster you can use the command
kubectl get nodes -o yaml | grep -B 1 "type: ExternalIP"
If you use minukube as the Kuberneterse cluster, then you can find the external IP by using theminikube ip
command.Code Block title Examples curl -X GET "https://localhost:32616/petstore/v1/pet/findByStatus?status=available" -H "accept: application/xml" -H "Authorization:Bearer $TOKEN" -k curl -X GET "https://localhost:32616/petstore/v1/pet/1" -H "accept: application/xml" -H "Authorization:Bearer $TOKEN" -k
In the above example, localhost is used as the Kubernetes node IP, because it was based on Kubenetes that was provided by Docker for Mac.