官方有原始的python3镜像,这里我不想用,打算自己做一个,
首先可以下载一个初始化的centos镜像
一、制作镜像
docker pull centos #下载centos最小化
docker run -dit centos /bin/bash #后台运行容器
docker exec -it a1b93d28dcfc /bin/bash #进入容器
yum install vim python3 #下载工具看个人选择
docker commit -m "python in the docker" -a "新上镜像" a1b93d28dcfc python_base
docker tag python_base registry.cn-hangzhou.aliyuncs.com/xxxprodk8s/python3:v1 #打包成镜像
docker push registry.cn-hangzhou.aliyuncs.com/xxx/python3:v1 #上传镜像到阿里云
二、CICD
创建gitlab仓库并添加文件
脚本文件 *.py
安装文件 requirements.txt
requirements.txt文件生成方法
pip install pipreqs
进入需要生成文件的目录执行: pipreqs ./
可能会遇见
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 776: illegal multibyte sequence
解决方法:指定编码格式
pipreqs ./ --encoding=utf8
oss上传文件
可根据环境细分
{build.sh,python.yaml,Dockerfile}
通过jenkins拉取gitlab代码,通过oss工具拉取build脚本生成镜像
build.sh
dates=`date +%Y%m%d-%H%M%S`
tag=`echo $tag |sed 's/\///g'`
registry='registry-vpc.cn-hangzhou.aliyuncs.com/xxxxx'
echo "${tag}-${GIT_COMMIT:0:8}-${dates}" > ${image}-${dates}.txt
ossutil -c /etc/oss/ossutilconfig cp -rf ./${image}-${dates}.txt oss://xxx/xxx-config/python/test/images_version/${image}.txt
image=${registry}/${image}:${tag}
rm -f Dockerfile && ossutil -c /etc/oss/ossutilconfig cp oss://xxx/xxx-config/python/Dockerfile ./Dockerfile
docker build -t ${image} . && \
docker tag ${image} ${image}-${GIT_COMMIT:0:8}-${dates} && \
docker push ${image}-${GIT_COMMIT:0:8}-${dates} && \
docker rmi -f ${image} ${image}-${GIT_COMMIT:0:8}-${dates}
Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/xxx/python3:v1
RUN mkdir /app/lib -p
RUN mkdir /home/appuser/logs/ -p
RUN echo "Asia/Shanghai" > /etc/timezone
WORKDIR /app
ADD appFindRedeemCode.py /app/lib/app.py
ADD requirements.txt /app/lib/
RUN pip3 install --no-cache-dir -r /app/lib/requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple
RUN chmod +w /home/appuser/logs/
CMD ["python3","/app/lib/app.py"]
python.yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${service-name}-deployment
labels:
app: ${service-name}
spec:
replicas: ${replicas} #副本数
selector:
matchLabels:
app: ${service-name}
template:
metadata:
labels:
app: ${service-name}
spec:
volumes:
- name: ${service-name}-log
emptyDir: {}
- name: tz-config
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
containers:
- name: ${service-name}
image: imagename #镜像
imagePullPolicy: Always
ports:
- containerPort: ${port} #启动端口
volumeMounts:
- name: ${service-name}-log
mountPath: /home/appuser/logs
- name: tz-config
mountPath: /etc/localtime
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: ${port} #检查存活端口
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: ${port} #检查存活端口
timeoutSeconds: 1
resources: #pod资源限制
limits:
cpu: ${cpu}m
ephemeral-storage: 10Gi
memory: ${mem}Mi
requests:
cpu: ${cpu}m
ephemeral-storage: 10Gi
memory: ${mem}Mi
env:
- name: "_node_name_"
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: "_node_ip_"
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: logtail #阿里云logtail日志收集服务
image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest
command:
- sh
- -c
- /usr/local/ilogtail/run_logtail.sh 10
livenessProbe:
exec:
command:
- /etc/init.d/ilogtaild
- status
initialDelaySeconds: 30
periodSeconds: 30
resources:
limits:
memory: 512Mi
requests:
cpu: 10m
memory: 30Mi
env:
- name: "ALIYUN_LOGTAIL_USER_ID"
value: "xxxxx" #阿里云账号id
- name: "ALIYUN_LOGTAIL_USER_DEFINED_ID"
value: "${service-name}-prod"
- name: "ALIYUN_LOGTAIL_CONFIG"
value: "/etc/ilogtail/conf/cn-hangzhou/ilogtail_config.json"
- name: "ALIYUN_LOG_ENV_TAGS"
value: "_pod_name_|_pod_ip_|_namespace_|_node_name_|_node_ip_"
- name: "_pod_name_"
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: "_pod_ip_"
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: "_namespace_"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: "_node_name_"
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: "_node_ip_"
valueFrom:
fieldRef:
fieldPath: status.hostIP
volumeMounts:
- name: ${service-name}-log
mountPath: /home/appuser/logs
affinity: #pod可用区亲和度调整
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- cn-hangzhou-i
- cn-hangzhou-h
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- cn-hangzhou-i
- weight: 50
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- cn-hangzhou-h
---
apiVersion: v1
kind: Service #SVC服务
metadata:
name: ${service-name}-svc
spec:
ports:
- port: 80
protocol: TCP
targetPort: ${port}
selector:
app: ${service-name}
sessionAffinity: None
type: ClusterIP
---
apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig #日志收集服务
metadata:
name: ${service-name}-prod-config
spec:
project: ${logname}
logstore: ${service-name}-prod-log-stdout
machineGroups:
- ${service-name}-prod
shardCount: 2
lifeCycle: 90
logtailConfig:
inputType: file
configName: ${service-name}-prod-config
inputDetail:
logType: common_reg_log
logPath: /home/appuser/logs
filePattern: '*.log'
dockerFile: false
整体流程从jenkins–>gitlab–>oss–>cicd流水线工具–>k8s容器发布
细节不过多赘述了