Kubernetes Template应用部署
yaml文件等基于: https://github.com/melodyfff/kubernetes-template
Deployment运行一个无状态项目
https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/
1 2 3 4 5 6 kubectl apply -f deployment-nginx-create.yaml kubectl apply -f deployment-nginx-update.yaml kubectl apply -f deployment-nginx-scale.yaml
创建ConfigMap
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 kubectl create configmap game-config --from-file=configmap/ kubectl describe configmaps game-config kubectl get configmaps game-config -o yaml kubectl create configmap game-config-2 --from-file=configmap/game.properties kubectl describe configmaps game-config-2 kubectl create configmap game-config-2 --from-file=configmap/game.properties --from-file=configmap/ui.properties kubectl create configmap game-config-env-file \ --from-env-file=configmap/game-env-file.properties kubectl get configmap game-config-env-file -o yaml kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file> kubectl create configmap game-config-3 --from-file=game-special-key=configmap/game.properties kubectl get configmaps game-config-3 -o yaml kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm cat <<EOF >configmap/kustomization.yaml configMapGenerator: - name: game-config-4 files: - game.properties #- game-special-key=game.properties EOF kubectl apply -k configmap/
使用ConfigMap数据定义容器环境变量
在ConfigMap
中将环境变量定义为键值对:1 kubectl create configmap special-config --from-literal=special.how=very
将ConfigMap
中定义的special.how
值分配给Pod
规范中的SPECIAL_LEVEL_KEY
环境变量
pod-single-configmap-env-variable.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: busybox:1.35.0 command: [ "/bin/sh" , "-c" , "env" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how restartPolicy: Never
创建 Pod:
1 kubectl create -f pod-single-configmap-env-variable.yaml
将ConfigMap 中的所有键值对配置为容器环境变量 创建 ConfigMap1 kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml
使用envFrom
将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称
1 kubectl create -f pod-configmap-envFrom.yaml
将ConfigMap数据添加到数据卷中的特定路径 1 2 3 4 5 kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml wget https://kubernetes.io/examples/pods/pod-configmap-volume-specific-key.yaml -O pod-configmap-volume-specific-key.yaml kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-volume-specific-key.yaml
参考 Minikube Document
K8S官网文档