Difference between revisions of "CKA: Create and configure basic Pods"

From wikieduonline
Jump to navigation Jump to search
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Create a Pod ==
+
Only available in first CKA versions
 +
 
 +
== Create a [[Pod]] ==
 
* To create a pod based on a yaml definition file:  <code>[[kubectl apply]] -f https://k8s.io/examples/pods/storage/redis.yaml</code><ref>https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/</ref>
 
* To create a pod based on a yaml definition file:  <code>[[kubectl apply]] -f https://k8s.io/examples/pods/storage/redis.yaml</code><ref>https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/</ref>
 
* To create a pod in a [[namespace]]:
 
* To create a pod in a [[namespace]]:
Line 5: Line 7:
 
:<code>[[kubectl apply]] -f https://k8s.io/examples/pods/storage/redis.yaml --namespace=MY_TEST_NAMESPACE</code><ref>https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/</ref>
 
:<code>[[kubectl apply]] -f https://k8s.io/examples/pods/storage/redis.yaml --namespace=MY_TEST_NAMESPACE</code><ref>https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/</ref>
 
:To verify container is running:  
 
:To verify container is running:  
::<code>[[kubectl get]] pod NAME_POD --namespace=MY_TEST_NAMESPACE</code>
+
::<code>[[kubectl get pod]] NAME_POD --namespace=MY_TEST_NAMESPACE</code>
  
 
==Set up a [[Volume]] for a [[Pod]]==
 
==Set up a [[Volume]] for a [[Pod]]==
The file system of a [[Container]] exists as long as the Container exists. Therefore, when a Container is destroyed or restarted, changes made to the file system are lost. For more consistent storage that is independent of the Container life cycle, you can use a Volume, review different Volume types in https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes.
+
Review different Volume types in https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes.
 
 
This feature is especially important for applications that must maintain a status, such as key-value storage engines (for example Redis) and databases.
 
 
 
Create a Pod that runs a single Container. This Pod has a Volume of type <code>emptyDir</code> (empty directory) (see [[Volume]] for volume types) that exists throughout the Pod's life cycle, even when the Container is destroyed and restarted. Here is the Pod configuration file:
 
  
 +
Create a Pod that runs a single Container. This Pod has a Volume of type <code>emptyDir</code> (empty directory) (see [[Volume]] for volume types) that exists throughout the Pod's life cycle, even when the Container is destroyed and restarted.
 +
Example:
 
<code>pods/storage/redis.yaml</code>
 
<code>pods/storage/redis.yaml</code>
  
<pre>apiVersion: v1
+
apiVersion: v1
kind: Pod
+
[[kind: Pod]]
metadata:
+
metadata:
  name: redis
+
  name: redis
spec:
+
spec:
  containers:
+
  containers:
  - name: redis
+
  - [[name:]] redis
    image: redis
+
    [[image]]: redis
    volumeMounts:
+
    [[volumeMounts]]:
    - name: redis-storage
+
    - name: redis-storage
      mountPath: /data/redis
+
      [[mountPath]]: /data/redis
  volumes:
+
  [[volumes:]]
  - name: redis-storage
+
  - name: redis-storage
    emptyDir: {}</pre>
+
    emptyDir: {}</pre>
 
 
  
'''1.Create the Pod'''
+
=== Steps ===
<code>kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml</code>
+
==== Create Pod ====
 +
:<code>kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml</code>
 +
:<code>pod/redis created</code>
  
'''2. Verify the Pod Container is running'''
+
==== Verify [[Pod]] is running ====
<code>kubectl get pod redis --watch</code>
 
  
output:
+
[[kubectl get pods]]
 +
NAME    READY  STATUS    RESTARTS  AGE
 +
redis  1/1    Running  0          3m29s
  
<pre>NAME     READY     STATUS    RESTARTS  AGE
+
==== List all Kubernetes resources created ====
redis     1/1       Running  0          13s</pre>
+
[[kubectl get all]]
 +
NAME       READY   STATUS    RESTARTS  AGE
 +
pod/redis   1/1     Running  0          10m
 +
 +
NAME                TYPE        CLUSTER-IP  EXTERNAL-IP  PORT(S)  AGE
 +
service/kubernetes  ClusterIP  10.96.0.1    <none>        443/TCP  3d21h
  
'''3.In another terminal, open an interactive session inside the Container that is running:'''
+
==== In another terminal, open an interactive session inside the Container that is running: ====
<code>kubectl exec -it redis -- /bin/bash</code>
+
<code>[[kubectl exec]] -it redis -- /bin/bash</code>
  
'''4.In the terminal, go to <code>/data/redis</code> and create a file:'''
+
'''5. Create a file in <code>/data/redis</code>'''
 
<pre>root@redis:/data# cd /data/redis/
 
<pre>root@redis:/data# cd /data/redis/
 
root@redis:/data/redis# echo Hello > test-file</pre>
 
root@redis:/data/redis# echo Hello > test-file</pre>
  
