DEV Community

Cover image for K8s Exercise : Core Concepts
Akshay Rao
Akshay Rao

Posted on • Updated on

K8s Exercise : Core Concepts

Introduction
Hi, I am Akshay Rao, will be starting a exercise series on k8s.
In this blog there will not explanation only problems and solutions.if you want explanation have a look at this series:-
https://dev.to/aksrao1998/series/24887

Pre-requisite
have minikube or kind running in the local machine.

Note:- k is alias for kubectl.

Let's Start

Problem 1
Create a namespace called 'mynamespace' and a pod with image nginx called nginx on this namespace.

Solution

[k8s-ckad (⎈|minikube:default)]$ k get ns
NAME              STATUS   AGE
default           Active   9d
hands-on          Active   9d
kube-node-lease   Active   9d
kube-public       Active   9d
kube-system       Active   9d

[k8s-ckad (⎈|minikube:default)]$ k create namespace mynamespace
namespace/mynamespace created
[k8s-ckad (⎈|minikube:default)]$ k config set-context --current --namespace=mynamespace
Context "minikube" modified.

# deploy a pod
[k8s-ckad (⎈|minikube:mynamespace)]$ kubectl run nginx --image=nginx --dry-run=client -o yaml > pods.yaml

# edit the pods.yaml
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
  namespace: mynamespace 

# verify
[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pods.yaml 
pod/nginx created
[k8s-ckad (⎈|minikube:mynamespace)]$ k get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          6s

Enter fullscreen mode Exit fullscreen mode

Problem 2
Create a busybox pod (using kubectl command) that runs the command "env". Run it and see the output.

Solution

[k8s-ckad (⎈|minikube:mynamespace)]$ k run soln2 --image=busybox --command --restart=Never -it --rm -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=soln2
TERM=xterm
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
HOME=/root
pod "soln2" deleted
Enter fullscreen mode Exit fullscreen mode

Problem 3
Create the YAML for a new ResourceQuota called 'myrq' with hard limits of 1 CPU, 1G memory and 2 pods without creating it

Solution

[k8s-ckad (⎈|minikube:mynamespace)]$ k create quota myrq  --hard=cpu=1,memory=1G,pods=2 --dry-run=client -o y
aml
apiVersion: v1
kind: ResourceQuota
metadata:
  creationTimestamp: null
  name: myrq
spec:
  hard:
    cpu: "1"
    memory: 1G
    pods: "2"
status: {}
Enter fullscreen mode Exit fullscreen mode

Problem 4

Create a pod with image nginx:1.25.1 called nginx and expose traffic on port 80

Solution

[k8s-ckad (⎈|minikube:mynamespace)]$ k run  mypod --image=nginx:1.25.1 --port=80
pod/mypod created

#Verify

[k8s-ckad (⎈|minikube:mynamespace)]$ k get po
NAME    READY   STATUS              RESTARTS   AGE
mypod   0/1     ContainerCreating   0          4s
nginx   1/1     Running             0          20m

[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it mypod bash -- nginx -v
nginx version: nginx/1.25.1

[k8s-ckad (⎈|minikube:mynamespace)]$ k describe pods mypod
Name:             mypod
Namespace:        mynamespace
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Thu, 12 Oct 2023 17:33:46 +0900
Labels:           run=mypod
Annotations:      <none>
Status:           Running
IP:               172.17.0.4
IPs:
  IP:  172.17.0.4
Containers:
  mypod:
    Container ID:   docker://147821ce3a12c57d9fef21026a57fcd0cee71360b411275db391a3dcccc25270
    Image:          nginx:1.25.1
    Image ID:       docker-pullable://nginx@sha256:67f9a4f10d147a6e04629340e6493c9703300ca23a2f7f3aa56fe615d75d31ca
    Port:           80/TCP
    Host Port:      0/TCP
Enter fullscreen mode Exit fullscreen mode

I hope this blog exercise has helped you.
Thank you

Top comments (0)