0%

神策logagent收集Kubernetes Pod日志


项目背景:

神策logagent需要收集到java程序输出的日志,用于分析用户行为等

想到两种方案

  1. pod指定部署到某台宿主机上,日志目录挂载到宿主机,宿主机上启动logagent程序。
  2. sidecar的形式

目前使用第一种方案,比较稳妥,后续技术扎实后会替换成第二种,更稳定高效。

部署logagent

官方部署文档

安装Jdk
1
yum install -y java-1.8.0-openjdk.x86_64
下载LogAgent部署包和解压
1
2
wget http://download.sensorsdata.cn/release/logagent/logagent_20190605.tgz
tar zxvf logagent_20190605.tgz
编辑配置文件
1
2
3
4
5
6
7
8
9
vim logagent/logagent.conf

path=/var/log/veercms #本地路径
logagent_id=veerservice_2_241 #id
pattern=cms.veerservice.log* #log名称格式
project=production
pid_file=/data/vcg/logagent/logagent.pid
host=172.16.249.136 #server端地址
port=8106
启动
1
nohup bin/logagent >/dev/null 2>&1 &
查看日志
1
tail -f /data/vcg/logagent/log/logagent.log

配置节点标签

两种方式实现:

  1. 控制台上添加标签

    image-20190619142748161

  2. 命令行

    1
    2
    3
    4
    5
    #例如给 cn-beijing.i-2ze7nm52ylwxd8j4d0lq 这个node添加标签:
    kubectl label nodes cn-beijing.i-2ze7nm52ylwxd8j4d0lq test-key=test-value

    #查看标签:
    kubectl get node --show-labels

配置k8s yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: com-veer-veerservice
labels:
app: com-veer-veerservice
namespace: default
spec:
replicas: 6
selector:
matchLabels:
app: com-veer-veerservice
template:
metadata:
labels:
app: com-veer-veerservice
spec:
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
imagePullSecrets:
- name: registry-vpc.cn-beijing.aliyuncs.com ## 保密字典中的仓储认证
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference: {}
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: vcgapp
operator: In
values:
- veerlogagent ## node节点亲和性
containers:
- name: com-veer-veerservice
image: 'registry-vpc.cn-beijing.aliyuncs.com/vcg/com-veer-veerservice:pro-20190610183629' ## 镜像地址
env:
- name: aliyun_logs_com-veer-veerservice ## 日志服务
value: stdout
imagePullPolicy: Always
volumeMounts:
- mountPath: /var/log/veerservice_log # 容器内路径
name: volume-veerservice
resources:
limits:
cpu: '1'
memory: 4Gi
requests:
cpu: 500m
memory: 1000Mi
livenessProbe: ## 存活检测
initialDelaySeconds: 80
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
tcpSocket:
port: 9101
readinessProbe: ## 就绪检测
initialDelaySeconds: 80
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
tcpSocket:
port: 9101
volumes:
- hostPath:
path: /var/log/veerservice # 主机路径
type: ''
name: volume-veerservice
---
apiVersion: v1
kind: Service
metadata:
name: com-veer-veerservice
labels:
app: com-veer-veerservice
namespace: default
spec:
selector:
app: com-veer-veerservice
ports:
- port: 80
protocol: TCP
targetPort: 9101
type: ClusterIP

阿里云OSS对象存储中的图片数据迁移到京东云OSS

下载数据

使用阿里云提供的 ossutil 工具进行下载操作

1
ossutil cp oss://static-vcg/veer/static/ veer/static/

上传数据

使用Amazon提供的 s3cmd 工具进行上传操作

1
s3cmd put --recursive static/ s3://vcg-veer-static/static/

异常问题

上传到京东云后发现浏览器访问oss里的图片,会以下载的形式展示,需要更改为在线展示。

询问京东技术后重新上传,增加 **–content-type=”image/svg+xml”**参数

1
nohup s3cmd put --recursive --content-type="image/svg+xml"   static/ s3://vcg-veer-static/static/ &

重新上传后,清除本地缓存,刷新CDN目录或指定资源即可正常访问。

调用京东云SDK获取OSS文件的属性

SDK-Python

安装pip和boto3
1
pip install boto3

创建client

1
2
3
4
5
6
7
8
9
10
11
12
import boto3  

ACCESS_KEY = 'your accesskey'
SECRET_KEY = 'your secretkey'
s3 = boto3.client(
's3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
#下面给出一个endpoint_url的例子
endpoint_url='https://s3.cn-north-1.jdcloud-oss.com'
)
s3.head_object(Bucket='vcg-veer-static',Key='static/landing/icon_search_white.svg')

