做者:Naresh L J(Infosys)html
此博客文章介紹了爲開發而設的多節點Kubernetes集羣所需的步驟。此設置提供了相似生產環境的羣集,能夠在本地計算機上進行。node
多節點Kubernetes集羣提供相似生產的環境,具備各類優點。儘管Minikube提供了很好的入門平臺,但它並無提供使用多節點集羣的機會,幫助解決與應用程序設計和體系結構相關的問題或錯誤。例如,Ops能夠在多節點集羣環境中重現問題,測試者能夠部署多個版本的應用程序來執行測試用例和驗證更改。這些優點使團隊可以更快地解決問題,從而提升敏捷性。linux
Vagrant可讓咱們輕鬆建立虛擬環境,消除致使「在個人機器能工做」現象的陷阱。它能夠與多個供應程序一塊兒使用,例如Oracle VirtualBox、VMware、Docker等。它容許咱們經過使用配置文件來建立一次性環境。docker
Ansible是自動化軟件配置管理的基礎架構自動化引擎。它是無代理的,容許咱們使用SSH密鑰鏈接到遠程計算機。Ansible playbooks以yaml編寫,以簡單的文本文件提供庫存管理。ubuntu
咱們將創建一個Kubernetes集羣,包含一個主節點和兩個工做節點。全部節點將運行Ubuntu Xenial 64位操做系統,配置使用Ansible playbooks。api
使用你喜歡的文本編輯器,建立名爲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文件中,添加如下代碼。ssh
咱們將安裝如下軟件包,而後將名爲「vagrant」的用戶添加到「docker」組。curl
--- - 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邀請你加入最終用戶社區