Kubernetes使用ConfigMap挂载文件

1,创建文件index.html





helloworld



Hello World!



2,创建configmap,里面的index.html内容设为步骤1的index.html

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

3,查看configmap index-html的内容

kubectl describe configmap index-html

内容如下

Name:         index-html
Namespace:    default
Labels:       
Annotations:  

Data
====
index.html:
----




helloworld



Hello World!




Events:  

4,创建deployment和service的yaml文件,sample-deploy-svc.yaml,内容如下

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: sample-volume
      volumes:
      - name: sample-volume
        configMap:
          name: index-html
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-svc
  namespace: default
spec:
  ports:
  - nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort

5,创建deployment和service

apply -f sample-deploy-svc.yaml

6,查看pod,service,deployment,replicaset。

kubectl get all

查看结果

NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deploy-8577cb6-2l4k8          1/1     Running   0          2m7s

NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/nginx-svc          NodePort    10.96.253.239           80:30080/TCP   2m7s

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deploy       1         1         1            1           2m9s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deploy-8577cb6          1         1         1       2m9s

7,访问nginx的index.html

curl http://:30080/index.html

结果为





helloworld



Hello World!



说明index.html通过configmap成功发布到nginx容器里了。

结尾!

你可能感兴趣的:(Kubernetes)