본문 바로가기

리눅스

우분투에서 Minikube를 사용하여 싱글 노드 쿠버네티스 클러스터를 구성하고 삭제하는 방법

728x90

우분투에서 Minikube를 사용하여 싱글 노드 쿠버네티스 클러스터를 구성하고 삭제하는 방법

테스트 환경

  • 운영체제 정보
$ lsb_release -d
Description:    Ubuntu 22.04.3 LTS

Minikube 클러스터 구성하기

Minikube 설치

  • 최신 버전의 Minikube를 설치합니다.
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
더보기

---

패키지 관리자를 통해 Minikube를 설치할 수도 있습니다.

sudo apt-get update
sudo apt-get install -y minikube

---

 

Minikube 버전 확인

minikube version
$ minikube version
minikube version: v1.32.0
commit: 8220a6eb95f0a4d75f7f2d7b14cef975f050512d

kubectl 설치

sudo apt-get install -y apt-transport-https ca-certificates curl
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
$ sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"

Repository: 'deb https://apt.kubernetes.io/ kubernetes-xenial main'
Description:
Archive for codename: kubernetes-xenial components: main
More info: https://apt.kubernetes.io/
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_apt_kubernetes_io_-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_apt_kubernetes_io_-jammy.list
Hit:1 https://mirror.kakao.com/ubuntu jammy InRelease
Hit:2 https://mirror.kakao.com/ubuntu jammy-updates InRelease
Hit:3 https://mirror.kakao.com/ubuntu jammy-backports InRelease
Hit:4 https://mirror.kakao.com/ubuntu jammy-security InRelease
Get:5 https://packages.cloud.google.com/apt kubernetes-xenial InRelease [8,993 B]
Get:6 https://packages.cloud.google.com/apt kubernetes-xenial/main amd64 Packages [69.9 kB]
Fetched 78.9 kB in 2s (36.0 kB/s)
Reading package lists... Done
W: https://apt.kubernetes.io/dists/kubernetes-xenial/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.

GPG 키 제거

  • GPG 키를 제거합니다.
