k8s-使用配合Jenkins-CICD-方案

40次阅读

共计 4328 个字符,预计需要花费 11 分钟才能阅读完成。

k8s 使用配合 Jenkins CICD 方案

环境描述:

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

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

![图片上传中 …]

下图为配置中心配置(替换图中 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

![图片上传中 …]

![图片上传中 …]

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

![图片上传中 …]


curl -v http://192.168.120.17 -H 'host: dev-platform-business.skong.com'

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


![图片上传中...]

 

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

 

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




 

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'

 

正文完
 0