WSO2 Microgateway on K3S
This has been a very frequent question for most of the developers and architects who are evaluating WSO2 API Microgateway for their solution implementation. First of all the answer to that frequent question is YES, microgateway can be deployed in k3s.
K3s is a lightweight, high available certified kubernetes distribution. Its a small (<40MB) distribution with less dependencies and simplified steps to install. In this tutorial we will see how we can deploy microgateway 3.1.0 in k3s.
Install API Microgateway Toolkit
- Lets download the microgateway toolkit from here
- Extract the downloaded binary zip file to your preferred location. Let’s call this extracted location $MGW_TOOLKIT_HOME.
- Set the bin folder of the toolkit to the path variable
export PATH=$MGW_TOOLKIT_HOME/bin:$PATH
Create MicroGateway Project and generate kubernetes artifacts
- First let’s create a microgateway project with pet store open API definition, which we will be exposed via microgateway. This will create a directory with name “petstore” which will be referred as $PROJECT_HOME
micro-gw init petstore -a https://petstore.swagger.io/v2/swagger.json
2. Then use the following deployment-config.toml file in the build process of the microgateway project
[kubernetes]
[kubernetes.kubernetesDeployment]
enable = true
name = 'petstore'
tag = 'v1'
replicas = '2'
buildImage = true
push = 'true'
imagePullPolicy = 'Always'
registry = 'index.docker.io/<$DOCKER_USERNAME>'
baseImage = 'wso2/wso2micro-gw:latest'
[kubernetes.kubernetesDeployment.livenessProbe]
enable = true
initialDelaySeconds = '20'
periodSeconds = '20'
[kubernetes.kubernetesDeployment.readinessProbe]
enable = true
initialDelaySeconds = '30'
periodSeconds = '30'
[kubernetes.kubernetesServiceHttps]
enable = true
name = 'httpsService'
serviceType = 'NodePort'
[kubernetes.kubernetesServiceHttp]
enable = true
name = 'httpService'
serviceType = 'NodePort'
[kubernetes.kubernetesConfigMap]
enable = true
ballerinaConf = '$MGW_TOOLKIT_HOME/resources/conf/micro-gw.conf'
Replace the <$DOCKER_USERNAME> with the username of your docker hub account and $MGW_TOOLKIT_HOME with the toolkit extracted location. This will build a microgateway docker image and push it to the docker hub, so when deploying in k3s it can pull the image from docker hub. In order to push to docker hub, we need to set the docker hub credentials in the current terminal as environment variables. Let’s set them as below.
export DOCKER_USERNAME=<docker_username>
export DOCKER_PASSWORD=<docker_password>
3. Now, lets build the microgateway project providing the above created deployment-config.toml
micro-gw build petstore -d <PATH_TO_deployment-config.toml>
This will automatically generate the kubernetes artifacts which we can use to deploy the microgateway in k3. The kubernetes artifacts will be created inside the folder $PROJECT_HOME/target/gen/target/kubernetes/petstore directory.
Install K3S
- If you are not using linux based OS, we will need an linux based OS. Because k3s currently support linux only. For this purpose we can use multipass to create instant ubuntu VMs. After installing mutlipass let’s spin up an ubuntu image with the below command.
multipass launch --name k3s --mem 3G --disk 20G --cpus 2 -v
This will create an ubuntu image with name “k3s” with 3GB memory and 2 cores. “multipass list” command will list down the installed ubuntu VMs. If you are using linux based OS then this step is not required.
2. Next lets go inside the VM and install the k3s on the VM
multipass shell k3s // Goes to the shell of VMcurl -sfL https://get.k3s.io | sh - // install the k3s on VM
Deploy Microgateway in K3S
- First let’s copy the generated kubernetes artifacts to the ubuntu VM, in which the k3s cluster is installed.
multipass copy-files $PROJECT_HOME/target/gen/target/kubernetes/petstore/petstore.yaml k3s:/home/multipass/
2. Then launch VM shell and apply the copied kubernetes artifacts to the kubernetes cluster
multipass shell k3ssudo kubectl apply -f /home/multipass/petstore.yaml
This will deploy two pods of microgateways , and two kubernetes services in k3s cluster. Now the microgateways are up and running in k3s.
Invoke the APIs
- Since the microgateways are exposed using the node port service type in kubernetes, let’s find out the target port of the https port.
sudo kubectl get svc | grep httpsservice
Get the target port(<TARGET_PORT>, this port will be around 30000) from the above command output.
2. Get an API key from microgateway to invoke the API.
TOKEN=$(curl -X get "https://localhost:<TARGET_PORT>/apikey" -H "Authorization:Basic YWRtaW46YWRtaW4=" -k)
This will set the api ket retrieved from microgateway to console.
3. Invoke the API.
curl -X GET "https://localhost:<TARGET_PORT>/v2/pet/1" -H "accept: application/xml" -H "api_key:$TOKEN" -k
Now we have successfully deployed the microgateway in k3s and able to invoke the APIs as well.
If you interested in asking questions and learn about microgateway, please join our slack channel https://apim-slack.wso2.com and also if you like what microgateway offers, please star the github repo https://github.com/wso2/product-microgateway/