sudo apt-get update
sudo apt install openjdk-11-jre-headless
wget -q -O - [https://pkg.jenkins.io/debian-stable/jenkins.io.key](https://pkg.jenkins.io/debian-stable/jenkins.io.key) | sudo apt-key add -
sudo sh -c 'echo deb [https://pkg.jenkins.io/debian-stable](https://pkg.jenkins.io/debian-stable) binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
sudo service jenkins status
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
/etc/sudoeres
파일을 vi 로 연다sudo vi /etc/sudoers
jenkins ALL=(ALL) NOPASSWD: ALL
sudo su - jenkins
sudo apt install [docker.io](http://docker.io/)
sudo usermod -aG docker jenkins
eksctl
을 사용하기 위해 AWS CLI를 설치한다.sudo apt install awscli
aws --version
aws configure
명령어를 입력하면 아래 내용을 입력해야한다.1. AWS Access Key ID [None]:
2. AWS Secret Access Key [None]:
3. Default region name [None]: 가용영역에서 확인
4. Default output format [None]: json
curl -LO "[https://storage.googleapis.com/kubernetes-release/release/$](https://storage.googleapis.com/kubernetes-release/release/$)(curl -s [https://storage.googleapis.com/kubernetes-release/release/stable.txt](https://storage.googleapis.com/kubernetes-release/release/stable.txt))/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin
kubectl version
curl --silent --location "[https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$](https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$)(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
eksctl version
eksctl create --name 클러스터 이름 --version 버전 --region 지역명(한국은 ap-northeast-2) --nodegroup-name 노드그룹이름 --node-type 노드타입 --nodes 노드개수 --nodes-min 지정할 노드 최소개수 --nodes-max 지정할 노드 최대개수 --ssh-access --ssh-public-key 워커노드접속에 사용할 키 --managed
kubectl get pods
명령을 입력해보면 아래와 같은 오류가뜨는데, 해당 글을 참조하여 https://enumclass.tistory.com/257 해결하였다.kubectl 1.24.x 버전에서 eks와 연동해서 사용할 경우 발생하는 오류이며 kubectl 버전을 1.23.6 버전을 내리니 문제가 해결되었다.
couldn't get current server API group list: ~~~
getting credentials: decoding stdout: no kind "ExecCredential" is registered for version "client.authentication.k8s.io/v1alpha1" in scheme "pkg/client/auth/exec/exec.go:62"
CloudBees AWS Credentials Plugin
Docker Pipeline
Amazon ECR plugin
/github-webhook/
을 붙여준다.Pipeline script from SCM
을 선택하고 Github 정보를 넣는다.(branch는 main으로 변경)GitHub hook trigger for GITScm polling
을 선택kubectl create serviceaccount jenkins-deployer
kubectl create clusterrolebinding jenkins-deployer-role --clusterrole**==**cluster-admin --serviceaccount**=**default:jenkins-deployer
clusterrolebinding
: 생성되는 오브젝트가 clusterrolebinding임을 나타냄jenkins-deployer-role
: 생성되는 오브젝트 이름--clusterrole**==**cluster-admin
: cluster-admin 역할을 부여--serviceaccount**=**default:jenkins-deployer
: default에 있는 jenkins-deployer 서비스어카운트에 이 권한을 부여secret.yaml
파일생성후 아래명령어로 secret 생성kubectl create -f ./secret.yaml
kubectl describe secret mysecretname
FROM openjdk:11-jre-slim
WORKDIR /backend
RUN ./gradlew build
WORKDIR /build/libs
COPY {빌드된 jar 파일이름} ./
CMD nohup java -jar {빌드된 jar 파일이름} & #jar파일 백그라운드 실행
EXPOSE 8080
pipeline {
agent any
environment{
REGION = 'ap-northeast-2'
EKS_API = {EKS Cluster Api Server Endpoint}
EKS_CLUSTER_NAME = {EKS Cluster 이름}
EKS_JENKINS_CREDENTIAL_ID = 'kubectl-deploy-credentials'
ECR_PATH = {ECR로 가서 Repository의 URI을 가져오되 /repository-name 은 제거}
ECR_IMAGE = 'ecr 이미지 이름'
AWS_CREDENTIAL_ID = 'AWSCredentials'
}
stages {
stage('Clone Repository'){
checkout scm
}
stage('Docker Build'){
docker.withRegistry("https://${ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_ID}"){
image = docker.build("${ECR_PATH}/${ECR_IMAGE}")
}
}
stage('Push to ECR'){
docker.withRegistry("https://{ECR_PATH}", "ecr:${REGION}:${AWS_CREDENTIAL_ID}"){
image.push("v${env.BUILD_NUMBER}")
}
}
stage('CleanUp Images'){
sh"""
docker rmi ${ECR_PATH}/${ECR_IMAGE}:v$BUILD_NUMBER
docker rmi ${ECR_PATH}/${ECR_IMAGE}:latest
"""
}
stage('Deploy to k8s'){
withKubeConfig([credentialsId: "{EKS_JENKINS_CREDENTIAL_ID}",
serverUrl: "${EKS_API}",
clusterName: "${EKS_CLUSTER_NAME}"]){
sh "sed 's/IMAGE_VERSION/v${env.BUILD_ID}/g' service.yaml > output.yaml"
sh "aws eks --region ${REGION} update-kubeconfig --name ${EKS_CLUSTER_NAME}"
sh "kubectl apply -f output.yaml"
sh "rm output.yaml"
}
}
}
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: springboot-app
image: {ECR_Path값/repository 이름}:IMAGE_VERSION
imagePullPolicy: Always
ports:
-containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
type: LoadBalancer
selector:
app: hello-world
ports:
- protocol: TCP
port: 80
targetPort: 8080
https://velog.io/@woo6_0/AWS-내-K8S를-위한-서비스-비용-정리
https://jhooq.com/aws-kubernetes-jenkins-pipeline/
http://incredible.ai/engineering/2021/10/18/Jenkins-Github-ECR-Kubernetes/#27-plugins