本节内容: configmap, secret
docker run
docker run -it image-name
运行镜像 -> 查看输出docker run -it image-name 5
-> 查看输出apiVersion: v1
kind: Pod
metadata:
name: fortune2s
spec:
containers:
- image: luksa/fortune:args
args: ["2"] # 配置启动时传入的参数, 也可以用 多行 '-' 来配置
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
apiVersion: v1
kind: Pod
metadata:
name: fortune-env
spec:
containers:
- image: luksa/fortune:env
env: # 配置环境变量, k-v形式的数组
- name: INTERVAL
value: "30"
- name: SECOND_VAR
value: "$(INTERVAL)-time" # 值为 "30-time", 可以引用定义的环境变量
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
kubectl get configmap [name -o yaml #输出成yaml]
kubectl delete configmap name
kubectl create -f configmap.yaml
kubectl create configmap myconfigmap --from-literal=foo=bar --from-literal=bar=baz
kubectl create configmap my-config --from-file=ssl.conf
#文件名为K,内容为V,文件名有效才能建kubectl create configmap my-config --from-file=key=ssl.conf
# 可以指定keykubectl create configmap my-config --from-file=configmap-files
#文件名为K,内容为V,文件名有效才能建apiVersion: v1
kind: Pod
metadata:
name: fortune-env-from-configmap
spec:
containers:
- image: luksa/fortune:env
env:
- name: INTERVAL # 环境变量名
valueFrom: # 使用这个字段来定义值的来源
configMapKeyRef:
optional: true # 可以设置为optional,否则有configmap才会启动容器
name: fortune-config
key: sleep-interval
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
apiVersion: v1
kind: Pod
metadata:
name: fortune-env-from-configmap
spec:
containers:
- image: luksa/fortune:env
envFrom: # 使用这个字段声明多个env需要导入
- prefix: CONFIG_ # 所有的key导入后都添加这个前缀
configMapRef:
name: my-config-map # 从map中导入
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
apiVersion: v1
kind: Pod
metadata:
name: fortune-args-from-configmap
spec:
containers:
- image: luksa/fortune:args
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: fortune-config
key: sleep-interval
args: ["$(INTERVAL)"] # 使用环境变量
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
kubectl port-forward fortune-configmap-volume 8080:80 &
curl -H "Accept-Encoding: gzip" -I localhost:8080
# 有开启gzip则成功kubectl exec fortune-configmap-volume -c web-server -- ls /etc/nginx/conf.d
# 查看挂载文件apiVersion: v1
kind: Pod
metadata:
name: fortune-configmap-volume
spec:
containers:
- image: luksa/fortune:env
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: my-config
key: sleep-interval
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
- name: config
mountPath: /etc/nginx/conf.d # 挂在文件到这个目录
# subPath: myconfig.conf # 使用subPath会让文件挂载时只影响当前文件,而不会影响其他。 不用则会导致文件夹下只有这个文件
readOnly: true
- name: config
mountPath: /tmp/whole-fortune-config-volume
readOnly: true
ports:
- containerPort: 80
name: http
protocol: TCP
volumes:
- name: html
emptyDir: {}
- name: config
configMap:
name: my-config
defaultMode: 0660 # configmap默认权限是 644,通过配置defaultMode来修改其权限
items: # 定义哪些key被使用
- key: my-nginx-config.conf # key 名称
path: gzip.conf # value 存储的文件名
configmap 在被更新后会同步文件到pod,但是如果pod不支持重载,那只有新的pod会生效。
同步并不是同步的,所有会不一致的情况。
用items时候不能用subPath
使用subPath挂载时不能接收configMap的更新
kubectl create secret generic fortune-https --from-file=https.key --from-file=https.cert --from-file=foo
#生成https所需的key,cert,以前有教程kubectl create configmap ssl-configmap-2 --from-file=ssl.conf --from-literal=sleep-interval=10
apiVersion: v1
kind: Pod
metadata:
name: fortune-https
spec:
containers:
- image: luksa/fortune:env
name: html-generator
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: ssl-configmap # 从configmap中获取环境变量,key则是configmap中的key名称
key: sleep-interval
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
- name: config
mountPath: /etc/nginx/conf.d
readOnly: true
- name: certs
mountPath: /etc/nginx/certs/
readOnly: true
ports:
- containerPort: 80
- containerPort: 443
volumes:
- name: html
emptyDir: {}
- name: config
configMap:
name: ssl-configmap #挂载configmap
items:
- key: ssl.conf #configmap中的key
path: https.conf
- name: certs
secret:
secretName: fortune-https #挂载secret
测试pod
kubectl port-forward fortune-https 8443:443 &
# 将容器端口放出curl https://localhost:8443 -k -v
# 在kube中直接访问容器,查看连接握手。使用secret设置dockerhub账号来拉取私有镜像
kubectl create secret docker-registry dockerhub-secret --docker-username=myname --docker-password=mypassword [email protected]
apiVersion: v1
kind: Pod
metadata:
name: private-pod
spec:
imagePullSecrets:
- name: dockerhub-secret
containers:
- image: username/private-image:tag
name: main
apiVersion: v1
kind: Pod
metadata:
name: private-pod
spec:
imagePullSecrets:
- name: dockerhub-secret
containers:
- image: username/private-image:tag
name: main
env:
- name: INTERVAL # 环境变量名称
valueFrom:
secretKeyRef:
name: test-secret
key: sleep-interval