The Nautilus DevOps team is working on a Kubernetes template to deploy a web application on the cluster. There are some requirements to create/use persistent volumes to store the application code, and the template needs to be designed accordingly.
- Create a
PersistentVolumenamed aspv-devops. Configure thespecas storage class should bemanual, set capacity to3Gi, set access mode toReadWriteOnce, volume type should behostPathand set path to/mnt/security(this directory is already created, you might not be able to access it directly, so you need not to worry about it). - Create a
PersistentVolumeClaimnamed aspvc-devops. Configure thespecas storage class should bemanual, request1Giof the storage, set access mode toReadWriteOnce. - Create a
podnamed aspod-devops, mount the persistent volume you created with claim namepvc-devopsat document root of the web server, the container within the pod should be named ascontainer-devopsusing imagehttpdwithlatesttag only (remember to mention the tag i.ehttpd:latest). - Create a node port type service named
web-devopsusing node port30008to expose the web server running within the pod.
Create the yaml file, in this example I'm using the name webapp-devops.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-devops
spec:
storageClassName: manual
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/security
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-devops
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: pod-devops
labels:
app: web-devops
spec:
containers:
- name: container-devops
image: httpd:latest
ports:
- containerPort: 80
volumeMounts:
- name: volume-devops
mountPath: /usr/local/apache2/htdocs
volumes:
- name: volume-devops
persistentVolumeClaim:
claimName: pvc-devops
---
apiVersion: v1
kind: Service
metadata:
name: web-devops
spec:
type: NodePort
selector:
app: web-devops
ports:
- port: 80
targetPort: 80
nodePort: 30008
Noted that there was an error when applying the yaml file.
thor@jumphost ~$ kubectl apply -f webapp-devops.yaml
persistentvolume/pv-devops created
persistentvolumeclaim/pvc-devops created
error: error parsing webapp-devops.yaml: error converting YAML to JSON: yaml: line 6: found character that cannot start any tokenTurns out that the yaml file is pretty strict when it comes to formatting. This usually means of the following needs rectifying:
- A stray tab character (\t)
- A colon (:) not followed by a space
- Bad indentation (mix of tabs and spaces)
- Extra invisible characters from copying text
Rewriting the YAML with consistent 2-space indentation fixes it.
Apply the newly formatted yaml file:
thor@jumphost ~$ kubectl apply -f webapp-devops.yaml
persistentvolume/pv-devops unchanged
persistentvolumeclaim/pvc-devops unchanged
pod/pod-devops created
service/web-devops created