Kubernetes (Google Cloud) - adonisv79/bytecommander.com GitHub Wiki

Installation (minikube)

Go to the installation folder and start minikube using the following command in the command prompt (as administrator)

// "minikube start --driver=<driver_name>" for valid driver name, see [drivers](https://kubernetes.io/docs/setup/learning-environment/minikube/#specifying-the-vm-driver)
minikube start --driver=hyperv

check if it is running

minikube status

Stop minikube

minikube stop

launch the Kubernetes dashboard

minikube dashboard

REF: Official Installation guide

Commands

Get version (will show the client version and server "or host" version)

kubectl version

Get cluster information

kubectl cluster-info

Get all the nodes in the cluster

kubectl get nodes

Make deployment

kubectl create deployment {deployment-name} --image={image-url}:{image-tag}

Get deployments

kubectl get deployments

Get all the pods (add "-o wide" to also show IP, Node used, etc)

kubectl get pods --all-namespaces

Get events (kubernetes logs)

kubectl get events

Expose a deployment's port

kubectl expose deployment {deployment-name} --type=LoadBalancer --port={port-number}

Drain a node

kubectl drain {node-name}

Delete the node from the cluster

kubectl delete nodes {node-name}

Persistent Volume and Claims

Get persistent volumes

kubectl get pv

Get persistent volume claims

kubectl get pvc

Templates (YAML files)

you can save these to a local yaml file and run with

// create a new managed configuration (make sure everything is ran with this first)
kubectl create -f {filename}.yaml
// when you updated your file, you can use the following to update the changes
// Note; this will error out when the config was not started with 'create'
kubectl apply -f {filename}.yaml

Storage Class

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: free-tier-storage
  labels:
    app: s-auth
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: Immediate

Persistent volume (make sure path has chmod to allow writes for the container)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: free-persist-volume
  labels:
    app: s-auth
spec:
  storageClassName: free-tier-storage
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Persistent Volume Claims

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: free-persist-volume-claim
  labels:
    app: s-auth
spec:
  storageClassName: free-tier-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: s-auth-deployment
  labels:
    app: s-auth
spec:
  replicas: 2
  selector:
    matchLabels:
      app: s-auth
  template:
    metadata:
      name: s-auth-pod
      labels:
        app: s-auth
    spec:
      containers:
        - name: redis-sess-container
          image: redis:5.0.8
          ports:
            - containerPort: 6379
        - name: mongo-users-container
          image: mongo:4.2.5
          ports:
            - containerPort: 27017
      volumes:
        - name: s-auth-volume
          persistentVolumeClaim:
            claimName: free-persist-volume-claim

Service (Exposed ports, etc)

apiVersion: "v1"
kind: "Service"
metadata:
  name: "s-auth-deployment-service"
  namespace: "default"
  labels:
    app: "s-auth"
spec:
  selector:
    app: "s-auth"
  ports:
  - name: "6379-to-6379-tcp"
    protocol: "TCP"
    port: 6379
    targetPort: 6379
  - name: "27017-to-27017-tcp"
    protocol: "TCP"
    port: 27017
    targetPort: 27017
  type: "LoadBalancer"
  loadBalancerIP: "35.227.147.98"

Secrets

Note values should use base64 strings so convert them first. ex: here shows 'cGFzc3dvcmQ=' which is just the base64 of the string 'password'

apiVersion: v1
kind: Secret
metadata:
  name: s-auth-secret
type: Opaque
data:
  REDIS_PASS: cGFzc3dvcmQ=
  MONGO_PASS: cGFzc3dvcmQ=