Skip to content

Software Engineering / Tools / Kubernetes


Installation

Installation on Ubuntu

# add the gpg key
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add;

# add the ppa
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main";

# install kubectl
sudo apt-get install kubectl;

# install kubeadm
sudo apt-get install kubeadm;

If the above instructions fail, you can find the instructions for Ubuntu 18.04 here: https://vitux.com/install-and-deploy-kubernetes-on-ubuntu/


Local Development

The following documents options available to run Kubernetes on a local machine. Primarily, they are:

Kubernetes-in-Docker (KIND)

Simple Setup

kind create cluster;

Named Cluster

kind create cluster --name mycluster;

Multi-Node Cluster Configuration

Place the following in a kind-config.yaml file:

apiVersion: kind.sigs.k8s.io/v1alpha3
kind: Cluster
nodes:
- role: control-plane
- role: worker
- role: worker

Run the following from the same directory as the kind-config.yaml file:

kind create cluster --config ./kind-config.yaml;

Loading Local Image Into Cluster

kind load docker-image ___;

Microk8s

Minikube


Resource Types Overview

ConfigMap

  • Mountable as a volume in a Pod

ConfigMap template for environment variables injection

apiVersion: v1
kind: ConfigMap
metadata:
  name: template
data:
  CONFIG_VALUE_ONE: "1"
  CONFIG_VALUE_TWO: "two"
  CONFIG_VALUE_BOOL: "true"

ConfigMap template for volume mounting

apiVersion: v1
kind: ConfigMap
metadata:
  name: template
data:
  some.json: |
    {
      "hello": "world",
      "i": 1,
      "to": "k8s"
    }
  some.yaml: |
    hello: world
    i: 1
    to: k8s

Containers

  • Containers are the most atomic compute instance
  • Containers are basically sandboxed applications running in a Pod

Deployments

Template for a Deployment resource type

apiVersion: apps/v1
kind: Deployment
metadata:
  name: template
  labels:
    app: template
    type: deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      name: echoserver
      type: pod
  template:
    metadata:
      name: echoserver
      labels:
        name: echoserver
        type: pod
    spec:
      containers:
      - name: echoserver
        image: zephinzer/demo-echoserver:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: pod-http
          containerPort: 11111
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 11111
            httpHeaders:
            - name: X-Healthcheck
              value: LivenessProbe
          initialDelaySeconds: 15
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /readyz
            port: 11111
            httpHeaders:
            - name: X-Healthcheck
              value: ReadinessProbe
          initialDelaySeconds: 15
          periodSeconds: 5
        resources:
          limits:
            cpu: 60m
            memory: 20Mi
          requests:
            cpu: 50m
            memory: 10Mi

Ingress

Template for an Ingress resource type

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: template
spec:
  rules:
  - host: testing.local
    http:
      paths:
      - path: /
        backend:
          serviceName: template
          servicePort: 11111

Persistent Volume

Persistent Volume Claim

Pod

  • A Pod contains one or more Containers
  • All Containers in a Pod can address each other via localhost
  • Every Pod has a unique IP address
  • Pods basically represent a virtual machine (VM)

Template for a static pod

apiVersion: v1
kind: Pod
metadata:
  name: echoserver
  labels:
    name: echoserver
    type: template
spec:
  containers:
  - name: echoserver
    image: zephinzer/demo-echoserver:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: main-http
      containerPort: 11111
      protocol: TCP
    resources:
      limit:
        cpu: 60m
        memory: 20Mi
      request:
        cpu: 50m
        memory: 10Mi

Replica Sets

Secret

  • Mountable as a volume in a Pod
  • Base64 encoded, not encrypted

Template for an opaque secret

apiVersion: v1
kind: Secret
metadata:
  name: template
type: Opaque
data:
  SECRET_VALUE_ONE: MQ==
  SECRET_VALUE_TWO: dHdv
  SECRET_VALUE_BOOL: dHJ1ZQ==

Services

  • 3 types of services: LoadBalancer, NodePort, ClusterIP

Template for a service

apiVersion: v1
kind: Service
metadata:
  name: template
  labels:
    app: template
    type: service
spec:
  type: NodePort
  selector:
    name: echoserver
    type: pod
  ports:
    - port: 11111
      protocol: TCP
      name: service-http
      targetPort: pod-http

Debugging

invalid object doesn't have additional properties

error: SchemaError(io.k8s.apimachinery.pkg.apis.meta.v1.APIGroup_v2): invalid object doesn't have additional properties

  • Check your kubectl version and confirm that the server/client version is compatible

Useful Tools

CLI

VSCode

Name Description Link
Docker Adds syntax highlighting, commands, hover tips, and linting for Dockerfile and docker-compose files. https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker
Kubernetes Develop, deploy and debug Kubernetes applications https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-kubernetes-tools

Community Events

Global

Singapore