本文以 WordPress 為例,演示在 Kubernetes 集群中運行有狀態(tài)服務(wù)乡括,以及相關(guān)命令和概念。
為 MYSQL 密碼創(chuàng)建 Secret
Secret 是一個對象,存儲一系列的敏感信息阻逮,比如 password 和 key。
創(chuàng)建 Secret 對象
kubectl create secret generic mysql-pass --from-literal=password=YOUR-PASSWORD
YOUR-PASSWORD 替換為你想實際應(yīng)用的密碼
查看 Secrets
kubectl get secrets
NAME TYPE DATA AGE
default-token-jdn58 kubernetes.io/service-account-token 3 1d
mysql-pass Opaque 1 6s
部署 MySQL
在 k8s 中秩彤,PersistentVolumeClaims(簡稱 PVC) 和 PersistentVolumes(簡稱PV) 用于數(shù)據(jù)的持久化存儲叔扼。
- PV:持久化卷,是集群的一部分漫雷。就像 Node 一樣瓜富,屬于集群中的資源。
- PVC:用戶對存儲的請求降盹。它與 Pod 相似食呻,Pod 消耗 Node 資源,PVC 消耗 PV 資源澎现。
1、新建 MySQL 部署文件 mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
2每辟、部署 MySQL
kubectl create -f mysql-deployment.yaml
3剑辫、確認 PersistentVolume 是動態(tài)配置的
kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound pvc-22264d66-543c-11e8-993e-080027736407 10Gi RWO standard 1m
4、確認 Pod 正常運行
kubectl get pod
NAME READY STATUS RESTARTS AGE
wordpress-mysql-7ddd5d59dd-njv2b 1/1 Running 0 5m
部署 WordPress
和 MySQL 類似渠欺,WordPress 也需要創(chuàng)建 Deployment 和 Service 妹蔽,也需要 PVC 做持久化存儲、Secret 做密碼保密挠将,另外胳岂,WordPress 需要暴露給集群外部的流量使用,還需要配置 type: NodePort
舔稀。
1乳丰、創(chuàng)建 WordPress 部署文件 wordpress-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: registry.docker-cn.com/library/wordpress:4.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
2、部署 WordPress
kubectl create -f wordpress-deployment.yaml
3内贮、稍作等待产园,查看整個 WordPress 服務(wù),可以看到 MySQL 和 WordPress 均正常工作
NAME READY STATUS RESTARTS AGE
pod/wordpress-5d7d6dfb58-mxr9k 1/1 Running 0 4m
pod/wordpress-mysql-7ddd5d59dd-njv2b 1/1 Running 0 24m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
service/wordpress LoadBalancer 10.107.47.102 <pending> 80:30435/TCP 4m
service/wordpress-mysql ClusterIP None <none> 3306/TCP 24m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/wordpress 1 1 1 1 4m
deployment.apps/wordpress-mysql 1 1 1 1 24m
NAME DESIRED CURRENT READY AGE
replicaset.apps/wordpress-5d7d6dfb58 1 1 1 4m
replicaset.apps/wordpress-mysql-7ddd5d59dd 1 1 1 24m
提醒: Minikube 只能通過 NodePort 暴露服務(wù). EXTERNAL-IP 永遠是 <pending>.
4夜郁、訪問 WordPress
minikube service wordpress
移除 WordPress
刪除 Secret
kubectl delete secret mysql-pass
刪除 Deployment
kubectl delete deployment -l app=wordpress
刪除 Service
kubectl delete service -l app=wordpress
刪除 PVC
kubectl delete pvc -l app=wordpress
-l, --selector='': 選擇器過濾, 不包含那些未初始化的.