资源整合


需求
一个mysql         资源控制器:dp
两个个wordpress   资源控制器:dp
172.16.1.31       nfs服务器

实战

MySQL部分

#### mysql的Deployment资源控制清单
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-dp
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
      name: mysql-dp
      namespace: default
    spec:
      nodeSelector:
        kubernetes.io/hostname: node03
      containers:
        - name: mysql-container
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          args:
            - --character-set-server=utf8
            - --collation-server=utf8_general_ci
          livenessProbe:
            exec:
              command: ["/bin/sh", "-c", "mysqladmin -uroot -p123 ping"]
            initialDelaySeconds: 10
            periodSeconds: 3
            timeoutSeconds: 3
            failureThreshold: 3
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: '123'
            - name: MYSQL_DATABASE
              value: 'wordpress'
            - name: MYSQL_USER
              value: 'wp_user'
            - name: MYSQL_PASSWORD
              value: '123'
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-data
          hostPath:
            path: /data/mysql/data
##### mysql 的clusterIP
---
apiVersion: "v1"
kind: "Service"
metadata:
  name: mysql-svc
  namespace: default
spec:
  selector:
    app: mysql
  ports:
  - name: mysql3306
    port: 3306
    protocol: TCP
    targetPort: 3306
  type: ClusterIP

wordpress部分

#### wordpress 的 dp资源清单
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-dp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: wordpress-pod
      namespace: default
      labels:
        app: wordpress
    spec:
      volumes:
        - name: wordpress-data
          nfs:
            server: 172.16.1.31
            path: /data
      containers:
        - name: wordpress-container
          image: wordpress
          imagePullPolicy: IfNotPresent
          readinessProbe:
            tcpSocket: 
              port: 80
              host: mysql-svc
            initialDelaySeconds: 10
            periodSeconds: 1
            timeoutSeconds: 1
            failureThreshold: 3
          livenessProbe:
            tcpSocket: 
              port: 80
            initialDelaySeconds: 3
            periodSeconds: 1
            timeoutSeconds: 1
            failureThreshold: 3
          env:
            - name: WORDPRESS_DB_HOST
              value: mysql-svc
            - name: WORDPRESS_DB_USER
              value: 'wp_user'
            - name: WORDPRESS_DB_PASSWORD
              value: '123'
            - name: WORDPRESS_DB_NAME
              value: 'wordpress'
          volumeMounts:
            - name: wordpress-data
              mountPath: /var/www/html/
##### wp的clusterIP
apiVersion: "v1"
kind: "Service"
metadata:
  name: wp-svc
  namespace: default
spec:
  selector:
    app: wordpress
  ports:
  - name: wp80
    port: 80
    protocol: TCP
    targetPort: 80
  type: ClusterIP
  

###### wp的HPA
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: wp-HPA
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 2
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: wordpress-dp
  targetCPUUtilizationPercentage: 50
##### wp的ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: wordpress-ingress

spec:
  rules:
    - host: www.wp.com
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: wp-svc
                port:
                  number: 80

! 不能加就绪性探针  目前问题未解决

image-20240925153232067

nfs相关配置

### 刚开始写入文件的用户 后期发生了更改 不能锁死IP
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)


chown 65534.65534 /data -R

[root@nfs ~]# cat /var/lib/nfs/etab 
/data   172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
因为要上传文件   不同版本要求不同 禁用 api即可

https://downloads.wordpress.org/plugin/disable-wp-rest-api.2.6.3.zip

image-20240925160539854

image-20240925160432796