jenkins + k8s + lnp
前提条件:
K8S 已部署好,traefik也部署完成
K8S 集群上都挂载NFS盘,防止jenkins挂掉后配置消失
基础镜像使用 lnp,PHP 5.6.31 nginx/1.8.1
基础镜像下载地址:docker pull liuyusheng/alpine-lnp:v1
docker私有仓库
[root@master jenkis]# cat jenkins-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: jenkins
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
template:
metadata:
labels:
app: jenkins-last
spec:
nodeSelector:
jenkins: jenkins
containers:
- name: jenkins
image: jenkins:alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
volumeMounts:
- name: jenkinshome
mountPath: /var/jenkins_home
volumes:
- name: jenkinshome
nfs:
server: 192.168.0.161
path: /data/jenkins
---
kind: Service
apiVersion: v1
metadata:
name: jenkins
spec:
ports:
- port: 8080
targetPort: 8080
name: web
- port: 50000
targetPort: 50000
name: agent
selector:
app: jenkins-last
jenkins-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: traefik-ingress
namespace: default
spec:
rules:
- host: my.jenkins.com
http:
paths:
- path: /
backend:
serviceName: jenkins
servicePort: 8080
启动安装jenkins
kubectl create -f .
traefik 上可以查看
配置好SSH KEY 连接,这里不做配置SSH-KEY 操作
jenkins SERVER="192.168.0.153" USER="root" PORT="22" GROUP_NAME="lin_lang" JOB_NAME="fabaoguo" DST_PATH="/data/tmp/" HOME_DIR="/var/jenkins_home" FILE_NAME=${JOB_NAME}.tar.gz DOCKER_REG="192.168.0.153:5000" NGINX_CONF="www.${JOB_NAME}.com.conf" BASE_IMG="alpine:v5" #BUILD_NUM="v1" WWW_NAME="www.fabaoguo.com" BUILD_NUM=`date +%F-%H-%M-%S` #init /usr/bin/test -d ${HOME_DIR}/releaseDir || /bin/mkdir -p ${HOME_DIR}/releaseDir /bin/tar -zcf ${HOME_DIR}/releaseDir/${FILE_NAME} --exclude=.svn --exclude releaseDir * \cp ${HOME_DIR}/scripts/www.example.com.conf ${HOME_DIR}/scripts/${NGINX_CONF} sh ${HOME_DIR}/scripts/replace.sh ${HOME_DIR}/scripts/${NGINX_CONF} ${JOB_NAME} #deploy /usr/bin/scp ${HOME_DIR}/releaseDir/${FILE_NAME} ${USER}@${SERVER}:${DST_PATH}; /usr/bin/scp ${HOME_DIR}/scripts/www.${JOB_NAME}.com.conf ${USER}@${SERVER}:${DST_PATH} /usr/bin/ssh -p ${PORT} ${USER}@${SERVER} "sh /data/scripts/build_lnp.sh ${FILE_NAME} ${NGINX_CONF} ${BASE_IMG} ${DOCKER_REG}/${JOB_NAME} ${BUILD_NUM}"; /usr/bin/ssh -p ${PORT} ${USER}@${SERVER} "sh /data/scripts/start_app.sh ${WWW_NAME} ${JOB_NAME} ${JOB_NAME} ${BUILD_NUM} ${JOB_NAME}.yaml" rm -f ${HOME_DIR}/releaseDir/${FILE_NAME} rm -f ${HOME_DIR}/scripts/www.${JOB_NAME}.com.conf
192.168.0.153服务器上
[root@master scripts]# cat build_lnp.sh #!/bin/bash APP_NAME=$1 NGINX_CONF=$2 BASE_IMG=$3 FINISH_IMG=$4 BUILD_NUM=$5 test -d /data/tmp || mkdir /data/tmp -p if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ] || [ ! -n "$4" ] || [ ! -n "$5" ] ;then echo "build error,please input vars" exit 1 else ADD_FILE="ADD ${APP_NAME} /opt/www/" ADD_TWO="ADD ${NGINX_CONF} /etc/nginx/conf.d/" cp /data/build/Dockerfile_template /data/tmp/Dockerfile sed -i "s#BASE_IMG#$BASE_IMG#" /data/tmp/Dockerfile sed -i "s#ADD_FILE#$ADD_FILE#" /data/tmp/Dockerfile sed -i "s#ADD_TWO#$ADD_TWO#" /data/tmp/Dockerfile docker build --no-cache=true -t ${FINISH_IMG}:${BUILD_NUM} /data/tmp/ docker push ${FINISH_IMG}:${BUILD_NUM} echo "build img is ${FINISH_IMG}:${BUILD_NUM}" fi rm -rf /data/tmp/*
DOCKERFILE 模板
[root@master build]# cat Dockerfile_template
FROM BASE_IMG
MAINTAINER netop
ENV REFRESHED_AT 2017-11-09
ADD_FILE
ADD_TWO
创建K8S pod
[root@master scripts]# cat start_app.sh #!/bin/sh WWW_NAME=$1 APP_NAME=$2 FINISH_IMG=$3 BUILD_NUM=$4 APP_DIR="/data/scripts" APP_YAML="$5" if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ] || [ ! -n "$4" ] || [ ! -n "$5" ];then echo "build error,please input vars" else \cp $APP_DIR/deploy/example-app.yaml $APP_DIR/app/$APP_YAML sed -i s"/example/${APP_NAME}/g" ${APP_DIR}/app/${APP_YAML} sed -i s"/myimages/"${FINISH_IMG}"/g" ${APP_DIR}/app/${APP_YAML} sed -i s"/build_num/${BUILD_NUM}/g" ${APP_DIR}/app/${APP_YAML} sed -i s"/www.host.com/${WWW_NAME}/g" ${APP_DIR}/app/${APP_YAML} fi kubectl apply -f $APP_DIR/app/$APP_YAML --record
k8s APP yaml模板
[root@master scripts]# cat deploy/example-app.yaml
apiVersion: v1
kind: Service
metadata:
name: alpine-example
spec:
ports:
- port: 80
targetPort: 80
selector:
app: alpine-example
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: alpine-example
spec:
replicas: 1
minReadySeconds: 60
strategy:
rollingUpdate:
maxSurge: 50%
template:
metadata:
labels:
app: alpine-example
spec:
containers:
- name: alpine-example
image: 192.168.0.153:5000/myimages:build_num
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: alpine-example
spec:
rules:
- host: www.host.com
http:
paths:
- path: /
backend:
serviceName: alpine-example
servicePort: 80
[root@master two]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE
alpine-fabaoguo-3144943877-l9l8j 1/1 Running 0 22m 10.254.97.131 master
Traefik 上查看