Skip to content

Latest commit

 

History

History
210 lines (158 loc) · 5.75 KB

File metadata and controls

210 lines (158 loc) · 5.75 KB

Kubernetes Hands-on Lab 1: Setup and the Basics

Part 1: Setup the Lab Environment

1. Download kubectl

  • Windows: winget install kubectl
  • Mac: brew install kubectl

2. Download the config file into the .kube folder in your home directory

  • ~/.kube/config

3. Run a command to test if it works:

  • kubectl get namespaces

4. Create your own namespace:

  • 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")

5. Use your namespace in all kubectl calls:

  • kubectl config set-context --current --namespace=<your name>

Alternatively, use the -n <your name> flag with every call.

6. [Optional] Configure: Alias, autocomplete, etc.

https://kubernetes.io/docs/reference/kubectl/quick-reference/

7. [Optional] Get familiar with kubectl doc and try out commands

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

8. [Optional] Install k9s

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

9. [Optional] View the Kubernetes-Dashboard

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)".

Part 2: The Basics

1. Get the yaml definition of your namespace via kubectl

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

3. Show all the pods in your namespace

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

4. Show all the pods in all namespaces

solution

kubectl get pods --all-namespaces

5. Delete your busybox pod from before

solution

kubectl delete pod busybox

6. Generate a basic yaml config for an nginx pod that exposes port 80

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

8. Deploy the yaml file

solution

kubectl create -f nginx.yaml

9. Scale your nginx deployment to 3 replicas

View the results by getting the deployment.

solution

kubectl scale --replicas=3 deployment/nginx-deployment
kubectl get deployment

Part 3: Services

1. Create a service that points to the nginx deployment and serves on port 8080

solution

kubectl expose deployment nginx-deployment --port=8080 --target-port=80

2. View all the resources in your namespaces.

You should see your new service, the deployment and its pods.

solution

kubectl get all

3. Connect to the service via port-forward command of kubectl and view the nginx start page

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

Part 4: kubectl explain

You can use kubectl explain to find out more about a Kubernetes resource and its fields:

1. Find out more about services using kubectl explain

solution

kubectl explain services

2. Find out more about pods using kubectl explain

solution

kubectl explain pods

3. Find out more about the containers within a pod using kubectl explain

solution

kubectl explain pods.spec.containers