wxtyixiao 2019-07-01
作者:Naresh L J(Infosys)
此博客文章介绍了为开发而设的多节点Kubernetes集群所需的步骤。此设置提供了类似生产环境的群集,可以在本地计算机上进行。
多节点Kubernetes集群提供类似生产的环境,具有各种优势。尽管Minikube提供了很好的入门平台,但它并没有提供使用多节点集群的机会,帮助解决与应用程序设计和体系结构相关的问题或错误。例如,Ops可以在多节点集群环境中重现问题,测试者可以部署多个版本的应用程序来执行测试用例和验证更改。这些优势使团队能够更快地解决问题,从而提高敏捷性。
Vagrant可以让我们轻松创建虚拟环境,消除导致“在我的机器能工作”现象的陷阱。它可以与多个供应程序一起使用,例如Oracle VirtualBox、VMware、Docker等。它允许我们通过使用配置文件来创建一次性环境。
Ansible是自动化软件配置管理的基础架构自动化引擎。它是无代理的,允许我们使用SSH密钥连接到远程计算机。Ansible playbooks以yaml编写,以简单的文本文件提供库存管理。
我们将建立一个Kubernetes集群,包含一个主节点和两个工作节点。所有节点将运行Ubuntu Xenial 64位操作系统,配置使用Ansible playbooks。
使用你喜欢的文本编辑器,创建名为Vagrantfile的文件,插入下面的代码。N的值表示集群中存在的节点数,可以相应地进行修改。在下面的示例中,我们将N的值设置为2。
IMAGE_NAME = "bento/ubuntu-16.04" N = 2 Vagrant.configure("2") do |config| config.ssh.insert_key = false config.vm.provider "virtualbox" do |v| v.memory = 1024 v.cpus = 2 end config.vm.define "k8s-master" do |master| master.vm.box = IMAGE_NAME master.vm.network "private_network", ip: "192.168.50.10" master.vm.hostname = "k8s-master" master.vm.provision "ansible" do |ansible| ansible.playbook = "kubernetes-setup/master-playbook.yml" 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}" node.vm.hostname = "node-#{i}" node.vm.provision "ansible" do |ansible| ansible.playbook = "kubernetes-setup/node-playbook.yml" end end end
在Vagrantfile相同的目录中创建名为kubernetes-setup的目录。在kubernetes-setup目录中创建两个名为master-playbook.yml和node-playbook.yml的文件。
在master-playbook.yml文件中,添加以下代码。
我们将安装以下软件包,然后将名为“vagrant”的用户添加到“docker”组。
--- - hosts: all become: true tasks: - name: Install packages that allow apt to be used over HTTPS apt: name: "{{ packages }}" 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 - containerd.io notify: - docker status - name: Add vagrant user to docker group user: name: vagrant group: docker
- 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
- 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
- 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
- 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
- name: Install calico pod network become: false command: kubectl create -f https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml
- name: Generate join command command: kubeadm token create --print-join-command register: join_command - name: Copy join command to local file local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"
handlers: - name: docker status service: name=docker state=started
在kubernetes-setup目录中创建名为node-playbook.yml的文件。
将以下代码添加到node-playbook.yml中
- 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
$ cd /path/to/Vagrantfile $ vagrant up
完成上述所有步骤后,Kubernetes集群应该已启动并运行。我们可以使用Vagrant登录主节点或工作节点,如下所示:
$ ## Accessing master $ vagrant ssh k8s-master vagrant@k8s-master:~$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 18m v1.13.3 node-1 Ready <none> 12m v1.13.3 node-2 Ready <none> 6m22s v1.13.3 $ ## Accessing nodes $ vagrant ssh node-1 $ vagrant ssh node-2
KubeCon + CloudNativeCon + Open Source Summit大会日期:
KubeCon + CloudNativeCon + Open Source Summit赞助方案
KubeCon + CloudNativeCon + Open Source Summit多元化奖学金现正接受申请
KubeCon + CloudNativeCon和Open Source Summit即将首次合体落地中国
KubeCon + CloudNativeCon + Open Source Summit购票窗口,立即购票!
CNCF邀请你加入最终用户社区