User:BryanDavis/Kubernetes

From Wikitech
Jump to navigation Jump to search

Fun things I have learned how to do on the Toolforge Kubernetes cluster.

Redirect requests for tools.wmflabs.org/my-tool to my-tool.toolforge.org

Host based tool routing is live for Toolforge tools! What is not live yet is a a simple way to tell webservice that you want all traffic to your tool to be redirected to its new hostname as the canonical URL even when folks find old links. If you are using the 2020 Toolforge Kubernetes cluster, you can setup this redirection manually by changing the annotations section of the webservice generated my-tool-legacy Ingress object.

$ become my-tool
$ kubectl edit ingress my-tool-legacy
: editor opens with a lot of yaml in it. See below for the before/after versions

Annotations generated by webservice:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^(/my-tool)$ $1/ redirect;
    nginx.ingress.kubernetes.io/rewrite-target: /my-tool/$2

Annotations to redirect to my-tool.toolforge.org:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: https://my-tool.toolforge.org/$2

Make a tool redirect to another tool WITHOUT running a webservice

This trick only works on the "new" (2020) Toolforge Kubernetes cluster. It relies on configuring the nginx-ingress system to handle the redirection.

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{TOOL_NAME}}-redirect
  namespace: tool-{{TOOL_NAME}}
  labels:
    name: {{TOOL_NAME}}-redirect
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/permanent-redirect: https://tools.wmflabs.org/{{TARGET_TOOL_NAME}}/$2
spec:
  rules:
    - host: tools.wmflabs.org
      http:
        paths:
          - backend:
              serviceName: unused
              servicePort: 8000
            path: /{{TOOL_NAME}}(/|$)(.*)
$ kubectl create --validate=true -f ingress.yaml
ingress.extensions/grid-jobs-redirect created

Run multiple pods for your webservice

Have a webservice that is under a lot of load? Scale it up!

$ kubectl scale --replicas=2 $(kubectl get deployment -o name)

Run a cronjob

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: archivebot
  labels:
    name: archivebot
    toolforge: tool
spec:
  schedule: "34 2 2 * *"  # 02:34 on the 2nd of each month
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            toolforge: tool
        spec:
          containers:
          - name: archivebot
            workingDir: /data/project/bd808-pywikibot
            image: docker-registry.tools.wmflabs.org/toolforge-python37-sssd-base:latest
            args:
            - /usr/bin/python3
            - /data/project/shared/pywikipedia/core/scripts/archivebot.py
            - User:BryanDavis/archivebot
            env:
            - name: PYWIKIBOT_DIR
              value: /data/project/bd808-pywikibot
            - name: HOME
              value: /data/project/bd808-pywikibot
          restartPolicy: Never
$ /usr/bin/kubectl apply --validate=true -f cronjobs.yaml
cronjob.batch/archivebot configured


Attach to a running pod

webservice --backend=kubernetes [runtime] shell will give you a shell inside a fresh Kubernetes pod, but sometimes you really want to poke around inside the pod that is running your webservice or bot.

$ kubectl exec -it $(kubectl get pods --output=jsonpath={.items..metadata.name}) -- /bin/bash