brunch

NKS1탄-2. 도커와 레파지토리-2/7

by Master Seo

# 도커 이미지를 만들고 쿠버네티에서 서비스 하는 과정까지 실습 해보자~


<1> 로키 리눅스 9.4 생성과 도커 설치하기 (필수)

<2> 도커 파일 만들기 1 (필수)

<3> 레파지토리에 docker 이미지 올리기 1 (필수)

<4> NKS 클러스터 만들기 (필수)

<5> 도커 파일 만들기 2 (선택)

<6> 레파지토리에 docker 이미지 올리기 2 (선택)

<7> 레파지토리에 git 이미지 올리기 (선택)





<1> 로키 리눅스 9.4 생성과 도커 설치하기



구성1

개발자 ------- Server(도커설치,도커 파일)----------(올리기)---Container Registry-------NKS--사용자 접속



구성2

개발자 --Server(도커설치,도커 파일)----(올리기)Git------(올리기)Container Registry ---NKS-사용자 접속



1

# Docker 설치하기

# 쿠버네티스 레포 비활성화 하기와 설치




sudo dnf config-manager --set-disabled kubernetes


dnf install -y docker-ce --allowerasing


sudo dnf -y install dnf-plugins-core

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo


sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin


docker -version

systemctl enable --now docker

systemctl status docker

systemctl start docker

systemctl enable docker



지원 버전

https://download.docker.com/linux/centos/




# 돌아가기


https://brunch.co.kr/@topasvga/4543




<2> 도커 파일 만들기 1



1


cat << EOF > Dockerfile

FROM nginx:latest

RUN echo '<h1> test nginx web page </h1>' >> index.html

RUN cp /index.html /usr/share/nginx/html

EOF


FROM : Base Image

RUN : shell command를 docker image에 실행

WORKDIR : 작업 디렉토리

EXPOSE : 호스트와 연결할 포트 번호를 지정

CMD : application을 실행하기 위한 명령어



2

# 빌드

# nginx 는 컨테이너 이름 , image는 이미지 이름

# 이미지를 만들고, 컨테이너를 실행 시킨다.


docker build -t test-image .




4

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

test-image latest 3eaf3b495f1d 11 seconds ago 192MB



완료




5

# 아래는 참고


docker run -p 8080:80 --name test-nginx test-image


docker run -p 8080:80 --name 컨테이너_이름 이미지_이름




6

# 별도 터미널2


docker ps

[root@demo1 environment]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

e3192fb93622 test-image " " About About 0.0.0.0:8080->80/tcp, :::8080->80/tcp test-nginx



docker logs -f test-nginx

[root@demo1 environment]# docker logs -f [CONTAINER]

Usage: docker logs [OPTIONS] CONTAINER




7

# 도커에 로그인 하는 법 = -it 옵션


docker exec -it test-nginx /bin/bash

[root@ip-172-31-40-122 ~]# docker exec -it test-nginx /bin/bash


root@f20316881b68:/# ls

bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home index.html lib lib64 media mnt opt proc root run sbin srv sys tmp usr var


root@f20316881b68:/# more index.html

<h1> test nginx web page </h1>


8

