지난번에는 kubeadm으로 직접 구성하는 방식을 진행해보았다면 이번에는 Kubespray를 사용하여 진행해보기로 했다.
기본적으로 클러스터 구성은 최소 3개이지만, 단순 실습만을 위해 다음과 같이 구성했다.
control-plane 1개
worker-node 2개
실습 환경은 Ubuntu 24.04를 이용하였다.
18.04나 20.04와 같은 LTS 버전도 있지만 18.04는 너무 오래되어서 현재 python3만을 사용할 수 있는 것과 달리 2를 사용할 수 있기 때문에 최근 버전에서 구성하는 방법이 다를 것 같아서 넘어갔다. 20.04나 22.04를 사용해도 되지만 기왕 처음부터 하는 거 나중 되면 결국 24.04를 사용할 때가 올 것이기 때문에 미리 최신버전에서 사용법을 익히고자 이걸로 선택했다. 단순 쿠버네티스만을 위한 실습이 목적이면 18.04나 20.04를 사용해도 된다. 하지만 최신 버전의 Kubespray를 사용하고자 하면 20.04에서는 ansible 9.8.0 설치가 안 되기 때문에 낮은 버전으로 낮춰서 설치해야 한다.
SSH 생성 및 배포
일단 control-plane으로 접속한다. 그 다음 아래 명령어로 SSH를 위한 키를 생성한다.
ssh-keygen -t rsa
명령어를 실행하면 무언가를 묻는 화면이 나오는데 실습을 위해 그냥 기본값을 사용한다. 그냥 엔터만 누르면 된다.
그다음 control-plane에서 worker node에 접속하기 위한 작업을 수행한다.
ssh-copy-id ness@192.168.0.42 # control-plane
ssh-copy-id ness@192.168.0.45 # worker-node-1
ssh-copy-id ness@192.168.0.47 # worker-node-2
Kubespray 설치
https://github.com/kubernetes-sigs/kubespray
GitHub - kubernetes-sigs/kubespray: Deploy a Production Ready Kubernetes Cluster
Deploy a Production Ready Kubernetes Cluster. Contribute to kubernetes-sigs/kubespray development by creating an account on GitHub.
github.com
git clone https://github.com/kubernetes-sigs/kubespray.git
cd kubespray/
git checkout -b release-2.26 origin/release-2.26
위 명령어로 저장소에서 kubespray를 가져온다.
게시글 작성일 기준 2.26 브랜치가 최신이어서 해당 브랜치로 이동하였다.
requirements.txt 파일에 필요한 구성 요소가 나와 있다. 해당 파일을 이용해서 설치를 진행할 것이다.
sudo apt install python3-pip
먼저 위에서 말한 필요한 구성요소들을 설치하기 위해 위 명령어로 pip 설치를 진행한다.
그런데 최신 버전의 우분투를 사용하고 있다면 이대로 설치가 되지 않을 것이다.
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.11/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
이런 오류가 발생할텐데 이때는 아래 글을 참고하여 가상 환경을 구성하여 진행하도록 한다.
pip install -r requirements.txt is failing: "This environment is externally managed"
Command: pip install -r requirements.txt Output: error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install pyth...
stackoverflow.com
sudo apt install python3-venv
python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
이후에 설치가 잘 되었는지 확인하기 위해 ansible 버전을 확인해 본다.
ansible --version
그다음에 기본 설정 파일이 있던 것을 이용해서 클러스터 설정을 할 것이다.
~/kubespray/inventory/sample 내의 템플릿을 복사해서 사용한다.
cp -rfp inventory/sample inventory/mycluster
위 명령어로 복사한다.
mycluster로 이동하여 해당 폴더 내의 inventory.ini 파일을 vi 편집기로 연다.
(.venv) pinpanel@controlplane:~/kubespray$ vi inventory/mycluster/inventory.ini
# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all]
controlplane ansible_host=192.168.0.42 ip=192.168.0.42
worker1 ansible_host=192.168.0.45 ip=192.168.0.45
worker2 ansible_host=192.168.0.47 ip=192.168.0.47
# node1 ansible_host=95.54.0.12 # ip=10.3.0.1 etcd_member_name=etcd1
# node2 ansible_host=95.54.0.13 # ip=10.3.0.2 etcd_member_name=etcd2
# node3 ansible_host=95.54.0.14 # ip=10.3.0.3 etcd_member_name=etcd3
# node4 ansible_host=95.54.0.15 # ip=10.3.0.4 etcd_member_name=etcd4
# node5 ansible_host=95.54.0.16 # ip=10.3.0.5 etcd_member_name=etcd5
# node6 ansible_host=95.54.0.17 # ip=10.3.0.6 etcd_member_name=etcd6
# ## configure a bastion host if your nodes are not directly reachable
# [bastion]
# bastion ansible_host=x.x.x.x ansible_user=some_user
[kube_control_plane]
controlplane
# node1
# node2
# node3
[etcd]
controlplane
# node1
# node2
# node3
[kube_node]
worker1
worker2
# node2
# node3
# node4
# node5
# node6
[calico_rr]
[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr
기본으로 작성되어 있는 내용을 참고하여 conrol-plane과 worker-node를 지정한다.
ansible-playbook -i inventory/mycluster/inventory.ini -v --become --become-user=root cluster.yml -K
그러고 나서 실제로 구성을 반영하기 위해 위 명령어를 입력하여 진행한다.
그냥은 오류 때문에 진행이 안 되기 때문에 root로 작동하도록 하였는데 이래도 진행이 안 된다. 비밀번호가 필요하다고 뜨기 때문이다. 그래서 찾아보니 현재 로그인한 유저로 비밀번호 없이 작동하도록 설정을 바꾸는 게 주로 나오는데 이 방법은 좀 아닌 거 같아서 더 찾아보았는데 비밀번호를 처음부터 입력하는 방법이 있었다.
https://forum.ansible.com/t/missing-sudo-password/4561/2
Missing sudo password
You need to add “-K” for asking the become password. It will re-use the ssh password if you leave it blank, but it is a required flag if a password is required for your become method.
forum.ansible.com
해당 유저의 비밀번호를 입력하여 정상적으로 진행할 수 있다.
각종 패키지를 다운로드하고 진행하는 시간이 오래 걸리기 때문에 잠깐 한숨 돌리고 와도 좋다.
다 진행되었으면 실행된 내역을 확인하여 오류가 발생하지 않았는지 살펴본다.

failed가 1개라도 있으면 다시 실행해야 한다.
나는 몇 번 failed가 계속 떴는데 그 이유는 inventory.ini에 ip주소를 잘못 입력하거나 worker-node가 이미 실행되고 있는 상태에서 control-plane만 다시 초기화해서 worker-node가 리스트에 뜨지 않기 때문이었다.
sudo kubectl get nodes
해당 명령어를 실행했을 때 다음과 같은 화면이 나타나면 성공한 것이다.

마지막으로 pod를 하나 생성하려고 하면 config가 없다고 API 관련 오류가 뜬다.
mkdir ~/.kube
sudo cp /etc/kubernetes/admin.conf ~/.kube/config
sudo chown ness:ness ~/.kube/config
위와 같이 admin.conf를 가져다 사용하면 정상적으로 작동한다.
'공부 > kubernates' 카테고리의 다른 글
| [Kubernates] 쿠버네티스 명령어 실습하기 (1) | 2024.09.05 |
|---|---|
| [Linux] VMware로 쿠버네티스 테스트 환경 구축하기 (0) | 2024.08.16 |
| [Kubernates] k8s 알아보고 설치하기 (0) | 2024.08.02 |