k8s

Ubuntu 20.04에 Kubernetes 설치 (2) Kubernetes 설치 및 kubernetes 클러스터 구성

Joon0464 2022. 5. 18. 21:59
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.100.0.104

1. 설치 전 구성

Master, Node1, Node2 모두 동일하게 설정한다.

# swap off
$ sudo swapoff -a && sudo sed -i '/swap/s/^/#/' /etc/fstab

# firewall off
$ sudo ufw disable

# docker cgroup driver 설정
$ sudo docker info | grep "Cgroup Driver" -> Cgroup Driver: cgroupfs 가 출력됨
$ sudo vi /etc/docker/daemon.json
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m"
    },
    "storage-driver": "overlay2"
}
입력 후 Escape + :wq!

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

$ sudo systemctl daemon-reload

$ sudo systemctl restart docker

$ sudo docker info | grep "Cgroup Driver" -> Cgroup Driver: systemd 로 변경된 것 확인

2. Kubernetes 패키지 설치 deb 파일 다운로드

Master, Node1, Node2 모두 kubeadm, kubelet, kubectl 설치 필요

# 패키지 설치 파일 다운로드 디렉터리 생성
$ sudo mkdir ~/kubernetes_install && sudo chmod 777 ~/kubernetes_install && cd ~/kubernetes_install

# 최신 버전 deb 설치 파일 확인
https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages

# cri-tools deb 설치 파일 다운로드
$ wget https://packages.cloud.google.com/apt/pool/cri-tools_1.13.0-01_amd64_4ff4588f5589826775f4a3bebd95aec5b9fb591ba8fb89a62845ffa8efe8cf22.deb

# kubeadm deb 설치 파일 다운로드
$ wget https://packages.cloud.google.com/apt/pool/kubeadm_1.20.1-00_amd64_7cd8d4021bb251862b755ed9c240091a532b89e6c796d58c3fdea7c9a72b878f.deb

# kubectl deb 설치 파일 다운로드
$ wget https://packages.cloud.google.com/apt/pool/kubectl_1.20.1-00_amd64_b927311062e6a4610d9ac3bc8560457ab23fbd697a3052c394a1d7cc9e46a17d.deb

# kubelet deb 설치 파일 다운로드
$ wget https://packages.cloud.google.com/apt/pool/kubelet_1.20.1-00_amd64_560a52294b8b339e0ca8ddbc480218e93ebb01daef0446887803815bcd0c41eb.deb

# kubernetes-cni deb 설치 파일 다운로드
$ wget https://packages.cloud.google.com/apt/pool/kubernetes-cni_0.8.7-00_amd64_ca2303ea0eecadf379c65bad855f9ad7c95c16502c0e7b3d50edcb53403c500f.deb

# 설치 파일 확인
$ ls -la ~/kubernetes_install

3. Kubernetes 설치

$ sudo apt-get install socat conntrack ebtables -y
$ 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

4. Master Node 구성

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

I0518 21:43:08.240351    3019 version.go:251] remote version is much newer: v1.24.0; falling back to: stable-1.20
[init] Using Kubernetes version: v1.20.15
[preflight] Running pre-flight checks
	[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.16. 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.example.com] and IPs [10.96.0.1 10.100.0.104]
[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.example.com] and IPs [10.100.0.104 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.example.com] and IPs [10.100.0.104 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 12.502182 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.example.com 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.example.com as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: zczacd.f957gwvop0vbj8jz
[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 10.100.0.104:6443 --token zczacd.f957gwvop0vbj8jz \
    --discovery-token-ca-cert-hash sha256:36f6c8b22d27152cb76ff965d8b8014216e52a44ac84048289ddfd75e7326399

사용자 계정에서 kubectl 을 사용하기 위한 설정을 위 가이드를 통해 진행한다. kubeadm join 명령어는 worker node가 클러스터에 조인하기 위해 사용할 예정이다.

$ kubectl get pods --all-namespaces && kubectl get nodes

Master Node 가 잘 구성되었는지 확인한다.

Pod의 네트워크 환경을 맞추기 위해 flannel을 사용할 것이다.

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

위와 같이 모든 static pod가 running 상태가 되면 구성 완료

명령어 자동 완성 및 kubectl 을 k로 사용할 수 있도록 다음 명령어를 사용하여 설정한다.

$ source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
$ echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
$ alias k=kubectl
$ complete -F __start_kubectl k

5. Worker Node 구성

Master Node에서 kubeadm init 명령어 수행 후 출력된 kubeadm join 명령어를 Worker Node에서 실행하면 Cluster에 Join할 수 있다.

$ sudo kubeadm join 10.100.0.104:6443 --token zczacd.f957gwvop0vbj8jz \
    --discovery-token-ca-cert-hash sha256:36f6c8b22d27152cb76ff965d8b8014216e52a44ac84048289ddfd75e7326399

Master Node에서 kubectl get nodes 명령어를 사용하여 확인