2023.08.27 - [인프라/Kubernetes] - Kubernetes 실습 및 서비스 구축
이미지 업로드 구현
package com.example.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletResponse;
@RestController
public class Controller {
@GetMapping("/")
public ResponseEntity<String> Main() throws Exception {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
String strIpAdress = inetAddress.getHostAddress();
String html = "2번 서비스 현재 내부 아이피 주소는 <b>" + strIpAdress+"</b> 입니다.";
return ResponseEntity.status(HttpStatus.OK).body(html);
} catch (UnknownHostException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("아이피 주소를 불러 올 수 없습니다");
}
}
@GetMapping("/image.jpg")
public void getMusic( HttpServletResponse response) throws Exception {
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String UPLOAD_PATH = "/upload/";
try {
response.setHeader("Content-Disposition", "inline;");
byte[] buf = new byte[1024];
fis = new FileInputStream(UPLOAD_PATH + "image.jpg");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(response.getOutputStream());
int read;
while ((read = bis.read(buf, 0, 1024)) != -1) {
bos.write(buf, 0, read);
}
} finally {
bos.close();
bis.close();
fis.close();
}
}
}
빌드 후 이미지 업로드
PS C:\eclipse-workspace\KubernetesDemo> docker image build -t sihyun2/secondservice .
[+] Building 291.9s (11/11) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 201B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:latest 0.0s
=> [1/6] FROM docker.io/library/openjdk 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 12.84kB 0.0s
=> CACHED [2/6] RUN microdnf install findutils 0.0s
=> CACHED [3/6] RUN mkdir /project 0.0s
=> CACHED [4/6] WORKDIR /project 0.0s
=> [5/6] COPY ./ . 0.1s
=> [6/6] RUN ./gradlew build 290.0s
=> exporting to image 1.6s
=> => exporting layers 1.6s
=> => writing image sha256:158e7f4d64f996a9130e9afb39b0e1868dec74eca24c5ca12021dee5f8244cd8 0.0s
=> => naming to docker.io/sihyun2/secondservice 0.0s
PS C:\eclipse-workspace\KubernetesDemo> git push sihyun2/secondservice
fatal: 'sihyun2/secondservice' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
PS C:\eclipse-workspace\KubernetesDemo> docker push sihyun2/secondservice
Using default tag: latest
The push refers to repository [docker.io/sihyun2/secondservice]
95dc43f42f43: Pushed
63088103e987: Pushed
5f70bf18a086: Mounted from sihyun2/firstservice
1c1b3bf25883: Mounted from sihyun2/firstservice
5d9f424e0267: Mounted from sihyun2/firstservice
56285d9a7760: Mounted from sihyun2/firstservice
077bff59ce57: Mounted from sihyun2/firstservice
9cd9df9ffc97: Mounted from sihyun2/firstservice
latest: digest: sha256:7b09c1bbf94cb0ab62001ab67bb2e485d08953e0c95bc166ee157d0fe3dea1b8 size: 2005
3번 노드에 배포할 예정이니까 3번 노드에 라벨링을 해줍니다.
root@DESKTOP-Q4VBHGG:/home/yellownyou# kubectl label node kubernetes-worker3 seletor=worker3
node/kubernetes-worker3 labeled
clusterIp및 nfs pods 정의 kind-nfs-server.yaml
kind: Service
apiVersion: v1
metadata:
name: nfs-service
spec:
selector:
role: nfs
ports:
# Open the ports required by the NFS server
# Port 2049 for TCP
- name: tcp-2049
port: 2049
protocol: TCP
# Port 111 for UDP
- name: udp-111
port: 111
protocol: UDP
---
kind: Pod
apiVersion: v1
metadata:
name: nfs-server-pod
labels:
role: nfs
spec:
containers:
- name: nfs-server-container
image: cpuguy83/nfs-server
securityContext:
privileged: true
args:
- /exports
nodeSelector:
seletor: worker3
yaml 파일 실행
root@DESKTOP-Q4VBHGG:/home/yellownyou# kubectl apply -f kind-nfs-server.yaml
service/nfs-service created
pod/nfs-server-pod created
persistentvolume 정의 kind-persistent.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 2Gi # 스토리지 용량 2GB
volumeMode: Filesystem # 파일 시스템 형식
accessModes: # 읽기/쓰기 옵션
- ReadWriteMany
storageClassName: manual
nfs:
path: /
server: 10.96.255.130
pvc 작성
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
새로운 파드 작성
apiVersion: v1
kind: Pod
metadata:
name: pv-pod
spec:
volumes:
- name: pv-storage
persistentVolumeClaim:
claimName: pv-claim
containers:
- name: pv-container
image: ubuntu
args: ["tail", "-f", "/dev/null"]
volumeMounts:
- mountPath: "/upload"
name: pv-storage
pv-pod /upload에 파일 작성
원래 이미지를 넣을려고 했는데 너무 복잡해질거 같아서 텍스트로 대체하였습니다.
kind-server2-pod 작성
nodeport작성 및 포트 개방
apiVersion: v1
kind: Service
metadata:
name: node-port
spec:
ports:
- name: web-server-port
port: 80
targetPort: 8080
selector:
app: my-web-server2
type: NodePort
테스트
파일이 정상적으로 들어있는 것을 볼 수 있습니다.
Kubernetes 관련 파일은
https://github.com/Sihyun3/LearningKubernetes/tree/main
여기서 확인 가능합니다.
'인프라 > Kubernetes' 카테고리의 다른 글
Jenkins를 통한 Kubernetes rolling update (0) | 2023.09.03 |
---|---|
Kubernetes Pods Auto Scalling (0) | 2023.09.03 |
Kubernetes 디플로이먼트 정의 및 노드 포트 서비스 연동 (0) | 2023.08.28 |
Kind(Kubernetes in Docker)를 통한 kubernetes 실습 환경 구축 (0) | 2023.08.27 |
Kubernetes 실습 및 서비스 구축 (0) | 2023.08.27 |