[Jenkins] Jenkins on Kubernetes (2)

gununoo·2022년 10월 28일
0

Jenkins

목록 보기
2/3
post-thumbnail

Gitlab 구성

  • 도커 기반의 컨테이너에서 설치된다

  • 설치 시에는 compose를 통해 설치되므로 사전에 docker-compose를 설치해 두어야 한다.

  • 192.168.8.200/24의 gitlab VM 준비

# /etc/netplan/01-network-manager-all.yaml 
network:
  ethernets:
    ens32:
      addresses: [192.168.8.200/24]
      gateway4: 192.168.8.2
      nameservers:
        addresses: [8.8.8.8, 168.126.63.1]
  version: 2 
  • kubeadm reset
root@gitlab:~# kubeadm reset 
[reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
[reset] Are you sure you want to proceed? [y/N]: y
[preflight] Running pre-flight checks
W1028 10:08:27.206962    3677 removeetcdmember.go:79] [reset] No kubeadm config, using etcd pod spec to get data directory
[reset] No etcd config found. Assuming external etcd
[reset] Please, manually reset etcd to prevent further issues
[reset] Stopping the kubelet service
[reset] Unmounting mounted directories in "/var/lib/kubelet"
[reset] Deleting contents of config directories: [/etc/kubernetes/manifests /etc/kubernetes/pki]
[reset] Deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf]
[reset] Deleting contents of stateful directories: [/var/lib/kubelet /var/lib/dockershim /var/run/kubernetes /var/lib/cni]

The reset process does not clean CNI configuration. To do so, you must remove /etc/cni/net.d

The reset process does not reset or clean up iptables rules or IPVS tables.
If you wish to reset iptables, you must do so manually by using the "iptables" command.

If your cluster was setup to utilize IPVS, run ipvsadm --clear (or similar)
to reset your system's IPVS tables.

The reset process does not clean your kubeconfig files and you must remove them manually.
Please, check the contents of the $HOME/.kube/config file.
  • compose 실행 파일 다운로드
root@gitlab:~# curl -L \
> "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 15.4M  100 15.4M    0     0  3042k      0  0:00:05  0:00:05 --:--:-- 3930k
  • compose 실행 파일 권한 변경
root@gitlab:~# chmod +x /usr/local/bin/docker-compose
  • compose 버전 확인
root@gitlab:~# docker-compose --version 
docker-compose version 1.24.1, build 4667896b
  • gitlab 설치
apt-get install -y curl openssh-server ca-certificates \
&& curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash \
&& sudo apt-get -y install gitlab-ce


       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Thank you for installing GitLab!
  • gitlab.rb 수정
root@gitlab:~# vi /etc/gitlab/gitlab.rb 
external_url 'http://192.168.8.200'
  • gitlab-ctl reconfigure
root@gitlab:~# gitlab-ctl reconfigure

  • 비밀번호 복사
cat /etc/gitlab/initial_root_password 

z/seGWt9W5n0XHc4bHBPdL+koDGAmN+ZS1kSOY9Amk8=

  • gitlab 접속 (192.168.8.200)

  • 비밀번호 변경 (test1234)

  • 새 user 생성
    Name: 홍길동
    Username: user1
    Email: user1@test.com

  • create group

    • group name: dev
  • create blank project

    • project name: test1
    • group: dev


user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1
$ git init
Initialized empty Git repository in C:/Users/user/Desktop/test1/.git/

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ touch test.txt

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ echo "hello" > test.txt

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git config --global user.name "Administrator"

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git config --global user.email "admin@example.com"

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git remote add origin http://192.168.8.200/dev/test1.git

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git add .
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git commit -m "Initial commit"
[master (root-commit) bdd71ea] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.8.200/dev/test1.git
 * [new branch]      master -> master


실습

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ touch Dockerfile index.html
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ vi index.html
<center>
        <h1> VERSION 1 </h1>
</center>
  • Dockerfile
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ vi Dockerfile
FROM nginx 
ADD index.html /usr/share/nginx/html/index.html
  • 로컬에 등록
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git add .
warning: LF will be replaced by CRLF in Dockerfile.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory
  • 커밋
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git commit -m "Dockerfile, index.html added"
[master 7b86ee1] Dockerfile, index.html added
 2 files changed, 5 insertions(+)
 create mode 100644 Dockerfile
 create mode 100644 index.html
  • gitlog
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git log
commit 7b86ee1206f2180b07c846d9ecb89a9fcc4a91c6 (HEAD -> master)
Author: Administrator <admin@example.com>
Date:   Fri Oct 28 11:42:16 2022 +0900

    Dockerfile, index.html added

commit bdd71ea50d83fec47f0d1830bcfba11cfe3f5d42 (origin/master)
Author: Administrator <admin@example.com>
Date:   Fri Oct 28 11:17:27 2022 +0900

    Initial commit
  • bdd71ea
  • 7b86ee1

  • git push origin master
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test1 (master)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 412 bytes | 412.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.8.200/dev/test1.git
   bdd71ea..7b86ee1  master -> master


