IT 3300 : Virtualization

Kubernetes and YAML

YAML

Can provide an easier way to repeat/automate our deployments. Most of the following was referenced from this site.

YAML

  • Convenience: You’ll no longer have to add all of your parameters to the command line
  • Maintenance: YAML files can be added to source control, so you can track changes
  • Flexibility: You’ll be able to create much more complex structures using YAML than you can on the command line

YAML

  • YAML is a superset of JSON
  • A MAP is a key:value pair
  • A LIST is a sequence of objects
  • Don't use tabs

Pod Example

    ---
    apiVersion: v1
    kind: Pod
    metadata:
     name: rss-site
     labels:
       app: web
    spec:
     containers:
       – name: front-end
         image: nginx
         ports:
           – containerPort: 80
       – name: rss-reader
         image: nickchase/rss-php-nginx:v1
         ports:
           – containerPort: 88

YAML

  • API: v1 of the api
  • kind: what we are creating (pod, svc, deployment, ...)
  • metadata: not necessarily required (name, label)
  • spec: What are the actual objects to make up the pod
    • containers
    • storage volumes
    • other properties
  • See here for more information

Action

  • Apply the changes like:

      kubectl create -f pod.yaml
    

or

    kubectl delete -f pod.yaml

YAML Deployment

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      creationTimestamp: null
      labels:
        app: nchase
      name: nchase
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nchase
      strategy: {}
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: nchase
        spec:
          containers:
          - image: nickchase/rss-php-nginx:v1
            name: rss-php-nginx
            resources: {}
    status: {}

YAML Service

    ---
    apiVersion: v1
    kind: Service
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
      name: nginx
    spec:
      ports:
      - name: 80-80
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: nginx
      type: LoadBalancer
    status:
      loadBalancer: {}

Notes

  • kubectl create -f deploy.yml
  • kubectl create -f mysql.yml
  • kubectl port-forward svc/rss-svc 30081:80
  • sometimes it takes 30 seconds for everything to come up, so wait before visiting the install.php page
  • kubectl delete -f deploy.yml
  • kubectl delete -f mysql.yml

Easy yaml creation for a pod

    kubectl run mypod --image=nginx:latest \
        --labels type=web \
        --dry-run=client -o yaml > mypod.yaml

Easy yaml creation for a pod service that exposes a node port

    kubectl expose pod mypod \
        --port=80 \
        --name mypod-service \
        --type=NodePort \
        --dry-run=client -o yaml > mypod-service.yaml

Easy yaml creation

Create a service type nodeport with port 30001 with service to pod TCP port mapping on port 80.

kubectl create service nodeport mypod
--tcp=80:80
--node-port=30001
--dry-run=client -o yaml > mypod-service.yaml

Easy yaml creation

Create a service type nodeport with port 30001 with service to pod TCP port mapping on port 80.

    kubectl create service nodeport mypod \
        --tcp=80:80 \
        --node-port=30001 \
        --dry-run=client -o yaml > mypod-service.yaml

Easy yaml creation

Create a deployment named mydeployment with image Nginx

    kubectl create deployment mydeployment \
        --image=nginx:latest \
        --dry-run=client -o yaml > mydeployment.yaml        

Easy yaml creation

Create a NodePort service YAML for deployment mydeployment with service port 8080

    kubectl expose deployment mydeployment \
        --type=NodePort \
        --port=8080 \
        --name=mydeployment-service \
        --dry-run=client -o yaml > mydeployment-service.yaml