~/tutorials/how-configure-grafana-dashboard-oauth-okta-with-kubernetes.md

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

2 min readApril 10, 2020by Gorav Singal#grafana#docker#nginx
// TL;DR

Deploy the Grafana Docker image on Kubernetes with Okta OAuth authentication by configuring environment variables in the Dockerfile and setting up Ingress rules for SSL.

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

Introduction

In our previous post How to configure Grafana on docker, we saw how we can run grafana docker container with SSL and oauth okta.

In this post, we will see how we can run this docker image on kubernetes cluster.

Note: I’m not going to detail out Kubernetes. I will just focus on Dockerfile and the environment variables for that.

Some Pre-requisites

I’m assumming you have configured Ingress rule, and exposed Kubernetes service for this grafana dashboard. And, the ingress rule should have the mapping from your cluster IP to app name: trainings

We are going to configure name of our app to trainings

kubernetes yaml file for service

Configuring service

apiVersion: v1
kind: Service
metadata:
  name: trainings
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: trainings

kubernetes file for ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: trainings
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: "<your host name>"
    http:
      paths:
      - backend:
          serviceName: trainings
          servicePort: 80
  tls:
    - hosts:
        - <your hostname>
      secretName: trainings-secret

Note: you also have to configure SSL certificate to your cluster.

Actual Dockerfile

FROM grafana/grafana:6.3.6

ENV GF_SERVER_HTTP_PORT=443

ENV GF_AUTH_ANONYMOUS_ENABLED=false
ENV GF_AUTH_GENERIC_OAUTH_NAME=Okta
ENV GF_AUTH_GENERIC_OAUTH_ENABLED=true
ENV GF_AUTH_GENERIC_OAUTH_SCOPES="openid profile email"
ENV GF_AUTH_GENERIC_OAUTH_AUTH_URL=https://<XYZ>.okta.com/oauth2/v1/authorize
ENV GF_AUTH_GENERIC_OAUTH_TOKEN_URL=https://<XYZ>.okta.com/oauth2/v1/token
ENV GF_AUTH_GENERIC_OAUTH_API_URL=https://<XYZ>.okta.com/oauth2/v1/userinfo
ENV GF_USERS_ALLOW_SIGN_UP=false
ENV GF_AUTH_DISABLE_LOGIN_FORM=true
ENV GF_AUTH_OAUTH_AUTO_LOGIN=true
ENV GF_SECURITY_ADMIN_USER=<your email>
ENV GF_SECURITY_COOKIE_SAMESITE=lax
ENV GF_SECURITY_COOKIE_SECURE=true

USER root

RUN mkdir -p /var/lib/grafana/dashboards
ADD grafana_dashboards/belts-dashboard.json /var/lib/grafana/dashboards/belts-dashboard.json
ADD grafana_dashboards/dashboards.yaml /etc/grafana/provisioning/dashboards/dashboards.yaml
ADD grafana_dashboards/elastic_datasource.yaml /etc/grafana/provisioning/datasources/elastic_datasource.yaml

EXPOSE 8080

Kubernetes configuration yaml

Lets take a look at the Kubernetes configmap yaml file:

apiVersion: v1
kind: ConfigMap
metadata:
  name: trainings
data:
  GF_SERVER_PROTOCOL: "http"
  GF_SERVER_ROOT_URL: "https://<your host name>"
  GF_AUTH_GENERIC_OAUTH_CLIENT_ID: "<client id>"
  GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET: "<secret>"
  GF_SERVER_HTTP_PORT: "8080"

Kubernetes deployment file

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: trainings
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: trainings
    spec:
      containers:
      - name: trainings
        image: <your artificatory path to grafana image>:<version>
        resources:
          limits:
            cpu: 1
            memory: 1024Mi
          requests:
            cpu: 1
            memory: 1024Mi
        envFrom:
          - configMapRef:
              name: trainings
      imagePullSecrets:
      - name: <your secret name>

Applying configurations

Apply config file

kubectl apply -f config/config.yml

Apply deployment file

kubectl apply -f deployments/deployment.yml

Hit your hostname, and it should redirect you to okta and then to your grafana dashboard.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

Introduction In most of cases, you are not pulling images from docker hub public…

Configure Grafana with OAuth Okta, SSL via Nginx, and JSON Dashboards on Docker

Configure Grafana with OAuth Okta, SSL via Nginx, and JSON Dashboards on Docker

Introduction In this post, we will see: use Grafana Community Edition (Free…

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Introduction You have a running kubernetes setup, and have a webservice (exposed…

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

Introduction I have my main website, which I run on Lets say: . Now, there is my…

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

Introduction In some of the cases, we need to specify namespace name along with…

How to Copy Local Docker Image to Another Host Without Repository and Load

How to Copy Local Docker Image to Another Host Without Repository and Load

Introduction Consider a scenario where you are building a docker image on your…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…