7-1 kubernetes与cicd
没什么花头
7-2 cicd实践(1)
构建成功
7-3 cicd实践(2)
定义环境变量
上传代码到git
pipline script如下
node { env.BUILD_DIR = "/root/build-workspace/" env.MODULE = "web-demo" stage('Preparation') { // for display purposes // Get some code from a GitHub repository git credentialsId: 'e379aaee-d144-42b5-8d82-7d25382269b5', url: 'https://gitee.com/PanDaWangLuoKeJi_hugo_xu/mooc-demo.git' // Get the Maven tool. // ** NOTE: This 'M3' Maven tool must be configured // ** in the global configuration. } stage('Maven Build') { sh "mvn -pl ${MODULE} -am clean package" } stage('Build image') { sh "/root/script/build-image-web.sh" } stage('Deploy') { sh "/root/script/deploy.sh" } }
创建构建脚本/root/script/build-image-web.sh
#!/bin/bash if [ "${BUILD_DIR}" == "" ];then echo "env 'BUILD_DIR' is not set" exit 1 fi DOCKER_DIR=${BUILD_DIR}/${JOB_NAME} if [ ! -d ${DOCKER_DIR} ];then mkdir -p ${DOCKER_DIR} fi echo "docker workspace: ${DOCKER_DIR}" JENKINS_DIR=${WORKSPACE}/${MODULE} echo "jenkins workspace: ${JENKINS_DIR}" if [ ! -f ${JENKINS_DIR}/target/*.war ];then echo "target war file not found ${JENKINS_DIR}/target/*.war" exit 1 fi cd ${DOCKER_DIR} echo BUILD_DIR: ${BUILD_DIR} DOCKER_DIR: ${DOCKER_DIR} JENKINS_DIR: ${JENKINS_DIR} rm -rf * unzip -oq ${JENKINS_DIR}/target/*.war -d ./ROOT mv ${JENKINS_DIR}/Dockerfile . if [ -d ${JENKINS_DIR}/dockerfiles ];then mv ${JENKINS_DIR}/dockerfiles . fi VERSION=$(date +%Y%m%d%H%M%S) IMAGE_NAME=harbor.pdabc.com/kubernetes/${JOB_NAME}:${VERSION} echo "${IMAGE_NAME}" > ${WORKSPACE}/IMAGE echo "docker build -t $IMAGE_NAME " docker build -t $IMAGE_NAME . echo "docker push ${IMAGE_NAME}" docker push ${I
增加执行权限
chmod +x /root/script/build-image-web.sh
需要在jenkins的机子上修改/etc/hosts 添加harbor地址 以及登录harbor
7-4 cicd实践(3)
创建kubernetes的模板/root/script/template/web.yaml
#deploy apiVersion: apps/v1 kind: Deployment metadata: name: {{name}} spec: selector: matchLabels: app: {{name}} replicas: 1 template: metadata: labels: app: {{name}} spec: containers: - name: {{name}} image: {{image}} ports: - containerPort: 8080 --- #service apiVersion: v1 kind: Service metadata: name: {{name}} spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: app: {{name}} type: ClusterIP --- #ingress apiVersion: extensions/v1beta1 kind: Ingress metadata: name: {{name}} spec: rules: - host: {{host}} http: paths: - path: / backend: serviceName: {{name}} servicePort: 80
创建/root/script/deploy.sh
#!/bin/bash name=${{JOB_NAME}} image=$(cat ${WORKSPACE}/IMAGE) host=${HOST} echo "deploying ... name: ${name}, image: ${image}, host: ${host}" rm -f web.yaml cp $(dirname "${BASH_SOURCE[0]}")/template/web.yaml . sed -i "s,{{name}},${name},g" web.yaml sed -i "s,{{image}},${image},g" web.yaml sed -i "s,{{host}},${host},g" web.yaml kubectl apply -f web.yaml cat web.
7-5 cicd实践(4)
修改deploy.sh如下 添加监控检查,注意[]中的空格 否则会报错/root/script/deploy.sh: line 37: [: missing `]' 从而导致健康检查失败.
#!/bin/bash name=${JOB_NAME} image=$(cat ${WORKSPACE}/IMAGE) host=${HOST} echo "deploying ... name: ${name}, image: ${image}, host: ${host}" rm -f web.yaml cp $(dirname "${BASH_SOURCE[0]}")/template/web.yaml . echo "copy success" sed -i "s,{{name}},${name},g" web.yaml sed -i "s,{{image}},${image},g" web.yaml sed -i "s,{{host}},${host},g" web.yaml echo "ready to apply" kubectl apply -f web.yaml echo "apply ok " cat web.yaml # health check success=0 count=60 # fen ge de shi hou zi dong sheng cheng su zhu IFS="," sleep 5 while [ ${count} -gt 0 ] do replicas=$(kubectl get deploy ${name} -o go-template='{{.status.replicas}},{{.status.updatedReplicas}},{{.status.readyReplicas}},{{.status.availableReplicas}}') echo "replicas: ${replicas}" arr=(${replicas}) if [ "${arr[0]}" == "${arr[1]}" -a "${arr[1]}" == "${arr[2]}" -a "${arr[2]}" == "${arr[3]}" ];then echo "health check success!" success=1 break fi ((count--)) sleep 2 done if [ ${success} -ne 1 ];then echo "health check failed!" e
8080 jenkins端口 可能会被master误认为是api的端口 默认是访问本地的8080.
需要同步一下.kube/config
由于master01上没有安装jenkins 就复制一下生成的web.yaml文件 测试一下最后一条启动命令了
web.yaml
#deploy apiVersion: apps/v1 kind: Deployment metadata: name: pipline-step2 spec: selector: matchLabels: app: pipline-step2 replicas: 1 template: metadata: labels: app: pipline-step2 spec: containers: - name: pipline-step2 image: harbor.pdabc.com/kubernetes/pipline-step2:20191221004921 ports: - containerPort: 8080 --- #service apiVersion: v1 kind: Service metadata: name: pipline-step2 spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: app: pipline-step2 type: ClusterIP --- #ingress apiVersion: extensions/v1beta1 kind: Ingress metadata: name: pipline-step2 spec: rules: - host: k8s-web.pdabc.com http: paths: - path: / backend: serviceName: pipline-step2 servicePort: 80
kubectl apply -f web.yaml
kubectl get pods
kubectl get deploy
查看启动pod的yaml文件
kubectl get deploy pipline-step2 -o yaml
镜像没问题
获取replicas的状态
kubectl get deploy pipline-step2 -o go-template='{{.status.replicas}},{{.status.updatedReplicas}},{{.status.readyReplicas}},{{.status.availableReplicas}}'
可以通过deployment.kubernetes.io/revision的值 判断是否进行更新
在master01上起了8081的jenkins 并测试