[root@eksctl-host ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

c39ffd44d510 test-image "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp test-nginx



9

# 도커에 로그인 하는 법 = -it 옵션


[root@eksctl-host ~]# docker exec -it c39ffd44d510 /bin/bash


root@c39ffd44d510:/# ls

bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home index.html lib lib64 media mnt opt proc root run sbin srv sys tmp usr var



10

root@c39ffd44d510:/# more index.html

<h1> test nginx web page </h1>





<3> 레파지토리에 docker 이미지 올리기 1



1

참고 자료


https://brunch.co.kr/@topasvga/2095



2

레지스트리 업로드 ?

레파지토리 이름은 test-image


<1> 버킷을 만들고 Container Registry 생성

<2> 도커 이미지 만들기

<3> 만든 도커 이미지를 레지스트리에 올리기

<4> Container Registry의 이미지 사용해 Pod 생성하기



<1> 버킷을 만들고 Container Registry 생성


1

버킷 만들기?

Storage > Object Storage > 이용신청


Bucket Management

버킷 생성

nginx-web-bucket

// 버킷 이름은 유일해야 한다. nginx-web-bucket1



2

Container Registry 생성 ?

Compute > Container Registry > 레지스트리 생성

nginx-web-reg

레지스트리 이름도 유일해야 한다. nginx-web-reg1


앞에서 생성한 버킷 지정 nginx-web-bucket1

생성


3

Private Endpoint 를 복사해 놓는다.

m6n7b87w.kr.private-ncr.ntruss.com




<2> 도커 이미지 만들기


1

리눅스 서버 로그인


2

cat << EOF > Dockerfile

FROM nginx:latest

RUN echo '<h1> Container Registry test 1 </h1>' >> index.html

RUN cp /index.html /usr/share/nginx/html

EOF


설명 ?

FROM : Base Image

RUN : shell command를 docker image에 실행

WORKDIR : 작업 디렉터리

EXPOSE : 호스트와 연결할 포트 번호를 지정

CMD : application을 실행하기 위한 명령어



3

빌드

test-image 를 만들자

docker build -t test-image .


// 리눅스 서버에 도커가 설치 되어 있어야 한다.

혹, 오류가 발생하면 업데이트 필요

container_linux.go:235: starting container process caused "process_linux.go:258: applying cgroup configuration for process caused \"Cannot set property TasksAccounting, or unknown property.\""


yum -y update

(10분 소요)


docker images


docker run -p 8080:80 --name test-nginx test-image



4

# 다른 터미널로 로그인 해 확인


docker ps

[root@s17e3bbc1fd7 ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

bfa5615ebff7 test-image "/docker-entrypoint.…" 36 seconds ago Up 36 seconds 0.0.0.0:8080->80/tcp test-nginx


5

docker logs -f test-nginx


6

docker exec -it test-nginx /bin/bash


[root@test-web01 ~]# docker exec -it test-nginx /bin/bash

root@e494216f26dd:/#

root@e494216f26dd:/# ls

bin dev docker-entrypoint.sh home lib media opt root sbin sys usr

boot docker-entrypoint.d etc index.html lib64 mnt proc run srv tmp var


root@e494216f26dd:/# exit




<3> 만든 도커 이미지를 레지스트리에 올리기


1

레지스트리에 올리기 위해 access key 가 필요하다.

access key 확인?

ncloud 콘솔

마이페이지

계정관리

인증키 관리


2

계정으로 레지스트리 로그인 하기

Access key id와 Secret key id 확인 - 포털 > 마이페이지 > 인증키 관리에서 확인



컨테이너 레지스트리에서 <private endpoint> 확인



docker login -u <access key id> < private endpoint>


docker login -u tUBxxxxxxxxxxx m6n7b87w.kr.private-ncr.ntruss.com

Password:

Login Succeeded



3

# 이미지에 태그 추가 ?

docker image tag image_apache <private endpoint>/ image_apache:1.0


docker image tag test-image lef1iv60.kr.private-ncr.ntruss.com/test-image:1.0



4

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

test-image latest f9b6f1e04c74 56 minutes ago 141MB

m6n7b87w.kr.private-ncr.ntruss.com/test-image 1.0 f9b6f1e04c74 56 minutes ago 141MB

nginx latest 605c77e624dd 10 days ago 141MB


// test-image 를 레파지토리 이미지 태그로 하나더 만든다.

m6n7b87w.kr.private-ncr.ntruss.com/test-image:1.0




4

# 도커를 컨테이너 레지스트리에 올리기, PUSH

# docker push 프라이빗 엔드 포인트/<TARGET_IMAGE[:TAG]>


docker push lef1iv60.kr.private-ncr.ntruss.com/test-image:1.0


docker push lef1iv60.kr.private-ncr.ntruss.com/test-image:1.1


The push refers to a repository [n0jw34vw.kr.private-ncr.ntruss.com/test-image]

b35a9b6182f4: Pushed

539984d66b34: Pushed

e4b39f949587: Pushed

53db376e88c7: Pushed




5

네이버 클라우드 콘솔에서 확인하자

Container Registry 가서 확인

이미지 리스트 이동

이미지 이름

test-image

130 test-image.png





<4> Container Registry의 이미지 사용해 Pod 생성하기


1

# Container Registry의 Access/ Secret 키를 저장하는 Secret 생성 ?

# 명령서버에서 작업한다.

# regcred를 Deployment/POD에서 imagePullSecrets로 참조하면 프라이빗 레지스트리에서 이미지를 정상적으로 가져올 수 있어요




2

k create secret docker-registry regcred --docker-server=<registry-end-point> --docker-username=<access-key-id> --docker-password=<secret-key> --docker-email=<your-email>


k create secret docker-registry regcred --docker-server=lef10.kr.private-ncr.ntruss.com --docker-username=ncp_iam_BPt2Ng --docker-password=ncp_iam_BPKMKR0mFbGTc5 --docker-email=topasvga@kakao.com

secret/regcred created


k get secret




3

# pod 생성 ?


ex)

containers:

- name: apache-pod

image: <private-endpoint>/image_apache:1.0


lef1iv60.kr.private-ncr.ntruss.com/test-image:1.1


# vi create_only_pod.yaml


cat <<EOF | k create -f -

apiVersion: v1

kind: Pod

metadata:

name: test-image

namespace: default

spec:

containers:

- name: test-image

image: lef1iv60.kr.private-ncr.ntruss.com/test-image:1.1

imagePullSecrets:

- name: regcred

EOF



k create -f create_only_pod.yaml --validate=false




4

# 요약!!!



1

docker login -u ncp_iam_BPAMKtA kfc7fbpi.kr.private-ncr.ntruss.com

2

[root@com2 ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

hello-world latest de82f377786d 7 minutes ago 192MB

test-image latest ae221739af30 7 hours ago 192MB

kfc7fbpi.kr.private-ncr.ntruss.com/test-image 1.0 ae221739af30 7 hours ago 192MB


docker image tag test-image kfc7fbpi.kr.private-ncr.ntruss.com/test-image:1.0


docker image tag hello-world kfc7fbpi.kr.private-ncr.ntruss.com/hello-world:1.0



3

docker push kfc7fbpi.kr.private-ncr.ntruss.com/test-image:1.0




<8> NKS 클러스터 만들기



1

https://brunch.co.kr/@topasvga/4194




2

# 쿠버네티스에 배포하기


cat <<EOF | k apply -f -

apiVersion: apps/v1

kind: Deployment

metadata:

name: apache-deployment

namespace: default

spec:

replicas: 3

selector:

matchLabels:

app: apache

template:

metadata:

labels:

app: apache

spec:

containers:

- name: apache

image: kfc7fbpi.kr.private-ncr.ntruss.com/hello-world:1.0

ports:

- containerPort: 80

imagePullSecrets:

- name: regcred

EOF

cat <<EOF | k apply -f -

apiVersion: v1

kind: Service

metadata:

name: example-service

annotations:

# 로드밸런서의 타입 및 설정을 지정합니다.

service.beta.kubernetes.io/ncloud-load-balancer-type: "public"

spec:

type: LoadBalancer

selector:

app: apache

ports:

- port: 80

targetPort: 80

EOF


Service 가 트래픽을 연결할 Pod은 반드시 label: app=apache 를 가져야 합니다.




3

kwn



4

로드밸런서로 접속하기


홈페이지 나오면 성공!!!


30 ok.png


# pod 설정 확인법

k exec -it <apache-pod> -- curl http://localhost:80


k exec -it apache-deployment-64f4d68c46-7s96g -- curl http://localhost:80


k exec -it apache-deployment-64f4d68c46-7s96g -- curl http://localhost:80







<5> 도커 파일 만들기 2


1

sudo su -


vi Dockerfile


FROM ubuntu:18.04

# Install dependencies

RUN apt-get update && \

apt-get -y install apache2



# Install apache and write hello world message

RUN echo 'Hello World!' > /var/www/html/index.html



# Configure apache

RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \

echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \

echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \

echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \

chmod 755 /root/run_apache.sh

EXPOSE 80

CMD /root/run_apache.sh





2

빌드 ?

docker build -t hello-world .



3

도커 이미지 확인 ?


docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

hello-world latest cd554e0b06c6 32 seconds ago 196MB

ubuntu 18.04 81bcf752ac3d 4 weeks ago 63.1MB



4

이미지를 컨테이너로 실행 ?


docker run -t -i -p 80:80 hello-world

// Could not reliably determine the server's fully qualified domain name" 메시지는 무시해도 됩니다.



5

docker ps

docker ps -a





6

#다른 터미널에서 exec로 컨테이너로 로그인 ?


docker exec -it 41f7105d2b3f /bin/bash

//41f7105d2b3f 는 Container ID이다.



7

# 컨테이너로 로그인 한 상태, 명령어 사용해 본다.


root@41f7105d2b3f:/# cd /var/www/html/


root@41f7105d2b3f:/var/www/html# ls

index.html



root@41f7105d2b3f:/var/www/html# more index.html

Hello World!



root@51e7d9d8864f:/var/www/html# exit

(컨테이너에서 나가기)



8

# 서비스 상태 Up 확인 ?


[root@ip-10-0-0-230 ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

41f7105d2b3f hello-world "/bin/sh -c /root/ru…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp elastic_dirac



9

# 중지하기 ?

[root@ip-10-0-0-230 ~]# docker stop 41f7105d2b3f



10

# 정상 종료 확인 = Exited 는 정상 종료



# 컨테이너 이름 확인


[root@ip-10-0-0-230 ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

41f7105d2b3f hello-world "/bin/sh -c /root/ru…" 5 min Exited (137) 3 sec elastic_dirac



11

# 컨테이너 삭제하기 ?


docker rm 컨테이너-이름


docker rm elastic_dirac





<6> 레파지토리에 docker 이미지 올리기 2



1

docker images


REPOSITORY TAG IMAGE ID CREATED SIZE

test-image latest 86a04572f50c 10 seconds ago 133MB

demo2 latest 6dca039085a0 26 minutes ago 60.7MB

demo-flask-backend latest 53d888792869 About an hour 198MB

hello-world latest 53d888792869 About an hour ago 198MB



2

# 레파지토리 만들기 ?


hello-repository







<7> 레파지토리에 git 이미지 올리기



# git에서 프로그램된 docker 파일을 가져와 빌드하고 push 하기도 한다.


Git에서 docker 파일을 가져온다.

빌드한다.

레파지토리에 Push 한다.

레파지토리에 이미지를 NKS에서 사용한다.



1

yum install git



2

# github에 공유한 배포할 파일 가져오기.


git clone https://github.com/ㅌㅌㅌㅌㅌㅌㅌㅌ/xxxxxx.git




2

# 레파지토리 만들기


리파지토리 이름과 이미지 태그 확인



keyword
매거진의 이전글NKS1탄-1.네이버 클라우드 쿠버네티스 -2024