kubernetes ๊ฐœ๋… ๋ฐ cluster ์„ค์น˜ ๋ฐฉ๋ฒ• (2)

kubernetes ๊ฐœ๋… ๋ฐ cluster ์„ค์น˜ ๋ฐฉ๋ฒ• (2) - kubernetes cluster ์„ค์น˜

1. kubernetes ๊ฐœ๋… ๋ฐ cluster ์„ค์น˜ ๋ฐฉ๋ฒ• (1) - kubernetes ๊ฐœ๋… ์ •๋ฆฌ

2. kubernetes ๊ฐœ๋… ๋ฐ cluster ์„ค์น˜ ๋ฐฉ๋ฒ• (2) - kubernetes ๊ฐœ๋… ์ •๋ฆฌ

๋ชฉ์ฐจ

1. ๋ชฉ์ฐจ

2. Cluster ๊ตฌ์กฐ

3. hosts ๋“ฑ๋ก

4. ์„œ๋ฒ„ ์‹œ๊ฐ„ ๋™๊ธฐํ™”

5. ๋ฐฉํ™”๋ฒฝ ์„ค์ •

6. ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์‚ฌ์šฉ ๋ชจ๋“ˆ ๋กœ๋“œ

7. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

8. ์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„ ๋‹ค์šด๋กœ๋“œ

9. kubernetes ๋‹ค์šด๋กœ๋“œ

10. NFS ๊ตฌ์ถ•

11. kubernetes ์„ค์น˜

12. CNI ๋‹ค์šด๋กœ๋“œ

13. kubernetes cluster ๊ตฌ์ถ•

14. Storage Class

15. ๋งˆ์น˜๋ฉฐ

Cluster ๊ตฌ์กฐ

hostname ip ์—ญํ• 
k8s-master1 10.1.1.1 ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ
k8s-master2 10.1.1.2 ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ
k8s-worker1 10.1.1.3 ์›Œ์ปค ๋…ธ๋“œ
k8s-worker2 10.1.1.4 ์›Œ์ปค ๋…ธ๋“œ
k8s-worker3 10.1.1.5 ์›Œ์ปค ๋…ธ๋“œ
gitlab 10.1.1.6 pv ์ €์žฅ์†Œ

hosts ๋“ฑ๋ก

1
2
3
4
5
6
7
# vim /etc/hosts
10.1.1.1 k8s-master1
10.1.1.2 k8s-master2
10.1.1.3 k8s-worker1
10.1.1.4 k8s-worker2
10.1.1.5 k8s-worker3
10.1.1.6 gitlab

์„œ๋ฒ„ ์‹œ๊ฐ„ ๋™๊ธฐํ™”

1
2
# dnf install chronyd
# vim /etc/chronyd.conf
1
2
3
# /etc/chronyd.conf
server time.bora.net iburst
allow 10.1.1.0/24
1
2
3
# /etc/chronyd.conf
server k8s-master1 iburst
server k8s-master2 iburst

๋ฐฉํ™”๋ฒฝ ์„ค์ •

1
2
# systemctl stop firewalld
# systemctl disable firewalld

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ iptables๋ฅผ ๊ธฐ๋ณธ ๋ฐฉํ™”๋ฒฝ ์ •์ฑ…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, firewall์€ ๋‚ด๋ ค์ฃผ๋„๋ก ํ•œ๋‹ค.

1
2
# dnf install iptables-services
# vim /etc/sysconfig/iptables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# /etc/sysconfig/iptables

#### KUBERNETES API ์„œ๋ฒ„
-I INPUT -p tcp --dport 6443 -j ACCEPT

#### ETCD SERVER ํด๋ผ์ด์–ธํŠธ API
-I INPUT -p tcp --dport 2379:2380 -j ACCEPT

#### KUBELET API
-I INPUT -p tcp --dport 10250 -j ACCEPT

#### KUBE-SCHEDULER
-I INPUT -p tcp --dport 10259 -j ACCEPT

#### KUBE-CONTROLLER-MANAGER
-I INPUT -p tcp --dport 10257 -j ACCEPT
1
2
3
4
5
#### KUBELET API
-I INPUT -p tcp --dport 10250 -j ACCEPT

#### NODEPORT SERVICE
-I INPUT -p tcp -m tcp --dport 30000:32767 -j ACCEPT

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์‚ฌ์šฉ ๋ชจ๋“ˆ ๋กœ๋“œ

1
2
3
4
5
6
7
8
9
10
# cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

# cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

iptables๊ฐ€ ๋ธŒ๋ฆฟ์ง€ ๋“œ๋ผ์ด๋ธŒ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ†ต์‹ ํ•˜๋Š” ํŒจํ‚ท์„ filtering ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด br_netfilter ๋ชจ๋“ˆ์„ ์ ์žฌํ•œ๋‹ค.

์ด ๋ชจ๋“ˆ์„ iptables๊ฐ€ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋„๋ก net.bridge.bridge-nf-call-iptables๋„ ํ•จ๊ป˜ ์ ์žฌํ•œ๋‹ค.

ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

