lab:kubernetes_app:step_3_-_set_up_kubernetes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lab:kubernetes_app:step_3_-_set_up_kubernetes [2024/02/01 03:33] – updated user | lab:kubernetes_app:step_3_-_set_up_kubernetes [2024/05/13 18:16] (current) – removed user | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Step 3 - Set up Kubernetes ====== | ||
- | In our previous step we [[Step 2 - Deploy the VMs|deployed our fleet of VMs]]. | ||
- | Now we are going to install Kubernetes on the VMs: | ||
- | * The first will be the Kubernetes (k8s) master node | ||
- | * The second will be the node running the SQL service | ||
- | * The remaining VMs will be the " | ||
- | |||
- | Purpose: | ||
- | * Demonstrate a running a complex workload of web applications on Kubernetes | ||
- | |||
- | References | ||
- | * [[https:// | ||
- | * [[https:// | ||
- | * [[https:// | ||
- | * [[https:// | ||
- | |||
- | ====== Update the inventory File ====== | ||
- | Modify the '' | ||
- | |||
- | <file ini inventory> | ||
- | [master] | ||
- | 192.168.99.201 | ||
- | [sql] | ||
- | 192.168.99.202 | ||
- | [workers] | ||
- | 192.168.99.203 | ||
- | 192.168.99.204 | ||
- | </ | ||
- | |||
- | Configure ansible.cfg to use the inventory file | ||
- | |||
- | <file yaml ansible.cfg> | ||
- | [defaults] | ||
- | inventory | ||
- | </ | ||
- | |||
- | Update the /etc/hosts files with the hostnames. This allows all nodes to resolve each other by name, but without DNS | ||
- | |||
- | <file yaml updatehostsfile.yml> | ||
- | --- | ||
- | - name: Update etc/hosts file | ||
- | hosts: all, localhost | ||
- | gather_facts: | ||
- | tasks: | ||
- | - name: Populate all /etc/hosts files | ||
- | tags: etchostsupdate | ||
- | become: true | ||
- | become_user: | ||
- | lineinfile: | ||
- | path: "/ | ||
- | regexp: '.*{{ item }}$' | ||
- | line: "{{ hostvars[item][' | ||
- | state: present | ||
- | with_items: '{{ groups.all }}' | ||
- | </ | ||
- | |||
- | Run the playbook, providing the ansible user password you set when prompted. | ||
- | < | ||
- | |||
- | Note that you can now run an ansible " | ||
- | |||
- | ====== Install Kubernetes using Ansible ====== | ||
- | // Check [[https:// | ||
- | - Install some prerequisites on ALL the Kubernetes nodes | ||
- | * <file yaml kube-dependencies.yml> | ||
- | --- | ||
- | - hosts: all | ||
- | become: true | ||
- | tasks: | ||
- | - fail: | ||
- | msg: "OS should be Ubuntu 22.04, not {{ ansible_distribution }} {{ ansible_distribution_version }}" | ||
- | when: ansible_distribution != ' | ||
- | |||
- | - name: Update APT packages | ||
- | apt: | ||
- | update_cache: | ||
- | |||
- | - name: Reboot and wait for reboot to complete | ||
- | reboot: | ||
- | |||
- | - name: Disable SWAP (Kubeadm requirement) | ||
- | shell: | | ||
- | swapoff -a | ||
- | |||
- | - name: Disable SWAP in fstab (Kubeadm requirement) | ||
- | replace: | ||
- | path: /etc/fstab | ||
- | regexp: ' | ||
- | replace: '# \1' | ||
- | |||
- | - name: Create an empty file for the Containerd module | ||
- | copy: | ||
- | content: "" | ||
- | dest: / | ||
- | force: false | ||
- | |||
- | - name: Configure modules for Containerd | ||
- | blockinfile: | ||
- | path: / | ||
- | block: | | ||
- | | ||
- | | ||
- | |||
- | - name: Create an empty file for Kubernetes sysctl params | ||
- | copy: | ||
- | content: "" | ||
- | dest: / | ||
- | force: false | ||
- | |||
- | - name: Configure sysctl params for Kubernetes | ||
- | lineinfile: | ||
- | path: / | ||
- | line: "{{ item }}" | ||
- | with_items: | ||
- | - ' | ||
- | - ' | ||
- | - ' | ||
- | |||
- | - name: Apply sysctl params without reboot | ||
- | command: sysctl --system | ||
- | |||
- | - name: Install APT Transport HTTPS | ||
- | apt: | ||
- | name: apt-transport-https | ||
- | state: present | ||
- | |||
- | - name: Add Docker apt-key | ||
- | apt_key: | ||
- | url: https:// | ||
- | state: present | ||
- | |||
- | - name: Add Docker' | ||
- | apt_repository: | ||
- | repo: "deb [arch=amd64] https:// | ||
- | filename: " | ||
- | |||
- | - name: Add Kubernetes apt-key | ||
- | apt_key: | ||
- | url: https:// | ||
- | state: present | ||
- | |||
- | - name: Add Kubernetes' | ||
- | apt_repository: | ||
- | repo: deb https:// | ||
- | state: present | ||
- | filename: ' | ||
- | |||
- | - name: Install Containerd | ||
- | apt: | ||
- | name: containerd.io | ||
- | state: present | ||
- | |||
- | - name: Create Containerd directory | ||
- | file: | ||
- | path: / | ||
- | state: directory | ||
- | |||
- | - name: Add Containerd configuration | ||
- | shell: / | ||
- | |||
- | - name: Configuring the systemd cgroup driver for Containerd | ||
- | lineinfile: | ||
- | path: / | ||
- | regexp: ' | ||
- | line: ' | ||
- | |||
- | - name: Enable the Containerd service and start it | ||
- | systemd: | ||
- | name: containerd | ||
- | state: restarted | ||
- | enabled: true | ||
- | daemon-reload: | ||
- | |||
- | - name: Install Kubelet | ||
- | apt: | ||
- | name: kubelet=1.26.* | ||
- | state: present | ||
- | update_cache: | ||
- | |||
- | - name: Install Kubeadm | ||
- | apt: | ||
- | name: kubeadm=1.26.* | ||
- | state: present | ||
- | |||
- | - name: Enable the Kubelet service, and enable it persistently | ||
- | service: | ||
- | name: kubelet | ||
- | enabled: true | ||
- | |||
- | - name: Load br_netfilter kernel module | ||
- | modprobe: | ||
- | name: br_netfilter | ||
- | state: present | ||
- | |||
- | - name: Set bridge-nf-call-iptables | ||
- | sysctl: | ||
- | name: net.bridge.bridge-nf-call-iptables | ||
- | value: 1 | ||
- | |||
- | - name: Set ip_forward | ||
- | sysctl: | ||
- | name: net.ipv4.ip_forward | ||
- | value: 1 | ||
- | |||
- | - name: Check Kubelet args in Kubelet config | ||
- | shell: grep " | ||
- | register: check_args | ||
- | |||
- | - name: Add runtime args in Kubelet config | ||
- | lineinfile: | ||
- | dest: "/ | ||
- | line: " | ||
- | insertafter: | ||
- | when: check_args.stdout == "" | ||
- | |||
- | - name: Reboot and wait for reboot to complete | ||
- | reboot: | ||
- | |||
- | - hosts: master | ||
- | become: true | ||
- | tasks: | ||
- | - name: Install Kubectl | ||
- | apt: | ||
- | name: kubectl=1.26.* | ||
- | state: present | ||
- | force: true # allow downgrades | ||
- | </ | ||
- | - Run '' | ||
- | - Configure kubernetes cluster on master node | ||
- | * <file yaml master.yml> | ||
- | --- | ||
- | - hosts: master | ||
- | become: true | ||
- | tasks: | ||
- | - name: Create an empty file for Kubeadm configuring | ||
- | copy: | ||
- | content: "" | ||
- | dest: / | ||
- | force: false | ||
- | |||
- | - name: Configuring the container runtime including its cgroup driver | ||
- | blockinfile: | ||
- | path: / | ||
- | block: | | ||
- | kind: ClusterConfiguration | ||
- | | ||
- | | ||
- | | ||
- | --- | ||
- | kind: KubeletConfiguration | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | cpu: 100m | ||
- | | ||
- | | ||
- | cpu: 100m | ||
- | | ||
- | | ||
- | - pods | ||
- | |||
- | - name: Initialize the cluster (this could take some time) | ||
- | shell: kubeadm init --config / | ||
- | args: | ||
- | chdir: / | ||
- | creates: cluster_initialized.log | ||
- | |||
- | - name: Create .kube directory | ||
- | become: true | ||
- | become_user: | ||
- | file: | ||
- | path: $HOME/.kube | ||
- | state: directory | ||
- | mode: 0755 | ||
- | |||
- | - name: Copy admin.conf to user's kube config | ||
- | copy: | ||
- | src: / | ||
- | dest: / | ||
- | remote_src: true | ||
- | owner: ansible | ||
- | |||
- | - name: Install Pod network | ||
- | become: true | ||
- | become_user: | ||
- | shell: kubectl apply -f https:// | ||
- | args: | ||
- | chdir: $HOME | ||
- | creates: pod_network_setup.log | ||
- | </ | ||
- | * run '' | ||
- | - SSH to the master and verify the master nodes gets status '' | ||
- | * '' | ||
- | |||
- | ====== Set up the SQL Node ====== | ||
- | Modify the file to replace MASTERIP with the IP address of your master node in two (2) places | ||
- | * <file yaml sql.yml> | ||
- | --- | ||
- | - hosts: master | ||
- | become: true | ||
- | # gather_facts: | ||
- | tasks: | ||
- | - name: Get join command | ||
- | shell: kubeadm token create --print-join-command | ||
- | register: join_command_raw | ||
- | |||
- | - name: Set join command | ||
- | set_fact: | ||
- | join_command: | ||
- | |||
- | - hosts: sql | ||
- | become: true | ||
- | tasks: | ||
- | - name: TCP port 6443 on master is reachable from worker | ||
- | wait_for: " | ||
- | |||
- | - name: Join cluster | ||
- | shell: "{{ hostvars[' | ||
- | args: | ||
- | chdir: / | ||
- | creates: node_joined.log | ||
- | </ | ||
- | |||
- | Run the playbook: '' | ||
- | |||
- | - SSH to the master and verify all the nodes return status '' | ||
- | * '' | ||
- | |||
- | ====== Set up the Worker Nodes ====== | ||
- | Modify the file to replace MASTERIP with the IP address of your master node in two (2) places | ||
- | * <file yaml workers.yml> | ||
- | --- | ||
- | - hosts: master | ||
- | become: true | ||
- | # gather_facts: | ||
- | tasks: | ||
- | - name: Get join command | ||
- | shell: kubeadm token create --print-join-command | ||
- | register: join_command_raw | ||
- | |||
- | - name: Set join command | ||
- | set_fact: | ||
- | join_command: | ||
- | |||
- | - hosts: workers | ||
- | become: true | ||
- | tasks: | ||
- | - name: TCP port 6443 on master is reachable from worker | ||
- | wait_for: " | ||
- | |||
- | - name: Join cluster | ||
- | shell: "{{ hostvars[' | ||
- | args: | ||
- | chdir: / | ||
- | creates: node_joined.log | ||
- | </ | ||
- | |||
- | Run the playbook: '' | ||
- | |||
- | - SSH to the master and verify all the nodes return status '' | ||
- | * '' | ||
- | |||
- | ====== Install kubectl on the Host ====== | ||
- | Install kubectl on the host (the Ansible controller) to allow for automation with Kubernetes | ||
- | |||
- | create file ''/ | ||
- | |||
- | <file yaml kubectlcontrolnode.yml> | ||
- | --- | ||
- | - hosts: localhost | ||
- | become: true | ||
- | gather_facts: | ||
- | tasks: | ||
- | - name: Update APT packages | ||
- | apt: | ||
- | pkg: | ||
- | - python3-pip | ||
- | update_cache: | ||
- | |||
- | - name: Add Kubernetes apt-key | ||
- | apt_key: | ||
- | url: https:// | ||
- | state: present | ||
- | |||
- | - name: Add Kubernetes' | ||
- | apt_repository: | ||
- | repo: deb https:// | ||
- | state: present | ||
- | filename: ' | ||
- | |||
- | - name: Install Kubectl | ||
- | apt: | ||
- | name: kubectl=1.26.* | ||
- | state: present | ||
- | force: true # allow downgrades | ||
- | - name: install pre-requisites | ||
- | pip: | ||
- | name: | ||
- | - openshift | ||
- | - pyyaml | ||
- | - kubernetes | ||
- | </ | ||
- | |||
- | Run the playbook and enter the password for the user ansible when prompted. | ||
- | < | ||
- | |||
- | Running '' | ||
- | |||
- | Copy credentials | ||
- | * '' | ||
- | |||
- | Confirm it's working now by running | ||
- | * '' | ||
- | * '' | ||
- | |||
- | ====== Apply Labels to the Nodes ====== | ||
- | |||
- | Experimental | ||
- | |||
- | <file yaml labels.yml> | ||
- | --- | ||
- | - hosts: localhost | ||
- | name: label-sql | ||
- | connection: local | ||
- | tasks: | ||
- | - name: Label node for sql | ||
- | k8s: | ||
- | definition: | ||
- | apiversion: v1 | ||
- | kind: Node | ||
- | metadata: | ||
- | name: node1 | ||
- | label: | ||
- | my-role: sql | ||
- | - hosts: localhost | ||
- | name: label-workers | ||
- | connection: local | ||
- | tasks: | ||
- | - name: Label nodes for workers | ||
- | k8s: | ||
- | definition: | ||
- | apiversion: v1 | ||
- | kind: Node | ||
- | metadata: | ||
- | name: "{{ item }}" | ||
- | label: | ||
- | my-role: " | ||
- | loop: | ||
- | - node2 | ||
- | - node3 | ||
- | </ | ||
- | |||
- | ====== Next Step ====== | ||
- | Continue to [[Step 4 - SQL Server]] | ||
- | |||
- | Or back to [[Step 2 - Deploy the VMs]] or [[Start]]. |
lab/kubernetes_app/step_3_-_set_up_kubernetes.1706758398.txt.gz · Last modified: 2024/02/01 03:33 by user