前言:在k8s中,使用一个容器镜像来启动一个container,其实有很多需要配套的问题待解决:
那么这些其实就是一些前置的校验
[root@master01 ~]# echo -n 'admin' > ./username.txt
[root@master01 ~]# echo -n '123456' > ./password.txt
//kubectl create secret指令将用户名密码写到secret中,并在apiserver创建Secret
[root@master01 ~]# kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
secret/db-user-pass created
[root@master01 ~]# kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 5s
[root@master01 ~]# kubectl describe secret db-user-pass
Name: db-user-pass
Namespace: default
Labels:
Annotations:
Type: Opaque
Data
====
password.txt: 6 bytes
username.txt: 5 bytes
[root@master01 ~]# echo -n 'admin' | base64
YWRtaW4=
[root@master01 ~]# echo -n '123456' | base64
MTIzNDU2
[root@master01 demo]# vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MTIzNDU2
[root@master01 demo]# kubectl create -f secret.yaml
secret/mysecret created
[root@master01 demo]# kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 2 5m59s
default-token-z5x9k kubernetes.io/service-account-token 3 30d
mysecret Opaque 2 6s
[root@master01 demo]# kubectl get secret mysecret -o yaml
apiVersion: v1
data:
password: MTIzNDU2
username: YWRtaW4=
kind: Secret
metadata:
creationTimestamp: 2020-05-30T07:26:50Z
name: mysecret
namespace: default
resourceVersion: "646326"
selfLink: /api/v1/namespaces/default/secrets/mysecret
uid: ed5578b0-a246-11ea-9fb4-000c2930d5cb
type: Opaque
//key: username赋值给SECRET_USERNAME
//key: password 赋值给SECRET_PASSWORD
[root@master01 demo]# echo 'MTIzNDU2' | base64 --decode
123456
[root@master01 demo]# ls
configmap.yaml secret-simple.yaml
[root@master01 demo]# cat secret-simple.yaml //为了让pod资源能够使用参数
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
# echo $SECRET_USERNAME
# echo $SECRET_PASSWORD
---
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
# cat /etc/foo/username
# cat /etc/foo/password
[root@master01 demo]# cat configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
---
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.properties: |
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
---
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
[root@master01 demo]# vim secret-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
[root@master01 demo]# kubectl apply -f secret-var.yaml
pod/mypod created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 21s
[root@master01 demo]# kubectl exec -it mypod bash
root@mypod:/# echo $SECRET_USERNAME
admin
root@mypod:/# echo $SECRET_PASSWORD
123456
[root@master01 demo]# vim secret-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
volumeMounts: //指定数据卷名称为foo
- name: foo
mountPath: "/etc/foo" //挂载路径位置
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
[root@master01 demo]# kubectl delete -f secret-var.yaml
pod "mypod" deleted
[root@master01 demo]# kubectl apply -f secret-vol.yaml
pod/mypod created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 26s
//容器看见里面的文件被挂载
[root@master01 demo]# kubectl exec -it mypod bash
root@mypod:/# ls /etc/foo
password username
root@mypod:/# cd /etc/foo
root@mypod:/etc/foo# cat username
admin
root@mypod:/etc/foo# cat password
123456
root@mypod:/etc/foo#
[root@master01 demo]# vim redis.properties //创建JAVA配置文件
redis.host=127.0.0.1
redis.port=6379
redis.password=123456 //写入用作演示的内容
[root@master01 demo]# kubectl create configmap redis-config --from-file=redis.properties
configmap/redis-config created
//查看资源
[root@master01 demo]# kubectl get configmap
NAME DATA AGE
redis-config 1 21s
//也可以用缩写查看
[root@master01 demo]# kubectl get cm
NAME DATA AGE
redis-config 1 95s
[root@master01 demo]# kubectl describe cm redis-config
Name: redis-config
Namespace: default
Labels:
Annotations:
Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
Events:
[root@master01 demo]# vim cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ] //执行一个命令查看redis配置文件
volumeMounts:
- name: config-volume
mountPath: /etc/config //挂载路径
volumes: //挂载的类型和资源名称
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
//删掉之前创建重名的mypod
[root@localhost demo]# kubectl delete pod mypod
pod "mypod" deleted
[root@master01 demo]# kubectl apply -f cm.yaml
pod/mypod created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 0/1 Completed 0 26s
[root@master01 demo]# kubectl logs mypod //查看日志
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
[root@master01 demo]# vim myconfig.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data: //两个参数
special.level: info
special.type: hello
[root@master01 demo]# kubectl apply -f myconfig.yaml
configmap/myconfig created
[root@master01 demo]# vim config-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ] //输出之前创建的两个参数
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
//清除已有的mypod资源
[root@master01 demo]# kubectl delete pod mypod
pod "mypod" deleted
//创建mypod资源
[root@master01 demo]# kubectl apply -f config-var.yaml
pod/mypod created
//查看变量的输出
[root@master01 demo]# kubectl logs mypod
info hello