Saturday, July 6, 2019

UBUNTU18+KVM+VAGRANT+KUBERNETES 8 Provisioning local disk and wordpress ...



HI.!
This short article shows how to provision and install wordpress  using local-disk provisioning.
In this case, pod shall be running the server(node) that has local-disk(physically).

!!!If you want to know more about local-disk provisioning visit below site!!!
https://kubernetes.io/blog/2019/04/04/kubernetes-1.14-local-persistent-volumes-ga/

Prerequisites) Knowledge about Linux command, docker,k8s and some articles.
  If you don't mind, conveniently you can see below youtubu. Sorry for not recording good quality though.^^'
https://www.youtube.com/channel/UCRGmm0EOwn8qRXigPLuPQig
github)
https://github.com/ohyoungjooung2/u18kvk8s/tree/master/k8s/wordpress

Creating wordpress cms.(storage is local-disk provision)
1)Creating storage class(sc) and persistent volumes for wordpress
2) Create service for wordpress(node port)
3) Create deployment wordpress
4) Connection test


1)Creating storage class(sc) and persistent volumes for wordpress
[vagrant@kubemaster wordpress]$ vi wordpress-sc.yaml
[vagrant@kubemaster wordpress]$ kb create -f wordpress-sc.yaml
storageclass.storage.k8s.io/wordpress-sc created
[vagrant@kubemaster wordpress]$ alias | grep kb
alias kb='kubectl'
[vagrant@kubemaster wordpress]$ cat wordpress-sc.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: wordpress-sc
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

[vagrant@kubemaster wordpress]$ kb describe sc  wordpress-sc
Name:                  wordpress-sc
IsDefaultClass:        No
Annotations:           <none>
Provisioner:           kubernetes.io/no-provisioner
Parameters:            <none>
AllowVolumeExpansion:  <unset>
MountOptions:          <none>
ReclaimPolicy:         Delete
VolumeBindingMode:     WaitForFirstConsumer
Events:                <none>




[vagrant@kubemaster wordpress]$ vi wordpress-pv.yaml
[vagrant@kubemaster wordpress]$ cat wordpress-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wordpress-pv
spec:
  capacity:
    storage: 1Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: wordpress-sc
  local:
    path: /web
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - kubeworker1
[vagrant@kubemaster wordpress]$ kb create -f wordpress-pv.yaml
persistentvolume/wordpress-pv created
[vagrant@kubemaster wordpress]$ kb get pv | grep wordpress
wordpress-pv          1Gi        RWO            Recycle           Available                                                wordpress-sc                 11

2) Create service for wordpress
[vagrant@kubemaster wordpress]$ vi wordpress-svc.yaml
[vagrant@kubemaster wordpress]$ cat wordpress-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress

  type: NodePort
  ports:
  - port: 8070
    nodePort: 32333
    targetPort: 80
    protocol: TCP
    name: http
[vagrant@kubemaster wordpress]$ kb create -f wordpress-svc.yaml
service/wordpress created

 

3) Create  wordpress deployment
[vagrant@kubemaster wordpress]$ cat wp-pvc.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: wordpress-sc
[vagrant@kubemaster wordpress]$ kb create -f wp-pvc.yaml
persistentvolumeclaim/wp-pv-claim created
[vagrant@kubemaster wordpress]$ kb get pvc | grep wp-pv-claim
wp-pv-claim                          Pending                                                   wordpress-sc        9s



[vagrant@kubemaster wordpress]$ vi kustomization.yaml
[vagrant@kubemaster wordpress]$ cat kustomization.yaml
secretGenerator:
- name: mariadb-pass
  literals:
  - password=StrongPass$^^$
resources:
  - wp-dp.yaml

[vagrant@kubemaster wordpress]$ vi wp-dp.yaml
[vagrant@kubemaster wordpress]$ cat wp-dp.yaml
---
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
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: mariadb-master
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mariadb-pass
              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

[vagrant@kubemaster wordpress]$ kb apply -k .
secret/mariadb-pass-hmt2hb8m6g unchanged
deployment.apps/wordpress created
[vagrant@kubemaster wordpress]$ kb get po | grep wordpress
wordpress-57646c5f5f-f7g7l   1/1     Running   0          8s
[vagrant@kubemaster wordpress]$


[vagrant@kubemaster wordpress]$ kb describe svc wordpress
Name:                     wordpress
Namespace:                default
Labels:                   app=wordpress
Annotations:              <none>
Selector:                 app=wordpress
Type:                     NodePort
IP:                       10.97.157.133
Port:                     http  8070/TCP
TargetPort:               80/TCP
NodePort:                 http  32333/TCP
Endpoints:                10.244.2.19:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>


4) Connection test
#Check node ip and port with telnet
[vagrant@kubemaster wordpress]$ telnet 10.1.0.4 32333
Trying 10.1.0.4...
Connected to 10.1.0.4.
Escape character is '^]'.
^C^]
telnet> quit
Connection closed.
[vagrant@kubemaster wordpress]$ telnet 10.1.0.3 32333
Trying 10.1.0.3...
Connected to 10.1.0.3.
Escape character is '^]'.
^]
telnet> q
Connection closed.
[vagrant@kubemaster wordpress]$ telnet 10.1.0.5 32333
Trying 10.1.0.5...
Connected to 10.1.0.5.
Escape character is '^]'.
^]
telnet> q
Connection closed.

[vagrant@kubemaster wordpress]$ kb get svc | grep word
wordpress        NodePort    10.97.157.133   <none>        8070:32333/TCP   92m

[vagrant@kubemaster wordpress]$ curl -v http://10.97.157.133:8070
* About to connect() to 10.97.157.133 port 8070 (#0)
*   Trying 10.97.157.133...
* Connected to 10.97.157.133 (10.97.157.133) port 8070 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.97.157.133:8070
> Accept: */*
>
< HTTP/1.1 302 Found
< Date: Sat, 06 Jul 2019 09:28:01 GMT
< Server: Apache/2.4.10 (Debian)
< X-Powered-By: PHP/5.6.32
< Expires: Wed, 11 Jan 1984 05:00:00 GMT
< Cache-Control: no-cache, must-revalidate, max-age=0
< Location: http://10.97.157.133:8070/wp-admin/install.php
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host 10.97.157.133 left intact




Conclusion)
In this  article, I introduce how to provisioing wordpress deployment and and storages.
In real production, maybe there are lots of other services related with wordpress such as cache servers and so on.


If you know more about deployment, please visit https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

Thanks for reading.

No comments:

Post a Comment