ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Kubernetes] Jenkins Pod 설치
    인프라/Kubernetes 2020. 12. 2. 16:01
     

    jenkins - Docker Hub

    DEPRECATION NOTICE This image has been deprecated in favor of the jenkins/jenkins:lts image provided and maintained by Jenkins Community as part of project's release process. The images found here will receive no further updates after LTS 2.60.x. Please ad

    hub.docker.com

     

    # vi jenkins.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: jenkins
      namespace: infra
      labels:
        app: jenkins
    spec:
      #nodeName: kube-node2
      containers:
      - name: jenkins
        image: jenkins:2.60.3
        ports:
        - containerPort: 8080
        #- containerPort: 50000 #agentPort
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
      volumes:
      - name: jenkins-home
        hostPath:
          path: /jenkins
          type: DirectoryOrCreate
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: jenkins
      namespace: infra
    spec:
      type: NodePort
      selector:
        app: jenkins
      ports:
        - protocol: TCP
          port: 8080
          targetPort: 8080
          nodePort: 30080
    

    Jenkins official tags가 2.60.3이 최신인줄알고 tag를 지정했는데 최신이 아니었지만 일단 넘어가고 아래에서 살펴보자

    위의 YAML을 30080 port로 외부에서 jenkins에 접근가능하고 mountPath는 여기를 참조하여 volume을 잡았다.

    ( 지금은 namespace가 infra인 Pods(ex: docker-registry, jenkins, svn(나중에생성))는 kube-node2한개씩만 생성할 것이기 때문에 volume을 hostPath로 하고 있다.

    이후 챕터로 spring-boot ehcache 동기화를 진행할 것인데 그때는 서로다른 node에 nfs 유형으로 volume을 생성하여 동기화 해 줄 것이다.

     

    Events:
      Type     Reason   Age              From     Message
      ----     ------   ----             ----     -------
      Normal   Pulling  39s              kubelet  Pulling image "jenkins:2.60.3"
      Normal   Pulled   6s               kubelet  Successfully pulled image "jenkins:2.60.3" in 33.007315529s
      Normal   Created  5s (x2 over 5s)  kubelet  Created container jenkins
      Normal   Pulled   5s               kubelet  Container image "jenkins:2.60.3" already present on machine
      Normal   Started  4s (x2 over 5s)  kubelet  Started container jenkins
      Warning  BackOff  3s (x2 over 4s)  kubelet  Back-off restarting failed container
    # kubectl logs pods/jenkins -n infra
    touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
    Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    그러나 BackOff가 뜨면서 실패를하는데 logs로 확인해보니 volume에 write Permission문제인 듯 보이다.

     

    [root@kube-node2 /]# ll | grep jenkins
    drwxr-xr-x.   2 root root    6 12월  2 11:56 jenkins

    해당 Node에 가서 생성되어있는 /jenkins 디렉토리의 others 권한에 write가 지정되어있지않아 발생한 듯 보이니 추가를 해주자.

    [root@kube-node2 /]# chmod 757 jenkins
    [root@kube-node2 /]# ll | grep jenkins
    drwxr-xrwx.   2 root root    6 12월  2 11:56 jenkins

    권한을 추가

     

    # kubectl replace --force -f jenkins.yaml
    pod "jenkins" deleted
    service "jenkins" deleted
    pod/jenkins replaced
    service/jenkins replaced
    # kubectl get pods -n infra
    NAME              READY   STATUS    RESTARTS   AGE
    docker-registry   1/1     Running   0          26m
    jenkins           1/1     Running   0          48s

    jenkins가 정상적으로 실행되었다!

     

    접속이 잘된다! Getting Started를 따라서 해보자.

     

    # kubectl exec -it jenkins -n infra -- cat /var/jenkins_home/secrets/initialAdminPassword
    f7c028b73427410b90cacd4721feed94

    입력해주자

    Install suggested plugins을 선택했다.

    아마 전부 다 실패가 뜰 것이다...

    # kubectl logs jenkins -n infra
    .
    .
    .
     - You must update Jenkins from v2.60.3 to v2.150.1 or later to run this plugin.
    .
    .
    .
     - You must update Jenkins from v2.60.3 to v2.138.4 or later to run this plugin.
    .
    .
    .
    Caused by: java.io.IOException: Jackson 2 API Plugin v2.11.1 failed to load.
     - You must update Jenkins from v2.60.3 to v2.164.3 or later to run this plugin.

    버전문제로 보인다. 수동으로 plugin을 설치해야겠다.

    continue로 넘어가고

    계정을 만들고 접속하자!

     

    정상접속 완료!

     

    왼쪽에 Jenkins 관리로 들어가보면

    이렇게 오류가 주루루룩 뜰 것이다.

    더 아래로 내려가보면 이렇게 신규버전을 받을 수 있다고 여기에 링크 걸어놓았다.

    여기에서 오른쪽을 클릭 후 링크를 복사하자.

    https://updates.jenkins.io/download/war/2.249.3/jenkins.war

    위의 경로가 보이니 jenkins.war를 이용해서 update 후 image도 새로 만들어야 할 듯 싶다.

     

    # vi Dockfile
    FROM jenkins:2.60.3
    USER root
    RUN wget https://updates.jenkins.io/download/war/2.249.3/jenkins.war && \
    mv ./jenkins.war /usr/share/jenkins/ && \
    chown jenkins:jenkins /usr/share/jenkins/jenkins.war
    # docker build -t docker-registry.com:30500/jenkins:v2.249.3 .

    Dockfile을 생성해서 update한 다음 image로 만들어보자!

    # docker images
    REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
    docker-registry.com:30500/jenkins    v2.249.3            ba1ad5a47d56        4 minutes ago       763MB

    이전에 만들었던 private docker registry 서버로 push를 날리자. (모르시는 분들은 여기를 참조)

    # docker push docker-registry.com:30500/jenkins:v2.249.3
    The push refers to repository [docker-registry.com:30500/jenkins]
    50076f410896: Pushed
    0577e068c587: Pushed
    b1891bf16fa7: Pushed
    37c1d818eb0b: Pushed
    d51e4482f53a: Pushed
    ceed640cbb93: Pushed
    047f9c957a2e: Pushed
    1db731634011: Pushed
    a0775f499ef1: Pushed
    c19390bb619a: Pushed
    6ff38243bfb8: Pushed
    9fe468dbb76f: Pushed
    571ae0d6961a: Pushed
    518c9e7eb326: Pushed
    c3ebb2aa7787: Pushed
    9c2e8b91bfa8: Pushed
    c477b6c8ca45: Pushed
    fa0c3f992cbd: Pushed
    ce6466f43b11: Pushed
    719d45669b35: Pushed
    3b10514a95be: Pushed
    v2.249.3: digest: sha256:e7e72744a03769cf03d7ce8ea90414db75b750013518203c00cd1d14e632d384 size: 4713
    # curl docker-registry.com:30500/v2/_catalog
    {"repositories":["jenkins","test"]}
    # curl docker-registry.com:30500/v2/jenkins/tags/list
    {"name":"jenkins","tags":["v2.249.3"]}

    docker-registry에 생성확인

     

    # kubectl delete -f jenkins.yaml
    pod "jenkins" deleted
    service "jenkins" deleted

    기존에 존재하던 jenkins Service, Pod를 삭제해주자

    apiVersion: v1
    kind: Pod
    metadata:
      name: jenkins
      namespace: infra
      labels:
        app: jenkins
    spec:
      nodeName: kube-node2
      containers:
      - name: jenkins
        #image: jenkins:2.60.3
        image: docker-registry.com:30500/jenkins:v2.249.3
        ports:
        - containerPort: 8080
        #- containerPort: 50000 #agentPort
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
      volumes:
      - name: jenkins-home
        hostPath:
          path: /jenkins
          type: DirectoryOrCreate
    ---
    .
    .
    .

    spec.containers.name[jenkins].image 를 jenkins:2.60.3 -> docker-registrydocker-registry.com:30500/jenkins:v2.249.3 로 변경하자

    # kubectl apply -f jenkins.yaml
    pod/jenkins created
    service/jenkins created

    그리고 다시 적용!

    # kubectl describe pods/jenkins -n infra
    .
    .
    .
    Events:
      Type    Reason   Age   From     Message
      ----    ------   ----  ----     -------
      Normal  Pulling  12s   kubelet  Pulling image "docker-registry.com:30500/jenkins:v2.249.3"
      Normal  Pulled   5s    kubelet  Successfully pulled image "docker-registry.com:30500/jenkins:v2.249.3" in 6.500047366s
      Normal  Created  5s    kubelet  Created container jenkins
      Normal  Started  5s    kubelet  Started container jenkins

    정상적으로 생성되었다.

     

    새로 적용이 되었다.

     

    Jenkins 관리 -> About Jenkins를 통해 버전을 확인하자

     

    Jenkins 관리 -> 플러그인 관리를 들어가보면 정상적으로 플러그인들이 설치되어있는 것을 확인할 수 있다.

     

    이제 SVN을 추가로 설치한 후 Jenkins + SVN을 통해 springboot application에 대한 image를 생성하고 docker-registry에 push 하여 kubernetes cluster가 pod를 update하는 과정을 진행해 봐야겠다.

     

    댓글

Designed by Tistory.