실습
Environment Variable
ConfigMap 생성
kubectl create configmap echo --from-literal=message="hello kubernetes"
생성된 ConfigMap 확인
kubectl get cm echo -o yaml
Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: echo spec: containers: - image: alpine name: echo command: [ "/bin/sh", "-c" ] args: [ "while true; echo \$MESSAGE; do sleep 5; done;" ] env: - name: MESSAGE valueFrom: configMapKeyRef: name: echo key: message EOF
Pod 로그 확인
kubectl logs echo
ConfigMap 수정
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: ConfigMap metadata: name: echo data: message: goodbye kubernetes EOF
수정한 ConfigMap 확인
kubectl get cm echo -o yaml
Pod 로그 확인
kubectl logs echo
Pod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: echo spec: containers: - image: alpine name: echo command: [ "/bin/sh", "-c" ] args: [ "while true; echo \$MESSAGE; do sleep 5; done;" ] env: - name: MESSAGE valueFrom: configMapKeyRef: name: echo key: message EOF
Pod 로그 확인
kubectl logs echo
ConfigMap 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: ConfigMap metadata: name: env data: DB_URL: db.example.com DB_NAME: mydb DB_USER: admin EOF
Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: env spec: containers: - image: alpine name: env command: [ "env" ] envFrom: - configMapRef: name: env restartPolicy: Never EOF
Pod 로그 확인
kubectl logs env
생성한 리소스 삭제
{ kubectl delete pod echo env kubectl delete cm echo env }
Volume
HTML 파일 생성
cat > index.html <<EOF hello world EOF cat > home.html <<EOF hello home EOF
ConfigMap 생성
kubectl create configmap www --from-file=index.html --from-file=home.html
생성된 ConfigMap 확인
kubectl get cm www -o yaml
Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - image: nginx name: nginx volumeMounts: - name: html mountPath: /usr/share/nginx/html volumes: - name: html configMap: name: www EOF
ConfigMap을 마운트한 경로에 있는 파일 확인
kubectl exec nginx -- ls /usr/share/nginx/html
배포한 웹서버를 호출해서 인덱스 페이지 확인
kubectl exec nginx -- curl -s localhost
ConfigMap에 데이터 추가
cat <<EOF | kubectl replace -f - apiVersion: v1 kind: ConfigMap metadata: name: www data: home.html: | hello home index.html: | hello world 404.html: | Not Found EOF
업데이트된 ConfigMap 확인
kubectl get cm www -o yaml
ConfigMap을 마운트한 경로에 있는 파일 확인 - 수정분이 반영되는지 시간이 걸릴수 있음
kubectl exec nginx -- ls /usr/share/nginx/html
ConfigMap에 데이터 변경
cat <<EOF | kubectl replace -f - apiVersion: v1 kind: ConfigMap metadata: name: www data: home.html: | hello home index.html: | hello kubernetes 404.html: | Not Found EOF
업데이트된 ConfigMap 확인
kubectl get cm www -o yaml
배포한 웹서버를 호출해서 인덱스 페이지 확인
kubectl exec nginx -- curl -s localhost
Pod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - image: nginx name: nginx volumeMounts: - name: html mountPath: /usr/share/nginx/html volumes: - name: html configMap: name: www items: - key: 404.html path: index.html EOF
ConfigMap을 마운트한 경로에 있는 파일 확인
kubectl exec nginx -- ls /usr/share/nginx/html
배포한 웹서버를 호출해서 인덱스 페이지 확인
kubectl exec nginx -- curl -s localhost
생성한 리소스 삭제
{ kubectl delete pod nginx kubectl delete cm www rm index.html home.html }
Last updated