Cheatsheets

kubectl Cheat Sheet (Official)

Kubernetes Examples

15 Kubectl Commands & Objects (spacelift)

Setup

Install Minikube

macOS

brew cask install minikube

Linux

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

Create cluster

http_proxy=http://host:port
https_proxy=https://host:port
driver=vmwarefusion          # or virtualbox

minikube start --vm-driver $driver --docker-env HTTP_PROXY=$http_proxy --docker-env HTTPS_PROXY=$https_proxy

Open dashboard

minikube dashboard

kubectl

Set/switch context (Docker for desktop)

kubectl config use-context docker-for-desktop

Deploy Kubernetes Dashboard

docs here

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

create a secure channel to communicate with dashboard

kubectl proxy

Create sample user and log in

# kubectl apply -f dashboard-adminuser.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
# kubectl apply -f dashboard-adminrole.yml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

Access dashboard

Now open http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/ in browser.

查询所有 namespace

kubectl get namespace

Run nginx

Method 1

# kubectl run nginx --image=nginx 
# kubectl expose pod nginx-7bb7cd8db5-88jqg --port=80 --type=LoadBalancer
service/nginx-7bb7cd8db5-88jqg exposed

kubectl get services

Now go to http://127.0.0.1:80 in browser

Method 2

kubectl create deployment --image nginx my-nginx
kubectl scale deployment --replicas 2 my-nginx
kubectl expose deployment my-nginx --port=80 --type=LoadBalancer

kubectl get services