[01] 在本地环境中,使用两台虚拟机来构建一个拥有一个主节点和一个工作节点的Kubernetes环境(虚拟机构建)

总结

以下是在本文中记录了在本地构建k8s时的配置表。本文将记录“为了引入k8s而进行的前期准备,包括虚拟机(以下简称VM)的构建”。

基本上,我们只是手动执行https://github.com/takara9/vagrant-kubernetes中使用Ansible执行的操作。
(稍微更改了一些清单)

No用途ノード名形態公開IP内部IPOS備考1k8sマスタmaster01VM192.168.1.91172.24.20.11Ubuntu18.04
2k8sノードnode01VM192.168.1.92172.24.20.12Ubuntu18.04

参考来源的网站和书籍

URL備考実践 Vagrant
15Stepで習得 Dockerから入るKubernetesK8s だけでなく、Ansible, Vagrant, GlusterFS のことなども学べる.https://github.com/takara9/vagrant-k8s『15Stepで習得 Dockerから入るKubernetes』の著者が公開されている GitHub.
Vagrant や Ansible コードを公開してくださっている.https://github.com/takara9/vagrant-kubernetes同上https://github.com/takara9/codes_for_lessons同上https://nextpublishing.jp/book/12197.html『解体kubeadm フェーズから読み解くKubernetesクラスタ構築ツールの全貌』を参考にして 1マスタ・1ノードを構築した.

环境

物理电脑和虚拟电脑的操作系统

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

电脑科学

已经安装了下面的软件。

    • Vagrant

 

    VirtualBox

步骤

把Vagrant和Ansible的文件放置在一起。

Vagrant和Ansible的文件是参考书籍《15步学习Docker进入Kubernetes》而来的。非常感谢。

.
|-- Vagrantfile
|-- ansible.cfg
|-- hosts
`-- playbook
    |-- install_master.yml
    |-- install_node.yml
    `-- kubernetes
        |-- defaults
        |   `-- main.yml
        `-- tasks
            `-- main.yml

文件的内容 de

安装节点的剧本安装过程。

---
- name: Kubernetes base
  hosts: nodes
  gather_facts: true
  become: true
  roles:
    - kubernetes

./playbook/install_master.yml 只需提供一个选项。

---
- name: Kubernetes base
  hosts: master01
  gather_facts: true
  become: true
  roles:
    - kubernetes

./剧本/ kubernetes /任务/ main.yml

# Master と Workerで共通のタスク
#- debug: msg="{{ ansible_facts }}"

##################################################
##  Docker CE のインストール
##################################################
################################################## Ubuntu
- name: Add Docker GPG key
  apt_key: url=https://download.docker.com/linux/ubuntu/gpg
  when:
    - ansible_facts.distribution == "Ubuntu"
- name: Add Docker APT repository
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable
  when:
    - ansible_facts.distribution == "Ubuntu"
- name: Install a list of packages
  apt:
    name: "{{ packages }}"
    state: present
    update_cache: yes
  vars:
    packages:
    - apt-transport-https
    - ca-certificates
    - curl
    - software-properties-common
    - nfs-common
    - docker-ce{{ docker_version_ubuntu }}
  when:
    - ansible_facts.distribution == "Ubuntu"
################################################### Ubuntu & CentOS    
- name: Add the user 'vagrant' with a specific uid and a primary group of 'docker'
  user:
    name: vagrant
    comment: docker exection user
    group: docker
- name: Start dockerd
  systemd:
    name: docker
    state: started
    enabled: yes

./playbook/kubernetes/defaults/main.yml 的中文本机翻译如下:

# default var file
---
# Docker Package Version
docker_version_ubuntu: =18.06.1~ce~3-0~ubuntu

./hosts 的中文含义是什么?

master01   ansible_connection=local
node01     ansible_connection=local

[nodes]
node01

./ansible.cfg => Ansible 配置文件

[defaults]
inventory = /home/vagrant/playbook/hosts
host_key_checking = no
log_path = ansible.log
interpreter_python = auto

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes

/Vagrantfile 的中文含义是:

# coding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :

linux_os = "ubuntu/bionic64"   # Ubuntu 18.04
#linux_os  = "generic/centos7"  # CentOS 7.7
bridge_if = "en0: Wi-Fi (Wireless)"

vm_spec = [
  { name: "master01", cpu: 2, memory: 2048,
    box: linux_os,
    private_ip: "172.24.20.11",
    public_ip: "192.168.1.91",
    storage: [], playbook: "install_master.yml",
    comment: "Master node" },

  { name: "node01", cpu: 4, memory: 8192,
    box: linux_os,
    private_ip: "172.24.20.12",
    public_ip: "192.168.1.92",
    storage: [], playbook: "install_node.yml",
    comment: "Worker node #1" },
]


Vagrant.configure("2") do |config|
  vm_spec.each do |spec|
    config.vm.define spec[:name] do |v|
      v.vm.box = spec[:box]
      v.vm.hostname = spec[:name]
      v.vm.network :private_network,ip: spec[:private_ip]
      #v.vm.network :public_network,ip:  spec[:public_ip], bridge: bridge_if
      v.vm.provider "virtualbox" do |vbox|
        vbox.gui = false
        vbox.cpus = spec[:cpu]
        vbox.memory = spec[:memory]
        i = 1
        spec[:storage].each do |vol|
          vdisk = "vdisks/sd-" + spec[:name] + "-" + i.to_s + ".vdi"
          if not File.exist?(vdisk) then
            if i == 1 then
              vbox.customize [
                'storagectl', :id,
                '--name', 'SATA Controller',
                '--add', 'sata',
                '--controller', 'IntelAHCI']
            end            
            vbox.customize [
              'createmedium', 'disk',
              '--filename', vdisk,
              '--format', 'VDI',
              '--size', vol * 1024 ]
          end
          vbox.customize [
            'storageattach', :id,
            '--storagectl', 'SATA Controller',
            '--port', i,
            '--device', 0,
            '--type', 'hdd',
            '--medium', vdisk]
          i = i + 1
        end
      end
      v.vm.synced_folder ".", "/vagrant", owner: "vagrant",
                            group: "vagrant", mount_options: ["dmode=700", "fmode=700"]

      v.vm.provision "ansible_local" do |ansible|
        ansible.playbook       = "playbook/" + spec[:playbook]
        ansible.verbose        = false #!
        ansible.install        = true
        ansible.limit          = spec[:name] 
        ansible.inventory_path = "hosts"
      end

      # v.vm.provision "shell", inline: <<-SHELL
      #   apt-get update
      #   apt-get install -y apache2
      # SHELL
    end
    config.ssh.forward_x11 = true
  end
end

启动虚拟机

$ vagrant up

确认两个虚拟机正在运行。

$ vagrant status

以上的内容。

广告
将在 10 秒后关闭
bannerAds