在GKE上创建一个WordPress环境【vol.2:尝试在ConfigMap中编写环境变量!】
您好,
我是Class Act基礎架構部門的大塚。
上次,我們在GKE上構建了一個簡單的WordPress環境。
由於未使用PV/PVC、ConfigMap、Secrets等,這個配置絕對不適合生產環境,但對於剛開始接觸Kubernetes並進行學習的人來說,這是一個相當不錯的環境。
这次的配置与之前一样,但是我想要停止直接在yaml文件中写入环境变量,并将其转移到ConfigMap中。
建立环境
预先准备的文件清单
★在上一次之后没有差异的文件不作列举。
wordpress-configmap.yaml 的中文释义为「WordPress配置映射文件」。
在Kubernetes中,将wordpress-configmap.yaml传递给系统时,ConfigMap的名称将以metadata.name中指定的名字生成,本例中为”wp-cm”。
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
WORDPRESS_DB_HOST: mysql-clusterip
WORDPRESS_DB_USER : devuser
WORDPRESS_DB_PASSWORD : password
WORDPRESS_DB_NAME : gke-wordpress-database
mysql-配置映射.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
MYSQL_USER : devuser
MYSQL_PASSWORD : password
MYSQL_ROOT_PASSWORD : password
MYSQL_DATABASE : gke-wordpress-database
华语版本:
wordpress-pod.yaml 的同义表达
“wp-cm”在spec.containers.envFrom.configMapRef.name中指的是通过wordpress-configmap.yaml部署的ConfigMap。
apiVersion: v1
kind: Pod
metadata:
name: wordpress-pod
labels:
app: wordpress
spec:
containers:
- name: wordpress-con
image: wordpress:php8.1-apache
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: wp-cm
mysql-pod.yaml文件
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
app: mysql
spec:
containers:
- name: mysql-con
image: mysql:8.0-debian
ports:
- containerPort: 3306
envFrom:
- configMapRef:
name: mysql-cm
建立
我们将从ConfigMap部署。
$ kubectl apply -f wordpress-configmap.yaml
configmap/wp-cm created
$ kubectl apply -f mysql-configmap.yaml
configmap/mysql-cm created
在Kubernetes上,可以像获取pod等资源一样,使用kubectl get或describe命令获取ConfigMap。
$ kubectl get cm -o wide
NAME DATA AGE
kube-root-ca.crt 1 11m
mysql-cm 4 46s
wp-cm 4 75s
$ kubectl describe cm wp-cm
Name: wp-cm
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
WORDPRESS_DB_HOST:
----
mysql-clusterip
WORDPRESS_DB_NAME:
----
gke-wordpress-database
WORDPRESS_DB_PASSWORD:
----
password
WORDPRESS_DB_USER:
----
devuser
BinaryData
====
Events: <none>
$ kubectl describe cm mysql-cm
Name: mysql-cm
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
MYSQL_DATABASE:
----
gke-wordpress-database
MYSQL_PASSWORD:
----
password
MYSQL_ROOT_PASSWORD:
----
password
MYSQL_USER:
----
devuser
BinaryData
====
Events: <none>
逐步部署Pod、Service等组件
$ kubectl apply -f wordpress-pod.yaml
Warning: Autopilot set default resource requests for Pod default/wordpress-pod, as resource requests were not specified. See http://g.co/gke/autopilot-defaults
pod/wordpress-pod created
$ kubectl apply -f mysql-pod.yaml
Warning: Autopilot set default resource requests for Pod default/mysql-pod, as resource requests were not specified. See http://g.co/gke/autopilot-defaults
pod/mysql-pod created
$ kubectl apply -f mysql-clusterip.yaml
service/mysql-clusterip created
$ kubectl apply -f wordpress-loadbalancer.yaml
service/wordpress-loadbalancer created
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/mysql-pod 1/1 Running 0 2m37s 10.112.0.130 gk3-my-pool-1-51c2fff1-cf9x <none> <none>
pod/wordpress-pod 1/1 Running 0 3m4s 10.112.0.131 gk3-my-pool-1-51c2fff1-cf9x <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.112.128.1 <none> 443/TCP 16m <none>
service/mysql-clusterip ClusterIP 10.112.128.189 <none> 3306/TCP 2m15s app=mysql
service/wordpress-loadbalancer LoadBalancer 10.112.129.17 34.72.240.175 60000:31724/TCP 2m1s app=wordpress
我也确认了可以在Web浏览器中访问和操作。
另一种导入 ConfigMap 的方法
对于 wordpress-pod.yaml 和 mysql-pod.yaml,不仅限于上述方法,以下的写法也可以接受。
上述方法是将整个 ConfigMap 导入的方式。以下的写法是逐个导入 ConfigMap 的每个环境变量。如果 ConfigMap 中存在不需要的环境变量,或者需要从多个 ConfigMap 导入环境变量的情况下,我认为应该使用这种方法。
apiVersion: v1
kind: Pod
metadata:
name: wordpress-pod
labels:
app: wordpress
spec:
containers:
- name: wordpress-con
image: wordpress:php8.1-apache
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_HOST
- name: WORDPRESS_DB_USER
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_USER
- name: WORDPRESS_DB_PASSWORD
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_PASSWORD
- name: WORDPRESS_DB_NAME
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_NAME
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
app: mysql
spec:
containers:
- name: mysql-con
image: mysql:8.0-debian
ports:
- containerPort: 3306
env:
- name: MYSQL_USER
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_USER
- name: MYSQL_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_PASSWORD
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_ROOT_PASSWORD
- name: MYSQL_DATABASE
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_DATABASE