【题目 1】Python 运维开发:基于 Kubernetes Restful API 实现 Deployment 创建
在提供的 OpenStack 私有云平台上,使用 k8s-python-dev 镜像创建 1 台云主机,云主机
类型使用 4vCPU/12G 内存/100G 硬盘。该主机中已经默认安装了所需的开发环境,登录默
认账号密码为“root/1DaoYun@2022”。
使用 Kubernetes Restful API 库,在/root 目录下,创建 api_deployment_manager.py 文件,
要求编写 python 代码,代码实现以下任务:
(1)编写 Python 程序实现 Deployment 资源的创建。Deployment 配置信息如下。如果
同名 Deployment 存在,先删除再创建。
(2)创建完成后,查询该 Deployment 的详细信息,执行结果控制台输出,以 yaml 格
式展示。
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Creates a deployment using AppsV1Api from file nginx-deployment.yaml.
"""
from os import path
import yaml
from kubernetes import client, config
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
dep = yaml.safe_load(f)
k8s_apps_v1 = client.AppsV1Api()
resp = k8s_apps_v1.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % resp.metadata.name)
if __name__ == '__main__':
main()
【题目 2】Python 运维开发:基于 Kubernetes Python SDK 实现 Job 创建
在前面已建好的 Kubernetes 开发环境云平台上。使用 Kubernetes python SDK 的
“kubernetes”Python 库,在/root 目录下,创建 sdk_job_manager.py 文件,要求编写 python 代
码,代码实现以下任务:
(1)编写 Python 程序实现 Job 资源的创建。Job 配置信息如下。如果同名 Job 存在,
先删除再创建。
(2)创建完成后,查询该 Job 的详细信息,执行结果控制台输出,以 json 格式展示。
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Creates, updates, and deletes a job object.
"""
from os import path
from time import sleep
import yaml
from kubernetes import client, config
JOB_NAME = "pi"
def create_job_object():
# Configureate Pod template container
container = client.V1Container(
name="pi",
image="perl",
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
# Create and configure a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "pi"}),
spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
# Create the specification of deployment
spec = client.V1JobSpec(
template=template,
backoff_limit=4)
# Instantiate the job object
job = client.V1Job(
api_version="batch/v1",
kind="Job",
metadata=client.V1ObjectMeta(name=JOB_NAME),
spec=spec)
return job
def create_job(api_instance, job):
api_response = api_instance.create_namespaced_job(
body=job,
namespace="default")
print("Job created. status='%s'" % str(api_response.status))
get_job_status(api_instance)
def get_job_status(api_instance):
job_completed = False
while not job_completed:
api_response = api_instance.read_namespaced_job_status(
name=JOB_NAME,
namespace="default")
if api_response.status.succeeded is not None or \
api_response.status.failed is not None:
job_completed = True
sleep(1)
print("Job status='%s'" % str(api_response.status))
def update_job(api_instance, job):
# Update container image
job.spec.template.spec.containers[0].image = "perl"
api_response = api_instance.patch_namespaced_job(
name=JOB_NAME,
namespace="default",
body=job)
print("Job updated. status='%s'" % str(api_response.status))
def delete_job(api_instance):
api_response = api_instance.delete_namespaced_job(
name=JOB_NAME,
namespace="default",
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
print("Job deleted. status='%s'" % str(api_response.status))
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
batch_v1 = client.BatchV1Api()
# Create a job object with client-python API. The job we
# created is same as the `pi-job.yaml` in the /examples folder.
job = create_job_object()
create_job(batch_v1, job)
update_job(batch_v1, job)
delete_job(batch_v1)
if __name__ == '__main__':
main()
【题目 3】Python 运维开发:Pod 资源的 Restful APIs HTTP 服务封装
编写 Python 程序实现 Pod 资源管理程序,将 Pod 资源管理的封装成 Web 服务。
在/root目录下创建pod_server.py程序,实现Pod的增删查改等Web访问操作。http.server
的 host 为 localhost,端口 8889;程序内部实现 Kubernetes 认证。
提示说明:Python 标准库 http.server 模块,提供了 HTTP Server 请求封装。
需要实现的 Restful API 接口如下:
GET /pod/{name} ,查询指定名称{name}的 Pod;Response 的 Body 以 json 格式输出。
POST /pod/{yamlfilename} 创建 yaml 文件名称为{yamlfilename}的 Pod;Response 的
Body 以 json 格式。
编码完成后,“手工下载”文件服务器主目录所有*.yaml 文件到 root 目录下,“手动执
行”所编写 pod_server.py 程序,提交答案进行检测。
评论区