Kubernetes Getting Started
Minikube cheatsheet
minikube start
to start minikube clusterminikube start --kubernetes-version v1.18.0
to start minikube with a specific kubernetes versionminikube dashboard
to load the dashboardminikube status
to get the cluster statusminikube addons list
to list all the addonsminikube addons (enable|disable) <addone-name>
to enable/disable the addonminikube stop
to stop the clusterminikube delete
to delete the cluster-
minikube start --driver=<driver_name>
with a specific VM driverCaution: If you use the none driver, some Kubernetes components run as privileged containers that have side effects outside of the Minikube environment. Those side effects mean that the none driver is not recommended for personal workstations.
Kubectl cheatsheet
kubectl version
get the kubectl cli version-
kubectl get (pods|services|svc|deployments|nodes|events|replicasets)
to get all the podsservices deployments nodes running kubectl get pod,svc -n kube-system
to view the pod and services created**kubectl config view
to view kubectl configurationkubectl create deployment <deployment-name> --image=<docker-image-path>:<tag>
to create a new deployment on a new nodekubectl expose deployment <deployment-name> --type=LoadBalancer --port=8000
to expose the pod to public internet.--type=LoadBalancer
flag indicates that you want to expose your service outside of the cluster.kubectl proxy
to forward communications into the cluster-wise, private network. The proxy can be terminated by ctrl+C and wont show any output while it is running.kubectl scale deployments/<deployment-name> --replicas=<replica-count>
to scale the replicakubectl set image deployments/<deployment-name> <deployment-name>=<NewOrOld-deployment-image>:<tag-name>
to apply the rolling updatekubectl rollout status deployments/<deployment-name>
to check the rollout statuskubectl rollout undo deployments/<deployment-name>
to rollout a failed deployment (invalid image deployment)
kubectl delete service <service-name>
to delete the servicekubectl delete deployment <service-name>
to delete the deploymentkubectl apply -f deployment.yaml
to create a deployment using a deployment.yaml file Troubleshootkubectl describe (pods|services|replicasets)[\name]
- show detailed information about a resourcekubectl logs <pod_name>
- print the logs from a container in podkubectl exec <pod_name> <command>
- execute a command on a container in a podkubectl exec <pod_name> env
list env variableskubectl exec -ti <pod_name> bash
start a bash session in the pod’s container. here you can execute any shell commands i.e. if your app is node app you can print the file contents, or run the curl command to see the output of your web app, etc.
Sample Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/name: load-balancer-example
name: hello-world
spec:
replicas: 5
selector:
matchLabels:
app.kubernetes.io/name: load-balancer-example
template:
metadata:
labels:
app.kubernetes.io/name: load-balancer-example
spec:
containers:
- image: gcr.io/google-samples/node-hello:1.0
name: hello-world
ports:
- containerPort: 8080
Terminology
- Master: The Master is responsible for managing the cluster. A Kubernetes cluster can be deployed on either physical or virtual machines.
- Node: A node is a VM or a physical computer that serves as a worker machine in a Kubernetes cluster. The nodes communicate with the master using the Kubernetes API, which the master exposes
- Node processes: runs inside a node
- Pod: A Pod is a basic execution unit of a Kubernetes application. Each Pod represent a part of a workload that is running on your cluster. They are visible and can be accessed from within the cluster by other pods and services but not outside the network but we can expose it to the internet. Each pod has a unique IP address even for Pods on the same Node. Learn more
- Service: A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them. Services enable a loose coupling between dependent Pods. A Service is defined using YAML (preferred) or JSON, like all Kubernetes objects. The set of Pods targeted by a Service is usually determined by a LabelSelector.
Types
- ClusterIP (default): Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.
- NodePort: Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using
: . Superset of ClusterIP. - LoadBalancer: Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.
- ExternalName: Exposes the Service using an arbitrary name (specified by
externalName
in the spec) by returning a CNAME record with the name. No proxy is used. This type requires v1.7 or higher ofkube-dns
.
- Label: Labels are key/value pairs attached to objects and can be used in any number of ways:
- Designate objects for development, test and production
- Embed version tags
- Classify an object using tags
Getting started on your local dev box
Pre-requisites
- Docker
- Hyper-V (hyper-v enabled on your device)
—
Kubernetes Cluster
Node Overview
Pods Overview
Services Labels
Scaling & Overview 1 Scaling & Overview 2
Installation
- Install Docker
-
Read the Kubectl reference for more details Verify installation by running
kubectl version --client
.Note: Docker Desktop for Windows adds its own version of
kubectl
to PATH. If you have installed Docker Desktop before, you may need to place your PATH entry before the one added by the Docker Desktop installer or remove the Docker Desktop’skubectl
.Verify kubectl configuration
Verify if the kubeconfig file is present. Else it will not work. Check that kubectl is properly configured by getting the cluster state
kubectl cluster-info
If kubectl cluster-info returns the url response but you can’t access your cluster, to check whether it is configured properly, use:
kubectl cluster-info dump
- Install Minikube. You can also install Kind to work on clusters with Kubernetes
Pre-requisite check if virtualization is supported
- Install minikube either by downloading binary/executable or using choco if you are on windows
- Confirm installation by checkking
minikube version
- Start minikube using
minikube start
. If you have a specific driver to specify, run `minikube start –driver= - Check minikube status after starting, which will show below output if everything is fine
host: Running kubelet: Running apiserver: Running kubeconfig: Configured
### Clean up local state If you have previously installed Minikube, and try to start it which returns an error saying
minikube does not exist
, then you need to delete minikube’s local state:minikube delete
Minikube with Kubernetes
- Start minikube cluster
minikube start
. Click here to start a cluster on a specific Kubernetes version, VM or container runtime. - Interact with your cluster with kubectl:
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
- Access hello-minikube deployment, expose it as a service:
kubectl expose deployment hello-minikube --type=NodePort --port=8080
- Check if the pod is running:
kubectl get pod
- Get the URL of the exposed service:
minikube service hello-minikube --url
- Copy over the URL in a browser to get the result of the service
- Delete the
hello-minikube
service:kubectl delete services hello-minikube
- Delete the
hello-minikube
deployment:kubectl delete deployment hello-minikube
- Stop the local minikube cluster:
minikube stop
- Delete the local minikube cluster:
minikube delete
Drivers available as of Aug 2020.
- docker (docker installation)
- virtualbox (driver insallation)
- podman (driver installation ***EXPERIMENTAL)
- vmwarefusion
- kvm2 (driver installation)
- hyperkit (driver installation)
- hyperv (driver installation) Note that the IP below is dynamic and can change. It can be retrieved with
minikube ip
- vmware (driver installation) (VMware unified driver)
- parallels (driver installation)
- none (Runs the Kubernetes components on the host and not in a virtual machine. You need to be running Linux and to have Docker installed.)