这是一个实验阶段的片段
大致思路是:
测试团队完成测试后的镜像,被push到线上系统的harbor镜像库,触发了harbor的webhooks,webhooks调用提前写好的接口,接口程序经过一系列的判断(包括是否强制要求必须比线上版本号要高才更新等)更新kubernetes的deployments;kubernetes完成更新。
前期问题:
1、搞清楚webhooks的json是什么 ?
2、根据json搭建接口处理json;
实施过程:
1、在harbor上配置一个push镜像就会触发的webhooks;
2、使用python创建一个处理json的脚本,并启动;
3、在现有的kubernetes平台创建一个nginx的deployments;
4、在本地创建一个与上述版本不同的nginx镜像;
5、push上述创建的镜像到harbor仓库;(既是image是存在的也很触发webhooks)
6、观察结果;
执行过程:
1、在harbor上配置一个push镜像就会触发的webhooks;
2、使用python创建一个处理json的脚本,并启动;
根据上述的webhooks的json,使用flask开一个端口处理请求;
#!/usr/bin/env python
#encoding=utf-8
from flask import Flask, request
import json
import os
app = Flask(__name__)
@app.route('/api', methods=['POST', 'GET'])
def push_image():
postfrom = request.form
postvalues = request.values
postjson = request.json
print(json.dumps(postjson, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
images = postjson['event_data']['resources'][0]['resource_url']
os.system('kubectl set image deployments/nginx-deployment nginx='+ images +' -n {your_namespace}') # 更新deployments的images,kubernetes会自动rollingupdate
return("ok")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9091)
3、在现有的kubernetes平台创建一个nginx的deployments;
nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx-deployment
namespace: wosms
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.18.0 # 设定的是1.18.0
name: nginx
tolerations:
- key: "key"
operator: "Equal"
value: "nginx"
effect: "NoSchedule"
创建deployments
Kubectl apply -f nginx.yaml
4、在本地创建一个与上述版本不同的nginx镜像;
docker pull nginx:1.20.0
docker tag nginx:1.20.0 xxx.xx.xx.xxx:8080/{your_repository}/nginx:1.20.0
5、push上述创建的镜像到harbor仓库;(既是image是存在的也很触发webhooks)
docker push xxx.xx.xx.xxx:8080/{your_repository}/nginx:1.20.0
6、观察结果;
出现结果:
deployment.apps/nginx-deployment image updated
执行命令:
kubectl describe deployment nginx-deployment -n {your_namespace}
执行结果:
Name: nginx-deployment
......
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: xxx.xx.xxx.xx:8080/{your_namespace}/nginx:1.20.0
.......
PS:
harbor的webhooks功能,post的json;
{
'occur_at': 1632819802,
'operator': 'amdin',
'type': 'PUSH_ARTIFACT',
'event_data':
{
'repository':
{
'date_created': 1632819802,
'repo_type': 'public',
'repo_full_name': '{your_repository}/nginx',
'namespace': '{your_repository}',
'name': 'nginx'
},
'resources': [{
'resource_url': 'xxx.xx.xx.xxx:8080/{your_repository}/nginx:1.20.0',
'tag': '1.20.0',
'digest': 'sha256:598057a5c482d2fb42092fd6f4ba35ea4cc86c41f5db8bb68d1ab92c4c40db98'}]
}
}