1

At work, you may need to develop based on the API provided by Kubernetes, such as developing a console that suits you. There are two problems with the official API documentation provided by Kubernetes:

  1. It’s very unintuitive. This problem is actually okay. It’s not a big problem to overcome it by yourself.
  2. There is only K8S api-server interface, this is difficult to do. If you have a new CRD resource, such as kubevirt, you have to go to kubevirt's api document. The key is that you can’t find it.

Today, Brother Ming will introduce you a tool that can help you solve this problem.

This tool is called swagger-ui, which can present the http interface documents exposed by k8s to you as a UI interface. You can even debug it and generate curl request commands.

1. How to deploy swagger-ui

Open two ssh connections to your K8S cluster.

Enter the first window, execute the following command to create a reverse proxy

kubectl proxy --port=8080

Enter the second window, execute the following command to establish and obtain the api document information of k8s-swagger.json , and output it to a 06181d813a8b19 file

curl localhost:8080/openapi/v2 > k8s-swagger.json

After getting it, the first window can be closed.

Then choose a window, execute the following command to run a container

docker run \
    --rm \
    -d \
    -p 80:8080 \
    -e SWAGGER_JSON=/k8s-swagger.json \
    -v $(pwd)/k8s-swagger.json:/k8s-swagger.json \
    swaggerapi/swagger-ui

2. Use swagger-ui

At this point, you can enter http://ip on the browser to see a visualized api document interface, which includes the model and path of all custom resources installed on the cluster!

There are a lot of APIs in it, and those you can use and those you don’t need are available here.
Not only the api-server API, but also all other CRD resources that you install separately.

For example, I installed the VirtualMachineInstance resource. According to the keyword, I immediately found the API for KubeVirt to create a virtual machine.

Click Try it out above to modify the parameters of the request body. Please generate a curl command to facilitate your debugging.

Talk a bit

I have written a lot of Python-related articles on SegmentFault, including Python utility tools, Python high-efficiency skills, and PyCharm usage skills. I am very happy to get the recognition and support of many friends who know.

With their encouragement, I sorted the past articles into three PDF e-books

PyCharm Chinese Guide

"PyCharm Chinese Guide" uses more than 300 GIF dynamic images to explain in detail 105 PyCharm efficient use skills that are most suitable for actual development. The content is easy to understand and suitable for all Python developers.

Online experience address: https://pycharm.iswbm.com

Python Black Magic Guide

"Python Black Magic Guide" currently ushered in the v3.0 version, which contains more than 100 development tips, which is very suitable for reading fragments in leisure time.

Online experience address: https://magic.iswbm.com

Python Chinese Guide

The best learning material for learning Python is always the official Python documentation. Unfortunately, most of the official documentation is now in English. Although there is a Chinese translation, the progress is really worrying. In order to take care of classmates who are not good at English, I wrote an online Python document for friends with zero foundation - "Python Chinese Guide"

Online experience address: https://python.iswbm.com

If it helps, remember to a thumbs up~


Python编程时光
135 声望26 粉丝