minikube에서 docker registry 설치하기(m1 mac)

Dev-Hi·2023년 4월 8일
0

k8s

목록 보기
2/2

개발 환경

  • mac os
  • apple silicon(m1)
  • docker desktop
  • minikube
  • curl 명령어
  • kubectl

1. 실행가이드

1.1 minikube 실행

다음의 명령어를 통해 kubernetes를 기존 docker runtime으로 설치한다.

minikube start --driver=docker

1.2 private docker registry 활성화

minikube addons enable registry

위의 명령어를 입력하면 registry가 사용하는 port번호가 다음과 같이 나온다는 것을 확인할 수 있다.

사용자(본인)가 tls 인증서 없이, image pull을 할 수 있도록 설정한다.

minikube config set insecure-registry "10.0.0.0/24"

curl 명령어를 통해, registry에 접속할 수 있는지 확인하자.

curl http://localhost:49802/v2/_catalog # 49802 대신, 터미널에 출력된 port번호 입력

저장한 image가 존재하지 않기 때문에, 위와 같이 repositories의 값이 []인 것을 확인할 수 있다.

위의 명령어에서 ip주소가 10.0.0.0/24인 이유는 아래의 공식문서 일부 참고.

Because the default service cluster IP is known to be available at 10.0.0.1, users can pull images from registries deployed inside the cluster by creating the cluster with minikube start --insecure-registry "10.0.0.0/24".


2. docker registry test

테스트에 사용된 도구

  • intellij
  • jib
  • jdk1.8
  • kubectl
  • chrome
  • curl

다음의 절차로 docker registry를 test한다.

  1. Push image of spring app to the registry
  2. Check existence of the image in the registry
  3. Deploy the image to kubernetes

이전에 window에서 동일한 작업을 했던 minikube에서 private docker registry 설치 및 활용하기에서 사용한 spring app을 사용하자

2.1 Push image of spring app to the registry

build.gradlejib.to.image에서 port번호 변경


plugins {
    id 'org.springframework.boot' version '2.7.3'
    id 'io.spring.dependency-management' version '1.0.13.RELEASE'
    id 'java'
    id 'com.google.cloud.tools.jib' version '3.3.1' // add the plugin if not exist
}


jib {
    to {
        image = "localhost:49802/example" 
    }

    // local docker registry를 사용하기 때문에, http 허용
    allowInsecureRegistries true
}

gradlew가 있는 경로에서 다음의 명령어를 통해, spring app 빌드부터 image push까지 진행한다.

./gradlew jib

2.2 Check existence of the image in the registry

이전에 입력했던 curl 명령어를 다시 한번 입력해서, image가 registry에 올라갔는지 확인하자.

curl http://localhost:49802/v2/_catalog # 49802 대신, 터미널에 출력된 port번호 입력

결과는 다음과 같다.

2.3 Deploy the image to Kubernetes

이전 글에서 사용됐던 deployment.yaml를 사용하자. 이를 위해서는 image가 들어있는 registry에 대한 cluster ip주소를 알아야한다. 다음의 명령어를 통해 ip주소를 알 수 있다.

kubectl get svc -n kube-system

NAMEregistry인 부분의 CLUSTER-IP를 확인하면된다.

이전에 사용했던 deployment.yamlspec.containers[].image 값에 반영하면된다.
작성자의 경우 window와 달리 mac에서는 pod에서 OOMKilled가 발생하여 resource.limit.memory 값을 512Mi로 늘렸다.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: 10.97.23.247/example
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              memory: "512Mi"
              cpu: "500m"
          ports:
            - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  name: myapp-svc
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 31212

아래의 명령어를 이용하여, service, deployment를 kubernetes에 적용하자.

kubectl apply -f example-app-deployment.yaml

kubectl get pods를 입력하여 spring app이 정상적으로 deploy 됐는지 확인하자.

이번에는 local port(브라우저에서 입력할 port번호)에서 service port로 forwarding하는 방식을 사용하자.

우선 위의 deployment.yaml에 존재하는 service에 따르면 port번호 80으로 traffic이 들어오면 이 요청을 port번호 8080으로 forwarding 하는 것을 알 수 있다. kubernetes 내에서 이 service의 port를 드러낸다.

한편, kubernetes 외부에 있는 우리의 브라우저(e.g. 크롬)는 우리가 정한 port에서 service port로 forwarding해서 사용하면된다. 아래의 명령어를 통해 이를 진행하자.

sudo kubectl port-forward service/myapp-svc 23456:80

위의 명령어에 의해, local port번호 23456(임의로 정한 번호) -> service port번호 80으로 forwarding 되고,
우리가 정의한 service에 의해, service port번호 80 -> container port번호 8080으로 forwarding 된다. 명령어의 결과는 다음과 같다.

이제 브라우저에서 요청을 하자. localhost:23456/ping을 브라우저에서 입력하면 화면에 pong이라는 글자가 나오는 것을 확인할 수 있다.

3. 참고사항(2022-04-08 기준)

registry container에서 image를 영속화하기 위한 volume이 없다. registry deployment의 volume 부분을 확인하고, k9s에서 pvc도 확인했는데 관련된 부분은 존재하지 않았다. 실제로 테스트도 진행했는데, ./gradlew jib를 이용하여 spring app image를 registry에 push 한 후, registry container를 재실행 후, curl로 확인하면 저장된 image가 존재하지 않는다는 것을 확인했다.

./gradlew jib 이후, curl로 다음의 결과를 확인한 후,

k9s를 이용하여 registry container를 재실행을 했다.

이후 다시 curl로 다시 확인하면,

이전에 올렸던 image가 존재하지 않은 것을 확인할 수 있다.

그리고 minikube는 docker container위에서 실행된다. 따라서 컴퓨터를 재부팅할 때, minikube도 다시 시작해야하는데, 문제는 시작할 때마다, registry의 port가 변경된다. 따라서, build.gradle에서 jib.image.to에서 registry port를 계속 변경해야하므로 번거롭다.

minikube에서 제공하는 registry를 사용하는 대신, registry에 대한 deployment와 service를 다음에 직접 작성해야겠다.

0개의 댓글