'''5.In the terminal, list the running processes:'''
+
'''6. List running processes:'''
 
<pre>root@redis:/data/redis# apt-get update
 
<pre>root@redis:/data/redis# apt-get update
 
root@redis:/data/redis# apt-get install procps
 
root@redis:/data/redis# apt-get install procps
Line 61: Line 68:
 
root        15  0.0  0.0  17500  2072 ?        R+  00:48  0:00 ps aux</pre>
 
root        15  0.0  0.0  17500  2072 ?        R+  00:48  0:00 ps aux</pre>
  
6.In the terminal, kill the Redis process:
+
'''6.In the terminal, kill the Redis process:'''
  
 
root@redis:/data/redis# kill <pid>
 
root@redis:/data/redis# kill <pid>
Line 67: Line 74:
 
where <pid>is the process ID (PID) of Redis.
 
where <pid>is the process ID (PID) of Redis.
  
7.In the original terminal, observe the changes in the Redis Pod. You will eventually see something like the following:
+
'''7.In the original terminal, observe the changes in the Redis Pod. You will eventually see something like the following:'''
  
 
NAME      READY    STATUS    RESTARTS  AGE
 
NAME      READY    STATUS    RESTARTS  AGE
Line 77: Line 84:
  
 
1.Open a terminal in the restarted Container:
 
1.Open a terminal in the restarted Container:
 
+
<code>[[kubectl exec]] -it redis -- /bin/bash</code>
[[kubectl exec]] -it redis -- /bin/bash
 
  
 
2.In the terminal, go to /data/redisand verify that it test-filestill exists:
 
2.In the terminal, go to /data/redisand verify that it test-filestill exists:
Line 87: Line 93:
  
 
3.Remove the Pod you created for this exercise:
 
3.Remove the Pod you created for this exercise:
 +
* <code>[[kubectl delete]] pod redis</code>
  
kubectl delete pod redis
+
== Related terms ==
 +
* [[Certified Kubernetes Administrator v1.21]]
  
 
== See also ==
 
== See also ==
* {{kubectl}}
+
* {{kubectl apply}}
* [[Certified Kubernetes Administrator]]
+
* {{CKA versions}}
 +
* {{CKA}}
  
 
{{CC license}}
 
{{CC license}}

Latest revision as of 16:12, 29 February 2024

Only available in first CKA versions

Create a Pod[edit]

kubectl create namespace MY_TEST_NAMESPACE
kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml --namespace=MY_TEST_NAMESPACE[2]
To verify container is running:
kubectl get pod NAME_POD --namespace=MY_TEST_NAMESPACE

Set up a Volume for a Pod[edit]

Review different Volume types in https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes.

Create a Pod that runs a single Container. This Pod has a Volume of type emptyDir (empty directory) (see Volume for volume types) that exists throughout the Pod's life cycle, even when the Container is destroyed and restarted. Example: pods/storage/redis.yaml

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis
    volumeMounts:
    - name: redis-storage
      mountPath: /data/redis
  volumes:
  - name: redis-storage

emptyDir: {}

Steps[edit]

Create Pod[edit]

kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml
pod/redis created

Verify Pod is running[edit]

kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
redis   1/1     Running   0          3m29s

List all Kubernetes resources created[edit]

kubectl get all
NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          10m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   3d21h

In another terminal, open an interactive session inside the Container that is running:[edit]

kubectl exec -it redis -- /bin/bash

5. Create a file in /data/redis

root@redis:/data# cd /data/redis/
root@redis:/data/redis# echo Hello > test-file

6. List running processes:

root@redis:/data/redis# apt-get update
root@redis:/data/redis# apt-get install procps
root@redis:/data/redis# ps aux

output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
redis        1  0.1  0.1  33308  3828 ?        Ssl  00:46   0:00 redis-server *:6379
root        12  0.0  0.0  20228  3020 ?        Ss   00:47   0:00 /bin/bash
root        15  0.0  0.0  17500  2072 ?        R+   00:48   0:00 ps aux

6.In the terminal, kill the Redis process:

root@redis:/data/redis# kill <pid>

where <pid>is the process ID (PID) of Redis.

7.In the original terminal, observe the changes in the Redis Pod. You will eventually see something like the following:

NAME READY STATUS RESTARTS AGE redis 1/1 Running 0 13s redis 0/1 Completed 0 6m redis 1/1 Running 1 6m

At this point, the Container has been destroyed and restarted. This is because the Redis Pod has a restartPolicy (restart policy) of Always(always).

1.Open a terminal in the restarted Container: kubectl exec -it redis -- /bin/bash

2.In the terminal, go to /data/redisand verify that it test-filestill exists:

root@redis:/data/redis# cd /data/redis/ root@redis:/data/redis# ls test-file

3.Remove the Pod you created for this exercise:

Related terms[edit]

See also[edit]

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.

Source: https://en.wikiversity.org/wiki/DevOps/Kubernetes/Certified_Kubernetes_Administrator/Create_and_configure_basic_Pods

Advertising: