Answers to Review Questions

Chapter 2, Core Concepts

  1. You can either use the imperative approach or the declarative approach. First, we’ll look at creating the namespace with the imperative approach:

    $ kubectl create namespace ckad
    

    Create the Pod:

    $ kubectl run nginx --image=nginx:1.17.10 --port=80 --namespace=ckad
    

    Alternatively, you can use the declarative approach. Create a new YAML file called ckad-namespace.yaml with the following contents:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: ckad

    Create the namespace from the YAML file:

    $ kubectl create -f ckad-namespace.yaml
    

    Create a new YAML file called nginx-pod.yaml with the following contents:

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.10
        ports:
        - containerPort: 80

    Create the Pod from the YAML file:

    $ kubectl create -f nginx-pod.yaml --namespace=ckad
    
  2. You can use the command-line option -o wide to retrieve the IP address of the Pod:

    $ kubectl get pod nginx --namespace=ckad -o wide
    

    The same information is available if you query for the Pod details:

    $ kubectl describe pod nginx --namespace=ckad | grep IP:
    
  3. You can use the command-line options --rm and -it to start a temporary Pod. The following command assumes that the IP address of the Pod named nginx is 10.1.0.66:

    $ kubectl run busybox --image=busybox --restart=Never --rm -it -n ckad \
      -- wget -O- 10.1.0.66:80
    
  4. To download the logs, use a simple logs command:

    $ kubectl logs nginx --namespace=ckad ...

Get Certified Kubernetes Application Developer (CKAD) Study Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.