1️⃣ 集中部署

1.集中部署wordpress
[root@k8s-master-node1 wordpress]# cat wordpress-all.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: wordpress
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress-app
        image: wordpress:latest
        imagePullPolicy: Never
        env:
        - name: WORDPRESS_DB_NAME
          value: wordpress
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        volumeMounts:
        - name:  wordpress-app
          mountPath: /var/www/html   
      - name: wordpress-mysql
        image: mysql:latest
        imagePullPolicy: Never
        env:
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress
        - name: MYSQL_RANDOM_ROOT_PASSWORD
          value: "1" 
        volumeMounts:
        - name: wordpress-mysql
          mountPath: /var/lib/mysql
      volumes:
      - name: wordpress-app
        hostPath: 
          path: /data/wordpress/app/data/
      - name: wordpress-mysql
        hostPath:
          path: /data/wordpress/mysql/data
  1. 部署wordpress-svc
[root@k8s-master-node1 wordpress]# cat wordpress-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: wordpress
spec:
  selector:
    app: wordpress
  ports:
  - port: 80
    nodePort: 30083
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
spec:
  selector:
    app: wordpress
  ports:
  - port: 3306
  type: ClusterIP

2️⃣ 分离部署

  1. 在master节点上部署NFS服务器,创建共享目录/data/kubernetes,基于该NFS服务器在Kubernetes集群中创建名为nfs-storage的默认动态存储类。基于nfs-storage动态存储类,在Kubernetes集群default命名空间下创建两个PVC,PVC1名称mysql-pvc,PVC2名称wordpress-pvc,大小均为20G,均使用ReadWriteMany(需要使用镜像nfs-provisioner)
##查看nfs-provisioner镜像
[root@k8s-master-node1 separate]# docker images | grep nfs
dyrnq/nfs-subdir-external-provisioner                                    v4.0.2            932b0bface75   2 years ago     43.8MB


##编写权限文档
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: nfs-provisioner-cluster
rules:
- apiGroups: [""]
  resources: ["persistentvolumes"]
  verbs: ["*"]
- apiGroups: [""]
  resources: ["persistentvolumeclaims"]
  verbs: ["*"]
- apiGroups: ["storage.k8s.io"]
  resources: ["storageclasses"]
  verbs: ["*"]
- apiGroups: [""]
  resources: ["events"]
  verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: nfs-provisioner-cluster
subjects:
- kind: ServiceAccount
  name: nfs-client-provisioner
  namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-cluster
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: nfs-proivisioner-role
  namespace: default
rules:
- apiGroups: [""]
  resources: ["endpoints"]
  verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: nfs-provisioner-role
  namespace: default
subjects:
- kind: ServiceAccount
  name: nfs-client-provisioner
  namespace: default
roleRef:
  kind: Role
  name: nfs-proivisioner-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: nfs-storage
parameters:
  archiveOneDelete: "true"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-provisioner
  labels:
    app: nfs-provisioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-provisioner
  template:
    metadata:
      labels:
        app: nfs-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
      - name: nfs-provisioner
        image: dyrnq/nfs-subdir-external-provisioner:v4.0.2
        volumeMounts:
        - name: nfs-mount
          mountPath: /persistentvolumes
        env:
        - name: PROVISIONER_NAME
          value: nfs-storage
        - name: NFS_SERVER
          value: 10.0.109.177
        - name: NFS_PATH
          value: /data/volumes
      volumes:
      - name: nfs-mount
        nfs:
          server: 10.0.109.177
          path: /data/volumes
      
##pvc文档
[root@k8s-master-node1 storageclasss]# cat pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
spec:
  accessModes:
  - ReadWriteMany
  storageClassName: nfs-storage
  resources:
    requests:
      storage: 20Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
  - ReadWriteMany
  storageClassName: nfs-storage
  resources:
    requests:
      storage: 20Gi
##查看provisioner
[root@k8s-master-node1 storageclasss]# kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
nfs-provisioner-97fdd9d79-9llqr   1/1     Running   0          18m

##查看pvc
[root@k8s-master-node1 storageclasss]# kubectl get pvc
NAME            STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pvc       Bound    pvc-30bcc9ff-da87-4cd7-af56-b337e45b8768   20Gi       RWX            nfs-storage    2m38s
wordpress-pvc   Bound    pvc-36cc34c4-445a-49fd-a63b-8c006826b204   20Gi       RWX            nfs-storage    2m38s
  1. 在Kubernetes集群default命名空间下创建一个Opaque类型的Secret,名称为mysql-pass,将数据库密码wordpress进行转码后存入Secret中。完成后提交master节点的用户名、密码和IP地址到答题框。(需要用到的软件包:WordPress.tar.gz)
[root@k8s-master-node1 separate]# cat secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
type: Opaque
stringData:
  password: UEBzc3cwcmQK
  1. 删除名为wordpress的Deployment,将Wordpress和MySQL分别部署。以Deployment方式将MySQL部署到Kubernetes集群default命名空间下,名称为wordpress-mysql,数据库密码使用已创建的Secret(mysql-pass),PVC使用已创建的mysql-pvc。以Deployment方式将WordPress部署到Kubernetes集群default命名空间下,名称为wordpress,PVC使用已创建的worpress-pvc
###mysql,如果我使用secret的时候不晓得为什么不会成功
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: mysql
      labels:
        app: wordpress
    spec:
      containers:
      - name: mysql
        image: mysql:latest
        imagePullPolicy: Never
        env:
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_RANDOM_ROOT_PASSWORD
          value: "1"
        volumeMounts:
        - name: mysql
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql
        persistentVolumeClaim:
          claimName: mysql-pvc
  

###Wordpres,注意wordpress创建如果挂载了pvc的话会很慢,需要耐心等待
[root@k8s-master-node1 separate]# cat wordpress.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-app
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: wordpress-app
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress-app
        image: wordpress:latest
        imagePullPolicy: Never
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_NAME
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
        - name: WORDPRESS_DB_USER
          value: wordpress
        volumeMounts:
        - name: wordpress-app
          mountPath: /var/www/html
      volumes:
      - name: wordpress-app
        persistentVolumeClaim:
          claimName: wordpress-pvc