반응형
K8s + sonarqube yaml로 배포하기
정적 소스분석 툴
: 20개 이상의 프로그래밍 언어에서 버그 , 중복 코드, 코드 복잡도, 보안 취약점 등을 발견할 목적으로 정적 코드 분석을하여 자동 리뷰를 수행하기 위한 지속적인 코드 품질 검사용 오픈 소스 플랫폼.
: 버전에 따라 지원하는 언어 혹은 툴이 다르기 때문에 공식문서 참고가 필수이다.
특히 , Jenkins와 연동 시 버전확인 꼭 필요.
중요 사항
- manifest (yaml) 로 설치
- Namespace : wgh-ns
- progre DB pod와 같이 쓰임
Secret 생성
- vi sonarqube-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: postgres-pwd
type: kubernetes.io/basic-auth
stringData:
password: P@s$w0rd # required field for kubernetes.io/basic-auth
PV 생성
- vi sonarqube-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonar-postgresql-pv
spec:
storageClassName: local-storage
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
persistentVolumeReclaimPolicy: Retain
hostPath:
path: k8s-data/sonar
PVC 생성
- vi sonarqube-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonarqube-postgresql-pvc
spec:
storageClassName: local-storage
volumeName: sonar-postgresql-pv # 연결할 PV 이름
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Deployment 생성 (postgres)
- vi sonarqube-progres-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube-postgres
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube-postgres
template:
metadata:
labels:
app: sonarqube-postgres
spec:
securityContext:
runAsUser: 0
fsGroup: 0
containers:
- image: postgres:14.5
name: sonarqube-postgres
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-pwd
key: password
- name: POSTGRES_USER
value: sonar
ports:
- containerPort: 5432
name: postgresport
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
subPath: postgres-data
volumes:
- name: postgres-data
persistentVolumeClaim:
claimName: sonarqube-postgresql-pvc
Service 생성 (postgres)
- vi sonarqube-progres-service.yml
apiVersion: v1
kind: Service
metadata:
name: sonarqube-postgres-svc
spec:
selector:
app: sonarqube-postgres
ports:
- port: 5432
targetPort: 5432
protocol: TCP
Deployment 생성 (sonarqube)
- vi sonarqube-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube
template:
metadata:
labels:
app: sonarqube
spec:
initContainers:
- name: increase-the-vm-max-map-count
image: busybox:1.28
command:
- sysctl
- -w
- vm.max_map_count=262144
securityContext:
privileged: true
- name: increase-the-ulimit
image: busybox:1.28
command:
- sh
- -c
- ulimit -n 65536
securityContext:
privileged: true
containers:
- image: sonarqube:8.9.9-community
name: sonarqube
env:
- name: SONARQUBE_JDBC_USERNAME
value: sonar
- name: SONARQUBE_JDBC_URL
value: jdbc:postgresql://sonarqube-postgres-svc:5432/sonar
- name: SONARQUBE_JDBC_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-pwd
key: password
ports:
- containerPort: 9000
protocol: TCP
Service 생성 (sonarqube)
- vi sonarqube-service.yml
apiVersion: v1
kind: Service
metadata:
name: sonarqube-svc
spec:
selector:
app: sonarqube
ports:
- name: http
port: 80
protocol: TCP
targetPort: 9000
nodePort: 32005 #포트고정
type: NodePort
실행 및 Test
- $ kubectl exec -it -n wgh-ns [Pod 명] -- psql -h localhost -U sonar --password -p 5432 sonar
- database 보기 : \ㅣ
- user 보기 : \du
- Cluster IP로 설치된 sonar-postgre 외부로 노출 (LoadBalancer / NodePort)
- kubectl patch svc sonarqube-svc -n wgh-ns -p '{"spec": {"type": "LoadBalancer"}}'
- kubectl patch svc sonarqube-svc -n wgh-ns -p '{"spec": {"type": "NodePort"}}'
참고사항
※ SonarQube는 sonar가 사용하는 DB인 postgre pod가 띄워져 있는 상태에서 접속을 확인한 후
SonarQube pod를 띄워 접속을 해야한다.
DB가 없는 상태에서 SonarQube pod가 먼저 뜨면 접속이 안된다.
$ curl [ 서버 IP ]:[port] 시 connection refused 가 나올 것.
※ 초기 ID / PWD 는 admin / admin 이다.
참고자료
반응형
'개발인생 > Kubernetes' 카테고리의 다른 글
Install) EFK(1) - ElasticSearch 설치 (0) | 2023.05.04 |
---|---|
install) kubernetes + Harbor (Helm으로 설치) (0) | 2023.02.27 |
install) kubernetes + jenkins (yaml 파일로 배포하기) (0) | 2023.02.09 |
install) kubernetes + GitLab (yaml 파일로 배포) (0) | 2023.02.07 |
install) kubernetes + Nexus3 (yaml 배포) (0) | 2023.02.07 |