1
# vim /etc/profile
1
2
# /etc/profile
export KUBE_VERSION='1.23' # kubernetes version

์ปจํ…Œ์ด๋„ˆ ๋Ÿฐํƒ€์ž„ ๋‹ค์šด๋กœ๋“œ

1
2
3
4
5
6
7
8
9
10
# curl -L -o \
/etc/yum.repos.d/devel:kubic:libcontainers:stable.repo \
[https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/CentOS_8/devel:kubic:libcontainers:stable.repo](https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/CentOS_8/devel:kubic:libcontainers:stable.repo)
# curl -L -o \
/etc/yum.repos.d/devel:kubic:libcontainers:stable:cri-o:${KUBE_VERSION}.repo [https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:${KUBE_VERSION}/CentOS_8/devel:kubic:libcontainers:stable:cri-o:${KUBE_VERSION}.repo](https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$%7BVERSION%7D/CentOS_8/devel:kubic:libcontainers:stable:cri-o:$%7BVERSION%7D.repo)

# dnf install cri-o cri-tools

# systemctl daemon-reload
# systemctl enable crio --now

kubernetes ๋‹ค์šด๋กœ๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg [https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg](https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg)
exclude=kubelet kubeadm kubectl
EOF

# dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# systemctl enable --now kubelet

NFS ๊ตฌ์ถ•

1
2
3
4
5
6
7
# dnf install -y nfs-utils
# systemctl start nfs-server
# systemctl enable nfs-server
# systemctl start rpcbind
# systemctl enable rpcbind
# mkdir /kube_store
# vim /etc/exports
1
2
# /etc/exports
/kube_store 10.1.1.0/24(rw,sync,no_root_squash)
1
2
3
4
5
# dnf install -y nfs-utils
# mkdir /kube_store
# showmount -e gitlab
# mount -t nfs gitlab:/kube_store /kube_store
# vim /etc/fstab
1
2
# /etc/fstab
gitlab:/kube_store /kube_store nfs sync 0 0

kubernetes ์„ค์น˜

1
# vim /kube_store/kubeadm-config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# /kube_store/kubeadm-config.yml
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
localAPIEndpoint:
  bindPort: 6443
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: v1.23.0
controlPlaneEndpoint: "k8s-master1:6334"
apiServer:
  extraArgs:
    authorization-mode: "Node,RBAC"
networking:
  podSubnet: "192.168.0.0/16"
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: JoinControlPlane
localAPIEndpoint:
  bindPort: 6443
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
1
# kubeadm init --config kubeadm-config.yml --upload-certs

CNI ๋‹ค์šด๋กœ๋“œ

1
2
# curl https://docs.projectcalico.org/manifests/calico.yaml -O --insecure
# kubectl apply -f clico.yaml
1
# vim /etc/sysconfig/iptables
1
2
3
4
5
6
# /etc/sysconfig/iptables

-I INPUT -p tcp --dport 179 -j ACCEPT
-I INPUT -p udp --dport 4789 -j ACCEPT
-I INPUT -p tcp --dport 5473 -j ACCEPT
-I INPUT -p tcp --dport 443 -j ACCEPT
1
# systemctl restart iptables

kubernetes cluster ๊ตฌ์ถ•

1
2
3
4
5
6
# kubeadm join k8s-master1:6334 \
--token aaly0a.sp3dz8w4z8pe5dbb \
--discovery-token-ca-cert-hash sha256:a848eba0c188c12ab66aeb2359dee021f778ebd350f7c0c538949c7d1a57c320 \
--control-plane \
--certificate-key cc191962adc302a7127ad2ff830cacba816afb610018c70b4b7efcdff060764e \
--apiserver-bind-port 6334
1
2
3
# kubeadm join k8s-master1:6334 \
--token aaly0a.sp3dz8w4z8pe5dbb \
--discovery-token-ca-cert-hash sha256:a848eba0c188c12ab66aeb2359dee021f778ebd350f7c0c538949c7d1a57c320

Storage Class

1
2
3
4
5
# cd /kube_store
# git clone https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner.git
# cd nfs-subdir-external-provisioner/deploy
# kubectl apply -f rbac.yaml
# vim deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# /kube_store/nfs-subdir-external-provisioner/deploy/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 10.1.1.6
            - name: NFS_PATH
              value: /kube_store
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.1.1.6
            path: /kube_store
1
2
# kubectl apply -f deployment.yaml
# kubectl apply -f class.yaml

๋งˆ์น˜๋ฉฐ

์—ฌ๊ธฐ๊นŒ์ง€ ๋งˆ์ณค๋‹ค๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์ถ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๊ฐœ๋…์„ ์ดํ•ดํ•˜์…จ๋‹ค๋ฉด ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ๊ตฌ์ถ•ํ•˜์…จ์„๊ฑฐ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ ์ƒํƒœ๋กœ๋Š” ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๊ฐ„์˜ failover๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ด์œ ๋Š” ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ณ  ์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์ด๋งŒ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.