Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

CKAD対策 Q4&A4


Q4
①以下の条件でconfigmapを作成する
 名前:test-config
 key/valueの組み合わせ:server/web
 namespace:ckad ※ないなら作成する
②以下の条件でPodを起動する
 namespace:ckad
 名前:configmap-pod
 configmapをボリュームとしてマウントするディレクトリ:/mnt/configmap
 使用するイメージ:nginx

A4 ①configmapの作成

kubeuser@master01:~$ kubectl get ns
NAME                   STATUS   AGE
blue                   Active   282d
cadvisor               Active   238d
default                Active   298d
ingress-nginx          Active   295d
istio-system           Active   72d
kube-node-lease        Active   298d
kube-public            Active   298d
kube-system            Active   298d
kubernetes-dashboard   Active   284d
metallb-system         Active   297d
orange                 Active   282d

kubeuser@master01:~$ kubectl create ns ckad
namespace/ckad created

kubeuser@master01:~$ kubectl get ns
NAME                   STATUS   AGE
blue                   Active   282d
cadvisor               Active   238d
ckad                   Active   2s
default                Active   298d
ingress-nginx          Active   295d
istio-system           Active   72d
kube-node-lease        Active   298d
kube-public            Active   298d
kube-system            Active   298d
kubernetes-dashboard   Active   284d
metallb-system         Active   297d
orange                 Active   282d

kubeuser@master01:~$ kubectl create configmap test-config --from-literal=server=web -n ckad
configmap/test-config created

kubeuser@master01:~$ kubectl get cm -n ckad
NAME                 DATA   AGE
istio-ca-root-cert   1      5m53s
kube-root-ca.crt     1      5m54s
test-config          1      15s

kubeuser@master01:~$ kubectl describe cm test-config -n ckad
Name:         test-config
Namespace:    ckad
Labels:       <none>
Annotations:  <none>

Data
====
server:
----
web

BinaryData
====

Events:  <none>

kubeuser@master01:~$ kubectl get cm test-config -n ckad -o yaml
apiVersion: v1
data:
  server: web
kind: ConfigMap
metadata:
  creationTimestamp: "2024-01-06T16:06:51Z"
  name: test-config
  namespace: ckad
  resourceVersion: "45478912"
  uid: a10be641-cfa2-4348-86e1-5fda939accad


②Podの起動

kubeuser@master01:~$ kubectl run configmap-pod --image=nginx -n ckad --dry-run=client -o yaml > q4_pod.yam
l

kubeuser@master01:~$ vi q4_pod.yaml 

kubeuser@master01:~$ cat q4_pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: configmap-pod
  name: configmap-pod
  namespace: ckad
spec:
  containers:
  - image: nginx
    name: configmap-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

kubeuser@master01:~$ vi q4_pod.yaml 

kubeuser@master01:~$ cat q4_pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: configmap-pod
  name: configmap-pod
  namespace: ckad
spec:
  containers:
  - image: nginx
    name: configmap-pod
    volumeMounts:
    - name: config-volume
      mountPath: /mnt/configmap
  volumes:
    - name: config-volume
      configMap:
        name: test-config

kubeuser@master01:~$ kubectl apply -f q4_pod.yaml 
pod/configmap-pod created

kubeuser@master01:~$ kubectl get pods -n ckad
NAME            READY   STATUS    RESTARTS   AGE
configmap-pod   1/1     Running   0          2m54s


kubeuser@master01:~$ kubectl describe pods configmap-pod -n ckad
Name:             configmap-pod
Namespace:        ckad
Priority:         0
Service Account:  default
Node:             worker02/192.168.1.46
Start Time:       Sat, 06 Jan 2024 16:29:25 +0000
Labels:           run=configmap-pod
Annotations:      cni.projectcalico.org/containerID: 36d98013b0c527ab3e6c60c2fc212f01edfb89a54e5d3ea43df30118d0f452a4
                  cni.projectcalico.org/podIP: 10.0.30.68/32
                  cni.projectcalico.org/podIPs: 10.0.30.68/32,fd12:b5e0:383e:0:7bf:50a7:b256:1e5e/128
Status:           Running
IP:               10.0.30.68
IPs:
  IP:  10.0.30.68
  IP:  fd12:b5e0:383e:0:7bf:50a7:b256:1e5e
Containers:
  configmap-pod:
    Container ID:   containerd://876029dd5a7817a96cbe67316b0d5d1336dc88c1cad30a79b4ffca452f047209
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:2bdc49f2f8ae8d8dc50ed00f2ee56d00385c6f8bc8a8b320d0a294d9e3b49026
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 06 Jan 2024 16:29:34 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /mnt/configmap from config-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tfzdm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  config-volume:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      test-config
    Optional:  false
  kube-api-access-tfzdm:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  3m14s  default-scheduler  Successfully assigned ckad/configmap-pod to worker02
  Normal  Pulling    3m9s   kubelet            Pulling image "nginx"
  Normal  Pulled     3m6s   kubelet            Successfully pulled image "nginx" in 2.860215834s (2.860370647s including waiting)
  Normal  Created    3m5s   kubelet            Created container configmap-pod
  Normal  Started    3m5s   kubelet            Started container configmap-pod

kubeuser@master01:~$ kubectl exec -it configmap-pod -n ckad -- /bin/bash

root@configmap-pod:/# ls /mnt/      
configmap

root@configmap-pod:/# ls -l /mnt
total 4
drwxrwxrwx 3 root root 4096 Jan  6 16:29 configmap

root@configmap-pod:/# ls /mnt/configmap
server

root@configmap-pod:/# ls -l /mnt/configmap
total 0
lrwxrwxrwx 1 root root 13 Jan  6 16:29 server -> ..data/server


おまけ:削除

kubeuser@master01:~$ kubectl delete pod configmap-pod -n ckad --force
Warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "configmap-pod" force deleted

kubeuser@master01:~$ kubectl delete cm test-config -n ckad
configmap "test-config" deleted

kubeuser@master01:~$ kubectl delete ns ckad
namespace "ckad" deleted


参照本家サイト
ConfigMaps
https://kubernetes.io/docs/concepts/configuration/configmap/
Configure a Pod to Use a ConfigMap
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

Opensourcetech by Takahiro Kujirai