k8s 使用配合Jenkins CICD 方案

k8s 使用配合Jenkins CICD 方案

环境描述:

192.168.0.63为k8s集群主节点,保证你的集群里dns与ingress已经安装了

编译打包就不叙述了,主要流程就是:
编译打包-> build镜像 -> 上传镜像到私服 -> 使用ansible-playbook拷贝模板文件到集群主节点 -> 更改模板文件 -> create pod,service,ingress

k8s 使用配合Jenkins CICD 方案

下面为配置中心配置(替换图中shell)

docker images | grep ${projectname}-${environment} | gawk '{print $3}' | xargs docker rmi;
docker images | grep none | gawk '{print $3}' | xargs docker rmi;docker build -t registry.skong.com/${projectname}-${environment}:${branch} .;
docker push registry.skong.com/${projectname}-${environment}:${branch};
cp /data/source/templet/templet.yaml .; cp /data/source/templet/ingress_templet.yaml .;
sed -i ‘s/projectname/${projectname}/’ templet.yaml;
sed -i ‘s/environment/${environment}/’ templet.yaml; 
sed -i ‘s/branch/${branch}/’ templet.yaml;
sed -i ‘s/projectname/${projectname}/’ ingress_templet.yaml;
sed -i ‘s/environment/${environment}/’ ingress_templet.yaml; 
sed -i ‘s/_/-/’ ingress_templet.yaml; sed -i ‘s/_/-/’  templet.yaml;
sed -i ‘s/projectname1/${projectname}/’ templet.yaml
ansible-playbook /data/ansible/playbook/linux/install_docker_service.yml --extra-vars 'hosts=192.168.0.63 Jenvironment=${environment} sourcepath=${projectname}                               warpath=/data/jenkins/workspace/${environment}-${projectname}';

Ansible-playbook文件

[root@jenkins-master templet]# pwd
/data/source/templet
[root@jenkins-master templet]# cat install_docker_service.yml 
- name: send {{sourcepath}}_{{Jenvironment}}
  hosts: '{{hosts}}'
  tasks:
        - name: check pod {{sourcepath}}-{{Jenvironment}}
          shell: source /etc/profile && kubectl get po |grep {{sourcepath}}_{{Jenvironment}} |wc -l
          register: checkpod
          ignore_errors: True

        - name: remove {{sourcepath}}_{{Jenvironment}}
          file: path=/data/source/{{sourcepath}}_{{Jenvironment}} state=absent
        
        - name: mkdir {{sourcepath}}_{{Jenvironment}}
          file: path=/data/source/{{sourcepath}}_{{Jenvironment}} state=directory
          
        - name: copy {{warpath}}/ingress_templet.yaml to {{sourcepath}}_{{Jenvironment}}
          copy: src={{warpath}}/ingress_templet.yaml dest=/data/source/{{sourcepath}}_{{Jenvironment}}/

        - name: copy {{warpath}}/{{sourcepath}}.yaml to {{sourcepath}}_{{Jenvironment}}
          copy: src={{warpath}}/{{sourcepath}}.yaml dest=/data/source/{{sourcepath}}_{{Jenvironment}}/
        
        - name: restart {{sourcepath}}_{{Jenvironment}}
          shell: source /etc/profile && kubectl set image deployment/{{sourcepath}}-{{Jenvironment}}-deployment {{sourcepath}}-{{Jenvironment}}-pod=registry.skong.com/{{sourcepath}}-{{Jenvironment}}:master && kubectl rollout history deployment/{{sourcepath}}-{{Jenvironment}}-deployment
          when: checkpod.stdout == "1"
        
        - name: start {{sourcepath}}_{{Jenvironment}}
          shell: cd /data/source/{{sourcepath}}_{{Jenvironment}}/ && source /etc/profile && kubectl create -f ingress_templet.yaml && kubectl create -f {{sourcepath}}.yaml --record
          when: checkpod.stdout == "0"
[root@jenkins-master templet]# 
[root@jenkins-master templet]#

模板yaml文件
templet.yaml用来创建pod,service的文件,需要替换里面的项目名和环境名(#不能出现下划线)

[root@jenkins-master templet]# cat templet.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: projectname-environment-deployment
spec:
  replicas: 2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: projectname-environment-pod
    spec:
      terminationGracePeriodSeconds: 60
      containers:
        - name: projectname-environment-pod
          image: basic-repository.skong.com/skong/projectname1-environment:branch
          ports:
            - containerPort: 80
      imagePullSecrets:
        - name: kubesystemsecret

---

apiVersion: v1
kind: Service
metadata:
  name: projectname-environment-service
  labels:
    app: projectname-environment-service
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: projectname-environment-pod
[root@jenkins-master templet]#

Ingress文件是相当与nginx一样的功能

[root@jenkins-master templet]# cat ingress_templet.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: projectname-environment-ingress
  namespace: default
spec:
  rules:
  - host: environment-projectname.skong.com
    http:
      paths:
      - path: /
        backend:
          serviceName: projectname-environment-service
          servicePort: 80

k8s 使用配合Jenkins CICD 方案

k8s 使用配合Jenkins CICD 方案

创建ingress,ingress功能可以理解成nginx

k8s 使用配合Jenkins CICD 方案

curl -v
68.120.17 -H 'host: dev-platform-business.skong.com'

//如果是测试写上host   192.168.120.17 dev-platform-business.skong.com

k8s 使用配合Jenkins CICD 方案

kubectl rollout history deployment //查看deployment更新记录历史

k8s 使用配合Jenkins CICD 方案

kubectl rollout history deployment/sk-platform-business-dev-deployment

k8s 使用配合Jenkins CICD 方案

kubectl rollout undo deployment/sk-platform-business-dev-deployment --to-revision=1
//这个步骤在install_docker.yml已经添加此命令,但是Jenkins应该是不会显示,应该通过ansible命令在最后执行:
ansible 192.168.0.63 -m shell -a '/data/kubernetes/bin/kubectl rollout history deployment/sk-platform-business-dev-deployment'

k8s 使用配合Jenkins CICD 方案

相关推荐