sudo rm /etc/apt/trusted.gpg.d/*kubernetes*

GPG 키 다시 추가

  • 쿠버네티스 저장소의 GPG 키를 다시 추가합니다.
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg add -

APT 업데이트

  • APT 패키지 목록을 업데이트합니다.
sudo apt-get update

kubectl을 설치합니다.

sudo apt-get install -y kubectl

kubectl 버전 확인

kubectl version
$ kubectl version  
Client Version: v1.28.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Minikube 클러스터 시작

Minikube를 시작합니다.

minikube start --driver=docker
$ minikube start --driver=docker
* minikube v1.32.0 on Ubuntu 22.04
* Using the docker driver based on user configuration
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* If you are running minikube within a VM, consider using --driver=none:
*   https://minikube.sigs.k8s.io/docs/reference/drivers/none/

X Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.

Minikube는 보안상의 이유로 루트 권한으로 Docker 드라이버를 사용하는 것을 권장하지 않습니다. 따라서 해당 오류가 발생하면 일반적으로 --force 플래그를 사용하여 루트 권한으로 Minikube를 실행할 수 있습니다.

minikube start --driver=docker --force
$ minikube start --driver=docker --force
* minikube v1.32.0 on Ubuntu 22.04
! minikube skips various validations when --force is supplied; this may lead to unexpected behavior
* Using the docker driver based on user configuration
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* If you are running minikube within a VM, consider using --driver=none:
*   https://minikube.sigs.k8s.io/docs/reference/drivers/none/
* Using Docker driver with root privileges
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Downloading Kubernetes v1.28.3 preload ...
    > preloaded-images-k8s-v18-v1...:  403.35 MiB / 403.35 MiB  100.00% 5.52 Mi
    > gcr.io/k8s-minikube/kicbase...:  453.90 MiB / 453.90 MiB  100.00% 4.96 Mi
* Creating docker container (CPUs=2, Memory=9000MB) ...
* Preparing Kubernetes v1.28.3 on Docker 24.0.7 ...
  - Generating certificates and keys ...
  - Booting up control plane ...
  - Configuring RBAC rules ...
* Configuring bridge CNI (Container Networking Interface) ...
  - Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Verifying Kubernetes components...
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Minikube 상태 확인

minikube status
$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

인그레스 컨트롤러 활성화

minikube addons enable ingress
$ minikube addons enable ingress
* ingress is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
  - Using image registry.k8s.io/ingress-nginx/controller:v1.9.4
  - Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20231011-8b53cabe0
  - Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20231011-8b53cabe0
* Verifying ingress addon...
* The 'ingress' addon is enabled

NGINX 인그레스 컨트롤러가 실행 중인지 확인

kubectl get pods -n ingress-nginx
$ kubectl get pods -n ingress-nginx
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-j7nhk        0/1     Completed   0          3m28s
ingress-nginx-admission-patch-gqsrj         0/1     Completed   1          3m28s
ingress-nginx-controller-7c6974c4d8-fnq2g   1/1     Running     0          3m28s

Nginx 컨테이너 배포하기

mkdir minikube
cd minikube

Nginx Deployment YAML 파일 작성

  • YAML 파일을 사용하여 Nginx Deployment를 정의합니다. 하나의 Replica를 사용하여 Nginx를 배포합니다.
vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Deployment 적용

  • 작성한 YAML 파일을 사용하여 Nginx Deployment를 Kubernetes에 배포합니다.
kubectl apply -f nginx-deployment.yaml
$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created

Nginx 서비스 설정

  • Nginx Deployment를 서비스로 노출시켜야 합니다. 서비스 정의 YAML 파일을 생성합니다.
vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort

서비스 적용

  • 작성한 YAML 파일을 사용하여 Nginx 서비스를 Kubernetes에 배포합니다.
kubectl apply -f nginx-service.yaml
$ kubectl apply -f nginx-service.yaml
service/nginx-service created

모든 서비스 확인

kubectl get services
$ kubectl get services
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP        15m
nginx-service   NodePort    10.101.135.133   <none>        80:32765/TCP   3m58s

nginx 서비스 확인

kubectl get services nginx-service
$ kubectl get services nginx-service
NAME            TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
nginx-service   NodePort   10.101.135.133   <none>        80:32765/TCP   7m26s

nginx 서비스의 포트 확인

kubectl get service nginx-service -o jsonpath='{.spec.ports[*].port}'
$ kubectl get service nginx-service -o jsonpath='{.spec.ports[*].port}'
80

Minikube의 IP 주소

$ minikube ip
192.168.49.2

노드포트(NodePort)를 통해 서비스에 접속

minikube service nginx-service --url
$ minikube service nginx-service --url
http://192.168.49.2:32765

cURL 명령어를 사용하여 웹 페이지 테스트

curl http://192.168.49.2:32765
$ curl http://192.168.49.2:32765
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

nginx-service 서비스와 관련된 파드와 서비스를 삭제하는 방법

파드와 서비스 확인

kubectl get pods,services
$ kubectl get pods,services
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-6f4d78c4dc-m6rtg   1/1     Running   0          45m

NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP        78m
service/nginx-service   NodePort    10.101.135.133   <none>        80:32765/TCP   66m

서비스(Service) 삭제

kubectl delete service nginx-service
$ kubectl delete service nginx-service
service "nginx-service" deleted

파드(Pod) 확인

kubectl get pods --selector=app=nginx
$ kubectl get pods --selector=app=nginx
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6f4d78c4dc-m6rtg   1/1     Running   0          47m

디플로이먼트(deployments) 삭제

kubectl delete deployment nginx-deployment
$ kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted

파드(Pod) 확인

kubectl get pods --selector=app=nginx
$ kubectl get pods --selector=app=nginx  
No resources found in default namespace

Minikube 클러스터 삭제하기

minikube delete

 

우분투에서 Minikube를 사용하여 싱글 노드 쿠버네티스 클러스터를 구성하고 삭제할 수 있습니다.

 

참고URL

- Kubernetes : NGINX 인그레스(Ingress) 컨트롤러로 Minikube에서 인그레스 설정하기

 

728x90