成功最有效的方法就是向有经验的人学习!

Kubernetes使用ConfigMap挂载文件

1,创建文件index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>

<body>
Hello World!
</body>

</html>

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:       <none>
Annotations:  <none>

Data
====
index.html:
----
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>

<body>
Hello World!
</body>

</html>

Events:  <none>

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   <none>        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://<NODE_IP>:30080/index.html

结果为

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>

<body>
Hello World!
</body>

</html>

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

赞(1) 打赏
未经允许不得转载:陈桂林博客 » Kubernetes使用ConfigMap挂载文件
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册