※当ブログではアフィリエイト広告を利用しています
前回記事では、kubeadm, kubelet, kubectlを導入したCentOSのVMを3インスタンス作成するところまでを実施しました。
今回はkubeadmを使ってKubernetesのMasterとNodeを構築し、Kubernetesクラスターを使えるところまで持っていきます。以下の3.と4.を実施します。
- Master VMを作成し、必要コンポーネントをインストール
- Master VMをコピーしてNode VMを作成
- Masterの構成
- Nodeの構成
そして、以下のような構成のクラスターを作ります。

ホスト名 | IPアドレス | 役割 |
k8smaster.local | 10.0.2.15/24 (NATネットワーク) 192.168.56.101/24 (ホストオンリーネットワーク) | Master |
k8snode-1.local | 10.0.2.16/24 192.168.56.102/24 | Node |
k8snode-2.local | 10.0.2.17/24 192.168.56.103/24 | Node |
Masterの構成
kubeadmで初期化
早速、k8smaster.localにてKubernetesのMasterを構成します。
k8smasterへログインし、以下のコマンドを実行します。
kubeadm init --apiserver-advertise-address=192.168.56.101 --pod-network-cidr=10.100.0.0/16
「–apiserver-advertise-address=192.168.56.101」では、引数名の通り、KubernetesのAPI ServerのIPアドレスを指定します。NICが複数ある場合は指定する必要があります。
今回の場合、VirtualBoxのホストから接続したいと思ったため、ホストオンリーネットワークのIPアドレスを指定します。
「–pod-network-cidr=10.100.0.0/16」では、KubernetesクラスターにデプロイされたPodに割り当てられるIPアドレスの範囲を指定します。これは既存のネットワークと重複しないように指定しましょう。
うまくいくと、数分で以下のように完了するはずです。
[root@k8smaster ~]# kubeadm init --apiserver-advertise-address=192.168.56.101 --pod-network-cidr=10.100.0.0/16 [init] Using Kubernetes version: v1.13.3 [preflight] Running pre-flight checks [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' [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] Activating the kubelet service [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "ca" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [k8smaster.local kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.56.101] [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [k8smaster.local localhost] and IPs [192.168.56.101 127.0.0.1 ::1] [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [k8smaster.local localhost] and IPs [192.168.56.101 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-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 [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 30.502606 seconds [uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8smaster.local" as an annotation [mark-control-plane] Marking the node k8smaster.local as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node k8smaster.local as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: 7x8km5.9ynusdbvw1etebcb [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master 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 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/ You can now join any number of machines by running the following on each node as root: kubeadm join 192.168.56.101:6443 --token 7x8km5.xxxxxxxxxxxxx --discovery-token-ca-cert-hash sha256:c19dbfbf771893e246893616xxxxxx42392c93bdxxxxxxd715771b5897f
最後の行はNodeをMasterと紐づけるために使用するコマンドのため、コピーしておきます。
kubectlの構成
kubectlコマンドでクラスターの操作ができるようにします。必要に応じて.bashrcにも追記してください。
export KUBECONFIG=/etc/kubernetes/admin.conf
source <(kubectl completion bash)
# 必要に応じて
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bashrc
echo "source <(kubectl completion bash)" >> ~/.bashrc
2行目を実行することで、kubectlコマンド内で自動補完できるようになります。
ネットワークアドオンの構成(Calico)
コンテナ間のネットワーク接続を実現するため、今回はCalicoを構成します。
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
Nodeの構成
k8snode-1.localにて、先ほどの最後に出てきたコマンドを実行します。
# トークンや証明書で認証するため、値は環境によって異なります
kubeadm join 192.168.56.101:6443 --token 7x8km5.xxxxxxxxxxxxx --discovery-token-ca-cert-hash sha256:c19dbfbf771893e246893616xxxxxx42392c93bdxxxxxxd715771b5897f
問題がなければ、以下のようなメッセージが出て完了します。
[preflight] Running pre-flight checks [discovery] Trying to connect to API Server "192.168.56.101:6443" [discovery] Created cluster-info discovery client, requesting info from "https://192.168.56.101:6443" [discovery] Requesting info from "https://192.168.56.101:6443" again to validate TLS against the pinned public key [discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "192.168.56.101:6443" [discovery] Successfully established connection with API Server "192.168.56.101:6443" [join] Reading configuration from the cluster... [join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' [kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" ConfigMap in the kube-system namespace [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] Activating the kubelet service [tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap... [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8snode-2.local" as an annotation 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 master to see this node join the cluster.
k8snode-2.localでも同じ操作を実行します。
スナップショットの取得
すべて完了したら、スナップショットを必ず取得しましょう。
最初のうちはいろいろ操作しているうちにいとも簡単に壊すことができてしまいます。
問題が起きた場合にもすぐに戻せるように、スナップショットを取得しておきましょう。
まとめ
ここまでで実験用のKubernetesクラスターが完成しました。
今後はこの環境を使いながら、様々なTipsや操作、テクニックなどを解説していきます。