0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KubernetesをalmalinuxでHA構成で構築する④ HA構成編

Posted at

最初に

この記事は下記urlの続きである。
HA構成をする前にsingle controller構成を構築した。
この記事ではここまでに作成したconfigを書き換えて構築する。

LBの構築

control nodeを分散させるためにHAProxyでLBを構築する。
これはload balancer nodeで実行する。

$ sudo vim /etc/selinux/config

SELINUX=disabled

$ sudo reboot
$ sudo firewall-cmd --add-port=6443/tcp --permanent
$ sudo firewall-cmd --reload
$ sudo dnf install haproxy -y
$ sudo mv /etc/haproxy/haproxy.conf /etc/haproxy/haproxy.conf.default
$ sudo vim /etc/haproxy/haproxy.conf

 global
    log         127.0.0.1 local2 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     256
    user        haproxy
    group       haproxy
    daemon

defaults
    mode               tcp
    log                global
    option             tcplog
    timeout connect    10s
    timeout client     30s
    timeout server     30s

frontend k8s
    bind *:6443
    mode               tcp
    default_backend    k8s_backend

backend k8s_backend
    balance            roundrobin
    server             k8s-master1 <control node0のip>:6443 check
    server             k8s-master2 <control node1のip>:6443 check
    server             k8s-master3 <control node2のip>:6443 check

$ sudo systemctl enable --now haproxy

kubernetesの構成の変更

これはcontrol nodeで実行する。

$ sudo rm -r .kube
$ sudo kubeadm reset
$ vim kubeadm-config.yml

apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: <control nodeのip>
  bindPort: 6443
nodeRegistration:
  criSocket: unix:///var/run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: control-node0 # 自由に変更
  taints: null
  kubeletExtraArgs:
    node-ip: <control nodeのip>
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.k8s.io
kind: ClusterConfiguration
kubernetesVersion: 1.29.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.2.0.0/16
  podSubnet: 10.1.0.0/16
scheduler: {}
controlPlaneEndpoint: <LBのip>:6443 # ここが追加されている。
---
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd

$ sudo kubeadm init --config kubeadm-default.yml

#前回作成したflannelのconfigを読み込む
$ sudo kubeadm apply -f kubeadm-flannel.yml

他nodeの設定

control nodeでtokenやkeyの変更を行う。jj:w
これはcontrol nodeで行う。

$ sudo kubeadm token delete abcdef.0123456789abcdef
$ sudo kubeadm token create
<token>
$ sudo kubeadm init phase upload-certs --upload-certs
<certs>
$ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
   openssl dgst -sha256 -hex | sed 's/^.* //'
<hash>

cotrol nodeのcluserへの参加

上記のcontrol node以外のcontrol nodeをclusterに参加させる。

$ vim kubeadm-config.yml

apiVersion: kubeadm.k8s.io/v1beta3
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
  bootstrapToken:
    apiServerEndpoint: <load balancerのip>:6443
    token: <token>
    caCertHashes:
    - sha256:<hash>
    unsafeSkipCAVerification: true
  timeout: 5m0s
  tlsBootstrapToken: <token>
kind: JoinConfiguration
nodeRegistration:
  criSocket: unix:///var/run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: control-node1 # 自由に変更
  taints: null
  kubeletExtraArgs:
    node-ip: <このnodeのip>
controlPlane:
  localAPIEndpint:
    advertiseAddress: <このnodeのip>
    bindPort: 6443
  certificateKey: <certs>

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

worker nodeのcluserへの参加

すでに一度clusterに参加させたworker nodeでは下記を実行する。

$ sudo kubeadm reset

worker nodeをclusterに参加させる。

$ vim kubeadm-config.yml

apiVersion: kubeadm.k8s.io/v1beta3
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
  bootstrapToken:
    apiServerEndpoint: <load balancerのip>:6443
    token: <token>
    caCertHashes:
    - sha256:<hash>
    unsafeSkipCAVerification: true
  timeout: 5m0s
  tlsBootstrapToken: <token>
kind: JoinConfiguration
nodeRegistration:
  criSocket: unix:///var/run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: worker-node0 # 自由に変更
  taints: null
  kubeletExtraArgs:
    node-ip: <worker nodeのip>

$ sudo kubeadm join --config kubeadm-config.yml

これでHA構成のkubernetes clusterが構築できた。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?