Day3ではIP poolsを設定して、 Calicoのノード(というかPod)に割り当てるIPの設定を行った。今回はようやくCalicoをCNIとしてインストールする。
まず最初に、master node上でCNIが利用するSSL通信のための証明書と鍵を作成する。
$ openssl req -newkey rsa:4096 \
-keyout cni.key \
-nodes \
-out cni.csr \
-subj "/CN=calico-cni"
次に、Kubernetesが持っているCA(Certification Authority/認証局)を利用して、作成した証明書に署名する。
$ sudo openssl x509 -req -in cni.csr \
-CA /etc/kubernetes/pki/ca.crt \
-CAkey /etc/kubernetes/pki/ca.key \
-CAcreateserial \
-out cni.crt \
-days 365
$ sudo chown user:user cni.crt
次に、CNI Plugin(つまりCalico)がKubernetesにアクセスするために使うkubeconfigファイルを作成する。
$ APISERVER=$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}')
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--server=$APISERVER \
--kubeconfig=cni.kubeconfig
kubectl config set-credentials calico-cni \
--client-certificate=cni.crt \
--client-key=cni.key \
--embed-certs=true \
--kubeconfig=cni.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user=calico-cni \
--kubeconfig=cni.kubeconfig
kubectl config use-context default --kubeconfig=cni.kubeconfig
ドキュメントによると、ここで作ったcni.kubeconfig
は各ノードにコピーしておいたほうが良いらしい。とはいえ、どこにコピーすれば良いのかわからなかったので、とりあえず各ノードの同じユーザーのホームディレクトリに置いといた。
次にCalicoのノードがKubernetesのリソースにアクセスするための権限を持ったRoleを作る。
kubectl apply -f - <<EOF
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: calico-cni
rules:
# The CNI plugin needs to get pods, nodes, and namespaces.
- apiGroups: [""]
resources:
- pods
- nodes
- namespaces
verbs:
- get
# The CNI plugin patches pods/status.
- apiGroups: [""]
resources:
- pods/status
verbs:
- patch
# These permissions are required for Calico CNI to perform IPAM allocations.
- apiGroups: ["crd.projectcalico.org"]
resources:
- blockaffinities
- ipamblocks
- ipamhandles
verbs:
- get
- list
- create
- update
- delete
- apiGroups: ["crd.projectcalico.org"]
resources:
- ipamconfigs
- clusterinformations
- ippools
verbs:
- get
- list
EOF
で、作ったRoleをCalicoのアカウントcalico-cni
にバインドする。
$ kubectl create clusterrolebinding calico-cni --clusterrole=calico-cni --user=calico-cni
ここまで来てようやくCalicoがインストールできる準備が整った。以下のコマンドを各ノードで、かつroot
ユーザーで行う必要がある。
...なんか特にこれといった説明もなくザザッと設定ファイルを作ってコマンドを叩いただけなので、次回はここでやっていることを1つずつ解説してみたいと思う。Calicoとは直接関係ないけど、KubernetesとCalicoを理解する上では役に立つと思うので。
今日はここまで。