Kubernetes
Local
https://kubernetes.io/releases/download/
https://minikube.sigs.k8s.io/docs/
Install
install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl.sha256"
echo "$(<kubectl.sha256) kubectl" | shasum -a 256 --check
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
sudo chown root: /usr/local/bin/kubectl
kubectl version --client
kubectl
install minikube: https://minikube.sigs.k8s.io/docs/start/#what-youll-need
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
minikube start # Downloads files on the first time
use minikube docker-daemon
minikube docker-env
eval $(minikube -p minikube docker-env)
docker image ls
GCP
https://cloud.google.com/sdk/docs/downloads-versioned-archives
$ gcloud components install kubectl
$ gcloud components update
On GCP create a project here, ensure billing is on and ensure Kubernetes Engine API is switched on.
$ gcloud auth login
$ gcloud config set project PROJECT_ID
Create a k8s cluster:
https://cloud.google.com/sdk/gcloud/reference/container/clusters/create
https://cloud.google.com/compute/docs/machine-types
https://cloud.google.com/compute/docs/regions-zones/
$ gcloud container clusters create --machine-type n1-standard-2 --num-nodes 2 --zone us-central1-b --cluster-version latest k8scluster
Test it is running:
$ kubectl get node # show how many nodes there are
$ kubectl get pods -o wide
$ kubectl config view
$ kubectl get pods -o wide --all-namespaces
$ kubectl describe pod NAME
$ kubectl get pods NAME -o jsonpath='{.spec.containers[*].name}'
$ kubectl get services
$ kubectl logs workerpod
Authenticate the project (https://cloud.google.com/docs/authentication/getting-started)
Put this in your .bash_profile
$ export GOOGLE_APPLICATION_CREDENTIALS="/Users/Ray/Downloads/daskk8s-f92d8191517a.json"
Install helm
...
Install dask kubernetes
$ pip install dask-kubernetes
'Log in the cluster'
$ pip install kubernetes
Deploy the kubernetes configuration onto the cluster:
$ git clone https://github.com/dask/dask-kubernetes.git
$ cd dask-kubernetes/dask_kubernetes
$ kubectl apply -f kubernetes.yaml
Delete the cluster
https://cloud.google.com/kubernetes-engine/docs/how-to/deleting-a-cluster
$ gcloud container clusters delete k8scluster --zone us-central1-b
Azure
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest
az aks install-cli (https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough#connect-to-the-cluster)
kubectl version
kubectl config view
kubectl config current-context
kubectl get namespaces
Create a namespace called development
namespace.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
name: development
kubectl create -f namespace.yaml
Set up the config to find the namespace
kubectl config set-context dev --namespace=development \
--cluster=kubernetes \ # taken from kubectl config current-context
--user=kubernetes-admin
Switch into that namespace
kubectl config use-context dev
kubectl config current-context
Create a pod in this name
pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: dev-app
spec:
containers:
- name: ubuntu
image: ubuntu:trusty
kubectl apply -f pod.yaml
kubectl get pods
kubectl delete pods NAME
kubectl delete --all pods
List container images in pods
kubectl get pods -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' | sort
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' | sort
Create a deployment in this namespace
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: dev-app
name: dev-app
spec:
replicas: 1
selector:
matchLabels:
app: dev-app
template:
metadata:
labels:
app: dev-app
spec:
containers:
- image: nginx:latest
name: nginx
kubectl apply -f deployment.yml
kubectl get deployment
kubectl describe deployments
Expose this app as a service with 30081 accessible
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: service-dev-app
spec:
selector:
app: dev-app
ports:
- name: http
port: 80
nodePort: 30081
type: NodePort
kubectl get svc
kubectl describe svc service-dev-app
Build a persistant volume
persistentvolumne.yaml:
apiVersion: v1
kind: PersistentVolume
metadata:
name: test-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/mnt/data"
reclaimPolicy: Delete
kubectl apply -f persistentvolumne.yaml
Replica sets
See how replica sets are in the cluster
kubectl get rs
kubectl get rs -o wide
kubectl describe rs/NAME
kubectl get rs new-replica-set -o yaml > new-replica-set.yaml
kubectl edit replicaset
kubectl scale --replicas=3 rc/NAME
replicaset.yaml:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3