kubernetes下jenkins 搭建实例

之前用Docker搭建过Jenkins,使用Ubuntu14的镜像,开通ssh后自己上传tomcat、jdk、jenkins包。所有东西都放到了容器里,整个容器体积会越来越大,不方便备份恢复数据。

最近开始测试kubernetes下自动化测试环境的搭建(参考:http://dockone.io/article/859)。

考虑到k8s的容器分散在各个机器上,不太方便ssh来ssh去,于是不再采用ssh的方法登录容器,而是直接把jenkins需要的软件环境放到物理机上(比如都在 /ceph/docker/下),通过volumeMounts把目录挂载到容器里。即解决了容器体积大的难题,同时方便了修改配置,有什么需要修改的东西直接改物理容器上的即可,一经修改,所有容器都生效。

这应该是k8s下创建容器比较正规的方法,即所有应用数据都通过volumeMounts挂载到容器里来运行,镜像只提供一个应用运行的环境,这样做好处大大地。


rc_jenkins.yaml:

apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-jenkins
spec:
  replicas: 1
  selector:
    name: jenkins
  template:
    metadata:
      labels:
        name: jenkins
    spec:
      containers:
        - name: jenkins
          image: kimqcn/u14.04
#         command: ["/usr/sbin/sshd","-D"]
#         command: ["/tomcat_rc_9999/bin/catalina.sh","run"]
          command: ["/tomcat/bin/catalina.sh","run"]
          ports:
            - containerPort: 22
            - containerPort: 8080
#            - containerPort: 9999
          volumeMounts:
            - name: jdk-home
              mountPath: /jdk1.7.0_75
#            - name: app-rc-home
#              mountPath: /tomcat_rc_9999
            - name: app-tomcat-home
              mountPath: /tomcat

            - name: jenkins-home
              mountPath: /jenkins_home
            - name: docker-home
              mountPath: /docker_home
            - name: tool-home
              mountPath: /tool
            - name: grails-home
              mountPath: /root/.grails
            - name: m2-home
              mountPath: /root/.m2
            - name: mozilla-home
              mountPath: /root/.mozilla

      volumes:
         - name: jdk-home
           hostPath:
              path: /ceph/docker/jdk1.7.0_75
#         - name: app-rc-home                                                                                     
#           hostPath:                                                                                             
#              path: /ceph/docker/tomcat_rc_9999                                                                  
         - name: app-tomcat-home                                                                                  
           hostPath:                                                                                              
              path: /ceph/docker/jenkins_8080 
                                                                    
         - name: jenkins-home                                                                                     
           hostPath:                                                                                              
              path: /ceph/docker/jenkins_home                                                                     
         - name: docker-home                                                                                      
           hostPath:                                                                                              
              path: /ceph/docker/                                                                                 
         - name: tool-home                                                                                        
           hostPath:                                                                                              
              path: /ceph/docker/tool                                                                             
         - name: grails-home                                                                                      
           hostPath:                                                                                              
              path: /ceph/docker/root/.grails                                                                     
         - name: m2-home                                                                                          
           hostPath:                                                                                              
              path: /ceph/docker/root/.m2                                                                         
         - name: mozilla-home                                                                                     
           hostPath:                                                                                              
              path: /ceph/docker/root/.mozilla

说明:

          image: kimqcn/u14.04  是定制过的ubuntu14.04镜像,主要是安装了unzip和wget,增加了zh_CN字符集的支持,调整了时区设置为Asia/Harbin等等,这都是对jenkins正常工作需要的东西。

          command: ["/tomcat/bin/catalina.sh","run"] 是启动jenkins的命令。这个地方是个小陷阱,如果不在rc里指定一个可以持续运行的脚本,容器启动后马上就会自动停止,然后k8s会欠欠儿的去执行自己的本职工作:重新再启动一个容器,然后容器又自动停止,再创建,直到永远...

          app-tomcat-home 是jenkins的volumeMounts挂载配置。其他volumeMounts配置也都是jenkins运行所需要的,比如jdk和.grails目录等。


启动rc:kubectl create -f rc_jenkins.yaml

再启动一个svc: kubectl create -f svc_jenkins.yaml

svc_jenkins.yaml :
apiVersion: v1
kind: Service
metadata:
  name: jenkins-svc-nodeport
spec:
  ports:
    - port: 8022
      name: ssh-port
      targetPort: 22
      protocol: TCP
    - port: 8080
      name: tomcat-app-port
      targetPort: 8080
      protocol: TCP
#    - port: 8099
#      name: tomcat-rc-port
#      targetPort: 9999
#      protocol: TCP
  type: NodePort
  selector:
    name: jenkins


测试通过,工作正常。


你可能感兴趣的:(kubernetes下jenkins 搭建实例)