skip to Main Content

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"
  ]
}

4

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. Here is a quick way to play with swagger and k8s api.

    Do not apply in prod.

    1. wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
    2. k3d cluster create -p "80:80@loadbalancer" -p "443:443@loadbalancer"
    3. kubectl apply -f tools/swagger/swagger.yml
    4. kubectl create token new-admin-sa
    5. replace token in tools/swagger/swagger.yml
    6. kubectl apply -f tools/swagger/swagger.yml
    7. echo "127.0.0.1 kubernetes" >> /etc/hosts
    8. echo "127.0.0.1 swagger-ui" >> /etc/hosts
    

    cat tools/swagger/swagger.yml

    apiVersion: traefik.containo.us/v1alpha1
    kind: ServersTransport
    metadata:
      name: mytransport
    spec:
      serverName: kubernetes
      insecureSkipVerify: true
    ---
    apiVersion: traefik.containo.us/v1alpha1
    kind: IngressRoute
    metadata:
      name: testroute
    spec:
      entryPoints:
        - websecure
      routes:
        - match: Host(`kubernetes`)
          kind: Rule
          services:
            - kind: Service
              name: kubernetes
              port: 443
              serversTransport: mytransport
          middlewares:
            - name: test-header
        - match: Host(`swagger-ui`)
          kind: Rule
          services:
            - kind: Service
              name: swagger-ui-service
              port: 8080
          middlewares:
            - name: test-header
    
    ---
    apiVersion: traefik.containo.us/v1alpha1
    kind: Middleware
    metadata:
      name: test-header
    spec:
      headers:
        customRequestHeaders:
          Authorization: "Bearer PASTE_YOUR_TOKEN_HERE"
        accessControlAllowMethods:
          - "GET"
          - "OPTIONS"
          - "PUT"
        accessControlAllowOriginList:
          - "https://kubernetes"
          - "https://swagger-ui"
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: swagger-ui
      labels:
        app: swagger-ui
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: swagger-ui
      template:
        metadata:
          labels:
            app: swagger-ui
        spec:
          containers:
            - name: swagger-ui
              image: swaggerapi/swagger-ui
              ports:
                - containerPort: 8080
              env:
                - name: URL
                  value: https://kubernetes/openapi/v2
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: swagger-ui-service # a random name
    spec:
      selector:  # to connect to Pod through label
        app: swagger-ui
      ports:
        - port: 8080  # service port
          protocol: TCP
          targetPort: 8080  # container port
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: new-admin-sa
      namespace: default
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: new-admin-crb
    subjects:
      - kind: ServiceAccount
        name: new-admin-sa
        namespace: default
    roleRef:
      kind: ClusterRole
      name: cluster-admin
      apiGroup: rbac.authorization.k8s.io
    

    Enjoy https://swagger-ui/

    UPD:
    if you see Failed to load API definition, open https://kubernetes/, click on Proceed to kubernetes (unsafe) and try again https://swagger-ui/.
    Otherwise make sure you follow the steps in order!

    Cleaning: k3d cluster delete

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search