apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
  labels:
    app: wordpress
spec: 
  replicas: 1 
  selector:
    matchLabels:
      app: wordpress
  template: 
    metadata:
      labels:
        app: wordpress
    spec:
      priorityClassName: priority-medium
      # 親和力設定
      affinity:
        podAffinity:
          # 儘可能貼近其他服務
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - mysql
                  - nginx
              topologyKey: "kubernetes.io/hostname"
        podAntiAffinity:
          # 儘可能讓同樣服務分散在其他主機
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - wordpress
              topologyKey: "kubernetes.io/hostname"
      initContainers:
      - name: init-mysql-service
        image: busybox:1.28
        command: ['sh', '-c', 'until nslookup mysql-service; do echo waiting for mysql-service; sleep 2; done']
      containers:    
      - name: wordpress
        image: wordpress:5.8.2-apache
        ports: 
        - containerPort: 80
        resources:
          limits:
            cpu: "500m"
            memory: "500Mi"
          requests:
            cpu: "150m"
            memory: "100Mi" 
        #volumeMounts:
        #- name: wordpress-content
        #  mountPath: /var/www/html/wp-content
        env: 
        - name: WORDPRESS_DB_HOST
          #value: "mysql-service.default.svc.cluster.local:3306"
          value: "mysql-service:3306"
        - name: WORDPRESS_DB_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_USER
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_PASSWORD  
        - name: WORDPRESS_DB_NAME
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_DATABASE
        # 確保MySQL正確啟動，再來啟動WordPress
        livenessProbe:
          tcpSocket:
            host: mysql-service
            port: 3306
          initialDelaySeconds: 60
          periodSeconds: 600
        volumeMounts:
        - mountPath: /var/www/html
          name: wordpress-hostpath
        # 優雅關閉 
        lifecycle:
          preStop:
            exec: 
              command: ["/etc/init.d/apache2", "stop"]
      terminationGracePeriodSeconds: 60
      volumes:
      - name: wordpress-hostpath
        persistentVolumeClaim:
          claimName: wordpress-hostpath
        #hostPath:
        #  path: /run/desktop/mnt/host/e/k8s-vol/wordpress-wp-content
        #  type: DirectoryOrCreate
 