Difference between revisions of "Create and configure a basic Pod"
Line 16: | Line 16: | ||
<code>pods/storage/redis.yaml</code> | <code>pods/storage/redis.yaml</code> | ||
− | + | apiVersion: v1 | |
− | kind: Pod | + | kind: Pod |
− | metadata: | + | metadata: |
− | + | name: redis | |
− | spec: | + | spec: |
− | + | containers: | |
− | + | - name: redis | |
− | + | image: redis | |
− | + | [[volumeMounts]]: | |
− | + | - name: redis-storage | |
− | + | mountPath: /data/redis | |
− | + | volumes: | |
− | + | - name: redis-storage | |
− | + | emptyDir: {}</pre> | |
Revision as of 16:06, 17 July 2021
Create a Pod
- To create a Pod based on a
yaml
definition file:kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml
[1] - To create a pod in a namespace:
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
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.
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 emptyDir
(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:
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: {}
1.Create the Pod
kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml
2. Verify the Pod Container is running
kubectl get pod redis --watch
output:
NAME READY STATUS RESTARTS AGE redis 1/1 Running 0 13s
3.In another terminal, open an interactive session inside the Container that is running:
kubectl exec -it redis -- /bin/bash
4.In the terminal, go to /data/redis
and create a file:
root@redis:/data# cd /data/redis/ root@redis:/data/redis# echo Hello > test-file
5.In the terminal, list the 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:
kubectl delete pod redis
See also
- Kubernetes: distributions, tools, CKA, CKS, Kubernetes interfaces: CSI, CNI, installation, workloads, networking,
kubeadm
,Kubernetes API
, Kubernetes API Server,kubectl, kubeadm, kubelet, kube-proxy
, Cloud services: EKS, GKE, TKE, DKS, Helm, Kubernetes RBAC, Kubernetes deployments, Minikube, Rancher, OpenShift, Charmed Kubernetes, Ingress, Kubernetes scheduler, Kubernetes Finalizers, logging, Kubernetes operator, Orka,kind:
, Kubernetes namespaces, Kubernetes dashboard, Kubernetes Metrics Server, Field Selectors, CoreDNS, CRI, Kubernetes Topology Manager, Kubernetes governance: (SIG, KEP), Kustomize, controllers,ReadinessProbe, LivenessProbe
, KOPS, K9s, Kui, k3s, ImagePullBackOff, PDB, EndPoints, Kots, metadata, Karpenter, Replicated.com, Kubernetes Authenticating, Kubernetes timeline, Changelog/Versions, service accounts, Kubernetes Pod Lifecycle, Kubernetes Conformance Certified, Kubernetes backup, Kubernetes Pod Security Admission, tEKS, Kubernetes events, Kubernetes ports, Kubernetes policies, Connect, addons, DoKC, Kubernetes control plane, Kubernetes Federation, Kubernetes info, Kubetest2, Sidecar (Kubernetes)
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: wikiversity
Advertising: