교육, 커뮤니티 후기/인프런 교육

<인프런> 대세는 쿠버네티스 [초급] - No.4 Kubernetes Overview

Somaz 2022. 7. 16. 17:25
728x90
반응형

Overview

이제 쿠버네티스 실습에 들어가보자!

 

아래는 시나리오이다.

 

 

N/W

서버 유형 Hostname OS CPU 메모리 서비스망 IP 내부망 IP 계정
가상 머신 dh-kube-master CentOS 7 2 Cores 4G 192.168.21.112 (/24) 10.1.1.112 (/8) root, clex
가상 머신 dh-kube-node CentOS 7 2 Cores 4G 192.168.21.113 (/24) 10.1.1.113 (/8) root, clex

 

 

 

VM은 KVM으로 구성한다.

 

$ qemu-img create -f qcow2 centos7.qcow2 50G
Formatting 'centos7.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16

 

 

virt-install --virt-type kvm --name centos7 \
  --ram 2048 \
  --cpu=host \
  --vcpus=2 \
  --os-type=centos \
  --os-variant=centos7.0 \
  --disk path=/data/dong/disk/centos7.qcow2,format=qcow2,bus=virtio \
  --cdrom=/data/dong/iso/CentOS-7-x86_64-Minimal-2009.iso  \
  --network bridge=mgmt,model=virtio \
  --network bridge=external,model=virtio \
  --graphics vnc,listen=0.0.0.0,password=xxx(비밀)

 

$ cat dh-k8s-master.sh
virt-clone \
--original centos7 \
--name dh-k8s-master \
--file /data/dong/disk/dh-k8s-master.qcow2

$ cat dh-k8s-node.sh 
virt-clone \
--original centos7 \
--name dh-k8s-node \
--file /data/dong/disk/dh-k8s-node.qcow2

$ virsh start dh-k8s-master
$ virsh start dh-k8s-node

 

1. Linux




CentOS에 nodejs 설치 :

yum -y update
yum install epel-release
yum -y install nodejs

1-1) hello.js

var http = require('http');
var content = function(req, resp) {
 resp.end("Hello Kubernetes!" + "\n");
 resp.writeHead(200);
}
var w = http.createServer(content);
w.listen(8000);

 

node hello.js

# 공용서버를 사용하고 있기 때문에 터널링으로 확인

 ssh -p [포트번호] -L localhost:9999:192.168.21.113:8000 [server id]@[server ip]

 

2. Docker


2-1) Dockerfile

vi Dockerfile

FROM node:slim
EXPOSE 8000
COPY hello.js .
CMD node hello.js

 

2-2) Docker Hub Site

https://hub.docker.com/

 

Docker Hub Container Image Library | App Containerization

We and third parties use cookies or similar technologies ("Cookies") as described below to collect and process personal data, such as your IP address or browser information. You can learn more about how this site uses Cookies by reading our privacy policy

hub.docker.com

2-3) Docker install

yum repolist
yum install -y yum-utils
# --config-manager 사용하기 위한 패키지 설치

yum-config-manager --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo
# repository 추가

yum install docker-ce docker-ce-cli containerd.io -y
# docker 설치

systemctl start docker
# docker 실행
systemctl enable docker
# reboot해도 유지되게 enable(활성화)

systemctl status docker
# docker 엔진 상태 확인

 

2-4) Docker Container Run

docker build -t tmkube/hello .
-t : 레파지토리/이미지명:버전

docker images
REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
somaz94/hello  latest    360e02156cdb   5 weeks ago   248MB
kubetm/hello   latest    360e02156cdb   5 weeks ago   248MB
node           slim      99defcfc3a10   6 weeks ago   248MB
tmkube/hello   latest    e5137372eeab   3 years ago   150MB

docker run -d -p 8100:8000 tmkube/hello
-d : 백그라운드 모드
-p : 포트변경

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                       NAMES
c589216cc396   tmkube/hello   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   0.0.0.0:8100->8000/tcp, :::8100->8000/tcp   eager_hamilton

docker exec -it c589216cc396 /bin/bash
# 동일하게 터널링으로 확인
 ssh -p [포트번호] -L localhost:9999:192.168.21.112:8100 [server id]@[server ip]

 

2-5) Docker Image Push

docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: somaz94
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

docker push somaz94/hello
Using default tag: latest
The push refers to repository [docker.io/somaz94/hello]
2fa21bb903cd: Pushed 
91664b9ef030: Mounted from library/node 
ccfc9c391c3f: Mounted from library/node 
9ae1f99bd2f3: Mounted from library/node 
fe5ae6be2231: Mounted from library/node 
43b3c4e3001c: Mounted from library/node 
latest: digest: sha256:e93ba9dcd7e9f41b988f9adb9319ba429de978a9136371df5b860328198031df size: 1574

위의 이미지를 통해서 kubernetes pod를 배포해 보려고 한다.

 


3. Kubernetes


 

 

3-1) Pod

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels:
    app: hello
spec:
  containers:
  - name: hello-container
    image: somaz94/hello
    ports:
    - containerPort: 8000

 

3-2) Service

 

v1.15

apiVersion: v1
kind: Service
metadata:
  name: hello-svc
spec:
  selector:
    app: hello
  ports:
    - port: 8200
      targetPort: 8000
  externalIPs:
  - 192.168.21.112
728x90
반응형