在K8s平台部署个人博客
实验步骤
python
kubectl create secret generic mysql-pass --from-literal=password=YOUR_PASSWORD
把mysql.tar.gz和wordpress.tar.gz上传到K8s工作节点,手动解压即可:
通过网盘分享的文件:wordpress.tar.gz
链接: https://pan.baidu.com/s/1nISFKNIyANvcuBAWE9MAMQ?pwd=u77k 提取码: u77k
通过网盘分享的文件:mysql.tar.gz
链接: https://pan.baidu.com/s/1NVw3fULvQ3NOAAKtjU7nOw?pwd=8r2v 提取码: 8r2v
python
docker load -i mysql.tar.gz
docker load -i wordpress.tar.gz
ctr --n=k8s.io images import mysql.tar.gz
ctr --n=k8s.io images import wordpress.tar.gz
部署服务
python
cat mysql.yaml
python
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
replicas: 1
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
hostPath:
path: /datawordpress
type: DirectoryOrCreate
python
cat wordpress.yaml
python
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
type: NodePort
ports:
- port: 80
nodePort: 30090
selector:
app: wordpress
tier: frontend
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
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: 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
hostPath:
path: /datamysqlwordpress
type: DirectoryOrCreate
python
kubectl apply -f mysql.yaml
kubectl apply -f wordpress.yaml
查看wordpress前端的service
python
kubectl get svc -l app=wordpress
python
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 10.110.119.68 <none> 80:30090/TCP 36s
wordpress-mysql ClusterIP None <none> 3306/TCP 43s