Kubernetes – Creating Pods on Windows 10

Posted: July 14, 2019 in kubernetes

A Pod (as in a pod of whales or pea pod) is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers.Kuberenets manages all those pods.

0-0.PNG

Pods needs to be defined in yml files.

Every pod have to have api version. Which API version to use ?

Find out in this blog post.

Below is example pod configuration file-first-pod.yml

Under metadata we simply define pod name, under specs we define container name and image from which this container should be derived (this section is same as when we define docker containers)

apiVersion: v1
kind: Pod
metadata:
  name: webapp
spec:
  containers:
  - name: webapp
    image: image:revision

Deploying pods

First we need to check Kubernetes status

minikube status
kubectl get all

0-1.PNG

If after minkube status command you get

error: the server doesn’t have a resource type “cronjobs”, simple stop minikube VM (from Hyper-V or VirtualBox and start it again-minikube start )

To deply Kubernetes pod run following command:

kubectl apply -f first-pod.yml

1.PNG

If you get error “Invalid object doesn’t have additional properties” 

it means threre is kubectl version mismatch between Docker kubctl version and kubectl version downloaded during kubectl installation. Simple copy kubectl.exe specified in environmental variable

2.PNG

to Docker installation directory

3.PNG

4

Check pod status with

kubectl get all command

5.PNG

to get pod status simple run

kubectl describe pod webapp

6.png

Also note in this output, in last section we can see pod events

7.png

To execute command inside pod (in this example to list folder content) type:

kubectl exec webapp ls
C:\Users\ja\Desktop>kubectl exec webapp ls
bin
dev
etc
home
lib
media
mnt
proc
root
run
sbin
srv
sys
tmp
usr
var

Exposing pods to the “outside” – using Services

Above pod won’t be available outside of minikube VM, so if we try accessing this pod from web browser, it will fail. Pods are “ephemeral”, they are re-created, restarted, they are not permanent.In order to connect to the pod we’re using services.

We can use “labels” to tag Pods and services. Labels are in key pairs format (key:value) – values are arbitratry, so mapping these key pairs between Services and pods we’re actually connecting services to pods. Service will look at key pair combinations and search to pods with correspondig key pairs, if it find one, it will make connection to that pod.

0.PNG

We’ll add key pair to above yml file-first-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: webapp
  labels:
    labelname: webapp
spec:
  containers:
  - name: webapp
    image: image:revision

And new service.yml file:

apiVersion: v1
kind: Service
metadata:
  name: webapp

spec:
  # This defines which pods are going to be represented by this Service
  # The service becomes a network endpoint for either other services
  # or maybe external users to connect to (eg browser)
  selector:
    labelname: webapp

  ports:
    - name: http
      port: 80
      nodePort: 30080

  type: NodePort

ports sections maps external port (80) to pod port (node port). Node port must be any port above 30000.

type: NodePort is used only if we’re hosting Kubernetes minikube VM locally (as in this case), otherwise, we need to use LoadBalancer type

Now apply these files and locate minikube IP

kubectl apply -f first-pod.yml
kubectl apply -f service.yml
minikube ip

In web browser type IP:30080 and you should be able to access pod from the outside.

To show pods type:

kubectl get pod
kubectl get pod --show-labels

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s