kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.81k stars 3.27k forks source link
client-python k8s k8s-sig-api-machinery kubernetes library

Kubernetes Python Client

Build Status PyPI version codecov pypi supported versions Client Capabilities Client Support Level

Python client for the kubernetes API.

Installation

From source:

git clone --recursive https://github.com/kubernetes-client/python.git
cd python
python setup.py install

From PyPI directly:

pip install kubernetes

Examples

list all pods:

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

watch on namespace object:

from kubernetes import client, config, watch

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
count = 10
w = watch.Watch()
for event in w.stream(v1.list_namespace, _request_timeout=60):
    print("Event: %s %s" % (event['type'], event['object'].metadata.name))
    count -= 1
    if not count:
        w.stop()

print("Ended.")

More examples can be found in examples folder. To run examples, run this command:

python -m examples.example1

(replace example1 with one of the filenames in the examples folder)

Documentation

All APIs and Models' documentation can be found at the Generated client's README file

Compatibility

client-python follows semver, so until the major version of client-python gets increased, your code will continue to work with explicitly supported versions of Kubernetes clusters.

Compatibility matrix of supported client versions

See here for an explanation of why there is no v13-v16 release.

Key:

See the CHANGELOG for a detailed description of changes between client-python versions.

Client version Canonical source for OpenAPI spec Maintenance status
5.0 Alpha/Beta Kubernetes main repo, 1.9 branch
5.0 Kubernetes main repo, 1.9 branch
6.0 Alpha/Beta Kubernetes main repo, 1.10 branch
6.0 Kubernetes main repo, 1.10 branch
7.0 Alpha/Beta Kubernetes main repo, 1.11 branch
7.0 Kubernetes main repo, 1.11 branch
8.0 Alpha/Beta Kubernetes main repo, 1.12 branch
8.0 Kubernetes main repo, 1.12 branch
9.0 Alpha/Beta Kubernetes main repo, 1.13 branch
9.0 Kubernetes main repo, 1.13 branch
10.0 Alpha/Beta Kubernetes main repo, 1.14 branch
10.0 Kubernetes main repo, 1.14 branch
11.0 Alpha/Beta Kubernetes main repo, 1.15 branch
11.0 Kubernetes main repo, 1.15 branch
12.0 Alpha/Beta Kubernetes main repo, 1.16 branch
12.0 Kubernetes main repo, 1.16 branch
17.0 Alpha/Beta Kubernetes main repo, 1.17 branch
17.0 Kubernetes main repo, 1.17 branch
18.0 Alpha/Beta Kubernetes main repo, 1.18 branch
18.0 Kubernetes main repo, 1.18 branch
19.0 Alpha/Beta Kubernetes main repo, 1.19 branch
19.0 Kubernetes main repo, 1.19 branch
20.0 Alpha/Beta Kubernetes main repo, 1.20 branch
20.0 Kubernetes main repo, 1.20 branch
21.0 Alpha/Beta Kubernetes main repo, 1.21 branch
21.0 Kubernetes main repo, 1.21 branch
22.0 Alpha/Beta Kubernetes main repo, 1.22 branch
22.0 Kubernetes main repo, 1.22 branch
23.0 Alpha/Beta Kubernetes main repo, 1.23 branch
23.0 Kubernetes main repo, 1.23 branch
24.0 Alpha/Beta Kubernetes main repo, 1.24 branch
24.0 Kubernetes main repo, 1.24 branch
25.0 Alpha/Beta Kubernetes main repo, 1.25 branch
25.0 Kubernetes main repo, 1.25 branch
26.0 Alpha/Beta Kubernetes main repo, 1.26 branch
26.0 Kubernetes main repo, 1.26 branch
27.0 Alpha/Beta Kubernetes main repo, 1.27 branch
27.0 Kubernetes main repo, 1.27 branch
28.0 Alpha/Beta Kubernetes main repo, 1.28 branch
28.0 Kubernetes main repo, 1.28 branch
29.0 Alpha/Beta Kubernetes main repo, 1.29 branch
29.0 Kubernetes main repo, 1.29 branch
30.0 Alpha/Beta Kubernetes main repo, 1.30 branch
30.0 Kubernetes main repo, 1.30 branch
31.0 Alpha/Beta Kubernetes main repo, 1.31 branch
31.0 Kubernetes main repo, 1.31 branch

See here for an explanation of why there is no v13-v16 release.

Key:

Kubernetes supports three minor releases at a time. "Support" means we expect users to be running that version in production, though we may not port fixes back before the latest minor version. For example, when v1.3 comes out, v1.0 will no longer be supported. In consistent with Kubernetes support policy, we expect to support three GA major releases (corresponding to three Kubernetes minor releases) at a time.

Note: There would be no maintenance for alpha/beta releases except the latest one.

Exception to the above support rule: Since we are running behind on releases, we will support Alpha/Beta releases for a greater number of clients until we catch up with the upstream version.

Homogenizing the Kubernetes Python Client versions

The client releases v12 and before following a versioning schema where the major version was 4 integer positions behind the Kubernetes minor on which the client is based on. For example, v12.0.0 is based on Kubernetes v1.16, v11.0.0 is based on Kubernetes v1.15 and so on.

This created a lot of confusion tracking two different version numbers for each client release. It was decided to homogenize the version scheme starting from the Kubernetes Python client based on Kubernetes v1.17. The versioning scheme of the client from this release would be vY.Z.P where Y and Z are the Kubernetes minor and patch release numbers from Kubernets v1.Y.Z and P is the client specific patch release numbers to accommodate changes and fixes done specifically to the client. For more details, refer this issue.

Community, Support, Discussion

If you have any problem on using the package or any suggestions, please start with reaching the Kubernetes clients slack channel, or filing an issue to let us know. You can also reach the maintainers of this project at SIG API Machinery, where this project falls under.

Code of Conduct

Participation in the Kubernetes community is governed by the CNCF Code of Conduct.

Troubleshooting

SSLError on macOS

If you get an SSLError, you likely need to update your version of python. The version that ships with macOS may not be supported.

Install the latest version of python with brew:

brew install python

Once installed, you can query the version of OpenSSL like so:

python -c "import ssl; print (ssl.OPENSSL_VERSION)"

You'll need a version with OpenSSL version 1.0.0 or later.

Hostname doesn't match

If you get an ssl.CertificateError complaining about hostname match, your installed packages does not meet version requirements. Specifically check ipaddress and urllib3 package versions to make sure they met requirements in requirements.txt file.

Why Exec/Attach calls doesn't work

Starting from 4.0 release, we do not support directly calling exec or attach calls. you should use stream module to call them. so instead of resp = api.connect_get_namespaced_pod_exec(name, ... you should call resp = stream(api.connect_get_namespaced_pod_exec, name, ....

Using Stream will overwrite the requests protocol in _core_v1api.CoreV1Api() This will cause a failure in non-exec/attach calls. If you reuse your api client object, you will need to recreate it between api calls that use stream and other api calls.

See more at exec example.

⬆ back to top