2023.08.27 - [인프라/Kubernetes] - Kubernetes 실습 및 서비스 구축
Kubernetes 실습 및 서비스 구축
예상 k8s 서비스 아키텍쳐 Kubernetes 란? 쿠버네티스는 컨테이너화된 서비스를 쉽고 빠르게 배포,확장,관리하기 자동화 해주는 오픈소스 플랫폼입니다. k8s 사용 이유 쿠퍼네티스는 분산 되어 있는
younyellow.tistory.com
로컬로 테스트하기에는 어렵기 때문에 ec2한 곳에 kind를 통해 k8s 구축
우선 롤링 업데이트를 하기 전에 도커 태그 관리를 위해서 깃허브 커밋의 해시 값을 가지고 태그를 관리 할 예정입니다.
ec2에 kind 설치
2023.08.27 - [인프라/Kubernetes] - Kind(Kubernetes in Docker)를 통한 kubernetes 실습 환경 구축
Kind(Kubernetes in Docker)를 통한 kubernetes 실습 환경 구축
2023.08.27 - [인프라/Kubernetes] - Kubernetes 실습 및 서비스 구축 Kubernetes 실습 및 서비스 구축 예상 k8s 서비스 아키텍쳐 Kubernetes 란? 쿠버네티스는 컨테이너화된 서비스를 쉽고 빠르게 배포,확장,관리
younyellow.tistory.com
참조해 주세요.
ec2에 설치할때 몇가지 설치가 안되어 추가로 설치 해주었습니다.
절대 snap으로 설치하면 안됩니다... 이것 때문에 고생좀 했네요
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv ./kubectl /usr/local/bin/kubectl
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 30000
- role: worker
- role: worker
워커 노드는 2개만 사용 했습니다.
kind-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-server1
spec:
replicas: 4
selector:
matchLabels:
app: my-web-server1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 1
template:
metadata:
name: my-web-server1
labels:
app: my-web-server1
spec:
containers:
- name: my-web-server1
image: sihyun2/firstservice
노드 포트
apiVersion: v1
kind: Service
metadata:
name: node-port
spec:
ports:
- name: web-server-port
port: 80
targetPort: 8080
nodePort: 30000
selector:
app: my-web-server1
type: NodePort
kubectl apply -f kind-deployment.yaml
kubectl apply -f kind-nodeport.yaml
보안 그룹 수정 후 테스트
마스터 노드 포트 포워딩
테스트
jenkinsfile 수정
pipeline {
agent any
environment{
hash = sh (script: "git log -1 --pretty=%h", returnStdout: true).trim()
}
stages {
stage("git clone") {
steps {
git branch: 'firstservice',
url:'https://github.com/Sihyun3/LearningKubernetes.git'
}
}
stage("Docker delete image"){
steps{
script {
try{
sh 'docker image rm $(docker images --filter=reference="sihyun2/firstservice:*" -q)'
}catch(e){
echo "삭제할 이미지가 없습니다."
}
}
}
}
stage("Build") {
steps {
sh "docker image build -t sihyun2/firstservice:${env.hash} ."
}
}
stage("Docker Login") {
steps {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'DockerCredential',
usernameVariable: 'DOCKER_USER_ID',
passwordVariable: 'DOCKER_USER_PASSWORD'
]]){
sh 'docker login -u $DOCKER_USER_ID -p $DOCKER_USER_PASSWORD'
}
}
}
stage("Docker push"){
steps{
sh "docker push sihyun2/firstservice:${env.hash}"
}
}
stage("publish"){
steps {
withCredentials([string(credentialsId: 'publicip', variable: 'credentialsId')]) {
sh 'echo $credentialsId'
sshagent(credentials: ['EC2SSH']) {
sh "ssh -o StrictHostKeyChecking=no ubuntu@$credentialsId 'mkdir testmaking'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@$credentialsId 'cd /home/ubuntu/temp | sudo kubectl set image deployment/my-web-server1 my-web-server1=sihyun2/firstservice:${env.hash}'"
}
}
}
}
}
}
controller 수정 후 커밋 후 푸쉬
테스트
Kubernetes 배포 자동화가 끝난 것을 볼 수 있습니다~!
Kubernetes 관련 파일은
https://github.com/Sihyun3/LearningKubernetes/tree/main
GitHub - Sihyun3/LearningKubernetes
Contribute to Sihyun3/LearningKubernetes development by creating an account on GitHub.
github.com
여기서 확인 가능합니다.
'인프라 > Kubernetes' 카테고리의 다른 글
AWS EKS 생성 (0) | 2023.09.12 |
---|---|
Kubernetes 인그레스 컨트롤러 (0) | 2023.09.03 |
Kubernetes Pods Auto Scalling (0) | 2023.09.03 |
Kubernetes 퍼시스턴트 볼륨 (0) | 2023.08.31 |
Kubernetes 디플로이먼트 정의 및 노드 포트 서비스 연동 (0) | 2023.08.28 |