Kubernetes - 실습 환경 구축

jsbak·2023년 6월 4일
0

Cloud

목록 보기
54/59

단일 노드 구축

  • VM 제원
  • minikube version: v1.23.2
  • kubectl version: v1.22.2
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:38:50Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:32:41Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}

Minikube 설치

  • Single Node 구축: Master Node + Worker Node
$ yum update -y 
$ hostnamectl set-hostname minikube

# 도커 7버전
$ curl -fsSL https://get.docker.com/ | sudo sh 
$ systemctl enable --now docker
$ yum install -y conntrack git

# minikube 내려받기
$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.23.2/minikube-linux-amd64 && chmod +x minikube
	# 해당 경로가 없으면 생성
$ mkdir -p /usr/local/bin/

# `minikube`를 `/usr/local/bin/` 에 위치 시킴
$ install minikube /usr/local/bin/
$ minikube version

# *minikube 실행*
$ minikube start --driver=none
$ minikube status
	위치를 옮김
$ sudo mv /root/.kube /root/.minikube $HOME
	소유권 변경, 자격증명 관련 권한 존재
$ sudo chown -R $USER $HOME/.kube $HOME/.minikube

kubectl 설치

  • kubectl <CMD> => 마스터 노드에 요청
	# 다운로드
$ curl -LO https://dl.k8s.io/release/v1.22.2/bin/linux/amd64/kubectl
	# kubectl 설치
$ install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
$ yum install bash-completion -y
$ source <(kubectl completion bash)
	# 시스템이 껏다 켜지더라도 자동완경 가능하게 설정
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ exit
$ kubectl version
	# 설치한 싱글노드인 "minikube" 확인가능
$ kubectl get node

멀티 노드 구축

All Node 설정

  • 가상머신 생성
  • 설정
$ cat <<EOF >> /etc/hosts
192.168.2.125 master1
192.168.2.126 worker1
192.168.2.127 worker2
EOF

$ hostnamectl set-hostname master
$ curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
$ sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/docker-ce.repo
$ yum --enablerepo=docker-ce-stable -y install docker-ce-19.03.15-3.el7
$ mkdir /etc/docker
$ cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
$ systemctl enable --now docker
$ systemctl daemon-reload
$ systemctl restart docker
$ systemctl disable --now firewalld

# SELinux 비활성화
$ setenforce 0
$ sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

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

$ cat <<EOF > /etc/sysctl.d/k8s.conf # kubernetes
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

$ sysctl --system
$ reboot

Kubeadm 설치

Multi Node: Master Node, Worker Node

$ cat <<'EOF' > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

$ yum -y install kubeadm-1.19.16-0 kubelet-1.19.16-0 kubectl-1.19.16-0 --disableexcludes=kubernetes
$ systemctl enable kubelet
$ poweroff

--- Master ---

# "kubeadm init" Kubernetes control plane 설정
# "--apiserver-advertise-address=192.168.2.125"
## API 서버가 수신 중임을 알릴 IP 주소입니다. 
## 설정하지 않으면 기본 네트워크 인터페이스 사용
# "--pod-network-cidr=10.244.0.0/16"
## 포드 네트워크의 IP 주소 범위를 지정
## 설정된 경우 컨트롤 플레인은 모든 노드에 CIDR을 자동으로 할당
$ kubeadm init --apiserver-advertise-address=192.168.2.125 --pod-network-cidr=10.244.0.0/16

$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config

$ kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
$ kubectl get pods --all-namespaces
$ source <(kubectl completion bash)
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ exit

--- Node ---

$ kubeadm join 192.168.2.125:6443 --token dvheus.2rzrpezo0ndia21q \
    --discovery-token-ca-cert-hash sha256:162b3363e8952d01bfa1a96193236b756ef0c49ea7318747e3af4b81d80af297
$ kubectl get nodes

기본 사용법

$ mkdir workspace && cd $_
$ kubectl [명령어] [유형] [자원 이름] [옵션]
$ kubectl get node
$ kubectl run nginx-pod --image=nginx # pending > ContainerCreating > Running
$ kubectl expose pod nginx-pod --name clusterip --type=ClusterIP --port 80
$ kubectl expose pod nginx-pod --name nodeport --type=NodePort --port 80
$ kubectl expose pod nginx-pod --name loadbalancer --type=LoadBalancer --external-ip 192.168.0.192 --port 80
$ kubectl get pod
$ kubectl get service
$ kubectl exec -it nginx-pod -- bash
$ kubectl get all
$ kubectl delete svc --all
$ kubectl delete pod nginx-pod
$ kubectl delete pod,svc --all
profile
끄적끄적 쓰는곳

0개의 댓글