UncleNUC Wiki

Second chance for NUCs

User Tools

Site Tools


lab:demonstrate_app_on_k8s

This is an old revision of the document!


Demonstrate App on k8s

From NUC 1, log in to the Ansible control node, NUC 2.

In our previous step we installed Kubernetes on our Stack of NUCs.

Now we are going to install a web app and expose it to our internal network.

Purpose:

  • Demonstrate a running a web application on Kubernetes

References

Steps:

  1. Create distribution
    • speedtester-deployment.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: speedtester
        labels:
          run: speedtester
      spec:
        selector:
          matchLabels:
            run: speedtester
        replicas: 2
        template:
          metadata:
            labels:
              run: speedtester
          spec:
            containers:
            - name: speedtester
              image: docker.io/doritoes/speedtester:latest
    • kubectl apply -f speedtester-deployment.yaml
  2. View distribution information
    • kubectl get pods
    • kubectl get deployment speedtester -o yaml
  3. Increase number of replicas
    • Exit the file speedtester-deployment.yaml to set the number of replicas to the number of Kubernetes worker nodes you have
    • kubectl apply-f speedtester-deployment.yaml
    • kubectl get pods -w
    • kubectl get deployments
    • kubectl describe deployments speedtester
    • kubectl get deployment speedtester -o yaml
  4. Create a service
    1. Using expose
      • kubectl expose deployment speedtester–port=8080 –name=speedtester-service –target-port=80
    2. Using a manifest
      • speedtester-service.yaml
        apiVersion: v1
        kind: Service
        metadata:
          name: speedtester-service
        spec:
          selector:
            run: speedtester
          type: NodePort
          ports:
            - protocol: TCP
              port: 8080
              targetPort: 80
              notePort: 30080
      • kubectl apply -f speedtester-service.yaml
  5. kubectl get all
  6. kubectl describe svc speedtester-service
  7. Test access
  8. Alternative test from NUC 1

Optional

Perform some examples off automation using Ansible with Kubernetes

  • restart pods?

example from documentation is creating namespaces

  • ansible web -m k8s -a “name=namespace2 state=present kind=Namespace api_version=v1”

Chaos Testing

Proper chaos testing: https://opensource.com/article/21/6/chaos-kubernetes-kube-monkey

Some small scale testing below.

Kill Pods

Literally kill the first pod in the list

kubectl delete pod $(kubectl get pods -l run=speedtester -o jsonpath='{.items[0].metadata.name}')

Forever loop to keep killing the first pod on the list

while true; do
kubectl delete pod $(kubectl get pods -l run=speedtester -o jsonpath='{.items[0].metadata.name}')
done

Kill all the current pods

This is slow because the loop structure is sequential.

ansible-playbook deletepod.yml

deletepod.yml
---
- name: Delete current pods matching speedtester
  hosts: localhost
  vars:
    pod_filter:
      - speedtester # beginning of the pod name
  gather_facts: false
  tasks:
    - name: Get a list of all pods from the namespace
      command: kubectl get pods --no-headers -o custom-columns=":metadata.name"
      register: pod_list
    - name: Print pod names
      debug:
        msg: "{{ item }}"
      loop: "{{ pod_list.stdout_lines }}"
      when: item is match(pod_filter|join('|'))
    - name: Delete matching pods
      command: kubectl delete pod "{{ item }}"
      loop: "{{ pod_list.stdout_lines }}"
      when: item is match(pod_filter|join('|'))

Using async we can limit the waiting to see if the command returns.

ansible-playbook deletepod-async.yml

deletepod-async.yml
---
- name: Delete current pods matching speedtester
  hosts: localhost
  vars:
    pod_filter:
      - speedtester # beginning of the pod name
  gather_facts: false
  tasks:
    - name: Get a list of all pods from the namespace
      command: kubectl get pods --no-headers -o custom-columns=":metadata.name"
      register: pod_list
    - name: Print pod names
      debug:
        msg: "{{ item }}"
      loop: "{{ pod_list.stdout_lines }}"
      when: item is match(pod_filter|join('|'))
    - name: Delete matching pods
      command: kubectl delete pod "{{ item }}"
      async: 5 # timeout in seconds
      poll: 1 # poll every second
      loop: "{{ pod_list.stdout_lines }}"
      when: item is match(pod_filter|join('|'))

Reboot a Random Ansible Node

randomreboot.yml
---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: limited_selection
      loop: "{{ (groups['all'] | shuffle)[0:1] }}"

- hosts: limited_selection
  gather_facts: no
  tasks:
    - reboot:
lab/demonstrate_app_on_k8s.1682266799.txt.gz · Last modified: 2023/04/23 16:19 by user