- Windows:
winget install kubectl
- Mac:
brew install kubectl
~/.kube/config
kubectl get namespaces
kubectl create namespace <your name>
Alternatively, use "ns" instead of "namespace" -> kubectl create ns <your name>
. Other resources can be shortened as well (e.g. "svc" instead of "service")
kubectl config set-context --current --namespace=<your name>
Alternatively, use the -n <your name>
flag with every call.
https://kubernetes.io/docs/reference/kubectl/quick-reference/
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
K9s is a terminal based UI to interact with your Kubernetes clusters: https://k9scli.io/
- Windows:
winget install k9s
- Mac:
brew install derailed/k9s/k9s
kubectl -n kubernetes-dashboard port-forward svc/kubernetes-dashboard 8443:443
Visit https://127.0.0.1:8443/ and login with your config file. Your browser will complain that the certificate is unknown: Press "Advanced" and "Proceed to 127.0.0.1 (unsafe)".
Hint: Use the "get" command and the output flag as described here: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get
solution
kubectl get ns <your short name> -o yaml
2. Run and interactively connect to a busybox container, view its environment variables, then exit the container
Hint: Use the "run" command as described here: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run
solution
kubectl run busybox --image=busybox -it
env
exit
Hint: Usually we'd need to select our namespace with the -n <your short name>
flag. Since we already configured our namespace in the ~/.kube/config
file, the flag is optional for these tasks.
solution
kubectl get pods
solution
kubectl get pods --all-namespaces
solution
kubectl delete pod busybox
Store the yaml as a file nginx.yaml
in your current directory.
Hint: Use the dry-run flag and the output flag
solution
kubectl run nginx --image=nginx --port=80 --dry-run=client -o yaml > nginx.yaml
7. Rewrite the yaml file from a pod configuration to a deployment named "nginx-deployment" with 2 replicas
Hint: Use the pod as the template for the deployment. See how a deployment is configured here: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
Hint 2: You can also use kubectl to create a deployment in dry-run mode and with output as yaml to see how a deployment looks like.
solution
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
dnsPolicy: ClusterFirst
restartPolicy: Always
solution
kubectl create -f nginx.yaml
View the results by getting the deployment.
solution
kubectl scale --replicas=3 deployment/nginx-deployment
kubectl get deployment
solution
kubectl expose deployment nginx-deployment --port=8080 --target-port=80
You should see your new service, the deployment and its pods.
solution
kubectl get all
Forward form the service port 8080 to your own port 8080: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#port-forward
solution
kubectl port-forward service/nginx-deployment 8080:8080
# Open http://localhost:8080/ in a browser
You can use kubectl explain
to find out more about a Kubernetes resource and its fields:
solution
kubectl explain services
solution
kubectl explain pods
solution
kubectl explain pods.spec.containers