how to show swagger ui for kubernetes api?

7/24/2020

I installed kubernetes use kubeadm and the kube-apiserver started successfully. My computer's operating system is Centos 7. Command like these:

kube-apiserver
      --advertise-address=10.211.55.9
      --allow-privileged=true
      --authorization-mode=Node,RBAC
      --client-ca-file=/etc/kubernetes/pki/ca.crt
      --enable-admission-plugins=NodeRestriction
      --enable-bootstrap-token-auth=true
      --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
      --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
      --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
      --etcd-servers=https://127.0.0.1:2379
      --insecure-port=0
      --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt
      --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key
      --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
      --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt
      --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key
      --requestheader-allowed-names=front-proxy-client
      --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
      --requestheader-extra-headers-prefix=X-Remote-Extra-
      --requestheader-group-headers=X-Remote-Group
      --requestheader-username-headers=X-Remote-User
      --secure-port=6443
      --service-account-key-file=/etc/kubernetes/pki/sa.pub
      --service-cluster-ip-range=10.96.0.0/12
      --tls-cert-file=/etc/kubernetes/pki/apiserver.crt
      --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
      --enable-swagger-ui=true
      --insecure-bind-address=0.0.0.0
      --insecure-port=8080

open web browser and search http://localhost:8080/swagger-ui/, I got the uri list without swagger ui. How to show swagger ui?

http://localhost:8080/swagger-ui/
{
  "paths": [
    "/apis",
    "/apis/",
    "/apis/apiextensions.k8s.io",
    "/apis/apiextensions.k8s.io/v1",
    "/apis/apiextensions.k8s.io/v1beta1",
    "/healthz",
    "/healthz/etcd",
    "/healthz/log",
    "/healthz/ping",
    "/healthz/poststarthook/crd-informer-synced",
    "/healthz/poststarthook/generic-apiserver-start-informers",
    "/healthz/poststarthook/start-apiextensions-controllers",
    "/healthz/poststarthook/start-apiextensions-informers",
    "/livez",
    "/livez/etcd",
    "/livez/log",
    "/livez/ping",
    "/livez/poststarthook/crd-informer-synced",
    "/livez/poststarthook/generic-apiserver-start-informers",
    "/livez/poststarthook/start-apiextensions-controllers",
    "/livez/poststarthook/start-apiextensions-informers",
    "/metrics",
    "/openapi/v2",
    "/readyz",
    "/readyz/etcd",
    "/readyz/log",
    "/readyz/ping",
    "/readyz/poststarthook/crd-informer-synced",
    "/readyz/poststarthook/generic-apiserver-start-informers",
    "/readyz/poststarthook/start-apiextensions-controllers",
    "/readyz/poststarthook/start-apiextensions-informers",
    "/readyz/shutdown",
    "/version"
  ]
}
-- double sun
kubernetes
swagger-ui

3 Answers

7/24/2020

The Kubernetes API server serves an OpenAPI spec via the /openapi/v2 endpoint. So you can use http://localhost:8080/openapi/v2

-- Arghya Sadhu
Source: StackOverflow

4/1/2022

The easiest way to get the openapi json is:

kubectl get --raw /openapi/v2  > k8s-openapi-v2.json

Now you can start a swagger-ui instance for that json like so:

docker run \
  -v $PWD/k8s-openapi-v2.json:/app/swagger.json \
  -p 8081:8080 \
  swaggerapi/swagger-ui

Navigate to https://localhost:8081
You'll see the swagger UI. It's pretty laggy though, because of the many APIs a Kubernetes cluster exposes.

-- stealthybox
Source: StackOverflow

11/19/2021

Open a reverse proxy to your Kubernetes API server:

kubectl proxy --port=8080

Save the Swagger file. Can't link directly due to CORS policy.

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

Spin up a docker container with Swagger UI

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

Reference: https://jonnylangefeld.com/blog/kubernetes-how-to-view-swagger-ui


In case you would like to unleash the full potential of Swagger, this guide might help you: https://github.com/olivernadj/Kubernetes-REST-API-feat-Swagger-UI

-- oliver nadj
Source: StackOverflow