사용한 코드입니다.
https://github.com/atgane/docker-k8s-network-tutorial/tree/main/blog4
GitHub - atgane/docker-k8s-network-tutorial
Contribute to atgane/docker-k8s-network-tutorial development by creating an account on GitHub.
github.com
클러스터내부에서 pod가 어떻게 통신하는지 확인하기 위한 사전작업을 진행하겠습니다.
컨테이너 이미지를 직접 만들어서 로컬 환경에 이미지 저장소를 만들고 로컬 이미지 저장소로부터 pod를 배포해 보겠습니다.
kind와 작업 환경 설치는 https://atgane.tistory.com/179 여기서 확인할 수 있습니다.
실습 환경은 다음과 같습니다(리눅스에 docker, kind가 설치된 환경이면 상관없지만 wsl은 다를 수 있습니다).
- virtualbox 7.0.6
- ubuntu 22.04.2 LTS
- docker
- kind 0.17.0
순서는 다음과 같습니다.
1. local registry에 연결된 kind k8s cluster 만들기
2. local registry에 이미지를 저장하고 배포하기
1. local registry에 연결된 kind k8s cluster만들기
직접 이미지를 local registry에 저장하고 k8s cluster에 배포할 수 있도록 환경을 구성해 봅시다. k8s-test라는 디렉토리를 만들고 여기서 작업을 진행하도록 하겠습니다. 이번 포스팅의 모든 작업 디렉토리는 k8s-test로 하겠습니다.
kind 공식 문서에서 local registry에 이미지를 저장하고 띄울 수 있도록 클러스터를 구성하는 방법을 가이드하고 있습니다. 그리고 멀티 노드로 클러스터를 구성할 수 있는 가이드도 제공하고 있습니다.
https://kind.sigs.k8s.io/docs/user/local-registry/
kind – Local Registry
Local Registry This guide covers how to configure KIND with a local container image registry. In the future this will be replaced by a built-in feature, and this guide will cover usage instead. Create A Cluster And Registry 🔗︎ The following shell scri
kind.sigs.k8s.io
https://kind.sigs.k8s.io/docs/user/quick-start/
kind – Quick Start
Quick Start This guide covers getting started with the kind command. If you are having problems please see the known issues guide. NOTE: kind does not require kubectl, but you will not be able to perform some of the examples in our docs without it. To inst
kind.sigs.k8s.io
이 두 문서를 참고해서 local registry를 이미지 저장소로 사용하고 worker node를 2개를 이용하는 클러스터를 만들기 위해 다음의 쉘 스크립트를 작성했습니다. create-kind-cluster.sh으로 저장했습니다.
#!/bin/sh
# create-kind-cluster.sh
set -o errexit
# create registry container unless it already exists
reg_name='kind-registry'
reg_port='5001'
if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then
docker run \
-d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \
registry:2
fi
# create a cluster with the local registry enabled in containerd
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
endpoint = ["http://${reg_name}:5000"]
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
# connect the registry to the cluster network if not already connected
if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
docker network connect "kind" "${reg_name}"
fi
# Document the local registry
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: local-registry-hosting
namespace: kube-public
data:
localRegistryHosting.v1: |
host: "localhost:${reg_port}"
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF
해당 쉘을 한 번 실행시켜 보겠습니다(만약 에러가 발생한다면 docker의 상태가 켜져 있는지 점검해 봅시다).
./create-kind-cluster.sh
kind는 노드를 컨테이너로 생성합니다. 따라서 docker로 생성된 노드를 확인할 수 있습니다. 1대의 컨트롤 플레인과 2대의 워커 노드가 생성됐음이 보입니다. 또한 local registry도 생성됐음이 보이는데, 127.0.0.1:5001이 해당 이미지 저장소의 주소임을 확인할 수 있습니다.
docker ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 436d13c08dc1 kindest/node:v1.25.3 "/usr/local/bin/entr…" About a minute ago Up About a minute kind-worker
# b5680048624c kindest/node:v1.25.3 "/usr/local/bin/entr…" About a minute ago Up About a minute 127.0.0.1:40203->6443/tcp kind-control-plane
# 475810e652e3 kindest/node:v1.25.3 "/usr/local/bin/entr…" About a minute ago Up About a minute kind-worker2
# 40272643544e registry:2 "/entrypoint.sh /etc…" About a minute ago Up About a minute 127.0.0.1:5001->5000/tcp kind-registry
2. local registry에 이미지를 저장하고 배포하기
이제 local registry에 기본 nginx 이미지를 올려보겠습니다. nginx 이미지를 가져오고 이미지의 태그를 바꾸고 로컬 저장소에 push 합니다.
docker pull nginx:latest
docker tag nginx:latest 127.0.0.1:5001/nginx:latest
docker push 127.0.0.1:5001/nginx
로컬 저장소에 저장된 이미지는 다음 명령을 통해 확인할 수 있습니다.
curl 127.0.0.1:5001/v2/_catalog
# {"repositories":["nginx"]}
이제 해당 이미지를 컨테이너로 파드를 만들어서 배포해 보겠습니다. pod.yaml파일을 다음과 같이 작성해 줍니다. local registry에 등록한 이미지는 localhost:5001을 붙여주면 됩니다.
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: localhost:5001/nginx:latest
ports:
- containerPort: 80
만든 pod.yaml을 배포해 봅시다.
kubectl apply -f pod.yaml
만들어진 pod를 확인합니다.
kubectl get pod nginx
# NAME READY STATUS RESTARTS AGE
# nginx 1/1 Running 0 5m51s
이렇게 local환경에서 직접 image를 만들고 배포해 볼 수 있습니다.
실습 환경 제거
kind delete cluster
docker rm -f kind-registry
'개발 > docker, k8s, CNCF' 카테고리의 다른 글
docker, k8s 네트워크 뜯기(6) - docker network none 상태에서 외부랑 통신해보자 (0) | 2023.03.09 |
---|---|
docker, k8s 네트워크 뜯기(5) - pod 내부의 container와 다른 pod 내부의 container는 어떻게 서로 통신할까? (0) | 2023.03.05 |
docker, k8s 네트워크 뜯기(3) - docker 컨테이너는 어떻게 서로 통신할까? (0) | 2023.03.05 |
docker, k8s 네트워크 뜯기(2) - docker, kind, helm, k9s 설치 (0) | 2023.03.05 |
docker, k8s 네트워크 뜯기(1) - virtualbox ubuntu 고정 IP로 ssh 접속 (0) | 2023.03.05 |
댓글