Saturday, 26 December 2020

Single Node K8S Cluster

- hosts: localhost
  become: yes
  tasks:
   - name: install gpg
     apt:
       name: gpg
       state: present
       update_cache: true

   - name: install Docker
     apt:
       name: docker.io
       state: present
       update_cache: true

   - name: Enable service
     service:
       name: docker
       enabled: yes

   - name: start service
     service:
       name: docker
       state: started

   - name: install APT Transport HTTPS
     apt:
       name: apt-transport-https
       state: present

   - name: add Kubernetes apt-key
     apt_key:
       url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
       state: present

   - name: add Kubernetes' APT repository
     apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present
      filename: 'kubernetes'

   - name: install kubelet
     apt:
       name: kubelet
       state: present
       update_cache: true

   - name: install kubeadm
     apt:
       name: kubeadm
       state: present

   - name: install kubectl
     apt:
       name: kubectl
       state: present
       force: yes

   - name: Disable SWAP since kubernetes can't work with swap enabled (1/2)
     shell: |
       swapoff -a
     when: ansible_swaptotal_mb > 0

   - name: Disable SWAP in fstab since kubernetes can't work with swap enabled (2/2)
     replace:
       path: /etc/fstab
       regexp: '^(.+?\sswap\s+sw\s+.*)$'
       replace: '# \1'

   - name: initialize the cluster
     shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster_initialized.txt
     args:
       chdir: $HOME
       creates: cluster_initialized.txt

   - name: create .kube directory
     file:
       path: $HOME/.kube
       state: directory
       mode: 0755

   - name: copy admin.conf to user's kube config
     copy:
       src: /etc/kubernetes/admin.conf
       dest: $HOME/.kube/config
       remote_src: yes

   - name: install Pod network
     become: yes
     shell: kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
     args:
       chdir: $HOME
       creates: pod_network_setup.txt

   - name: Single Node Cluster
     shell: kubectl taint nodes --all node-role.kubernetes.io/master-


No comments:

Post a Comment