Clicky

A to Z Kubernetes with CIVO

August 20, 2024

Download your kubeconfig configuration and point to it:

export KUBECONFIG=~/<path_to_your_kubeconfig>

Create a Kubernetes, my-api.yml, deployment file with the following contents:

apiVersion: v1
kind: Service
metadata:
  name: my-api
  labels:
    app: my-api
spec:
  ports:
    - port: 3000
      targetPort: api-port
      protocol: TCP
  selector:
    app: api
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  selector:
    matchLabels:
      app: api
  replicas: 1
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: nmatsui/hello-world-api:latest
          ports:
            - containerPort: 3000
              name: api-port
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-cluster-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-api
            port:
              number: 3000

Apply the above yml:

kubectl apply -f ./my-api.yml

Return to the Civo console, copy your external ip and call your api endoint:

curl http://<your_kubernetes_cluster_external_ip>

You should get a reply as:

{“message”:“hello world!"}

Let’s try something harder

Kubernetes config maps

Create a new Kubernetes, my-custom-env-reader-api.yml, deployment file with the following contents:

apiVersion: v
kind: Service
metadata:
  name: api-service
  labels:
    app: api-service
spec:
  ports:
    - port: 5000
      targetPort: api-port
      protocol: TCP
  selector:
    app: api
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  selector:
    matchLabels:
      app: api
  replicas: 1
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: joelmatajunk/my-custom-env-reader-api
          ports:
            - containerPort: 5000
              name: api-port
          env:
            - name: config-map-key-1
              valueFrom:
                configMapKeyRef:
                  name: api-config-map
                  key: config-map-key-1
            - name: config-map-key-2
              valueFrom:
                configMapKeyRef:
                  name: api-config-map
                  key: config-map-key-2
            - name: config-map-key-3
              valueFrom:
                configMapKeyRef:
                  name: api-config-map
                  key: config-map-key-3
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: api-service
            port:
              number: 5000
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: api-config-map
data:
  config-map-key-1: "config-map-value-1"
  config-map-key-2: "config-map-value-2"
  config-map-key-3: "config-map-value-3"

Apply the above yml:

kubectl apply -f ./my-custom-env-reader-api.yml

Check your config map:

kubectl get configmap

and,

kubectl describe configmap/api-config-map

Call the api to retrieve the environment variables (plus, the ones set using the config-map):

  1. Either set a port forward (& to keep running it in the background)
kubectl port-forward --namespace default svc/api-service 5000 &

then,

curl http://localhost:5000/env
  1. Call it via the ingress rule
curl http://74.220.30.67/env

Kubernetes with API and REDIS

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
  namespace: default
spec:
  serviceName: redis-cluster-service
  replicas: 3
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      labels:
        app: redis-cluster
    spec:
      containers:
      - name: redis
        image: redis
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster
  labels:
    app: redis-cluster
  namespace: default
spec:
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis-cluster

Apply the above yaml:

kubectl apply -f ./<filename.yaml>

Check your pods:

kubectl get pods

Check your redis pods:

kubectl get pods | grep redis

Get inside one of the redis pods:

kubectl exec --stdin --tty redis-cluster-0 -- /bin/bash

Add some values:

redis-cli
set mykey1 myvalue1
set mykey2 myvalue2
set mykey3 myvalue3

Get some value:

set mykey1 myvalue1