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
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 $simplePodYamlcat << 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.yamlThe pod definition contains the
nginxcontainer that listens to port 80.Create the pod using the YAML definition:
$simplePodYaml | kubectl apply -f -kubectl apply -f nginx-pod.yamlNow, 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 podskubectl get podsYou should see a pod named nginx-pod
Tip
If the pod has the STATUS
ContainerCreatingand shows READY0/1, this means the pod is still starting up. Wait a few seconds and run the commend again.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 podscat << 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
Show all the labels in the pods
kubectl get pods --show-labelskubectl get pods --show-labelsLet’s say you want to list pods that have a label named kind=web associated with them. You can use
-lswitch to apply filter based on labels.kubectl get pod -l kind=webkubectl get pod -l kind=webTo prove that this works as expected, run the command again but change the value of label kind to
db. Notice, this timekubectldoesn’t return any pods because there are no pods that match the label kind and a value ofdb.kubectl get pod -l kind=dbkubectl get pod -l kind=db
Task 3 - View complete definition of the pod
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.yamlkubectl get pods nginx-pod -o yaml > mypod.yamlTip
To view the JSON version, use the
-o jsonflag instead.View the contents of the generated file in VS Code (or an editor of your choice).
code mypod.yamlcode mypod.yamlTip
Observe all the properties that Kubernetes populated with default values when it saved the pod definition to its database.


