@@ -352,102 +352,6 @@ exampleWithKubeConfig = do
352
352
>>= print
353
353
```
354
354
355
+ ## {{% heading "whatsnext" %}}
355
356
356
- ### Accessing the API from within a Pod
357
-
358
- When accessing the API from within a Pod, locating and authenticating
359
- to the API server are slightly different to the external client case described above.
360
-
361
- The easiest way to use the Kubernetes API from a Pod is to use
362
- one of the official [ client libraries] ( /docs/reference/using-api/client-libraries/ ) . These
363
- libraries can automatically discover the API server and authenticate.
364
-
365
- #### Using Official Client Libraries
366
-
367
- From within a Pod, the recommended ways to connect to the Kubernetes API are:
368
-
369
- - For a Go client, use the official [ Go client library] ( https://github.com/kubernetes/client-go/ ) .
370
- The ` rest.InClusterConfig() ` function handles API host discovery and authentication automatically.
371
- See [ an example here] ( https://git.k8s.io/client-go/examples/in-cluster-client-configuration/main.go ) .
372
-
373
- - For a Python client, use the official [ Python client library] ( https://github.com/kubernetes-client/python/ ) .
374
- The ` config.load_incluster_config() ` function handles API host discovery and authentication automatically.
375
- See [ an example here] ( https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py ) .
376
-
377
- - There are a number of other libraries available, please refer to the [ Client Libraries] ( /docs/reference/using-api/client-libraries/ ) page.
378
-
379
- In each case, the service account credentials of the Pod are used to communicate
380
- securely with the API server.
381
-
382
- #### Directly accessing the REST API
383
-
384
- While running in a Pod, the Kubernetes apiserver is accessible via a Service named
385
- ` kubernetes ` in the ` default ` namespace. Therefore, Pods can use the
386
- ` kubernetes.default.svc ` hostname to query the API server. Official client libraries
387
- do this automatically.
388
-
389
- The recommended way to authenticate to the API server is with a
390
- [ service account] ( /docs/tasks/configure-pod-container/configure-service-account/ ) credential. By default, a Pod
391
- is associated with a service account, and a credential (token) for that
392
- service account is placed into the filesystem tree of each container in that Pod,
393
- at ` /var/run/secrets/kubernetes.io/serviceaccount/token ` .
394
-
395
- If available, a certificate bundle is placed into the filesystem tree of each
396
- container at ` /var/run/secrets/kubernetes.io/serviceaccount/ca.crt ` , and should be
397
- used to verify the serving certificate of the API server.
398
-
399
- Finally, the default namespace to be used for namespaced API operations is placed in a file
400
- at ` /var/run/secrets/kubernetes.io/serviceaccount/namespace ` in each container.
401
-
402
- #### Using kubectl proxy
403
-
404
- If you would like to query the API without an official client library, you can run ` kubectl proxy `
405
- as the [ command] ( /docs/tasks/inject-data-application/define-command-argument-container/ )
406
- of a new sidecar container in the Pod. This way, ` kubectl proxy ` will authenticate
407
- to the API and expose it on the ` localhost ` interface of the Pod, so that other containers
408
- in the Pod can use it directly.
409
-
410
- #### Without using a proxy
411
-
412
- It is possible to avoid using the kubectl proxy by passing the authentication token
413
- directly to the API server. The internal certificate secures the connection.
414
-
415
- ``` shell
416
- # Point to the internal API server hostname
417
- APISERVER=https://kubernetes.default.svc
418
-
419
- # Path to ServiceAccount token
420
- SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount
421
-
422
- # Read this Pod's namespace
423
- NAMESPACE=$( cat ${SERVICEACCOUNT} /namespace)
424
-
425
- # Read the ServiceAccount bearer token
426
- TOKEN=$( cat ${SERVICEACCOUNT} /token)
427
-
428
- # Reference the internal certificate authority (CA)
429
- CACERT=${SERVICEACCOUNT} /ca.crt
430
-
431
- # Explore the API with TOKEN
432
- curl --cacert ${CACERT} --header " Authorization: Bearer ${TOKEN} " -X GET ${APISERVER} /api
433
- ```
434
-
435
- The output will be similar to this:
436
-
437
- ``` json
438
- {
439
- "kind" : " APIVersions" ,
440
- "versions" : [
441
- " v1"
442
- ],
443
- "serverAddressByClientCIDRs" : [
444
- {
445
- "clientCIDR" : " 0.0.0.0/0" ,
446
- "serverAddress" : " 10.0.1.149:443"
447
- }
448
- ]
449
- }
450
- ```
451
-
452
-
453
-
357
+ * [ Accessing the Kubernetes API from a Pod] ( /docs/tasks/run-application/access-api-from-pod/ )
0 commit comments