K8S编排 之 ConfigMap

本文的主线 引入 => 命令 => 文件

引入

  • 回顾
Pod解决了容器的”超亲密关系”设计

Deployment解决了Pod水平扩展的问题

Service解决了Deployment对外访问的问题

Ingress解决了Service负载均衡的问题
  • ConfigMap解决了容器镜像和环境配置解耦的问题

命令

kubectl create configmap config-from-literal --from-literal=hello=world

kubectl get configmap config-from-literal -o yaml
vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: config-test1
spec:
  containers:
    - name: config-test1-container
      image: registry.cn-hangzhou.aliyuncs.com/google_containers/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: ENV_HELLO
          valueFrom:
            configMapKeyRef:
              name: config-from-literal
              key: hello
  restartPolicy: Never
kubectl apply -f pod.yml

kubectl get pods
# NAME           READY   STATUS      RESTARTS   AGE
# config-test1   0/1     Completed   0          17h

kubectl logs config-test1 | grep ENV_HELLO
# ENV_HELLO=world

文件

vim index.html



    
    hello world



    ConfigMap

kubectl create configmap config-from-file --from-file=index.html

kubectl describe configmap config-from-file
vim deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.20.1
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: html-volume
      volumes:
      - name: html-volume
        configMap:
          name: config-from-file
kubectl apply -f deploy.yml

kubectl get pods
# NAME                            READY   STATUS    RESTARTS   AGE
# nginx-deploy-65f94fdb78-78hjk   1/1     Running   0          4s
# nginx-deploy-65f94fdb78-hnbx5   1/1     Running   0          4s
# nginx-deploy-65f94fdb78-k4vlf   1/1     Running   0          4s

kubectl exec -it nginx-deploy-65f94fdb78-78hjk -- /bin/bash

cat /usr/share/nginx/html/index.html
vim service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort
kubectl apply -f service.yml

kubectl get svc
# NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
# nginx-service   NodePort    10.99.50.129           80:31289/TCP   3s
curl http://172.10.66.201:31289/



    
    hello world



    ConfigMap

参考

  • Kubernetes ConfigMap

  • 配置 Pod 使用 ConfigMap

  • k8s mysql 配置文件_k8s使用ConfigMap配置mysql和nginx (13)

你可能感兴趣的:(K8S编排 之 ConfigMap)