k8s实战-wordpress博客搭建

k8s版本

[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:12:17Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}

一、yaml文件编写

mysql-db.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: 5Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
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
  • 创建数据库密码
kubectl create secret generic mysql-pass --from-literal='password=123456'
  • 创建足够的PV
    pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: wordpress-mysql-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 5Gi # 根据你创建的pvc大小设置
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/pv/wordpress"

执行创建

kubectl apply -f pv.yaml
  • 创建数据库pod
kubectl apply -f mysql-db.yaml

编写wordpress.yaml

wordpress.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
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 # 对应创建的secretw
              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
  • 创建wordpress pod
kubectl apply -f wordpress.yaml
  • 检查运行情况
kubectl get pod --all-namespaces
[root@k8s-master ~]# kubectl get pod --all-namespaces

NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                wordpress-578744754c-bxq4v                   1/1     Running   3          166m
default                wordpress-mysql-5b697dbbfc-vm9b4             1/1     Running   2          3h21m
kube-system            calico-kube-controllers-7cc97544d-z9mxz      1/1     Running   14         7d20h
kube-system            calico-node-7mc7r                            1/1     Running   0          7d2h
kube-system            calico-node-dpkwx                            1/1     Running   10         7d3h
kube-system            calico-node-zqkd5                            1/1     Running   14         7d20h
kube-system            coredns-674df8b8bd-g662l                     1/1     Running   14         7d20h
kube-system            coredns-674df8b8bd-rv7lr                     1/1     Running   15         7d20h
kube-system            etcd-k8s-master                              1/1     Running   15         7d20h
kube-system            kube-apiserver-k8s-master                    1/1     Running   17         7d20h
kube-system            kube-controller-manager-k8s-master           1/1     Running   49         7d20h
kube-system            kube-proxy-h7mh4                             1/1     Running   15         7d20h
kube-system            kube-proxy-sd5cz                             1/1     Running   0          7d2h
kube-system            kube-proxy-vxm62                             1/1     Running   7          7d3h
kube-system            kube-scheduler-k8s-master                    1/1     Running   41         7d20h
kubernetes-dashboard   dashboard-metrics-scraper-7b8b58dc8b-gtq65   1/1     Running   4          4d23h
kubernetes-dashboard   kubernetes-dashboard-5f5f847d57-ljngx        1/1     Running   4          4d20h

查看services端口

kubectl get services
NAME              TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP      10.96.0.1              443/TCP        7d20h
wordpress         NodePort   10.96.183.43        80:30593/TCP   3h6m
wordpress-mysql   ClusterIP      None                   3306/TCP       3h41m

访问 matster节点IP:30593


image.png

问题排查

  • 创建pod失败
    describe命令执行
kubectl describe pod xxx

问题信息pvc一直处于Pending
kubectl describe pvc

no persistent volumes available for this claim and no storage class is set
Mounted By:
实际为没有合适的pv,创建合适的pv即可。


公众号


公众号.jpg

你可能感兴趣的:(k8s实战-wordpress博客搭建)