copypastegineer
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Kubernetes Python Client in Minikube Suppose the goal is to delegate some tasks from a pod to another pod using HTTP requests, then it's necessary to know to IP of each pod. [Kubernetes Python Client](https://github.com/kubernetes-client/python) can do the job. --- ###### tags: `Kubernetes` `Python` --- ## Pip Install For testing purposes, it is convenient to use shell in the running container. ``` root@api-minikube-7f4b5d6dd6-56z8s:/app# which python /app/venv/bin/python root@api-minikube-7f4b5d6dd6-56z8s:/app# pip install kubernetes ``` ## Try It Out After installing successfully, open python shell in the container. ``` root@api-minikube-7f4b5d6dd6-56z8s:/app# python Python 3.8.10 (default, May 12 2021, 15:56:47) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from kubernetes import client >>> from kubernetes import config ``` [According to the example here](https://github.com/kubernetes-client/python#examples) ``` >>> config.load_kube_config() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/app/venv/lib/python3.8/site-packages/kubernetes/config/kube_config.py", line 813, in load_kube_config loader = _get_kube_config_loader( File "/app/venv/lib/python3.8/site-packages/kubernetes/config/kube_config.py", line 772, in _get_kube_config_loader raise ConfigException( kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found. ``` So, the above command does not work in clusters. In clusters, the command as shown below should be used. ``` config.load_incluster_config() ``` Then, the next step would be listing all pods using `list_pod_for_all_namespaces` function. If it's desired to list only pods in a specific namespace, the function `list_namespaced_pod` should be used. ``` >>> v1 = client.CoreV1Api() >>> ret = v1.list_pod_for_all_namespaces(watch=False) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 16864, in list_pod_for_all_namespaces return self.list_pod_for_all_namespaces_with_http_info(**kwargs) # noqa: E501 File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 16967, in list_pod_for_all_namespaces_with_http_info return self.api_client.call_api( File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 348, in call_api return self.__call_api(resource_path, method, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 180, in __call_api response_data = self.request( File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 373, in request return self.rest_client.GET(url, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 239, in GET return self.request("GET", url, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 233, in request raise ApiException(http_resp=r) kubernetes.client.exceptions.ApiException: (403) Reason: Forbidden HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Content-Type-Options': 'nosniff', 'X-Kubernetes-Pf-Flowschema-Uid': '7efca18f-3cc3-4a99-be09-59586a316041', 'X-Kubernetes-Pf-Prioritylevel-Uid': 'c1eae4b0-1a62-4ba2-8ec6-677d6541bc5d', 'Date': 'Tue, 22 Jun 2021 04:47:02 GMT', 'Content-Length': '274'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pods is forbidden: User \"system:serviceaccount:default:default\" cannot list resource \"pods\" in API group \"\" at the cluster scope","reason":"Forbidden","details":{"kind":"pods"},"code":403} ``` The most important part in this error message is `cannot list resource \"pods\" in API group`. Clearly, the issue relates to authorization. To solve this issue, it's necessary to use [RBAC Authorization](https://hackmd.io/AoMPVOpBTSiBBI24ABtaFA) The YAML file below is from the [here](https://github.com/kubernetes-client/python/issues/605#issuecomment-415217369) ``` # rbac_authorization.yml kind: apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list rules: - apiGroups: [""] resources: ["pods"] verbs: ["list"] --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list subjects: - kind: ServiceAccount name: default namespace: default roleRef: kind: Role name: pods-list ``` Then, apply this YAML file using kubectl. ``` > kubectl apply -f rbac_authorization.yml clusterrole.rbac.authorization.k8s.io/pods-list created clusterrolebinding.rbac.authorization.k8s.io/pods-list created ``` Inspect the change using kubectl. ``` > kubectl describe roles pods-list Name: pods-list Labels: <none> Annotations: <none> PolicyRule: Resources Non-Resource URLs Resource Names Verbs --------- ----------------- -------------- ----- pods [] [] [list] ``` As shown above, the information shown on the terminal is identical to the YAML file. Go to the python shell in the container again. ``` >>> ret = v1.list_pod_for_all_namespaces(watch=False) >>> pod_ips = [i.status.pod_ip for i in ret.items] >>> pod_ips ['172.17.0.7', '172.17.0.6', '172.17.0.5', '172.17.0.8', '172.17.0.3', '172.17.0.4'] ``` Now, it works. If it is preferred to use `read_namespaced_pod_status` function, instead of `list_pod_for_all_namespaces` or `list_namespaced_pod`,. For example, ``` >>> import os >>> POD_NS = os.environ['MY_POD_NAMESPACE'] >>> from kubernetes import client, config >>> config.load_incluster_config() >>> >>> v1 = client.CoreV1Api() >>> ret = v1.list_namespaced_pod(namespace=POD_NS) >>> pod_nm = [i.metadata.name for i in ret.items] >>> pod_stat = [v1.read_namespaced_pod_status(i, namespace=POD_NS) for i in pod_nm] ``` The error message would be similar as below. ``` Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <listcomp> File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 23089, in read_namespaced_pod_status return self.read_namespaced_pod_status_with_http_info(name, namespace, **kwargs) # noqa: E501 File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 23176, in read_namespaced_pod_status_with_http_info return self.api_client.call_api( File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 348, in call_api return self.__call_api(resource_path, method, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 180, in __call_api response_data = self.request( File "/app/venv/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 373, in request return self.rest_client.GET(url, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 239, in GET return self.request("GET", url, File "/app/venv/lib/python3.8/site-packages/kubernetes/client/rest.py", line 233, in request raise ApiException(http_resp=r) kubernetes.client.exceptions.ApiException: (403) Reason: Forbidden HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Content-Type-Options': 'nosniff', 'X-Kubernetes-Pf-Flowschema-Uid': '7efca18f-3cc3-4a99-be09-59586a316041', 'X-Kubernetes-Pf-Prioritylevel-Uid': 'c1eae4b0-1a62-4ba2-8ec6-677d6541bc5d', 'Date': 'Fri, 25 Jun 2021 10:27:02 GMT', 'Content-Length': '363'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pods \"api-minikube-7b9d4659db-8pjq8\" is forbidden: User \"system:serviceaccount:default:default\" cannot get resource \"pods/status\" in API group \"\" in the namespace \"default\"","reason":"Forbidden","details":{"name":"api-minikube-7b9d4659db-8pjq8","kind":"pods"},"code":403} ``` In particular, `cannot get resource \"pods/status\" in API group` Modify the above YAML file. ``` # rbac_authorization.yml kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list rules: - apiGroups: [""] resources: ["pods", "pods/status"] verbs: ["list"] --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list subjects: - kind: ServiceAccount name: default namespace: default roleRef: kind: Role name: pods-list apiGroup: rbac.authorization.k8s.io ``` The only difference is adding another resources called "pods/status". Apply the YAML file again. ``` > kubectl apply -f rbac_authorization.yml clusterrole.rbac.authorization.k8s.io/pods-list configured clusterrolebinding.rbac.authorization.k8s.io/pods-list unchanged ``` It turns out the above modified YAML file does not work either. To make it work, other verbs should be added. ``` # rbac_authorization.yml kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list rules: - apiGroups: [""] resources: ["pods", "pods/status"] verbs: ["get", "watch", "list"] --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: pods-list subjects: - kind: ServiceAccount name: default namespace: default roleRef: kind: Role name: pods-list apiGroup: rbac.authorization.k8s.io ``` Apply the YAML file again. ``` > kubectl apply -f rbac_authorization.yml clusterrole.rbac.authorization.k8s.io/pods-list configured clusterrolebinding.rbac.authorization.k8s.io/pods-list unchanged ``` Inspect the change using kubectl. ``` > kubectl describe roles pods-list Name: pods-list Labels: <none> Annotations: <none> PolicyRule: Resources Non-Resource URLs Resource Names Verbs --------- ----------------- -------------- ----- pods/status [] [] [get watch list] pods [] [] [get watch list] ``` By adding verbs `get` and `watch`, it should work as expected. ``` >>> pod_stat = [v1.read_namespaced_pod_status(i, namespace=POD_NS) for i in pod_nm] >>> [pod.status.phase for pod in pod_stat] ['Running', 'Running', 'Running', 'Running', 'Running', 'Running'] ``` # Reference - [**Official DOCS**](https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AutoscalingV1Api.md) - [Using RBAC Authorization](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) - https://github.com/kubernetes-client/python/issues/519 - https://github.com/kubernetes-client/python/issues/605 - https://github.com/kubernetes-client/python/blob/master/examples/in_cluster_config.py

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully