Exercise 2: Create A Pod

This Exercise demonstrates the use of a YAML file to create a pod declaratively.

Task 1 - Create A Pod Declaratively

  1. Create a pod definition using YAML:

    $simplePodYaml = @"
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod  
      labels:
        kind: web
        target: dev
    spec:
      containers:
      - image: k8sonazureworkshoppublic.azurecr.io/nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      nodeSelector:
        kubernetes.io/os: linux
    "@
    
    # Review the YAML
    $simplePodYaml
    cat << EOF > nginx-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod  
      labels:
        kind: web
        target: dev
    spec:
      containers:
      - image: k8sonazureworkshoppublic.azurecr.io/nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      nodeSelector:
        kubernetes.io/os: linux
    EOF
    
    # Review the YAML
    cat nginx-pod.yaml

    The pod definition contains the nginx container that listens to port 80.

  2. Create the pod using the YAML definition:

    $simplePodYaml | kubectl apply -f -
    kubectl apply -f nginx-pod.yaml
  3. Now, make sure pod is up and running. You may need to re-run the command if it takes a while for the pod to start.

    kubectl get pods
    kubectl get pods

    You should see a pod named nginx-pod

    Simple Pod Simple Pod

    Tip

    If the pod has the STATUS ContainerCreating and shows READY 0/1, this means the pod is still starting up. Wait a few seconds and run the commend again.

  4. Add a second pod, then check the list again.

    $simplePod2Yaml = @"
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod2
      labels:
        kind: api
        target: dev
    spec:
      containers:
      - image: k8sonazureworkshoppublic.azurecr.io/nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      - image: k8sonazureworkshoppublic.azurecr.io/mysql:5.7
        name: mysql
        ports:
        - containerPort: 3306
          protocol: TCP      
        env: 
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "true"
      nodeSelector:
        kubernetes.io/os: linux
    "@
    
    $simplePod2Yaml | kubectl apply -f -
    kubectl get pods
    cat << EOF > nginx-pod2.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod2
      labels:
        kind: api
        target: dev
    spec:
      containers:
      - image: k8sonazureworkshoppublic.azurecr.io/nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
      - image: k8sonazureworkshoppublic.azurecr.io/mysql:5.7
        name: mysql
        ports:
        - containerPort: 3306
          protocol: TCP      
        env: 
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "true"
      nodeSelector:
        kubernetes.io/os: linux
    EOF
    
    kubectl apply -f nginx-pod2.yaml
    kubectl get pods

Task 2 - Filter Pods Based On A Label

  1. Show all the labels in the pods

    kubectl get pods --show-labels
    kubectl get pods --show-labels

    Labels Labels

  2. Let’s say you want to list pods that have a label named kind=web associated with them. You can use -l switch to apply filter based on labels.

    kubectl get pod -l kind=web
    kubectl get pod -l kind=web
  3. To prove that this works as expected, run the command again but change the value of label kind to db. Notice, this time kubectl doesn’t return any pods because there are no pods that match the label kind and a value of db.

    kubectl get pod -l kind=db
    kubectl get pod -l kind=db

Task 3 - View complete definition of the pod

  1. Query Kubernetes to return the complete definition of a pod from its internal database by exporting the output (-o) to YAML. Then pipe the result to a file.

    kubectl get pods nginx-pod -o yaml > mypod.yaml
    kubectl get pods nginx-pod -o yaml > mypod.yaml
    Tip

    To view the JSON version, use the -o json flag instead.

  2. View the contents of the generated file in VS Code (or an editor of your choice).

    code mypod.yaml
    code mypod.yaml

    Pod Details Pod Details

    Tip

    Observe all the properties that Kubernetes populated with default values when it saved the pod definition to its database.