freestyle project
test1

'Restrict where this project can be run' 체크 해제

소스코드 관리 git 선택

저장소 url 입력 -> gitlab에서 clone with HTTP 주소 복사
https://github.com/ptah0414/test1.git

build
execute shell command

# 로컬에서 이미지 만들기 
sudo docker build -t 192.168.100:5000/web:1.0 .

# 로컬에 생성된 이미지를 private-registry에 등록하기 
sudo docker push 192.168.8.100:5000/web:1.0 

# 노드에서 private registry 등록된 이미지를 끌어다가 pod로 배포하기 
sudo kubectl create deploy web--image=192.168.8.100:5000/web:1.0 

# 생성된 web 디플로이먼트를 LB와 연결하여 외부에 서비스하기
sudo kubectl expose deploy web --type=LoadBalancer --name=web-lb --port=80 --target-port=80 

빌드 실행 상태

빌드 성공

  • 접속 확인

Quiz. 기존 프로젝트를 실행한 뒤, 만약 다시 한번 실행한다면 기존 deploy, svc 와 겹치므로 오류가 발생할 것이다. 하지만 재 실행할 때마다 새로운 이미지가 만들어지고 새로운 deploy,svc 가 생성되어 모든 서비가 정상적으로 동작하도록 하고 싶다면???

# 로컬에서 이미지 만들기
docker build -t 192.168.8.100:5000/web:$(date +%y%m%d%H%M) .

# 로컬에 생성된 이미지를 private-registry 에 등록하기
docker push 192.168.8.100:5000/web:$(date +%y%m%d%H%M)

# 노드에서 p-registry 등록된 이미지를 끌어다가 Pod 로 배포하기
kubectl create deploy web-$(date +%y%m%d%H%M) --image=192.168.8.100:5000/web:$(date +%y%m%d%H%M)

# 생성된 web 디플로이먼트를 LB 와 연결하여  외부에 서비스 하기
kubectl expose deploy web-$(date +%y%m%d%H%M) --type=LoadBalancer --name=web-lb-$(date +%y%m%d%H%M) --port=80 --target-port=80

실습

  • github repo 생성

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$  touch Jenkinsfile Dockerfile index.html

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$ ll
total 0
-rw-r--r-- 1 user 197609 0 Oct 28 15:49 Dockerfile
-rw-r--r-- 1 user 197609 0 Oct 28 15:49 Jenkinsfile
-rw-r--r-- 1 user 197609 0 Oct 28 15:49 index.html
  • index.html
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$ echo "<h2> first verision </h2>" > index.html
  • Dockerfile
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$ vi Dockerfile
FROM nginx 
ADD index.html /usr/share/nginx/html/index.html
  • Jenkinsfile
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$ vi Jenkinsfile
pipeline {
    agent any
    stages { 
        stage('git clone or git pull') {
            steps {
                git url: 'https://github.com/ptah0414/test2.git', branch: 'master'
            }
        }

        stage('docker image build and push to private registry') {
            steps { 
                sh '''
                docker build -t 192.168.8.100:5000/testweb:blue . // 이미지 생성
                docker push 192.168.8.100:5000/testweb:blue       // 이미지 푸쉬
                '''
            }
        }

        stage('deployment, svc creation') {
            steps {
                sh '''
                kubectl create deploy testweb \
                --image=192.168.8.100:5000/testweb:blue 
                
                kubectl expose deploy testweb \
                --type=LoadBalancer \
                --port=80 \
                --target-port=80 \
                --name=testweb-svc 
                '''
            }
        }

    }
}
  • git push
user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2
$ git init
Initialized empty Git repository in C:/Users/user/Desktop/test2/.git/

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2 (master)
$ git remote add origin https://github.com/ptah0414/test2.git

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2 (master)
$ git add .
warning: LF will be replaced by CRLF in Dockerfile.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in Jenkinsfile.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2 (master)
$ git commit -m "initial commit"
[master (root-commit) d649e48] initial commit
 3 files changed, 39 insertions(+)
 create mode 100644 Dockerfile
 create mode 100644 Jenkinsfile
 create mode 100644 index.html

user@LAPTOP-CISI8I61 MINGW64 ~/Desktop/test2 (master)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 737 bytes | 737.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ptah0414/test2.git
 * [new branch]      master -> master

  • pipeline 생성
  • 다음과 같이 추가
  • 빌드 배포 확인
root@master:~# k get deploy,svc
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/jenkins   1/1     1            1           24h
deployment.apps/testweb   1/1     1            1           48m

NAME                    TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)        AGE
service/jenkins         LoadBalancer   10.101.100.100   192.168.8.201   80:30782/TCP   24h
service/jenkins-agent   ClusterIP      10.106.40.101    <none>          50000/TCP      24h
service/kubernetes      ClusterIP      10.96.0.1        <none>          443/TCP        31h
service/testweb-svc     LoadBalancer   10.98.39.107     192.168.8.202   80:31059/TCP   48m
  • 접속 확인
profile
take a look

0개의 댓글