先下载实战-在k8s平台部署个人博客-资源包

再K8s部署个人博客

实验步骤如下:

[root@k8s-master]# kubectl create secret generic mysql-pass --from-literal=password=YOUR_PASSWORD

#把mysql.tar.gz和wordpress.tar.gz上传到K8s工作节点,手动解压即可:

[root@k8s-node1]# docker load -i mysql.tar.gz

[root@k8s-node1]# docker load -i wordpress.tar.gz

#部署服务

[root@k8s-master]# kubectl apply -f mysql.yaml

[root@k8s-master]# kubectl apply -f wordpress.yaml

说明:mysql.yaml文件内容

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

wordpress.yaml文件内容

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

#查看wordpress前端的service

[root@k8s-master ~]# kubectl get svc -l app=wordpress

在k8s平台部署个人博客(三)_Deployment

备注;node_ip可以是K8s集群控制节点ip,也可以是工作节点ip

http://192.168.190.61:30090

在k8s平台部署个人博客(三)_mysql_02