返回值如下:

jd-sdk-python.jpg

需求

调用阿里云API实现 根据docker compose文件进行Swarm集群容器应用的更新、重启

脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#调用阿里容器api
'''
alics.py
参数1 操作 update restart
参数2 集群名称 pre test pro
参数2 应用名称
参数3 dockercompose文件的绝对路径
参数4 时间戳
'''
import requests
import sys
import json
import time
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

Action=sys.argv[1]
TimeStamp=str(time.time())

def UpdataService(ClusterName,Service,File,TimeStamp):
with open(File) as file:
contents = file.read().replace('\n', '\r\n')
data = {
"template": contents,
"version": TimeStamp,
}
if ClusterName == "pre":
print '预发环境的 %s 应用进行更新' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行更新' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行更新' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,data=json.dumps(data),verify=verify,cert=cert)
print res
print res.text
if '202' in str(res):
print '\n应用更新请求已发送到阿里云容器服务!\n'
sys.exit(0)
else:
print '更新失败,请查看compose文件是否缺少参数'
sys.exit(1)

def CreateService(ClusterName,Service,File,TimeStamp):
with open(File) as file:
contents = file.read().replace('\n', '\r\n')
data = {
"name": Service,
"template": contents,
"version": TimeStamp,
}
if ClusterName == "pre":
print '创建预发环境的 %s 应用' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '创建生产环境的 %s 应用' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '创建测试环境的 %s 应用' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,data=json.dumps(data),verify=verify,cert=cert)
print res
print res.text
if '201' in str(res):
print '\n应用更新请求已发送到阿里云容器服务!\n'
sys.exit(0)
else:
print '更新失败,请查看compose文件是否缺少参数'
sys.exit(1)


def RestartService(ClusterName,Service):
if ClusterName == "pre":
print '预发环境的 %s 应用进行重启' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行重启' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行重启' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
while True:
res = requests.post(url,verify=verify,cert=cert)
print res
print res.text
if '200' in str(res):
print '\n应用重启完成!\n'
break
else:
print '程序正在更新,10秒后重试!'
time.sleep( 10 )

def GetService(ClusterName,Service):
if ClusterName == "pre":
print '获取预发环境的 %s 应用的信息' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '获取生产环境的 %s 应用的信息' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '获取测试环境的 %s 应用的信息' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
print res
print json.loads(res.text)

def RedeployService(ClusterName,Service):
if ClusterName == "pre":
print '预发环境的 %s 应用进行重新部署' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/redeploy'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行重新部署' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/redeploy'
verify = '/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行重新部署' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/redeploy'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,verify=verify,cert=cert)
print res
print res.text

def GetComposeService(ClusterName,Service):
if ClusterName == "pre":
File = '/jenkins/vcgapp/compose-back/pre/' + Service
print '获取预发环境的 %s 应用的Compose文件,保存本地后的文件为 %s' % (Service,File)
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
File = '/jenkins/vcgapp/compose-back/pro/' + Service
print '获取生产环境的 %s 应用的Compose文件,保存本地后的文件为 %s' % (Service,File)
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
with open(File,'w') as file:
file.write(json.loads(res.text).get('template').replace('\r\n','\n'))

def GetAllService(ClusterName):
if ClusterName == "pre":
print '获取预发环境所有应用的信息'
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '获取生产环境所有应用的信息'
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
print res
#print res.text
for i in json.loads(res.text):
#print i.get('template').replace('\r\n','\n')
if ClusterName == "pre":
File = '/jenkins/vcgapp/compose-back/pre/' + i.get('name')
elif ClusterName == "pro":
File = '/jenkins/vcgapp/compose-back/pro/' + i.get('name')
composefile = i.get('template').replace('\r\n','\n')
with open(File,'w') as file:
file.write(composefile)
print "%s 的 compose 文件已备份到 %s " % (i.get('name'),File)

if Action == "update":
ClusterName = sys.argv[2]
Service = sys.argv[3]
File = sys.argv[4]
UpdataService(ClusterName,Service,File,TimeStamp)
elif Action == "create":
ClusterName = sys.argv[2]
Service = sys.argv[3]
File = sys.argv[4]
CreateService(ClusterName,Service,File,TimeStamp)
elif Action == "restart":
ClusterName = sys.argv[2]
Service = sys.argv[3]
RestartService(ClusterName, Service)
elif Action == "getinfo":
ClusterName = sys.argv[2]
Service = sys.argv[3]
GetService(ClusterName, Service)
elif Action == "redeploy":
ClusterName = sys.argv[2]
Service = sys.argv[3]
RedeployService(ClusterName, Service)
elif Action == "getcompose":
ClusterName = sys.argv[2]
Service = sys.argv[3]
GetComposeService(ClusterName, Service)
elif Action == "getallservice":
ClusterName = sys.argv[2]
GetAllService(ClusterName)

