Kubernetes 설치를 해보자 한다.

 

 

Master Node & Worker Node 각 1대씩 구성을 하고자 한다.

 

물리적으로 분리된 BareMetal 구성을 하려고 했지만,

자유로운 설정을 통해 학습해보기 위해서 VirtualBox로 우선 진행해 보았다.

 

 

정말 훌륭한 레퍼런스는 아래 링크와 같다 !!!

  - https://medium.com/finda-tech/overview-8d169b2a54ff

 

 

 

 

1. Master & Worker Node H/W

 

  - Prerequisite

 

구분  CPU Memory Storage 
 Master 2 core 3 GB 30 GB
 Worker 2 core 2 GB 30 GB

 

  - VirtualBox

 

구분  CPU Memory Storage 
 Master 2 core 4 GB 50 GB
 Worker 2 core 4 GB 50 GB

 

    . 네트워크는 NAT가 아니라 Bridge로 잡아줬다. (공유기 환경)

 

 

 

 

 

2. Master & Worker Node S/W

 

  - Ubuntu 20.04 LTS Server 설치를 하면서, 기본적인 환경 설치는 아래 링크와 동일하게 구성하였다.

    . https://www.whatwant.com/entry/notebook-ubuntu-server

 

  - Docker 까지는 미리 설치해두었다. 설치 방법은 아래 링크와 같이 진행하였다.

    . https://www.whatwant.com/entry/Docker-Install-Ubuntu-Server-2004

 

    . kubernetes validated versions이 따로 있으니 이것에 맞추는 것을 추천한다.

 

 

 

 

3. Swap off

 

  - kubelet에서 swap을 지원하지 않기 때문에, Master/Worker 모두 swap 기능을 꺼야 한다.

 

$ sudo swapoff -a

 

  - 영구적으로 기능을 끄기 위해서는 추가 작업도 해줘야 한다.

 

$ sudo nano /etc/fstab

 

  - swap 부분을 찾아서 주석 처리 해주면 된다.

 

#/swap.img  none    swap    sw  0   0

 

 

 

 

4. cgroup driver 설정

 

  - 이미 설치된 docker에 대해서 cgroup driver를 변경해줘야 한다.

 

$ docker info

 

 

  - 지금은 "cgroupfs"로 설정되어 있는 것을 볼 수 있다. "systemd"로 변경해보자.

 

$ sudo nano /etc/docker/daemon.json

 

{

    "exec-opts": ["native.cgroupdriver=systemd"],

    "log-driver": "json-file",

    "log-opts": {

        "max-size": "100m"

    },

    "storage-driver": "overlay2"

}

 

$ sudo mkdir -p /etc/systemd/system/docker.service.d

 

$ sudo systemctl daemon-reload

 

$ sudo systemctl restart docker

 

 

  - "cgroup driver"가 잘 변경된 것을 볼 수 있다.

 

 

 

 

5. Kubernetes 기본 패키지 설치

 

  - Master/Worker Node 모두 [ kubeadm, kubelet, kubectl ] 3개의 패키지가 설치되어야 한다.

    . 부수적으로 [ kubernetes-cni, cri-tools ] 2개 패키지도 필요하다.

 

  - 아래에서 최신 버전의 주소(파일명) 확인 (16.04 이후 버전 모두 동일)

    . https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages

 

$ wget https://packages.cloud.google.com/apt/pool/cri-tools_1.13.0-01_amd64_4ff4588f5589826775f4a3bebd95aec5b9fb591ba8fb89a62845ffa8efe8cf22.deb

 

$ wget https://packages.cloud.google.com/apt/pool/kubeadm_1.20.1-00_amd64_7cd8d4021bb251862b755ed9c240091a532b89e6c796d58c3fdea7c9a72b878f.deb

 

$ wget https://packages.cloud.google.com/apt/pool/kubectl_1.20.1-00_amd64_b927311062e6a4610d9ac3bc8560457ab23fbd697a3052c394a1d7cc9e46a17d.deb

 

$ wget https://packages.cloud.google.com/apt/pool/kubelet_1.20.1-00_amd64_560a52294b8b339e0ca8ddbc480218e93ebb01daef0446887803815bcd0c41eb.deb

 

$ wget https://packages.cloud.google.com/apt/pool/kubernetes-cni_0.8.7-00_amd64_ca2303ea0eecadf379c65bad855f9ad7c95c16502c0e7b3d50edcb53403c500f.deb

 

  - 설치도 진행하자

 

$ sudo apt-get install socat conntrack ebtables

 

$ sudo dpkg --install ./kubernetes-cni_0.8.7-00_amd64_ca2303ea0eecadf379c65bad855f9ad7c95c16502c0e7b3d50edcb53403c500f.deb

 

$ sudo dpkg --install ./kubelet_1.20.1-00_amd64_560a52294b8b339e0ca8ddbc480218e93ebb01daef0446887803815bcd0c41eb.deb

 

$ sudo dpkg --install ./cri-tools_1.13.0-01_amd64_4ff4588f5589826775f4a3bebd95aec5b9fb591ba8fb89a62845ffa8efe8cf22.deb

 

$ sudo dpkg --install ./kubectl_1.20.1-00_amd64_b927311062e6a4610d9ac3bc8560457ab23fbd697a3052c394a1d7cc9e46a17d.deb

 

$ sudo dpkg --install ./kubeadm_1.20.1-00_amd64_7cd8d4021bb251862b755ed9c240091a532b89e6c796d58c3fdea7c9a72b878f.deb

 

 

 

 

