Kubernetes – Deployments

Posted: July 17, 2019 in kubernetes

In previous article we created Kubernetes ReplicaSets, in this one we’ll use Deployments, it’s advanced version of ReplicaSets, it rolls out pods updates withh zero (0) downtime.Deployments create ReplicaSets,services and pods automatically.

As in previous example,we have 2 files: pods.yml and services.yml

pods.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  # minReadySeconds: 30
  selector:
    matchLabels:
      app: webapp
  replicas: 2
  template: # template for the pods
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: image_name:release0

services.yml

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:
    app: webapp

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

  type: NodePort

1.PNG

As we can see, deployment is created

2.PNG

Monitoring deployment

kubectl rollout status deployment webapp

With above commands we can see deployment status.

3.PNG

To see deployment version run following command

kubectl rollout history deployment webapp

4.PNG

Rolling back deployments

In emeregency, we can rollback to previous pod version by using following command

kubectl rollout undo deployment webapp --to-revision=2

 

5.PNG

Advertisements

Kubernetes – ReplicaSet

Posted: July 16, 2019 in kubernetes

In previus article we created Kubernetes pods, the disadvantage of this approach is that once pod fails for any reason, application running on that pod will fail. ReplicaSet simple will create new pod in case one goes down.In this example ReplicaSet will ensure that one pod is always running.

1.PNG

As in previous example we’ll need 2 files, one for pod and one for service (maps pod using key pair-selectors).

pod.yml:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp
spec:
  selector:
    matchLabels:
      app: webapp
  replicas: 1
  template: # template for the pods
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: some_image:release0

service.yml

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:
    app: webapp

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

  type: NodePort

After applying these changes, in case any pod fails, new one will be created automatically.

 

kubectl apply -f pods.yml
kubectl apply -f service.yml

 
2.PNG

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

In previous post we installed Minkube VM on Windows 10 Professonal machine.

Minikube VM has Docker deamon installed by default.

In this one we’ll install Docker on Windows 10 and “map” local docker commands to Minikube Docker deamon.

0.PNG

Installing Docker for Windows

Create account on hub.docker.com and install Docker Desktop for Windows 

Once docker is instaled, if we run docker image ls

0.PNG

As we can see from output, only images available on host machine are available.

To list docker images located on minikube VM (which has docker deamon installed by default) we need to do following:

On Windows 10 machine run following command:

minikube docker-env

We’re getting following output

C:\WINDOWS\system32>minikube docker-env
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.0.18:2376
SET DOCKER_CERT_PATH=C:\Users\ja\.minikube\certs
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('minikube docker-env') DO @%i

We just need to copy last line (excpet REM) and paste it in new line in CMD

@FOR /f "tokens=*" %i IN ('minikube docker-env') DO @%i

0.PNG

If we now type docker image ls
we’ll get images located on minikube VM

C:\WINDOWS\system32>docker image ls

0.PNG

Prerequisites:

Hyper-V or Virtual Box

Note: If using Windows 10 Home, then Hyper-V is not an option.Virtual Box needs to be used instead, in this exampe Hyper-V will be used.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

0.PNG

Reboot machine

Installing kubectl and minikube

kubectl is controller program for kubernetes, minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM)

new-item c:\Kubernetes\ -type directory
Invoke-WebRequest https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/
amd64/kubectl.exe -OutFile "C:\Kubernetes\kubectl.exe"

Invoke-WebRequest https://github.com/kubernetes/minikube/releases/latest/download/minikube-installer.exe -OutFile C:\users\ja\download\minikube.exe

Add kubectl.exe file path to environmental variable

Capture.PNG

Open CMD and type  kubectl version

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.0", GitCommit:"e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529", GitTreeState:"clean", BuildDate:"2019-06-19T16:32:14Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

Installing minikube

Double click on minikube.exe (dowloaded in previous step) and install it

0.PNG

After installation is done, open CMD and type minikube, output should be as below:

 

4.PNG

Create Hyper-V switch for minikube VM

Click Virtual Switch Manager from Actions pane

1.png

Type-External

1-2.PNG

Check External network and Allow management operating system to share this network adapter.

1-3.PNG

Install minikube VM

Open comand prompt as administrator

Specify vm driver and hyper-v switch

minikube start --vm-driver "hyperv" --hyperv-virtual-switch "kubernetes_switch"

5.PNG

If all went fine, VM should be installed

6.PNG

default username is root (password is not needed)

7.PNG

SSH connection to minikube Virtula machine

If using SSH connection username is docker, password tcuser

To find out VMs IP address type

kubectl config view

 or

minikube ip

0.PNG

Today i wasn’t able to create VM from OVA/OVF template, i tried Chrome and Mozilla Firefox (newest versions)

Although VMware client intergration plugin was installed (and recognized by both browsers)

1.PNG

I still was getting:The client Intergration Plug-in must be installed to enable OVF functionality and pop up The VMware Client Integration Plugin has udated its SSL certificate in Firefox.Please restart Firefox, even when i was using Chrome

2.PNG

The only solution i was able to find was to:

Untitled

  • Install The client Integration Plug-in
  • Start Mozilla
  • Disable Updates:
  1. Select the “Menu” Menu Button button in the upper-right corner, then choose “Options“.
  2. Select “General” on the left pane.
  3. Click “Advanced” then “Update”
  4. Select Never check for updates

4

Locate firefox.exe and double click on it

 

3.png

Then i logged in to vSphere,again allowed Intergration plug in and finally was able to deploy VM from template

 

Microsoft Key Management Services (KMS) provides a way to automatically activate volume license editions of Microsoft Windows and Microsoft Office.

Detecting KMS servers

From Command prompt type:

nslookup -type=srv _vlmcs._tcp.test.com

test.com is domin name

Output should be something like below:

_vlmcs._tcp.test.com SRV service location:
priority = 0
weight = 100
port = 1688
svr hostname = kms.test.com

If this test fails, you will need to have the following DNS record added to the DNS zone:

_vlmcs._tcp.test.com. 3600 IN SRV 0 100 1688 kms.test.com

Activating Windows

Open a command prompt with elevation

Install default product key (bellow is example for Windows Server 2016 datacenter)

cscript.exe c:\windows\system32\slmgr.vbs /ipk WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY

For other keys refer to this link

Run the following command to point Windows to the KMS server.

cscript c:\windows\system32\slmgr.vbs -skms kms.test.com

Run the following command to activate Windows.

cscript c:\windows\system32\slmgr.vbs -ato

Run the following command to check activation staus

slmgr.vbs -dlv

Untitled.png

In case you try activating evaluation version of Windows server 2016 if trying above steps you’ll get following error:

Error: 0xC004F069 On a computer running Microsoft Windows non-core edition, run ‘slui.exe 0x2a 0xC004F069’ to display the error text.

In this case run following commands:

Find available target editions

DISM.exe /Online /Get-TargetEditions

Change your target edition (bellow is example for Windows server 2016 datacenter)

DISM /online /Set-Edition:ServerStandard /ProductKey:WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY /AcceptEula

Reboot server, repeat above steps and you should be good to go