자격증/CKA

Udemy Mock Exam - 1

14ben 2024. 7. 30. 23:19
728x90

1. Deploy a pod named nginx-pod using the nginx:alpine image.

Deploy a pod named nginx-pod using the nginx:alpine image.

Once done, click on the Next Question button in the top right corner of this panel. 
You may navigate back and forth freely between all questions. 
Once done with all questions, click on End Exam. 
Your work will be validated at the end and score shown. Good Luck!

- Name: nginx-pod
- Image: nginx:alpine

 

 

controlplane ~ ➜  k run nginx-pod --image=nginx:alpine --dry-run=client -o yaml > pod.yaml

controlplane ~ ➜  ls
pod.yaml  sample.yaml

controlplane ~ ➜  k apply -f pod.yaml 
pod/nginx-pod created

 

 


2. Deploy a messaging pod using the redis:alpine image with the labels set to tier=msg.

Deploy a messaging pod using the redis:alpine image with the labels set to tier=msg.


- Pod Name: messaging

- Image: redis:alpine

- Labels: tier=msg

 

controlplane ~ ➜  k run --help |grep labels
  # Start a hazelcast pod and set labels "app=hazelcast" and "env=prod" in the container
  
  kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"
    
    -l, --labels='':
        Comma separated labels to apply to the pod. Will override previous values.

 

controlplane ~ ➜  k run messaging --image=redis:alpine --labels="tier=msg" --dry-run=client -o yaml > message.yaml

controlplane ~ ➜  cat message.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    tier: msg
  name: messaging
spec:
  containers:
  - image: redis:alpine
    name: messaging
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}



controlplane ~ ➜  k apply -f message.yaml 
pod/messaging created

 

 

 


3. Create a namespace named apx-x9984574.

Create a namespace named apx-x9984574.


- Namespace: apx-x9984574

 

controlplane ~ ✖ k create --help |grep namespace
  namespace             Create a namespace with the specified name

controlplane ~ ➜  k create namespace apx-x9984574
namespace/apx-x9984574 created


controlplane ~ ➜  k get ns
NAME              STATUS   AGE
apx-x9984574      Active   18s
default           Active   124m
finance           Active   12m
kube-flannel      Active   124m
kube-node-lease   Active   124m
kube-public       Active   124m
kube-system       Active   124m

 

 


4. Get the list of nodes in JSON format and store it in a file at 

     /opt/outputs/nodes-z3444kd9.json. 

Get the list of nodes in JSON format and store it in a file at 
/opt/outputs/nodes-z3444kd9.json. 

- Task completed

 

controlplane ~ ➜  k get nodes  -o json > /opt/outputs/node-z3444kd9.json

 

 

Examples using kubectl and JSONPath expressions:

kubectl get pods -o json
kubectl get pods -o=jsonpath='{@}'
kubectl get pods -o=jsonpath='{.items[0]}'
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
kubectl get pods -o=jsonpath='{.items[0].metadata.labels.kubernetes\.io/hostname}'

https://kubernetes.io/docs/reference/kubectl/jsonpath/

 


5. Create a service messaging-service to expose

    the messaging application within the cluster on port 6379.

Create a service messaging-service to expose 
the messaging application within the cluster on port 6379.

Use imperative commands.

- Service: messaging-service
- Port: 6379
- Type: ClusterIp
- Use the right labels

 

controlplane ~ ✖ k get pods
NAME        READY   STATUS             RESTARTS       AGE
deploy      0/1     CrashLoopBackOff   8 (5m6s ago)   21m
messaging   1/1     Running            0              10m
nginx-pod   1/1     Running            0              16m

controlplane ~ ➜  k get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   132m

 

controlplane ~ ➜  k expose pod messaging --port 6379 --name messaging-service
service/messaging-service exposed

controlplane ~ ➜  k get svc
NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes          ClusterIP   10.96.0.1       <none>        443/TCP    137m
messaging-service   ClusterIP   10.110.44.209   <none>        6379/TCP   5s

controlplane ~ ➜  k describe svc messaging-service |grep -i end
Endpoints:         10.244.0.6:6379

