configmap使用-完整的configmap文档

转发      https://www.jianshu.com/p/cf3e2218f283

转发       https://www.kubernetes.org.cn/3138.html


注意:configmap不用也罢



1、通过文件创建,把tomcat的配置文件catalina.sh挂到容器内

创建configmap


从文件中创建

[root@lab2 ceshi]# ls /usr/local/k8s/configmap/file/dandang/
catalina.sh
[root@lab2 ceshi]# kubectl create configmap dandang-config -n development --from-file=/usr/local/k8s/configmap/file/dandang/catalina.sh
[root@lab2 ceshi]# kubectl get configmap -n development
NAME             DATA      AGE
dandang-config   1         2m


获得configmap的yaml文件

[root@lab2 ~]# kubectl get configmap dandang-config -n development -o yaml







从文本中创建,直接指定key的名字,创建后没有yaml文件

[root@lab2 ~]# kubectl create  configmap dandang-configmap -n development --from-file=game-special-key=/usr/local/k8s/configmap/dbconfig.properties


获得configmap详细信息

[root@lab2 ~]# kubectl  describe configmap dandang-config -n development






创建服务,使用configmap

[root@lab2 ceshi]# cat /yunwei/dandang/dandang.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: dandang
  namespace: development
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: dandang
    spec:
      containers:
      - name: dandang
        image: 10.1.1.71:5000/library/dandang.ceshi:v2
        ports:
        - containerPort: 8080
        volumeMounts:
          - name: config-volume
            mountPath: /root/apache-tomcat-8.5.31/bin
      volumes:
        - name: config-volume
          configMap:
            name: dandang-config
---
apiVersion: v1
kind: Service
metadata:
  name: dandang
  namespace: development
spec:
  type: NodePort
  ports: 
  - port: 8080
    targetPort: 8080
  selector:
    name: dandang
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: dandang
  namespace: development
spec:
  rules:
  - host: www.dandang.com      
    http:
      paths:
      - path: /
        backend:
          serviceName: dandang
          servicePort: 8080


[root@lab2 ceshi]# kubectl create -f /yunwei/dandang/dandang.yaml 
replicationcontroller/dandang created
service/dandang created
ingress.extensions/dandang created








2、通过yaml创建


[root@lab2 ceshi]# pwd
/yunwei/ceshi
[root@lab2 ceshi]# ls
configmap.yaml  nginx.yaml



创建configmap

[root@lab2 configmap]# cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo
  namespace: default
data:
  index.html: |
      Hello Everyone





创建服务,使用configmap

[root@lab2 configmap]# cat nginx.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: configmap-demo2
spec:
  template:
    metadata:
      labels:
        app: configmap-demo2
    spec:
      containers:
        - name: configmap-demo2
          image: 192.168.43.65:5000/library/nginx/latest
          ports:
            - containerPort: 80
          volumeMounts:
          - name: config-volume
            mountPath: /usr/share/nginx/html/
      volumes:
        - name: config-volume
          configMap:
            name: configmap-demo


[root@lab2 ceshi]# kubectl create -f .
configmap/configmap-demo created


[root@lab2 ceshi]# kubectl get po -o wide
NAME                    READY     STATUS    RESTARTS   AGE       IP            NODE
configmap-demo2-5wkw6   1/1       Running   0          30s       10.244.5.19   lab3



访问测试:

[root@lab2 ceshi]# curl  10.244.5.19:80
Hello Everyone






更新配置文件

[root@lab2 ceshi]# vi configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo
  namespace: default
data:
  index.html: |
      Hello World!



重新生成下nginx.yaml    注意:不是configmap.yaml

[root@lab2 ceshi]# kubectl create -f nginx.yaml 
Error from server (AlreadyExists): error when creating "nginx.yaml": replicationcontrollers "configmap-demo2" already exists


访问测试:

[root@master change]# curl http://192.168.0.76
Hello World! 





进容器里面看配置文件是否改掉

[root@lab2 ceshi]# kubectl exec -it configmap-demo2-5wkw6 sh
# cd /usr/share/nginx/html/
# ls
index.html
# cat index.html
Hello World!



  
单个文件挂在实例

volumeMounts:
          - name: gitlab-etc
            mountPath: "/etc/gitlab/gitlab.rb"
            subPath: gitlab.rb
          - name: gitlab-data
            mountPath: "/var/opt/gitlab"
      volumes:
        - name: gitlab-etc
          configMap:
            name: gitlab-etc-configmap
            #Add ConfigMap data to a specific path in the Volume
            items:
            - key: gitlab.rb
              path: gitlab.rb

















猜你喜欢

转载自www.cnblogs.com/effortsing/p/10014656.html