상세 컨텐츠

본문 제목

K8s hello world Spring boot deploy

Splunk/Observability

by 야솔아빠 2023. 7. 6. 23:47

본문

반응형

https://yogeshpatel.hashnode.dev/hello-world-in-kubernetes

위 링크를 참고

1.1 ~ 1.2는 아래 방법으로 변경

1.1 
Select maven, java(8), jar and add spring web dependency

 

 

1.2 새로운 HelloWorldController.java 파일 생성 및 아래 코드 추가

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
public class HelloWorldController {
    @RequestMapping(value = "hello-world", method= RequestMethod.GET)
    public String sayHello() {
        return "hello world";
    }
}

1.3

mvn clean install

1.4

java -jar target/demo-0.0.1-SNAPSHOT.jar

1.5 test in your web browser

http://localhost:8080/hello-world

Step-2 Create a docker image

2.1 Dockerfile 생성

vi Dockerfile

아래 코드 추가 및 저장

FROM openjdk:17

WORKDIR /opt/app

COPY target/demo-*.jar /opt/app/app.jar

ADD docker-entrypoint.sh /opt/app/docker-entrypoint.sh

RUN chmod a+rx /opt/app/docker-entrypoint.sh

ENTRYPOINT ["/opt/app/docker-entrypoint.sh"]

2.2 docker-entrypoint.sh 생성

#!/bin/bash

java -jar /opt/app/app.jar

2.3 Docker build

docker build -t demo:1.0.0 .

2.4 check docker image in registry

docker images

2.5 run docker image

docker run -p 8081:8080 -d --name=hello-world-1 -t demo:1.0.0

2.6 docker process check run or not

docker ps

2.7 show docker logs

docker logs hello-world-1

2.8 test in your web browser

http://localhost:8080/hello-world

2.9 Stop docker container

docker stop hello-world-1

 

 

Step-3 Create Kubernetes Pod

3.1 Create deployment.yaml file 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
  namespace: default
  labels:
    app: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: ijung075/demo:1.0.0
        ports:
        - containerPort: 80

3.2 docker login

docker login

3.3 docker build

ijung075:  docker hub의 user name

docker build --tag ijung075/demo:1.0.0 .

3.4 puch image to docker hub

docker push ijung075/demo:1.0.0

docker hub 에 접속하여 image push 되었는지 확인

 

3.5 apply

kubectl apply -f ./deployment.yaml

3.6 pod check

kubectl get pods -A

3.7 To check logs of a specific pod

kubectl logs hello-world-deployment-cbd86776-29flx

3.8 Expose pod to outside cluster. (여기부터 안됨)

kubectl expose pod hello-world --type=LoadBalancer --port=8080 --target-port=8080

3.9 extneral ip 확인

kubectl get service

3.10 test

http://ip:8080/hello-world
반응형

관련글 더보기

댓글 영역