controlplane ~ ➜  k describe svc messaging-service |grep -i type
Type:              ClusterIP

 

 


6. Create a deployment named hr-web-app using the image kodekloud/webapp-color with 2 replicas.

Create a deployment named hr-web-app using the image kodekloud/webapp-color with 2 replicas.


- Name: hr-web-app
- Image: kodekloud/webapp-color
- Replicas: 2

 

controlplane ~ ➜  k create deployment hr-web-app --image kodekloud/we
bapp-color --replicas 2
deployment.apps/hr-web-app created

controlplane ~ ➜  k get deployment
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
hr-web-app   2/2     2            2           7s

 

 

 


7. Create a static pod named static-busybox on the controlplane node

    that uses the busybox image and the command sleep 1000.

Create a static pod named static-busybox on the controlplane node 
that uses the busybox image and the command sleep 1000.


- Name: static-busybox
- Image: busybox

 

controlplane ~ ➜  k run static-busybox --image=busybox --dry-run=client -o yaml --command -- sleep 1000 > static.yaml


controlplane ~ ✖ k apply -f static.yaml 
pod/static-busybox created


controlplane ~ ➜  k get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
hr-web-app-5d6b77db78-f8slm   1/1     Running   0          10m   10.244.0.7   controlplane   <none>           <none>
hr-web-app-5d6b77db78-v4rjn   1/1     Running   0          10m   10.244.0.6   controlplane   <none>           <none>
messaging                     1/1     Running   0          24m   10.244.0.5   controlplane   <none>           <none>
nginx-pod                     1/1     Running   0          27m   10.244.0.4   controlplane   <none>           <none>
static-busybox                1/1     Running   0          15s   10.244.0.8   controlplane   <none>           <none>
더보기
Q. 7
 
Task

Create a static pod named static-busybox on the controlplane node that uses the busybox image and the command sleep 1000.

 

Solution

Create a pod definition file in the manifests directory. For that use command kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -oyaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml

 

Details

Name: static-busybox

Image: busybox

 

controlplane ~ ➜  k run static-busybox --image=busybox --dry-run=client -o yaml --command -- sleep 1000 > static-busybox.yaml

controlplane ~ ➜  k get pods
NAME                          READY   STATUS    RESTARTS   AGE
hr-web-app-5d6b77db78-qsm6k   1/1     Running   0          23m
hr-web-app-5d6b77db78-v5hkq   1/1     Running   0          23m
messaging                     1/1     Running   0          25m
nginx-pod                     1/1     Running   0          25m
static-busybox-controlplane   1/1     Running   0          18s

 

 


8. Create a POD in the finance namespace named temp-bus with the image redis:alpine.

Create a POD in the finance namespace named temp-bus with the image redis:alpine.


- Name: temp-bus
- Image Name: redis:alpine

 

 

controlplane ~ ✖ k run temp-bus --image=redis:alpine -n finance
pod/temp-bus created

 

 

 


9. A new application orange is deployed. There is something wrong with it. Identify and fix the issue.

A new application orange is deployed. There is something wrong with it. 
Identify and fix the issue.


- Issue fixed

 

 

controlplane ~ ➜  k get pods
NAME                          READY   STATUS                  RESTARTS      AGE
hr-web-app-5d6b77db78-f8slm   1/1     Running                 0             19m
hr-web-app-5d6b77db78-v4rjn   1/1     Running                 0             19m
messaging                     1/1     Running                 0             34m
nginx-pod                     1/1     Running                 0             36m
orange                        0/1     Init:CrashLoopBackOff   4 (55s ago)   2m25s
static-busybox                1/1     Running                 0             9m49s


controlplane ~ ✖ k describe pod orange
...
..
.

