Code Monkey home page Code Monkey logo

apsc's Introduction

active-passive sidecar

A sidecar container for labeling an active-passive kubernetes workload using kubernetes lease mechanism.

Usage

RBAC

We need a service account with rbac rules for the container to be able to set the pod label

apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
  name: apsc-test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: apsc
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: apsc
subjects:
- kind: ServiceAccount
  name: apsc-test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: apsc
rules:
- apiGroups: [ "coordination.k8s.io" ]
  resources: [ "leases" ]
  verbs: [ "*" ]
- apiGroups: [ "" ]
  resources: [ "pods" ]
  verbs: [ "get", "list" ]
- apiGroups: [ "" ]
  resources: [ "pods" ]
  verbs: [ "update" ]

Deployment

The container needs its pod's name and namespace to function, we can parse those using env with fieldRefs. Furthermore, the label key can be set via LABEL_KEY env variable - it defaults to apsc.knoppiks.de/state and will be set to active on the leading pod.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apsc-test
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app.kubernetes.io/name: apsc-test
      app.kubernetes.io/component: server
  template:
    metadata:
      labels:
        app.kubernetes.io/name: apsc-test
        app.kubernetes.io/component: server
    spec:
      automountServiceAccountToken: true
      serviceAccountName: apsc-test
      containers:
      - name: server
        image: httpd:2.4-alpine
        ports:
        - name: http
          containerPort: 80
        readinessProbe:
          httpGet:
            port: http
        livenessProbe:
          httpGet:
            port: http
      - name: apsc
        image: knoppiks/apsc
        imagePullPolicy: Always
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: LABEL_KEY
          value: "example.com/state"

Utilize in service

We can now select the extra label in our service, so that traffic is only sent to the active instance.

---
apiVersion: v1
kind: Service
metadata:
  name: apsc-test
spec:
  ports:
  - name: http
    port: 80
    targetPort: http
  selector:
    app.kubernetes.io/name: apsc-test
    app.kubernetes.io/component: server
    example.com/state: active

apsc's People

Contributors

knoppiks avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.