// for k8s configMap implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes-client-config' // for spring cloud feign client implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' // for k8s service loadbalancer implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer' }
# namespace/spring created # role.rbac.authorization.k8s.io/namespace-reader created # rolebinding.rbac.authorization.k8s.io/namespace-reader-binding created
1 2 3 4
kubectl apply -f calc-deployment.yaml
# deployment.apps/calc-deployment created # service/calc-service created
반환문자열 이 ConfigMap 에 지정한 문자열 bootstrap.properties 에 ConfigMap 이 정상적으로 등록된것을 확인할 수 있다.
feign client
Spring Cloud 플랫폼에선 Eureka 를 사용해 Discovery Client 가 이루어졌지만 k8s 에선 Discovery Client 를 할 필요가 없다.
k8s CoreDNS, CNI 에 의해 라우팅, 로드밸런싱이 이루어진다.
1 2 3 4 5
@FeignClient(name = "calc-service")// service name publicinterfaceCalculatingClient { @GetMapping("/calculating/{num1}/{num2}") Long addNumbers(@PathVariable Long num1, @PathVariable Long num2); }
feignClient 와 같은 간단한 RestTemplate 구현체는 k8s DNS 에서 사용하는 정규화된 도메인 이름(FQDN) 만으로 서비스 호출이 가능하다.
Discovery Client 할 필요가 없고 내부에 client server list 또한 가지고 있을 필요가 없기에 서비스 입장에선 효율적이다.
k8s 의존성 제거
위와같이 org.springframework.cloud 라이브러리를 사용해 k8s 와 연동하는 방법도 좋지만, k8s 라이브러리 의존성을 없에고 외부에서 환경변수를 사용해 주입시키는 방법도 있다.
SpringBoot 버전에 따라 ConfigMap 을 가져오는 라이브러리도 달라지고, 언어 별로 k8s API 를 사용하는 방식이 다 다르기 때문에 k8s API 를 사용하지 않고 설정값을 가져올 수 있게 하는것이 좀 더 낫다 할 수 있다.
설정값은 아래와 같이 envFrom 을 통해 환경변수로 등록하여 사용
1 2 3 4 5 6 7 8 9 10 11
... spec: containers: -name:greet-deployment image:${REGISTRY_URL}/greeting:latest # image: greeting # imagePullPolicy: Never # 이미지가 로컬에 존재할 경우 imagePullPolicy:Always envFrom: -secretRef: name:greeting-secret
더잉상 spring-cloud-starter-loadbalancer 의존성을 통해 서버 라우팅을 진행하지 않고 k8s Service 리소스 url 을 통해 접근하도록 설정
1 2 3 4 5 6
@FeignClient(name="calc", url = "http://calc-service:8080") // @FeignClient(name="calc", url = "http://calc-service.namespace.svc.cluster.local:8080") publicinterfaceCalculatingClient { @GetMapping("/calculating/{num1}/{num2}") Long addNumbers(@PathVariable Long num1, @PathVariable Long num2); }