Events:
  Type     Reason     Age                     From               Message
  ----     ------     ----                    ----               -------
  Normal   Scheduled  9m27s                   default-scheduler  Successfully assigned default/orange to controlplane
  Normal   Pulled     9m26s                   kubelet            Successfully pulled image "busybox" in 180ms (180ms including waiting). Image size: 2160406 bytes.
  Normal   Pulled     9m25s                   kubelet            Successfully pulled image "busybox" in 187ms (187ms including waiting). Image size: 2160406 bytes.
  Normal   Pulled     9m10s                   kubelet            Successfully pulled image "busybox" in 254ms (254ms including waiting). Image size: 2160406 bytes.
  Normal   Pulled     8m47s                   kubelet            Successfully pulled image "busybox" in 157ms (157ms including waiting). Image size: 2160406 bytes.
  Normal   Created    8m46s (x4 over 9m26s)   kubelet            Created container init-myservice
  Normal   Started    8m46s (x4 over 9m25s)   kubelet            Started container init-myservice
  Normal   Pulling    7m58s (x5 over 9m26s)   kubelet            Pulling image "busybox"
  Normal   Pulled     7m58s                   kubelet            Successfully pulled image "busybox" in 151ms (151ms including waiting). Image size: 2160406 bytes.
  Warning  BackOff    4m24s (x24 over 9m24s)  kubelet            Back-off restarting failed container init-myservice in pod orange_default(3e84f7af-8b5e-4049-b1a3-634f8e5d5b60)
  
  
  controlplane ~ ✖ k get pod orange -o yaml > orange.yaml
  
  
  controlplane ~ ➜  k logs orange 
Defaulted container "orange-container" out of: orange-container, init-myservice (init)
Error from server (BadRequest): container "orange-container" in pod "orange" is waiting to start: PodInitializing

controlplane ~ ✖ k logs orange init-myservice
sh: sleeeep: not found

 

controlplane ~ ➜  vim orange.yaml 
  1 apiVersion: v1
  2 kind: Pod
  3 metadata:
  4   creationTimestamp: "2024-07-31T14:01:42Z"
  5   name: orange
  6   namespace: default
  7   resourceVersion: "6369"
  8   uid: 3e84f7af-8b5e-4049-b1a3-634f8e5d5b60
  9 spec:
 10   containers:
 11   - command:
 12     - sh
 13     - -c
 14     - echo The app is running! && sleep 3600
 15     image: busybox:1.28
 16     imagePullPolicy: IfNotPresent
 17     name: orange-container
 18     resources: {}
 19     terminationMessagePath: /dev/termination-log
 20     terminationMessagePolicy: File
 21     volumeMounts:
 22     - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
 23       name: kube-api-access-wgmhs
 24       readOnly: true
 25   dnsPolicy: ClusterFirst
 26   enableServiceLinks: true
 27   initContainers:
 28   - command:
 29     - sh
 30     - -c
 31     - sleeeeep 2; ->  - sleep 2;

 

controlplane ~ ✖ k delete pod orange
pod "orange" deleted

controlplane ~ ➜  k apply -f orange.yaml 
pod/orange created

controlplane ~ ➜  k get pod
NAME                          READY   STATUS     RESTARTS       AGE
hr-web-app-5d6b77db78-f8slm   1/1     Running    0              32m
hr-web-app-5d6b77db78-v4rjn   1/1     Running    0              32m
messaging                     1/1     Running    0              47m
nginx-pod                     1/1     Running    0              49m
orange                        0/1     Init:0/1   0              3s
static-busybox                1/1     Running    1 (6m1s ago)   22m

controlplane ~ ➜  k get pod
NAME                          READY   STATUS    RESTARTS       AGE
hr-web-app-5d6b77db78-f8slm   1/1     Running   0              32m
hr-web-app-5d6b77db78-v4rjn   1/1     Running   0              32m
messaging                     1/1     Running   0              47m
nginx-pod                     1/1     Running   0              49m
orange                        1/1     Running   0              6s
static-busybox                1/1     Running   1 (6m4s ago)   22m

 

더보기
Q. 9
 
Task

A new application orange is deployed. There is something wrong with it. Identify and fix the issue.

 

Solution

To know more details of orange pod:

kubectl describe po orange

and look under the initContainers section. There is an issue with the given command.

 Command:
      sh
      -c
      sleeeep 2;

 

