Exercise 2.3: Create A Basic Pod
Exercise 2.3: Create A Basic Pod
1. The smallest unit we directly control with Kubernetes is the pod. We will create a pod by creating a minimal YAML
file. First we will get a list of current API objects and their APIGROUP. If value is not shown it may not exist, as with
SHORTNAMES. Note that pods does not declare an APIGROUP. At the moment this indicates it is part of the stable v1
group.
2. Finding no declared APIGROUP we will use v1 to denote a stable object. With that information we will add the other three
required sections such as metadata, with a name, and spec which declares which Docker image to use and a name for
the container. We will create an eight line YAML file. White space and indentation matters. Don’t use Tabs. There is a
basic.yaml file available in the tarball, as well as basic-later.yaml which shows what the file will become and can
be helpful for figuring out indentation.
basic.yaml
1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: basicpod
5 spec:
6 containers:
7 - name: webcont
8 image: nginx
3. Create the new pod using the recently created YAML file.
4. Make sure the pod has been created then use the describe sub-command to view the details. Among other values in
the output you should be about to find the image and the container name.
6. We will now configure the pod to expose port 80. This configuration does not interact with the container to determine
what port to open. We have to know what port the process inside the container is using, in this case port 80 as a web
server. Add two lines to the end of the file. Line up the indentation with the image declaration.
student@ckad-1:˜$ vim basic.yaml
basic.yaml
1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: basicpod
5 spec:
6 containers:
7 - name: webcont
8 image: nginx
9 ports: #<--Add this and following line
10 - containerPort: 80
7. Create the pod and verify it is running. Use the -o wide option to see the internal IP assigned to the pod, as well
as NOMINATED NODE, which is used by the scheduler and READINESS GATES which show if experimental features are
enabled. Using curl and the pods IP address you should get the default nginx welcome web page.
<output_omitted>
8. We will now create a simple service to expose the pod to other nodes and pods in the cluster. The service YAML will
have the same four sections as a pod, but different spec configuration and the addition of a selector.
basicservice.yaml
1 apiVersion: v1
2 kind: Service
3 metadata:
4 name: basicservice
5 spec:
6 selector:
7 type: webserver
8 ports:
9 - protocol: TCP
10 port: 80
9. We will also add a label to the pod and a selector to the service so it knows which object to communicate with.
basic.yaml
1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: basicpod
5 labels: #<-- Add this line
6 type: webserver #<-- and this line which matches selector
7 spec:
8 ....
10. Create the new pod and service. Verify both have been created.
11. Test access to the web server using the CLUSTER-IP for the basicservice.
<output_omitted>
12. We will now expose the service to outside the cluster as well. Delete the service, edit the file and add a type declaration.
basicservice.yaml
1 apiVersion: v1
2 kind: Service
3 metadata:
4 name: basicservice
5 spec:
6 selector:
7 type: webserver
8 type: NodePort #<--Add this line
9 ports:
10 - protocol: TCP
11 port: 80
13. Create the service again. Note there is a different TYPE and CLUSTER-IP and also a high-numbered port.
14. Using the public IP address of the node and the high port you should be able to test access to the webserver. In the
example below the public IP is 35.238.3.83, yours will be different. The high port will also probably be different. Note
that testing from within a GCE or AWS node will not work. Use a local to you terminal or web browser to test.