需求

今天哥们提了一个需求,想实现调阿里云SLB的SDK实现脚本调整权重、增加或移除后端ECS实例

脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
#安装python3和pip
#安装以下sdk模块
#pip install aliyun-python-sdk-core-v3
#pip install aliyun-python-sdk-slb
import sys
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerAttributeRequest
from aliyunsdkslb.request.v20140515 import SetBackendServersRequest
from aliyunsdkslb.request.v20140515 import AddBackendServersRequest
from aliyunsdkslb.request.v20140515 import RemoveBackendServersRequest

# 需填写ak信息,可用区
AccessKey=''
AccessKeySecret=''
RegionId = "cn-beijing"

client = AcsClient(AccessKey,AccessKeySecret,RegionId);
helpInfo='''
Basic Commands :
get 获取SLB信息
edit 配置SLB的后端权重
add 添加SLB的后端实例
remove 删除SLB的后端实例

Usage:
获取所有实例的信息
python slb-0604.py get all
获取某一实例的信息
python slb-0604.py get lb-2zedfhvgznrm7eqbr00uv
编辑单个权重
python slb-0604.py edit lb-2zedfhvgznrm7eqbr00uv [{'ServerId': 'i-2zedna91yczcq12zm60q', 'Weight': 99, 'Type': 'ecs'}]
编辑多个权重(最多20个)
python slb-0604.py edit lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
添加单个后端ecs
python slb-0604.py add lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
添加多个后端ecs
python slb-0604.py add lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
删除后端ecs
python slb-0604.py remove lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
'''
def main():
print(helpInfo)

def DescribeLoadBalancers(resource):
request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()
response = client.do_action_with_exception(request)
SLBInfo = json.loads(response)
LoadBalancerIdList = []
for SLBInstance in SLBInfo['LoadBalancers']['LoadBalancer']:
LoadBalancerIdList.append(SLBInstance['LoadBalancerId'])

if resource == 'all':
Ali_Slb_Info = {}
for SLBInstance in SLBInfo['LoadBalancers']['LoadBalancer']:
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
request.set_LoadBalancerId(SLBInstance['LoadBalancerId'])
response = client.do_action_with_exception(request)
Ali_Slb_Info[SLBInstance['LoadBalancerId']] = json.loads(response.decode('utf-8'))
print(LoadBalancerIdList)
elif resource in LoadBalancerIdList:
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
response = json.loads(response.decode('utf-8'))
print(response['BackendServers'])

else:
print("输入错误,请输入 all 或 SLB实例ID !")


def SetBackendServers(resource,BackendServers):
request = SetBackendServersRequest.SetBackendServersRequest()
request.set_accept_format('json')
request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)

response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

def AddBackendServers(resource,BackendServers):
request = AddBackendServersRequest()
request.set_accept_format('json')

request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

def RemoveBackendServers(resource,BackendServers):
request = RemoveBackendServersRequest()
request.set_accept_format('json')

request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

if __name__ == '__main__':
if len(sys.argv) == 1:
main()
else:
userInput = sys.argv[1:]
if userInput[0] == 'get' and userInput[1]:
resource = userInput[1]
DescribeLoadBalancers(resource)
if userInput[0] == 'edit' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
SetBackendServers(resource,BackendServers)
if userInput[0] == 'add' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
AddBackendServers(resource,BackendServers)
if userInput[0] == 'remove' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
RemoveBackendServers(resource,BackendServers)
else:
print("输入错误")

需求

有的Pod重启次数过多

1
2
3
4
5
6
7
8
9
[cicd-test@ops-jenkins-master ~]$ kubectl get pods |awk '{if ($4 >= 1) print $0 }'
NAME READY STATUS RESTARTS AGE
cmsservice-vcg-com-57974bc4cf-vq6mk 1/1 Running 1 6m18s
edgeservice-vcg-com-755b96fb78-v96s5 1/1 Running 1 3d23h
edgeserviceweb-vcg-com-5655fc9899-8lxql 1/1 Running 1 3d23h
node-visualchina-web-8575cf78dc-8jr2k 1/1 Running 1 4h11m
vdam-gateway-service-6b7d864d67-rdzrg 1/1 Running 1 3h36m
vdam-passport-service-7c97d84b58-xhn6k 1/1 Running 6 3d22h

脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash

#Author:zhy
#Version:1.0
#Date:20190603
########
#删除重启次数超过5次的POD

set -e

Time=`date +%Y%m%d%H%M%S`

echo $Time >> /home/cicd-pro/cron/k8s_pod_restart.log

if [[ `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $0 }' |grep -v 'NAME' |wc -l` -eq 0 ]];then
echo '无重启次数超过5次的POD'
echo '无重启次数超过5次的POD' >> /home/cicd-pro/cron/k8s_pod_restart.log
else
echo '将要删除如下pod:'
echo '将要删除如下pod:' >> /home/cicd-pro/cron/k8s_pod_restart.log
/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }'| grep -v 'NAME'
echo `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }'| grep -v 'NAME'` >> /home/cicd-pro/cron/k8s_pod_restart.log
for i in `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }' | grep -v 'NAME'`;
do
echo $i
/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config delete pod $i
done
fi

nginx日志按天进行切割,通过写shell脚本,创建以日期命名文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash

set -e
# 定义nginx 日志路径
LOG_PATH="/var/gb/logs/"

# 定义nginx 访问日志文件名称
ACCESS_LOG="access.log"
ERROR_LOG="error.log"

for i in `find $LOG_PATH -name "$ACCESS_LOG"`; do
cd $(dirname $i)
# 切割access日志
if [[ -f $ACCESS_LOG ]]; then
cp {,$(date +%F)-}${ACCESS_LOG}
: > $ACCESS_LOG
fi

# 如果error日志>20m,切
if [[ -f $ERROR_LOG ]]; then
ERROR_SIZE=`ls -l $ERROR_LOG | awk '{ print $5 }'`
if [[ $ERROR_SIZE -gt 20971520 ]]; then
cp {,$(date +%F)-}${ERROR_LOG}
: > ${ERROR_LOG}
fi
fi
done

# 查找nginx 日志目录下7天前的日志并删除
find ${LOG_PATH} -type f -name "*-${ACCESS_LOG}" -mtime +7 -delete
find ${LOG_PATH} -type f -name "*-${ERROR_LOG}" -mtime +7 -delete

hexo-helper-live2d GitHub地址

安装插件
  1. 在hexo根目录执行

    1
    npm install --save hexo-helper-live2d
  2. 选择动画模型

    模型地址

    • live2d-widget-model-chitose
    • live2d-widget-model-epsilon2_1
    • live2d-widget-model-gf
    • live2d-widget-model-haru
    • live2d-widget-model-haruto
    • live2d-widget-model-hibiki
    • live2d-widget-model-hijiki
    • live2d-widget-model-izumi
    • live2d-widget-model-koharu
    • live2d-widget-model-miku
    • live2d-widget-model-ni-j
    • live2d-widget-model-nico
    • live2d-widget-model-nietzsche
    • live2d-widget-model-nipsilon
    • live2d-widget-model-nito
    • live2d-widget-model-shizuku
    • live2d-widget-model-tororo
    • live2d-widget-model-tsumiki
    • live2d-widget-model-unitychan
    • live2d-widget-model-wanko
    • live2d-widget-model-z16
  3. 本次选择安装live2d-widget-model-haruto

    1
    npm install live2d-widget-model-haruto
拷贝资源
  1. 在hexo根目录live2d_models

    1
    mkdir -p live2d_models
  2. cp模型文件

    1
    cp -r node_modules/live2d-widget-model-haruto/ live2d_models/
编辑主配置

编辑_config.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
live2d:
enable: true
scriptFrom: local
pluginRootPath: live2dw/
pluginJsPath: lib/
pluginModelPath: assets/
tagMode: false
debug: false
model:
use: live2d-widget-model-haruto
display:
position: right
width: 150
height: 300
mobile:
show: true
重新部署
1
hexo clean && hexo g && hexo d

我部署到GitHub上的域名正常,服务器上的没生效,重启进程后生效。

编辑主题配置

编辑 themes/next/_config.yml

1
2
3
4
5
6
7
8
#canvas_nest: false
canvas_nest:
enable: true
onmobile: true # display on mobile or not
color: '0,0,255' # RGB values, use ',' to separate
opacity: 0.5 # the opacity of line: 0~1
zIndex: -1 # z-index property of the background
count: 99 # the number of lines
重新部署
1
hexo clean && hexo g && hexo d