你还用 yaml编排 kubernetes 吗?你是否意识到YAML的局限性,例如你无法定义变量,不能循环重复内容,不能跟高级语言互动,于是你转向了 HELM, helm 提供模版技术,可以在模版中实现包含引用,定义变量,循环等等操作,但也仅此而已。
YAML 和 HELM 方案更多是给运维人员准备的,对开发并不友好,那么有没有更好的解决方案呢?
来看看我用 python 写的一个工具吧 netkiller-devops
安装方法
pip install netkiller-devops
下面编排一个 nginx 给大家演示一下。运行环境使用 macOS + k3d
k3s 是由 Rancher Labs 推出的一款轻量级 Kubernetes 发行版,满足在边缘计算环境中运行在 x86、ARM64 处理器上的小型、易于管理的 Kubernetes 集群日益增长的需求。
k3s 除了在边缘计算领域的应用外,在研发侧的表现也十分出色。我们可以快速在本地拉起一个轻量级的 k8s 集群,而 k3d 则是 k3s 社区创建的一个小工具,可以在一个 docker 进程中运行整个 k3s 集群,相比直接使用 k3s 运行在本地,更好管理和部署。
安装 k3d
brew install k3d
启动集群
k3d cluster create mycluster --api-port 6443 --servers 1 --agents 1 --port '80:80@loadbalancer' --port '443:443@loadbalancer'
现在创建一个 python 文件 例如 nginx.py 把下面内容复制进去
import os, sys
module = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(module)
sys.path.insert(0, module)
from netkiller.kubernetes import *
namespace = Namespace()
namespace.metadata.name('development')
namespace.metadata.namespace('development')
# namespace.debug()
service = Service()
service.metadata().name('nginx')
service.metadata().namespace('development')
service.spec().selector({'app': 'nginx'})
service.spec().type('NodePort')
service.spec().ports([{
'name': 'http',
'protocol': 'TCP',
'port': 80,
'targetPort': 80
}])
deployment = Deployment()
deployment.apiVersion('apiVersion: apps/v1')
deployment.metadata().name('nginx').labels({'app': 'nginx'}).namespace('development')
deployment.spec().replicas(2)
deployment.spec().selector({'matchLabels': {'app': 'nginx'}})
deployment.spec().template().metadata().labels({'app': 'nginx'})
deployment.spec().template().spec().containers().name('nginx').image(
'nginx:latest').ports([{
'containerPort': 80
}])
# deployment.debug()
# # deployment.json()
ingress = Ingress()
ingress.apiVersion('networking.k8s.io/v1')
ingress.metadata().name('nginx')
ingress.metadata().namespace('development')
ingress.metadata().annotations({'ingress.kubernetes.io/ssl-redirect': "false"})
ingress.spec().rules([{
# 'host': 'www.netkiller.cn',
'http': {
'paths': [{
'path': '/',
'pathType': 'Prefix',
'backend': {
'service': {
'name': 'nginx',
'port': {
'number': 80
}
}
}
}]
}
}])
# ingress.debug()
compose = Compose('development')
compose.add(namespace)
compose.add(service)
compose.add(deployment)
compose.add(ingress)
# compose.debug()
# compose.yaml()
# compose.save('/tmp/test.yaml')
kubernetes = Kubernetes()
kubernetes.compose(compose)
# kubernetes.debug()
# print(kubernetes.dump())
kubernetes.main()
查看帮助信息 /usr/bin/python3 nginx.py -h
➜ devops git:(master) ✗ /usr/bin/python3 nginx.py -h
Usage: nginx.py [options] <command>
Options:
-h, --help show this help message and exit
-e development|testing|production, --environment=development|testing|production
environment
-l, --list print service of environment
Cluster Management Commands:
-g, --get Display one or many resources
-c, --create Create a resource from a file or from stdin
-d, --delete Delete resources by filenames, stdin, resources and
names, or by resources and label selector
-r, --replace Replace a resource by filename or stdin
Namespace:
-n, --namespace Display namespace
-s, --service Display service
Others:
--logfile=LOGFILE logs file.
-y, --yaml show yaml compose
--export export docker compose
--debug debug mode
-v, --version print version information
现在开始部署 nginx 使用参数 -c :
/usr/bin/python3 nginx.py -c
➜ devops git:(master) ✗ /usr/bin/python3 nginx.py -c
namespace/development created
service/nginx created
deployment.apps/nginx created
ingress.networking.k8s.io/nginx created
查看部署状态
➜ devops git:(master) ✗ kubectl get namespace
NAME STATUS AGE
default Active 3h15m
kube-system Active 3h15m
kube-public Active 3h15m
kube-node-lease Active 3h15m
development Active 21m
➜ devops git:(master) ✗ kubectl get service -n development
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.43.19.13 <none> 80:31258/TCP 21m
➜ devops git:(master) ✗ kubectl get deployment -n development
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 21m
➜ devops git:(master) ✗ kubectl get ingress -n development
NAME CLASS HOSTS ADDRESS PORTS AGE
nginx <none> * 172.23.0.2,172.23.0.3 80 21m
检验 nginx 启动情况
➜ devops git:(master) ✗ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
我们可以看到反馈结果,现在开大 Safari 看看效果
一切跟预料的一样,我们成功了,Yeah!!!