内容
マルチノードのKubernetes環境をAnsibleとVagrantを用いて構築します. 内容はほぼのKubernetesのブログの内容日本語訳です. 新しいバージョンをインストールするので,若干ブログの内容と違います.
ゴール
ローカルマシンにプロダクション環境のようなマルチノードKubernetesクラスタを作成することです.
モチベーション
本番環境のようなマルチノードのKubernetesクラスタを作るといろいろなご利益がある. MiniKubeのような優れたものがあるが,アプリケーションの設計やアーキテクチャーによる問題やバグは解決できない. マルチノードのKubernetes環境を作ることでそうした問題を再現して解決できるようになる. だから,マルチノードのKubernetesクラスタを作れると嬉しい.
動作環境
Ubuntu18.04
事前準備
Vagrant
以下のスクリプトを実行する.
#!/bin/bash
set -eu
sudo apt-get update
sudo apt-get install -y vagrant
virtualbox
以下のスクリプトを実行する.
#!/bin/bash
set -eu
sudo apt-get update
sudo apt-get install -y virtualbox
Ansible
以下のスクリプトを実行する.
#!/bin/bash
set -eu
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible -y
sudo apt-get update
sudo apt-get install -y ansible
セットアップ
Step 1: Vagrantfileを作る
# VagrantのBox
IMAGE_NAME = "bento/ubuntu-16.04"
# ノード数
N = 2
# "2"はVagrantの設定のバージョン."1"と"2"があるが,"2"を選択.
Vagrant.configure("2") do |config|
# 仮想マシンで一つのSSHキーを使うように設定
config.ssh.insert_key = false
# providerの設定.virtualboxを選択
# https://www.vagrantup.com/docs/virtualbox/
config.vm.provider "virtualbox" do |v|
# memory MB
v.memory = 1024
# cpu core
v.cpus = 2
end
# マルチノード構成にするために複数マシンを定義するため config.vm.define を用いる.
# https://www.vagrantup.com/docs/multi-machine/
config.vm.define "k8s-master" do |master|
# box名
# https://www.vagrantup.com/docs/boxes.html
master.vm.box = IMAGE_NAME
# networkの設定
# private_network, forwarded_port, public_networkがあるが,今回は外部と通信しないので,private_network.
# mac addressをそれぞれのゲストでユニークになるように変える必要がある
# https://kubernetes.io/ja/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#mac%E3%82%A2%E3%83%89%E3%83%AC%E3%82%B9%E3%81%A8product-uuid%E3%81%8C%E5%85%A8%E3%81%A6%E3%81%AE%E3%83%8E%E3%83%BC%E3%83%89%E3%81%A7%E3%83%A6%E3%83%8B%E3%83%BC%E3%82%AF%E3%81%A7%E3%81%82%E3%82%8B%E3%81%93%E3%81%A8%E3%81%AE%E6%A4%9C%E8%A8%BC
# https://www.vagrantup.com/docs/networking/
master.vm.network "private_network", ip: "192.168.50.10", :mac => "5CA1AB1E0000"
master.vm.hostname = "k8s-master"
# ansibleでプロビジョニングする
# https://www.vagrantup.com/docs/provisioning/ansible.html
master.vm.provision "ansible" do |ansible|
ansible.playbook = "kubernetes-setup/master-playbook.yml"
ansible.extra_vars = {
node_ip: "192.168.50.10",
}
end
end
(1..N).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.box = IMAGE_NAME
node.vm.network "private_network", ip: "192.168.50.#{i + 10}", :mac => "5CA1AB1E000#{i}"
node.vm.hostname = "node-#{i}"
node.vm.provision "ansible" do |ansible|
ansible.playbook = "kubernetes-setup/node-playbook.yml"
ansible.extra_vars = {
node_ip: "192.168.50.#{i + 10}",
}
end
end
end
end
Step 2: Kubernetes Master用のAnsible Playbookを作る
先程Vagrantfile
を作成したディレクトリ内ににkubernetes-setup
というディレクトリを作成する.
そして,kubernetes-setup
の中にmaster-playbook.yml
とnode-playbook.yml
を作成する.
$ ls
Vagrantfile
$ mkdir kubernetes-setup
$ ls
kubernetes-setup/ Vagrantfile
$ touch kubernetes-setup/master-playbook.yml kubernetes-setup/node-playbook.yml
kubernetes-setup/master-playbook.yml
---
---
# 対象ホスト
- hosts: all
# ルート権限を使う
become: true
tasks:
# パッケージをインストールする
- name: Install packages that allow apt to be used over HTTPS
apt:
name: "{{ packages }}"
# playbook実行後の状態
state: present
update_cache: yes
vars:
# インストールするパッケージたち
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
- name: Add an apt signing key for Docker
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add apt repository for stable version
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
state: present
- name: Install docker and its dependecies
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- docker-ce
- docker-ce-cli
notify:
- docker status
# cgroup-driverをcgroupfsではなく,systemdにする
# https://kubernetes.io/ja/docs/setup/production-environment/container-runtimes/
- name: Setup Docker cgroup-driver
copy:
content: {"exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": {"max-size": "100m"}, "storage-driver": "overlay2"}
dest: /etc/docker/daemon.json
- name: Setup Docker systemd
file: path=/etc/systemd/system/docker.service.d state=directory owner=root group=root mode=0744
- name: Restart docker
service:
name: docker
daemon_reload: yes
state: restarted
- name: Add vagrant user to docker group
user:
name: vagrant
group: docker
# swapを無効化する
# swapが有効だとKubeletが起動しないので
- name: Remove swapfile from /etc/fstab
mount:
name: "{{ item }}"
fstype: swap
state: absent
with_items:
- swap
- none
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
# kubelet, kubeadm, kubectlをインストールする
- name: Add an apt signing key for Kubernetes
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Adding apt repository for Kubernetes
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes.list
- name: Install Kubernetes binaries
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- kubelet
- kubeadm
- kubectl
# DebianやUbuntuでは/etc/default/kubeletは消されている
# https://github.com/kubernetes/release/pull/672
# 自分で作ってとのことなので作る
- name: Configure node ip
copy:
content: |
KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
dest: /etc/default/kubelet
# lineinfile:
# path: /etc/default/kubelet
# line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
- name: Restart kubelet
service:
name: kubelet
daemon_reload: yes
state: restarted
# kubeadmを用いてKubernetesクラスタを初期化する
- name: Initialize the Kubernetes cluster using kubeadm
command: kubeadm init --apiserver-advertise-address="192.168.50.10" --apiserver-cert-extra-sans="192.168.50.10" --node-name k8s-master --pod-network-cidr=192.168.0.0/16
# vagrantユーザがKubernetesクラスタにアクセスするための設定を行う
- name: Setup kubeconfig for vagrant user
command: "{{ item }}"
with_items:
- mkdir -p /home/vagrant/.kube
- cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
- chown vagrant:vagrant /home/vagrant/.kube/config
# pod感の通信のためにCalicoを導入します
# https://techblog.yahoo.co.jp/infrastructure/kubernetes_calico_networking/
- name: Install calico pod network
become: false
command: kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml
# command: kubectl create -f https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml
# ノードをKubernetesクラスタに加えるためにJOINコマンドを生成し,登録する
- name: Generate join command
command: kubeadm token create --print-join-command
register: join_command
- name: Copy join command to local file
become: false
local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"
# docker daemonの状態を確認
handlers:
- name: docker status
service: name=docker state=started
Step 3: Kubernetes Node用のAnsible Playbookを作る
kubernetes-setup/node-playbook.yml
---
# 対象ホスト
---
# 対象ホスト
- hosts: all
# ルート権限を使う
become: true
tasks:
# パッケージをインストールする
- name: Install packages that allow apt to be used over HTTPS
apt:
name: "{{ packages }}"
# playbook実行後の状態
state: present
update_cache: yes
vars:
# インストールするパッケージたち
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
- name: Add an apt signing key for Docker
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add apt repository for stable version
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
state: present
- name: Install docker and its dependecies
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- docker-ce
- docker-ce-cli
- containerd.io
notify:
- docker status
- name: Setup Docker cgroup-driver
copy:
content: {"exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": {"max-size": "100m"}, "storage-driver": "overlay2"}
dest: /etc/docker/daemon.json
- name: Setup Docker systemd
file: path=/etc/systemd/system/docker.service.d state=directory owner=root group=root mode=0744
- name: Restart docker
service:
name: docker
daemon_reload: yes
state: restarted
- name: Add vagrant user to docker group
user:
name: vagrant
group: docker
# swapを無効化する
# swapが有効だとKubeletが起動しないので
- name: Remove swapfile from /etc/fstab
mount:
name: "{{ item }}"
fstype: swap
state: absent
with_items:
- swap
- none
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
# kubelet, kubeadm, kubectlをインストールする
- name: Add an apt signing key for Kubernetes
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Adding apt repository for Kubernetes
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes.list
- name: Install Kubernetes binaries
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- kubelet
- kubeadm
- kubectl
# DebianやUbuntuでは/etc/default/kubeletは消されている
# https://github.com/kubernetes/release/pull/672
# 自分で作ってとのことなので作る
- name: Configure node ip
copy:
content: |
KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
dest: /etc/default/kubelet
# lineinfile:
# path: /etc/default/kubelet
# line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
- name: Restart kubelet
service:
name: kubelet
daemon_reload: yes
state: restarted
# nodeをKubernetesクラスタに追加する
- name: Copy the join command to server location
copy: src=join-command dest=/tmp/join-command.sh mode=0777
- name: Join the node to cluster
command: sh /tmp/join-command.sh
# docker daemonの状態を確認
handlers:
- name: docker status
service: name=docker state=started
Step 4: Vagrantfileを実行する
$ ls
kubernetes-setup/ Vagrantfile
$ vagrant up
動作確認
$ ## Accessing master
$ vagrant ssh k8s-master
vagrant@k8s-master:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 12h v1.17.2
node-1 Ready <none> 12h v1.17.2
node-2 Ready <none> 12h v1.17.2
$ ## Accessing nodes
$ vagrant ssh node-1
$ vagrant ssh node-2