on
Hiper-V 에서 Kubernetes (쿠버네티스) 설치하기
Hiper-V 에서 Kubernetes (쿠버네티스) 설치하기
728x90
# 홈쿠를 해보자
(AWS나 네이버 클라우드, GCP 등에서 쿠버네티스를 운영하면 더 좋으나 우리에겐 비용이라는 거대한 산이 존재하므로, 홈쿠는 필수...쩝)
(버추얼박스로 홈쿠도 가능하나 라이센스 문제도 있고, 기업용 PC에서는 사용이 불가능 하므로, 감사하게도 윈도우즈에 탑재된 VM으로 구성을 해보자~)
1. VM끼리 통신을 하기위한 네트워크 스위치를 생성한다.
- 가상스위치 관리자 > 내부 > 가상 스위치 만들기 순으로 클릭하여 생성한다.
- 네트워크 설정 확인
- 네트워크 연결 설정에서 현재 연결되어있는 인터넷 > 우측 마우스클릭 > 속성 > 공유 > 이전에 생성한 k8s 네트워크 선택 후 확인
- 이제 VM들끼리 연결 가능한 네트워크가 생성이 되었고, 다음은 VM을 생성해 봅시다.
2. VM 생성하기
- 새로만들기 > 가상컴퓨터 클릭
- 1세대는 legacy 모델 이므로, 2세대 선택
- VM 3대를 띄울 예정이므로 메모리는 2048로... 그리고 동적 메모리는 체크 해제... 메모리가 부족하므로...
- 이전에 생성한 네트워크로 연결
- 디스크는 넉넉하게 100GB 정도로 할당
- 이미지 파일 마운트는 다운받은 우분투.iso 파일로 선택
- 마침으로 생성 작업을 끝냄
3. VM을 시작하기 전에 생성한 VM의 CPU 수를 늘려주자
가상머신 > 설정 > 가상 프로세스 수를 2개로 늘려줌
4. VM을 시작 후 OS를 설치한다.
- 해당 에러 발생 시 설정 > 보안 > 보안부팅 사용 체크해제
- 보안 부팅 체크 해제 후 다시 VM을 시작한다.
- 우분투 설치 과정은 아래의 링크 클릭하여 진행
2021.11.04 - [Hyper-V] - Hyper-V 에 우분투 Ubuntu OS 설치
5. VM을 SSH로 접근하기
- 아래의 유틸을 설치하자
sudo apt-get update sudo apt-get install openssh-server sudo apt-get install gedit
- SSH 설정 편집
sudo gedit /etc/ssh/sshd_config
- 주석 제거 후 저장
- VM의 IP주소를 확인하기 위해 설치
sudo apt install net-tools ifconfig
- eth0 의 ip를 메모장에 기록 합니다.
- SSH에 접근해 봅시다
ssh {계정명}@{vm의 ip}
- 접속 성공 (이젠 편하게 작업할 수 있겠다...)
PS C:\WINDOWS\system32> ssh [email protected] The authenticity of host '192.168.137.50 (192.168.137.50)' can't be established. ECDSA key fingerprint is SHA256:xg0ta5sHDbWeBnS8FV43J0JB8XJzekSyfhzrHn5BbsI. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.137.50' (ECDSA) to the list of known hosts. [email protected]'s password: Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.11.0-38-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 104 updates can be applied immediately. 41 of these updates are standard security updates. 추가 업데이트를 확인하려면 apt list --upgradable 을 실행하세요. Your Hardware Enablement Stack (HWE) is supported until April 2025. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. k8s-master-node@k8smasternode-Virtual-Machine:~$
6. 워커노드 생성하기 및 SSH 접속
- 마스터 노드는 위에서 생성 하였으므로 워커노드를 2개 생성 해봅시다.
2021.11.04 - [Hyper-V] - Hyper-V 에 우분투 Ubuntu OS 설치
- 2개 생성 완료
- SSH 접속 명령어
ssh [email protected] ssh [email protected] ssh [email protected]
- 3개의 SSH 접속환경 확인
7. 각 노드들의 ping 테스트 하기
- 마스터 > 워커노드01 > 결과 : 정상통신
k8s-master-node@k8smasternode-Virtual-Machine:~$ ping 192.168.137.105 PING 192.168.137.105 (192.168.137.105) 56(84) bytes of data. 64 bytes from 192.168.137.105: icmp_seq=1 ttl=64 time=0.803 ms 64 bytes from 192.168.137.105: icmp_seq=2 ttl=64 time=0.765 ms 64 bytes from 192.168.137.105: icmp_seq=3 ttl=64 time=0.389 ms ^C --- 192.168.137.105 ping statistics --- 13 packets transmitted, 13 received, 0% packet loss, time 12233ms rtt min/avg/max/mdev = 0.263/0.560/0.894/0.192 ms
- 마스터 > 워커노드02 > 결과 : 정상통신
k8s-master-node@k8smasternode-Virtual-Machine:~$ ping 192.168.137.254 PING 192.168.137.254 (192.168.137.254) 56(84) bytes of data. 64 bytes from 192.168.137.254: icmp_seq=1 ttl=64 time=0.976 ms 64 bytes from 192.168.137.254: icmp_seq=2 ttl=64 time=0.324 ms 64 bytes from 192.168.137.254: icmp_seq=3 ttl=64 time=1.03 ms ^C --- 192.168.137.254 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2019ms rtt min/avg/max/mdev = 0.324/0.776/1.028/0.320 ms
- 워커노드01 > 워커노드02 > 결과 : 정상통신
k8s-worker-node-01@k8sworkernode01-Virtual-Machine:~$ ping 192.168.137.254 PING 192.168.137.254 (192.168.137.254) 56(84) bytes of data. 64 bytes from 192.168.137.254: icmp_seq=2 ttl=64 time=0.872 ms 64 bytes from 192.168.137.254: icmp_seq=3 ttl=64 time=0.736 ms 64 bytes from 192.168.137.254: icmp_seq=4 ttl=64 time=0.630 ms ^C --- 192.168.137.254 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3006ms rtt min/avg/max/mdev = 0.630/0.858/1.197/0.213 ms
- hosts 파일에 등록하여 hosts 이름으로 통신 가능하도록 설정
sudo vi /etc/hosts
- hosts 등록 (각각의 VM에 등록)
192.168.137.50 k8s-master-node 192.168.137.105 k8s-worker-node-01 192.168.137.254 k8s-worker-node-02
8. 도커 설치 (모든 VM)
sudo apt-get install docker.io -y sudo systemctl start docker sudo systemctl enable docker
- 쿠버네티스 레포지토리 등록 (모든 VM)
sudo apt-get install apt-transport-https curl -y curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
- 가상메모리를 비활성화 수행 (모든 VM)
sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
- 쿠버네티스 설치 (모든 VM) > 버전명 잘 나오면 성공
sudo apt-get install kubeadm -y kubeadm version
- 쿠버네티스 컨테이너를 도커로 사용하도록 설정
sudo vi /etc/docker/daemon.json
- 아래 내용을 복사 > 붙여넣기 > 저장
//https://gist.github.com/KimKiHyuk/692803efd97da482cd9c4b0bbe96a18d { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" }
- 이제부터 마스터 노드와 워커 노드가 분리된 작업을 수행
9. 쿠버네티스 클러스터 생성 및 설정 (마스터 노드)
- 마스터 노드에서 명령어 실행
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
- 혹시 아래와 같은 오류가 발생 한다면
http://localhost:10248/healthz: dial tcp 127.0.0.1:10248: connect: connection refused
더보기 sudo mkdir /etc/docker cat <
- 아래 명령어 마스터 노드에서 실행
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 워커 노드에서 실행 (마스터 노드에서 init 했을때 나온 명령어)
- 마스터 노드 init 했을때의 나오는 안내 코드
더보기 Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.137.50:6443 --token niat1k.y2clmlaoegjvbkm7 \ --discovery-token-ca-cert-hash sha256:f45e7956e7ffd084bbc390b906d95e0ff564330d97536eeb76ef0067257f3929
- 워커노드에서 아래의 코드를 입력하여 클러스터에 join 함
kubeadm join 192.168.137.50:6443 --token niat1k.y2clmlaoegjvbkm7 \ --discovery-token-ca-cert-hash sha256:f45e7956e7ffd084bbc390b906d95e0ff564330d97536eeb76ef0067257f3929
- 워커노드 01번 조인 성공
- 워커노드 02번 조인 성공
- 해당 토큰값을 다시 얻고싶은 경우
$ kubeadm token list $ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^.* //’
- flannel 설치 (마스터 노드)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+ podsecuritypolicy.policy/psp.flannel.unprivileged created clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created daemonset.apps/kube-flannel-ds created
10. 정상적으로 클러스터링이 되었는지 확인하기
- 노드 리스트를 확인
kubectl get nodes
k8s-master-node@k8smasternode-Virtual-Machine:~$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8smasternode-virtual-machine Ready control-plane,master 16m v1.22.3 k8sworkernode01-virtual-machine Ready 8m55s v1.22.3 k8sworkernode02-virtual-machine Ready 7m v1.22.3
- kubernetes pod 확인
kubectl get pods --all-namespaces
k8s-master-node@k8smasternode-Virtual-Machine:~$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-78fcd69978-6twjf 1/1 Running 0 19m kube-system coredns-78fcd69978-qzkrk 1/1 Running 0 19m kube-system etcd-k8smasternode-virtual-machine 1/1 Running 2 19m kube-system kube-apiserver-k8smasternode-virtual-machine 1/1 Running 2 19m kube-system kube-controller-manager-k8smasternode-virtual-machine 1/1 Running 2 19m kube-system kube-flannel-ds-fm2wd 1/1 Running 0 4m14s kube-system kube-flannel-ds-htjtr 1/1 Running 0 4m14s kube-system kube-flannel-ds-zcsnv 1/1 Running 0 4m14s kube-system kube-proxy-f6sxt 1/1 Running 0 9m50s kube-system kube-proxy-fsv7s 1/1 Running 0 19m kube-system kube-proxy-n2tm4 1/1 Running 1 11m kube-system kube-scheduler-k8smasternode-virtual-machine 1/1 Running 2 19m
- 정상적으로 조회되는것을 확인...
끝... 홈쿠 성공?
다음에는 워드프레스나 nginx 및 jenkins등을 올려서 테스트를 해보자.
728x90
from http://may9noy.tistory.com/339 by ccl(A) rewrite - 2021-11-05 01:59:06