Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83f92cc

Browse files
add example to demonstrate a rolling restart of the deployment
1 parent 66a6654 commit 83f92cc

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Copyright 2021 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
This example demonstrates the following:
17+
- Creation of a k8s deployment using dynamic-client
18+
- Rolling restart of the deployment (demonstrate patch/update action)
19+
- Listing & deletion of the deployment
20+
"""
21+
22+
23+
from kubernetes import config, dynamic
24+
from kubernetes.client import api_client
25+
import datetime
26+
import pytz
27+
28+
def main():
29+
# Creating a dynamic client
30+
client = dynamic.DynamicClient(
31+
api_client.ApiClient(configuration=config.load_kube_config())
32+
)
33+
34+
# fetching the deployment api
35+
api = client.resources.get(api_version="apps/v1", kind="Deployment")
36+
37+
name = "nginx-deployment"
38+
39+
deployment_manifest = {
40+
"apiVersion": "apps/v1",
41+
"kind": "Deployment",
42+
"metadata": {"labels": {"app": "nginx"}, "name": name},
43+
"spec": {
44+
"replicas": 3,
45+
"selector": {"matchLabels": {"app": "nginx"}},
46+
"template": {
47+
"metadata": {"labels": {"app": "nginx"}},
48+
"spec": {
49+
"containers": [
50+
{
51+
"name": "nginx",
52+
"image": "nginx:1.14.2",
53+
"ports": [{"containerPort": 80}],
54+
}
55+
]
56+
},
57+
},
58+
},
59+
}
60+
61+
# Creating deployment `nginx-deployment` in the `default` namespace
62+
63+
deployment = api.create(body=deployment_manifest, namespace="default")
64+
65+
print("\n[INFO] deployment `nginx-deployment` created\n")
66+
67+
# Listing deployment `nginx-deployment` in the `default` namespace
68+
69+
deployment_created = api.get(name=name, namespace="default")
70+
71+
print("%s\t%s\t\t\t%s\t%s" % ("NAMESPACE", "NAME", "REVISION", "RESTARTED-AT"))
72+
print(
73+
"%s\t\t%s\t%s\t\t%s\n"
74+
% (
75+
deployment_created.metadata.namespace,
76+
deployment_created.metadata.name,
77+
deployment_created.metadata.annotations,
78+
deployment_created.spec.template.metadata.annotations,
79+
)
80+
)
81+
82+
# Patching the `spec.template.metadata` section to add `kubectl.kubernetes.io/restartedAt` annotation
83+
# In order to perform a rolling restart on the deployment `nginx-deployment`
84+
85+
deployment_manifest["spec"]["template"]["metadata"] = {
86+
"annotations": {
87+
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
88+
.replace(tzinfo=pytz.UTC)
89+
.isoformat()
90+
}
91+
}
92+
93+
deployment_patched = api.patch(
94+
body=deployment_manifest, name=name, namespace="default"
95+
)
96+
97+
print("\n[INFO] deployment `nginx-deployment` restarted\n")
98+
print(
99+
"%s\t%s\t\t\t%s\t\t\t\t\t\t%s"
100+
% ("NAMESPACE", "NAME", "REVISION", "RESTARTED-AT")
101+
)
102+
print(
103+
"%s\t\t%s\t%s\t\t%s\n"
104+
% (
105+
deployment_patched.metadata.namespace,
106+
deployment_patched.metadata.name,
107+
deployment_patched.metadata.annotations,
108+
deployment_patched.spec.template.metadata.annotations,
109+
)
110+
)
111+
112+
# Deleting deployment `nginx-deployment` from the `default` namespace
113+
114+
deployment_deleted = api.delete(name=name, body={}, namespace="default")
115+
116+
print("\n[INFO] deployment `nginx-deployment` deleted\n")
117+
118+
119+
if __name__ == "__main__":
120+
main()

0 commit comments

Comments
 (0)