Kubernetes Basics

The aim of this article is to be get familiar with creating and interacting with pods inside your Kubernetes cluster.

Pod Specifications

After our cluster is up and running we should create a YAML file containing the pod specifications

apiVersion: v1
kind: Pod
metadata:
  name: web-pod
  labels:
    app: web
spec:
  containers:
  - name: web-server
    image: nginx

Create the pod with “kubectl apply” command using just created YAML template.

kubectl apply -f nginx-pod.yaml

Verify that the pod has been created successfully using “kubectl get pods” command.

kubectl get pods

image

To get the IP address of the NGINX pod, we can use “kubectl describe pod” comand.

kubectl describe pod web-pod

image

Busybox Container

Let’s use Busybox as Linux operating system due to it’s small size and since it’s suitable for our example.

Create a container in a pod running busybox with the following command:

kubectl run --generator=run-pod/v1 -i --tty busybox --image=radial/busyboxplus -- sh

/ # curl <IP of the web-pod>

image

Expose your NGINX pod using a service

apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
  ports:
    - protocol: TCP 
      port: 80
      targetPort: 80

Use kubectl apply to create the service using the above file:

kubectl apply -f filename.yaml

Verify that the service is created

kubectl get service

kubectl describe service web-service

Verify you have access to the NGINX pod using curl command from busybox -pod

kubectl exec -it busybox -- sh

curl <IP of the web-service>

Create Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-depl 
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web-server
          image: nginx:1.7.9

Use kubectl apply command to create the deployment using the above file

kubectl apply -f filename.yaml

Verify that the deployment is created along your NGINX pod

kubectl get deployment

kubectl get pods

kubectl describe deployment web-depl

ReplicaSet

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-depl
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web-server
          image: nginx:1.7.9

Use “kubectl apply”- command to apply the changes to your deployment using the above file:

kubectl apply -f filename.yaml

Verify that new NGINX pods are created and the current, desired, running number has changed to match the requirement.

kubectl get pods

kubectl get replicaset

kubectl describe replicaset <Name of the replicaset>

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  mykey: myvalue
  mykey2: myvalue2

Edit your existing deployment file and map environmental variables to the ConfigMap’s key:value pairs on the container spec - section.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-depl 
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web-server
          image: nginx:1.7.9
          volumeMounts:
          - mountPath: /data
            name: ivailo
          env:
          - name: LAB_ENV_VARIABLE
            valueFrom:
              configMapKeyRef:
                name: nginx-config
                key: mykey
          - name: LAB_ENV_VARIABLE2
            valueFrom:
              configMapKeyRef:
                name: nginx-config
                key: mykey2

Attach to the NGINX pod and verify the data from the ConfigMap is present within the container

kubectl get pod

kubectl exec -it nginx-pod-xxxxx -- /bin/sh

echo $LAB_ENV_VARIABLE

echo $LAB_ENV_VARIABLE2

Create/Modify the following file for deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-depl 
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web-server
          image: nginx:1.7.9
          volumeMounts:
          - mountPath: /data
            name: ivailo
          env:
          - name: LAB_ENV_VARIABLE
            valueFrom:
              configMapKeyRef:
                name: nginx-config
                key: mykey
          - name: LAB_ENV_VARIABLE2
            valueFrom:
              configMapKeyRef:
                name: nginx-config
                key: mykey2
          livenessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
      - name: ivailo
        persistentVolumeClaim:
          claimName: ivailo-pvc

Use “kubectl apply” command to update your deployment

kubectl apply -f filename.yaml

Use kubectl logs - command to tail the NGINX pod’s logs and verify the health - probes are coming in

kubectl logs -l app=web -f

Loadbalancer

Create a YAML file for the loadbalancer

apiVersion: v1
kind: Service
metadata:
  name: nginx-lb 
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
  type: LoadBalancer

Create the loadbalancer using kubectl apply

kubectl apply -f loadbalancer.yml

Get the DNS address of the loadbalancer

kubectl get service

Curl the loadbalancer DNS - address every now and then to validate when it’s ready (it might take up to 5 minutes for an Elastic Loadbalancer to become ready)

curl <LOADBALANCER-URL>

Contents