CKA Exam Prep Flashcards
What is the command to create a POD?
kubectl run pod_name –image=image_name
What is the command to create a POD manifest file without creating the resources?
kubectl run pod_name –image=image_name –dry-run=client -o yaml
How to create a deployment?
kubectl create deployment deployment_name –image=image_name
e.g. kubectl create deployment my-nginx –image=nginx
This creates a Deployment named my-nginx that runs Pods using the nginx image. Kubernetes will automatically ensure that one instance of the nginx container is running unless scaled otherwise.
Kubernetes automatically assigns labels as well to the pod and uses those as selectors in deployment
Create deployment with 4 replicas
kubectl create deployment deployment_name –image=image_name –replicas=4
e.g.: kubectl create deployment nginx –image=nginx –replicas=4
How to scale a deployment
kubectl scale deployment deployment_name –replicas=6
e.g.: kubectl scale deployment nginx –replicas=4
How to create a namespace?
kubectl create namespace namespace_name
e.g. kubectl create namespace dev
How to create a POD in a specific namespace
kubectl run pod_name –image=image_name –namespace=namespace_name
e.g. kubectl run nginx –image=nginx –namespace=dev
How to edit a resource?
kubectl edit resource_type resource_name
e.g.
kubectl edit deployment deployment_name
kubectl edit pod pod_name
This edits the in-memory file and changes the resources but the main config file is untouched
How to apply a configuration file?
kubectl apply -f config_file.yaml
How to set a new image for an existing deployment?
kubectl set image deployment/deployment_name image_name=new_image_name
e.g. kubectl set image deployment/my-deployment nginx=nginx:1.21
This command directly modifies the configuration of the Deployment in the Kubernetes cluster. It creates a rolling update where Pods are gradually replaced with new Pods using the updated image.
How to describe a resource?
kubectl describe pod pod_name
How to view logs
kubectl logs pod_name
How to get resources? pods, services, deployments, namespaces, nodes
kubectl get pods
kubectl get services
kubectl get deploymnets
kubectl get nodes
kubectl get all
How to see resources for all namespaces?
kubectl get all –all-namespaces
How to get resources with wide output?
kubectl get pods -o wide
How to delete a deployment?
kubectl delete deployment deployment_name
How to create a resource file for deployment from Live configuration?
kubectl get deployment deployment_name -o yaml > deployment.yaml
How to dry run to generate a YAML file? Consider a deployment
kubectl create deployment deployment_name -image=image_name –dry-run=client -o yaml
How to update objects within a configuration file?
kubectl replace -f config_file.yaml
you can use kubectl apply -f config_file.yaml
How to apply resources in a directory instead of specific file?
kubectl apply -f /path/to/config-files
Does deployments automatically create all the resources including replica sets?
Yes
How to check the rollout status of a deployment?
kubectl rollout status deployment/deployment_name
How to check the rollout history of a deployment?
kubectl rollout history deployment/deployment_name
How to undo a rollout for a deployment?
kubectl rollout undo deployment/deployment_name
How to scale a replicaset?
kubectl scale -f replicaset-definitionfile.yaml –replicas=6
You can also update the replicaset directly
kubectl scale replicaset replicaset_name –replicas=6
How to connect to a resource in another namespace? Lets say its a mysql database and the namespace name is dev and the resource name is db-service?
mysql.connect(db-service.dev.svc.cluster.local)
How to get pods in a specific namespace?
kubectl get pods –namespace=namespace_name
How to switch contexts permanently for a namespace to dev?
kubectl config set context $(kubectl config current-context) –namespace=dev
How to create a service to route traffic to a deployment?
kubectl expose deployment deployment_name –port 80
- If you want the service to be accessible outside the cluster, you can specify –type=NodePort or –type=LoadBalancer.
- If the nginx deployment exposes containers on a port other than 80, you need to add –target-port=<container-port> to map the service's port to the container's port.</container-port>
What are the different types of ports?
port: ServicePort
targetPort: Container Port (where the traffic should be forwarded)
nodePort: where the service is exposed outside the cluster
The only mandatory field is port. If targetPort is not entered it gets the same port as port.
If the nodePort is not entered kubernetes automatically assigns a port between 30000-32627
What is the difference between replace & apply?
kubectl replace -f nginx.yaml
kubectl replace –force -f nginx.yaml
kubectl apply -f nginx.yaml
replace: the resource should exist. It will delete the resources & create them fresh with the supplied config file
apply: if the resource does not exist, it will create the resource. If the resource already exist, it will apply the changes.
If you are making changes that are not supported as an in-place update (e.g., changing certain immutable fields like spec.selector in a Deployment), you may need to delete and recreate the resource.
Use kubectl apply to create or update resources without deleting them first.
Delete resources only when you need to reset their state or make changes that are not supported in-place.
When to use kubectl edit command
For quick changes and you are sure that you are not going to reply on object configuration file in future. Because changes are not made in the config file.
How to Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379. Just do a dry run
kubectl expose pod redis –port=6379 –name=redis-service –dry-run=client -o yaml
By default this command uses pod labels as selectors as you cannot pass selectors in imperative commands
How to create a pod with labels
kubectl run nginx –image=nginx –labels=”env=production,app=nginx”
How to Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379. Just do a dry run
kubectl create service redis-service –port-80 –dry-run=client -o yaml
> service-fil.yaml
This will not use the pods labels as selectors, instead it will assume selectors as app=redis-service. So it does not work very well if your pod has a different label set. So generate the file and modify the selectors before creating the service
Create a Service named nginx of type NodePort to expose pod nginx’s port 80 on port 30080 on the nodes:
Pod labels as selectors:
kubectl expose pod nginx –port=80 –type=NodePort –name=nginx –dry-run=client -o yaml > service-file.yaml
This will automatically use the pod’s labels as selectors, but you cannot specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.
Pod Labels are not used as selectors:
kubectl create service –type=nodeport –name=nginx –port=80 –node-port=30080 –dry-run=client -o yaml
Both the above commands have their own challenges. While one of it cannot accept a selector the other cannot accept a node port. I would recommend going with the kubectl expose command. If you need to specify a node port, generate a definition file using the same command and manually input the nodeport before creating the service.
To specify nodePort:
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
selector:
app: my-app
ports:
- protocol: TCP
port: 80 # The port the service will expose internally
targetPort: 8080 # The port on the container to forward traffic to
nodePort: 30007 # The specific NodePort to expose on each node
Summary of lab commands
controlplane ~ ➜ kubectl run nginx-pod –image=nginx:alpine
pod/nginx-pod created
controlplane ~ ➜ kubectl run redis –image=redis:alpine –labels=”tier=db”
pod/redis created
controlplane ~ ✖ kubectl expose pod redis –port=6379 –name=redis-service
service/redis-service exposed
controlplane ~ ➜ kubectl create deployment webapp –image=kodekloud/webapp-color –replicas=3
deployment.apps/webapp created
controlplane ~ ➜ kubectl run custom-nginx –image=nginx –port=80
pod/custom-nginx created
controlplane ~ ➜ kubectl delete pod custom-nginx
pod “custom-nginx” deleted
controlplane ~ ✖ kubectl run custom-nginx –image=nginx –port=8080
pod/custom-nginx created
controlplane ~ ➜ kubectl create namespace dev-ns
namespace/dev-ns created
controlplane ~ ➜ kubectl create deployment redis-deploy –namespace=dev-ns –image=redis –replicas=2
deployment.apps/redis-deploy created
controlplane ~ ➜ kubectl run httpd –image=httpd:alpine
pod/httpd created
controlplane ~ ➜ kubectl expose pod httpd –port=80
service/httpd exposed
How to create a POD declaratively?
apiVersion: v1
kind: Pod
metadata:
name: redis-pod
labels:
name: redis-pod
app: demo-voting-app
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
How to use help command say for e.g. kubectl expose
kubectl expose –help
How to create a deployment declaratively?
apiVersion: apps/v1
kind: Deployment
metadata:
name: voting-app-deploy
labels:
name: voting-app-deploy
app: demo-voting-app
spec:
template:
metadata:
name: voting-app-pod
labels:
name: voting-app-pod
app: demo-voting-app
spec:
containers:
- name: voting-app
image: kodekloud/examplevotingapp_vote:v1
ports:
- containerPort: 80
selector:
matchLabels:
name: voting-app-pod
app: demo-voting-app
replicas: 1
How to create a service declaratively?
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
name: result-app-service
app: demo-voting-app
spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30005
selector:
name: result-app-pod
app: demo-voting-app