In the above, we need to correct the sleeeep command.

To update the pod with an easiest way by running command:

kubectl edit po orange

It's not possible to update the changes in the running pod so after saving the changes. It will create a temporary file in the default location /tmp/.

Use that manifest file and replace with the existing pod:

kubectl replace -f /tmp/kubectl-edit-xxxx.yaml --force

Above command will delete the existing pod and will recreate the new pod with latest changes.

 
 
Details
 

- Issue fixed


10. Expose the hr-web-app as service hr-web-app-service application 
      on port 30082 on the nodes on the cluster.

Expose the hr-web-app as service hr-web-app-service application 
on port 30082 on the nodes on the cluster.


The web application listens on port 8080.

- Name: hr-web-app-service
- Type: NodePort
- Endpoints: 2
- Port: 8080
- NodePort: 30082

 

controlplane ~ ➜  k get svc
NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hr-web-app-service   NodePort    10.101.138.173   <none>        8080:30260/TCP   21s
kubernetes           ClusterIP   10.96.0.1        <none>        443/TCP          94m
messaging-service    ClusterIP   10.96.213.11     <none>        6379/TCP         46m

 

 

controlplane ~ ➜  k edit svc hr-web-app-service 
service/hr-web-app-service edited

 17   spec:
 18     clusterIP: 10.101.138.173
 19     clusterIPs:
 20     - 10.101.138.173
 21     externalTrafficPolicy: Cluster
 22     internalTrafficPolicy: Cluster
 23     ipFamilies:
 24     - IPv4
 25     ipFamilyPolicy: SingleStack
 26     ports:
 27     - nodePort: 300082  ## <<-----fixed
 28       port: 8080
 29       protocol: TCP
 30       targetPort: 8080
 31     selector:
 32       app: hr-web-app
 33     sessionAffinity: None
 34     type: NodePort

 

 

11. Use JSON PATH query to retrieve the osImages of all the nodes and store it in a file /opt/outputs/nodes_os_x43kj56.txt.

Use JSON PATH query to retrieve the osImages of all the nodes and store it in a file /opt/outputs/nodes_os_x43kj56.txt.


The osImages are under the nodeInfo section under status of each node.

Task Completed

 

 

 

Examples using kubectl and JSONPath expressions:

kubectl get pods -o json
kubectl get pods -o=jsonpath='{@}'
kubectl get pods -o=jsonpath='{.items[0]}'
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
kubectl get pods -o=jsonpath='{.items[0].metadata.labels.kubernetes\.io/hostname}'
controlplane ~ ➜  k get nodes -o jsonpath='{.items[*].status.nodeInfo.osImages}' > /opt/outputs/nodes_os_x43kj56.txt

 

더보기
Q. 11
 
Task

Use JSON PATH query to retrieve the osImages of all the nodes and store it in a file /opt/outputs/nodes_os_x43kj56.txt.

The osImages are under the nodeInfo section under status of each node.

 

Solution

Run the command: kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.osImage}' > /opt/outputs/nodes_os_x43kj56.txt

 

Details

Task Completed


 

 

12. Create a Persistent Volume with the given specification: -

Create a Persistent Volume with the given specification: -

Volume name: pv-analytics

Storage: 100Mi

Access mode: ReadWriteMany

Host path: /pv/data-analytics


Is the volume name set?

Is the storage capacity set?

Is the accessMode set?

Is the hostPath set?

 

 

https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistent-volumes

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

 

수정

controlplane ~ ➜  cat pvc.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-analytics
spec:
  capacity:
    storage: 100Mi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /pv/data-analytics
728x90

'자격증 > CKA' 카테고리의 다른 글

[직장인] 2024년 9월 CKA 합격 후기  (10) 2024.10.14
Udemy Mock Exam - 2  (0) 2024.08.25
[CKA] Udemy Lightning Lab (7/7)  (0) 2024.07.29
[CKA] Udemy Lightning Lab (6/7)  (0) 2024.07.29
[CKA] Udemy Lightning Lab (5/7)  (0) 2024.07.25