Ops

kind 이용해서 k8s 다뤄보기

khjoon 2024. 12. 4. 22:15

Intro

쿠버네티스 실습을 해보기 위해서 kind를 이용해서 간단하게 쿠버네티스 환경을 구축한 뒤 실습을 진행했습니다.

kind 설치하기

kind를 이용하면 간단하게 쿠버네티스 cluster를 설치할 수 있습니다.
설치 가이드: https://kind.sigs.k8s.io/docs/user/quick-start/#installation

 

 

클러스터 yaml파일 작성

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: kind-cluster
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  image: kindest/node:v1.29.4
  extraPortMappings:
  - containerPort: 80 # 컨테이너 포트
    hostPort: 80 # 내컴퓨터 포트
    protocol: TCP
  - containerPort: 443
    hostPort: 443
    protocol: TCP
  - containerPort: 6443
    hostPort: 6443
    protocol: TCP
- role: worker
  image: kindest/node:v1.29.4
- role: worker
  image: kindest/node:v1.29.4
networking:
  serviceSubnet: "10.120.0.0/16"
  podSubnet: "10.110.0.0/16"
  # apiServerAddress: 127.0.0.1
  # apiServerPort: 6443
//kind 설치
$ brew install kind

// kind yaml 파일 작성후 yaml 위치에서 클러스터 생성
$ kind create cluster  --config ./3-node-cluster.yaml

//kubectl 설치
$ brew install kubectl

 

생성된 노드 확인하기 

 

 

실습은 vs코드 서버 이미지와 쿠버네티스와 도커 등 실습에 필요한 환경을 구축한 이미지를 만들고 이미지를 이용해서 컨테이너를 생성해서 실습을 진행했습니다.

도커 파일에 실습에 필요한 환경 세팅하기

# https://hub.docker.com/r/linuxserver/code-server
FROM linuxserver/code-server:4.90.3

# ENV for Code-server (VSCode)
ENV TZ="Asia/Seoul"
ENV PUID=1000
ENV PGID=1000

# Make DIR for code-server
RUN mkdir /code && chown 1000:1000 /code

# Update & Install the packages
RUN apt-get update && apt-get -y upgrade

RUN apt install -y ca-certificates curl gnupg software-properties-common wget unzip apt-transport-https telnet net-tools vim iputils-ping

# Docker CLI
RUN install -m 0755 -d /etc/apt/keyrings  \
    && curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc  \
    && chmod a+r /etc/apt/keyrings/docker.asc

RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      tee /etc/apt/sources.list.d/docker.list > /dev/null

RUN apt-get update && apt-get install -y docker-ce docker-ce-cli

# Kubectl
RUN curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg  \
    && echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list

RUN apt-get update && apt-get install -y kubectl

# Kubectx & Kubens
RUN git clone https://github.com/ahmetb/kubectx /opt/kubectx  \
    && ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx  \
    && ln -s /opt/kubectx/kubens /usr/local/bin/kubens

 

Dockerfile 빌드하기

$ docker build -t webide .

 

IDE docker-compose.yml 작성

IDE 컨테이너도 kind cluster와 같은 네트워크를 사용하도록 했고 docker.sock를 마운트해주고 로컬코드와도 마운트해줬습니다.

version: "3.8"
name: "practice"

services:
  server:
    image: webide
    container_name: "ide"
    networks:
      - kind_network
    environment:
      FILE__PASSWORD: /run/secrets/code-server-password
    env_file:
      - .env
    working_dir: /code
    ports:
      - "8444:8443"
    secrets:
      - code-server-password
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /Users/hyoungjoon/Documents/kind_practice/src/local_code:/code/local

networks:
  kind_network:
    name: kind
    external: true

secrets:
  code-server-password:
    file: password.txt

 

webide 실행하기

$ docker compose up -d

 

웹 브라우저로 vs코드가 잘 접속되는 것을 확인할 수 있습니다.

ide 컨테이너에서도 kubectl을 사용하기 위해서 로컬 홈 디렉토리에서 .kube/config를 ide 컨테이너 홈 디렉토리에도 추가해주면 ide에서도 쿠버네티스 조작이 가능합니다.


ide 컨테이너에 .kube/config에서 server: 부분만 kind 네트워크에서 control plane의 ip주소를 넣어줘야 합니다.

//kind network 확인
$ docker inspect kind

 

출처: dangtong님