6. Master Node 셋업

 

  - kubeadm을 이용하여 Master Node 셋업을 진행하자.

 

  - Master Node의 IP를 확인하자

 

$ ifconfig

 

  - 이제 셋업 시작~!! (뒤의 IP는 방금 확인한 IP로 교체!!)

 

$ sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.100.119

 

❯ sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.100.119

[init] Using Kubernetes version: v1.20.1

[preflight] Running pre-flight checks

[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.1. Latest validated version: 19.03

[preflight] Pulling images required for setting up a Kubernetes cluster

[preflight] This might take a minute or two, depending on the speed of your internet connection

[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'

[certs] Using certificateDir folder "/etc/kubernetes/pki"

[certs] Generating "ca" certificate and key

[certs] Generating "apiserver" certificate and key

[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master-stg] and IPs [10.96.0.1 192.168.100.119]

[certs] Generating "apiserver-kubelet-client" certificate and key

[certs] Generating "front-proxy-ca" certificate and key

[certs] Generating "front-proxy-client" certificate and key

[certs] Generating "etcd/ca" certificate and key

[certs] Generating "etcd/server" certificate and key

[certs] etcd/server serving cert is signed for DNS names [localhost master-stg] and IPs [192.168.100.119 127.0.0.1 ::1]

[certs] Generating "etcd/peer" certificate and key

[certs] etcd/peer serving cert is signed for DNS names [localhost master-stg] and IPs [192.168.100.119 127.0.0.1 ::1]

[certs] Generating "etcd/healthcheck-client" certificate and key

[certs] Generating "apiserver-etcd-client" certificate and key

[certs] Generating "sa" key and public key

[kubeconfig] Using kubeconfig folder "/etc/kubernetes"

[kubeconfig] Writing "admin.conf" kubeconfig file

[kubeconfig] Writing "kubelet.conf" kubeconfig file

[kubeconfig] Writing "controller-manager.conf" kubeconfig file

[kubeconfig] Writing "scheduler.conf" kubeconfig file

[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"

[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"

[kubelet-start] Starting the kubelet

[control-plane] Using manifest folder "/etc/kubernetes/manifests"

[control-plane] Creating static Pod manifest for "kube-apiserver"

[control-plane] Creating static Pod manifest for "kube-controller-manager"

[control-plane] Creating static Pod manifest for "kube-scheduler"

[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s

[apiclient] All control plane components are healthy after 13.002889 seconds

[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace

[kubelet] Creating a ConfigMap "kubelet-config-1.20" in namespace kube-system with the configuration for the kubelets in the cluster

[upload-certs] Skipping phase. Please see --upload-certs

[mark-control-plane] Marking the node master-stg as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"

[mark-control-plane] Marking the node master-stg as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]

[bootstrap-token] Using token: t4tcwj.22xh9lzstu56qyrb

[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles

[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes

[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials

[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token

[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster

[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace

[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key

[addons] Applied essential addon: CoreDNS

[addons] Applied essential addon: kube-proxy

 

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.100.119:6443 --token t4tcwj.22xh9lzstu56qyrb \

    --discovery-token-ca-cert-hash sha256:eb3765b58c9140c9a89daf7ea21444ca44a142939ebb93aedad1ebc03202a1d7

 

  - 사용자 계정에서 kubectl 실행을 위해 위의 가이드 내용 그대로 진행을 해보자.

 

$ mkdir -p $HOME/.kube

 

$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

 

$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

 

  - 잘 설치가 된 것을 확인해보자.

 

$ kubectl get pods --all-namespaces

 

$ kubectl get nodes

 

 

 

  - Pod Network 환경을 맞춰야 하는데, k8s 를 위한 layer 3 환경을 구축해주는 flannel을 사용해보겠다.

    . https://github.com/coreos/flannel/

 

$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

 

 

  - coredns가 pending에서 running으로 바뀌었고, flannel이 추가 되었고, Master Node가 Ready 상태가 된 것을 볼 수 있다.

 

 
 
7. Worker Node 셋업
 
  - 기본 환경 설치를 모두 했다면, 아래 명령어만 실행하면 된다.
  - 아래 명령어는 Master Node 설치할 때 나왔던 가이드 밑에 있는 부분 그대로 하면 된다. (각자의 환경에 따라서 실행할 것!!!)
 
$ sudo kubeadm join 192.168.100.119:6443 --token t4tcwj.22xh9lzstu56qyrb \
    --discovery-token-ca-cert-hash sha256:eb3765b58c9140c9a89daf7ea21444ca44a142939ebb93aedad1ebc03202a1d7
 
❯ sudo kubeadm join 192.168.100.119:6443 --token t4tcwj.22xh9lzstu56qyrb \
    --discovery-token-ca-cert-hash sha256:eb3765b58c9140c9a89daf7ea21444ca44a142939ebb93aedad1ebc03202a1d7
 
[preflight] Running pre-flight checks
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.1. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
 
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
 
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
 
  - Master Node에서 잘 붙었는지 확인해보자.
 
$ kubectl get nodes
 

 

 
 
 
 
 
8. hello kubernetes
 
  - Master Node에서 다음과 같이 deployment를 실행해보자.
 
$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
 
  - 잘 되었는지 확인도 해보자.
 
$ kubectl get deployments
 
$ kubectl get pods -o wide
 

 

  - Worker Node에서 해당 서비스가 잘 되는지 확인해보자.

 

$ curl http://10.244.1.2:8080

 

 

 

 

 

 

여기까지~

 

 

반응형

+ Recent posts