Sample Exchange

 View Only

Deploy a Kubernetes Stateful Application on PKS - WordPress 

Nov 01, 2018 08:24 PM

Introduction:

This page provides steps to successfully deploy a Kubernetes Stateful Application on PKS.
The application is WordPress.

This code has been tested on the following environment:

  • PKS 1.2.0
  • NSX-T 2.3

 

Step1: Define a default storage class

file storage-class.yaml:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: standard-sc
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: kubernetes.io/vsphere-volume
parameters:
    diskformat: thin

apply the manifest file:

kubectl apply -f storage-class.yaml

 

Step2: Initiate Persistent Volume Claims for MySQL and WordPress

 

file pvc-mysql.yaml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-volumeclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

Apply the manifest file:

kubectl apply -f pvc-mysql.yaml

 

file pvc-wordpress.yaml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: wordpress-volumeclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

Apply the manifest file:

kubectl apply -f pvc-wordpress.yaml

 

Step3: Set password for MySQL

kubectl create secret generic mysql --from-literal=password=vmware

 

Step4: Deploy MySQL

file mysql.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - image: mysql:5.6
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-volumeclaim

Apply the manifest file:

kubectl apply -f mysql.yaml

 

Step5: Deploy K8s Service for MySQL

 

file svc-mysql.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  type: ClusterIP
  ports:
    - port: 3306
  selector:
    app: mysql

 

Apply the manifest file:

kubectl apply -f svc-mysql.yaml

 

Step6: Deploy WordPress

file wordpress.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
        - image: wordpress
          name: wordpress
          env:
          - name: WORDPRESS_DB_HOST
            value: mysql:3306
          - name: WORDPRESS_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql
                key: password
          ports:
            - containerPort: 80
              name: wordpress
          volumeMounts:
            - name: wordpress-persistent-storage
              mountPath: /var/www/html
      volumes:
        - name: wordpress-persistent-storage
          persistentVolumeClaim:
            claimName: wordpress-volumeclaim

 

Apply the manifest file:

kubectl apply -f wordpress.yaml

 

Step7: Deploy K8s service for WordPress

file svc-wordpress.yaml:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: wordpress

 

Apply the manifest file:

kubectl apply -f svc-wordpress.yaml

 

 

Access WordPress application

Retrieve the IP address of the LB:

# kubectl get svc
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP                 PORT(S)        AGE
kubernetes   ClusterIP      10.100.200.1     <none>                      443/TCP        22h
mysql        ClusterIP      10.100.200.47    <none>                      3306/TCP       2m
wordpress    LoadBalancer   10.100.200.178   10.40.14.86,100.64.112.69   80:30216/TCP   2m

 

Open a web browser and use the following URL:

http://10.40.14.86

You will see this page:

Click on Continue

Fill the requested fields.

Then select install wordpress. You should be able to see:

Click on Log In:

enter admin/VMware1!

 

You will then see:

 

Some check commands

# kubectl get pod
NAME                         READY     STATUS    RESTARTS   AGE
mysql-5bfd5f74dd-zktdn       1/1       Running   0          20m
wordpress-78c9b8d684-bhzkw   1/1       Running   0          19m

 

# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                           STORAGECLASS   REASON    AGE
pvc-29567f0b-de21-11e8-94ed-005056847ca8   20Gi       RWO            Delete           Bound     default/mysql-volumeclaim       standard-sc              20m
pvc-30da5918-de21-11e8-894e-005056849d06   20Gi       RWO            Delete           Bound     default/wordpress-volumeclaim   standard-sc              20m

 

# kubectl get pvc
NAME                    STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-volumeclaim       Bound     pvc-29567f0b-de21-11e8-94ed-005056847ca8   20Gi       RWO            standard-sc    21m
wordpress-volumeclaim   Bound     pvc-30da5918-de21-11e8-894e-005056849d06   20Gi       RWO            standard-sc    20m

 


#TanzuKubernetesGridIntegrated(TKGI)
#OtherLanguage
#Apache2.0
#PivotalContainerService(PKS)

Statistics
0 Favorited
0 Views
1 Files
0 Shares
0 Downloads
Attachment(s)
txt file
snippet.txt   418 B   1 version
Uploaded - Apr 09, 2024

Tags and Keywords

Related Entries and Links

No Related Resource entered.