>K8s实现wordpress项目操作步骤!
K8S 部署机器IP: 10.0.0.11
K8S wordpress 项目需要的容器
[root@techopskr ~]# docker pull mysql:5.7
[root@techopskr ~]# docker tag mysql:5.7  www.harbor.mobi/common_services/mysql:5.7
[root@techopskr ~]# docker push www.harbor.mobi/common_services/mysql:5.7
[root@techopskr ~]# docker pull wordpress:latest
[root@techopskr ~]# docker tag wordpress:latest www.harbor.mobi/common_services/wordpress:latest
[root@techopskr ~]# docker push www.harbor.mobi/common_services/wordpress:latest

>1.【共享目录设置-安装配置NFS挂载】
>1.1 master节点
yum install -y nfs-utils-*
mkdir /data  /code
vim /etc/exports
/data 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
/code 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
systemctl restart rpcbind
systemctl restart nfs
systemctl enable rpcbind
systemctl enable nfs
[root@k8s-m yml]# showmount -e 
/data 10.0.0.0/24
/code 10.0.0.0/24
>1.2 node节点
yum install -y nfs-utils-*
systemctl restart rpcbind
systemctl restart nfs
systemctl enable rpcbind
systemctl enable nfs

>2.【PV的制作-data for MYSQL|code for WORDPRESS】
>2.1 MYSQL的PV
[root@k8s-m yml]# cat  > pv_mysql.yml <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-data
  labels:
    type: pv-mysql
spec:
  capacity:
    storage: 10Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: "/data"
    server: 10.0.0.11
    readOnly: false
EOF
>2.2 Wordpress的PV
[root@k8s-m yml]# cat > pv_wp.yml  <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-code
  labels:
    type: pv-wp
spec:
  capacity:
    storage: 10Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: "/code"
    server: 10.0.0.11
    readOnly: false
EOF
[root@k8s-m yml]# kubectl create -f pv_mysql.yml
[root@k8s-m yml]# kubectl create -f pv_wp.yml
[root@k8s-m yml]# kubectl get pv

>3.【PVC的制作 -data for MYSQL|code for WORDPRESS】
>3.1 MYSQL的PVC
[root@k8s-m yml]# cat > pvc_mysql.yml <<EOF     
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-mysql
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      pv: pv-mysql
EOF
>3.2 Wordpress的PVC
[root@k8s-m yml]# cat > pvc_wp.yml <<EOF     
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-wp
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      pv: pv-wp
EOF
[root@k8s-m yml]# kubectl create -f pvc_mysql.yml 
[root@k8s-m yml]# kubectl create -f pvc_wp.yml  
[root@k8s-m yml]# kubectl get pvc

> 4.【创建 MYSQL 的RC】
> image: docker.io/mysql:5.7 可以通过下载上传到自己的harbor仓库,然后从本地仓库拉去.
[root@k8s-m yml]# cat > mysql-rc.yml <<EOF
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: www.harbor.mobi/common_services/mysql:5.7
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: nfs-vol
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        - name: MYSQL_DATABASE
          value: "wordpress"
        - name: MYSQL_USER
          value: "wordpress"
        - name: MYSQL_PASSWORD
          value: "wordpress"
      volumes:
      - name: nfs-vol
        persistentVolumeClaim:
          claimName: pvc-mysql
EOF
[root@k8s-m yml]# kubectl create -f mysql-rc.yml 

> 5.【创建MYSQL 的 SVC】
[root@k8s-m yml]# cat > mysql-svc.yml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      nodePort: 32345
  selector:
    app: mysql
EOF
[root@k8s-m yml]# kubectl create -f mysql-svc.yml           
[root@k8s-m yml]# kubectl get svc
mysql        10.254.77.254     <none>        3306/TCP       19s          
>备注: 通过docker.io/mysql:5.7 跑起来的mysql SVC 的访问地址为: 10.254.77.254, 然后此地址需要映射到workprocess SVC 中.

> 6.【创建wordpress的RC】
>docker.io/wordpress:latest 可以通过下载上传到自己的harbor仓库,然后从本地仓库拉去.
[root@k8s-m yml]# cat > wp-rc.yml  <<EOF
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: www.harbor.mobi/common_services/wordpress:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nfs-vol
          mountPath: /var/www/html
        env:
        - name: WORDPRESS_DB_HOST
          value: '10.254.77.254'
        - name: WORDPRESS_DB_USER
          value: 'wordpress'
        - name: WORDPRESS_DB_PASSWORD
          value: 'wordpress'
      volumes:
      - name: nfs-vol
        persistentVolumeClaim:
          claimName: pvc-wp
EOF
[root@k8s-m yml]# kubectl create -f wp-rc.yml 

> 6.【创建wordpress的SVC】
[root@k8s-m yml]# cat > wp-svc.yml  <<EOF
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30008
  selector:
    app: myweb
EOF
[root@k8s-m yml]# kubectl create -f wp-svc.yml 

> 7.【访问wordpress】
IE: 10.0.0.11:30008 (虚拟机的IP为:10.0.0.11)
------------------------------------------------------------------------------------------------------------------
 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