109
Deploying Windows Containers with Kubernetes @Ben_Hall [email protected] Katacoda.com

Deploying windows containers with kubernetes

Embed Size (px)

Citation preview

Page 1: Deploying windows containers with kubernetes

Deploying Windows Containers with

Kubernetes

@[email protected]

Page 2: Deploying windows containers with kubernetes
Page 3: Deploying windows containers with kubernetes

Deploying Windows Containers with

Kubernetes

@[email protected]

Page 4: Deploying windows containers with kubernetes

WH

O A

M I?

Page 5: Deploying windows containers with kubernetes

Learn via Interactive Browser-Based LabsKatacoda.com

Page 6: Deploying windows containers with kubernetes

Agenda

• Building Windows Containers

• Deploying Containers on Kubernetes

• Operating Kubernetes

Page 7: Deploying windows containers with kubernetes

> docker run -e 'ACCEPT_EULA=Y’ \

-e 'SA_PASSWORD=yourStrong133tPassword' \

-p 1433:1433 microsoft/mssql-server-linux

This is an evaluation version. There are [154] days left in the evaluation period.

2017-06-09 22:16:14.27 Server Setup step is copying system data file 'C:\templatedata\master.mdf' to '/var/opt/mssql/data/master.mdf'.

2017-06-09 22:16:14.36 Server Setup step is copying system data file 'C:\templatedata\mastlog.ldf' to '/var/opt/mssql/data/mastlog.ldf'.

2017-06-09 22:16:14.37 Server Setup step is copying system data file 'C:\templatedata\model.mdf' to '/var/opt/mssql/data/model.mdf'.

2017-06-09 22:16:14.40 Server Setup step is copying system data file 'C:\templatedata\modellog.ldf' to

'/var/opt/mssql/data/modellog.ldf'.

2017-06-09 22:16:14.42 Server Setup step is copying system data file 'C:\templatedata\msdbdata.mdf' to

'/var/opt/mssql/data/msdbdata.mdf'.

2017-06-09 22:16:14.45 Server Setup step is copying system data file 'C:\templatedata\msdblog.ldf' to '/var/opt/mssql/data/msdblog.ldf'.

2017-06-09 22:16:14.57 Server Microsoft SQL Server 2017 (CTP2.1) - 14.0.600.250 (X64)

May 10 2017 12:21:23

Copyright (C) 2017 Microsoft Corporation. All rights reserved.

Developer Edition (64-bit) on Linux (Ubuntu 16.04.2 LTS)

2017-06-09 22:16:14.58 Server UTC adjustment: 0:00

2017-06-09 22:16:14.58 Server (c) Microsoft Corporation.

2017-06-09 22:16:14.58 Server All rights reserved.

2017-06-09 22:16:14.58 Server Server process ID is 4116.

Page 8: Deploying windows containers with kubernetes

> docker run -it microsoft/azure-cli bashd5f51519a9b1:/# azure

info: _ _____ _ ___ ___

info: /_\ |_ / | | | _ \ __|

info: _ ___/ _ \__/ /| |_| | / _|___ _ _

info: (___ /_/ \_\/___|\___/|_|_\___| _____)

info: (_______ _ _) _ ______ _)_ _

info: (______________ _ ) (___ _ _)

info:

info: Microsoft Azure: Microsoft's Cloud Platform

info:

info: Tool version 0.10.11

Page 9: Deploying windows containers with kubernetes

Building ContainerisedASP.NET Core app

Page 10: Deploying windows containers with kubernetes

> cat Program.cs

namespace dotnetapp {

public class Program {

public static void Main(string[] args) {

var host = new WebHostBuilder()

.UseKestrel()

.UseContentRoot(Directory.GetCurrentDirectory())

.UseUrls("http://0.0.0.0:5000")

.UseIISIntegration()

.UseStartup<Startup>()

.Build();

host.Run();

}

}

}

> cat Startup.cs

namespace dotnetapp {

public class Startup {

public void Configure(IApplicationBuilderapp, IHostingEnvironment env, ILoggerFactoryloggerFactory) {

loggerFactory.AddConsole();

app.Run(async (context) => {

await context.Response.WriteAsync("Request processed by " + System.Environment.MachineName);

});

}

}

}

Page 11: Deploying windows containers with kubernetes

$ cat Dockerfile

FROM microsoft/dotnet:1.1.1-sdk

Page 12: Deploying windows containers with kubernetes

$ cat Dockerfile

FROM microsoft/dotnet:1.1.1-sdk

RUN mkdir /app

WORKDIR /app

COPY dotnetapp.csproj /app/

RUN dotnet restore

COPY . /app

RUN dotnet publish -c Release -o out

EXPOSE 5000/tcp

CMD ["dotnet", "out/dotnetapp.dll"]

Page 13: Deploying windows containers with kubernetes

$ docker build -t katacoda/dotnet-example:v1 .Sending build context to Docker daemon 137.7 kB

Step 1/11 : FROM microsoft/dotnet:1.1.1-sdk

Step 2/11 : RUN mkdir /build

Step 3/11 : WORKDIR /build

Step 4/11 : COPY dotnetapp.csproj .

Step 5/11 : RUN dotnet restore

Step 6/11 : COPY . .

Step 7/11 : RUN dotnet publish -c Release -o out

---> Running in fcde25425eee

Microsoft (R) Build Engine version 15.1.548.43366

Copyright (C) Microsoft Corporation. All rights reserved.

dotnetapp -> /build/bin/Release/netcoreapp1.1/dotnetapp.dll

Step 8/11 : FROM microsoft/dotnet:1.1.1-runtime

Step 9/11 : WORKDIR /app

Step 10/11 : CMD dotnet dotnetapp.dll

Step 11/11 : COPY --from=0 /build/out /app/

Successfully built 1dbec44d4150

Successfully tagged katacoda/dotnet-example:v1.1

Page 14: Deploying windows containers with kubernetes

$ docker run -d \

-t -p 5000:5000 \

--name app \

katacoda/dotnet-example:v1

$ curl dockerhost:5000

Request processed by zb8gh24wva

Page 15: Deploying windows containers with kubernetes
Page 16: Deploying windows containers with kubernetes

# First StageFROM microsoft/dotnet:1.1.1-sdkWORKDIR /appCOPY dotnetapp.csproj /app/RUN dotnet restoreCOPY . /app/RUN dotnet publish -c Release -o out

# Second StageFROM microsoft/dotnet:1.1.1-runtimeWORKDIR /appCMD ["dotnet", "dotnetapp.dll”]COPY --from=build out /app/

Page 17: Deploying windows containers with kubernetes

Optimised Image

REPOSITORY TAG IMAGE ID CREATED SIZE

katacoda/dotnet-example v1 d69cf725c406

5 seconds ago 266.3 MB

katacoda/dotnet-example v1 b8f8b523d3ca

6 minutes ago 894.7 MB

Page 18: Deploying windows containers with kubernetes

http://windows-wallpapers.net/wp-content/uploads/images/1c/windows-98.png

Page 19: Deploying windows containers with kubernetes

Windows Server Core

Windows Nano

Windows Containers

Windows Hyper-V

Containers

Page 20: Deploying windows containers with kubernetes

Windows Containers

Windows Kernel

Windows Server 2016

SQL Server

MSMQIIS /

ASP.NET

Docker Engine

Page 21: Deploying windows containers with kubernetes

Windows Hyper-V Containers

Windows Kernel

Windows Server 2016

SQL Server

MSMQIIS /

ASP.NET

Windows Kernel

Windows Utility VM

Hyper-V

Docker Engine

Page 22: Deploying windows containers with kubernetes
Page 23: Deploying windows containers with kubernetes

Windows Server Core

• Nearly Win32 Compatible

• Same behaviour of Windows

• Install all of the same tooling

Page 24: Deploying windows containers with kubernetes

Windows Nano

• Stripped down

• Smallest footprint

• 1/20th the size of Windows Server Core

• Only essential components

– Hyper-V, Clustering, Networking, Storage, .Net, Core CLR

Page 25: Deploying windows containers with kubernetes

Windows Server Core => Ubuntu Linux

Windows Nano => Alpine Linux

Windows Server Core => Legacy Apps?

Windows Nano => Modern Apps?

Page 26: Deploying windows containers with kubernetes

What does this mean for us?

Page 27: Deploying windows containers with kubernetes
Page 28: Deploying windows containers with kubernetes
Page 29: Deploying windows containers with kubernetes
Page 30: Deploying windows containers with kubernetes
Page 31: Deploying windows containers with kubernetes
Page 32: Deploying windows containers with kubernetes
Page 33: Deploying windows containers with kubernetes
Page 34: Deploying windows containers with kubernetes

Dockerfile

Page 35: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/windowsservercore:10.0.14393.693

RUN powershell.exe Install-WindowsFeature web-server

> docker build –t iis .

Page 36: Deploying windows containers with kubernetes
Page 37: Deploying windows containers with kubernetes
Page 38: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/iis:windowsservercore-10.0.14393.693

SHELL ["powershell", "-command“]

Page 39: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/iis:windowsservercore-10.0.14393.693

SHELL ["powershell", "-command"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET; Install-

WindowsFeature Web-Asp-Net45

Page 40: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/iis:windowsservercore-10.0.14393.693

SHELL ["powershell", "-command"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET; Install-

WindowsFeature Web-Asp-Net45

RUN Remove-Website -Name 'Default Web Site'; \

mkdir c:\NerdDinner; \

New-Website -Name 'nerd-dinner' \

-Port 80 -PhysicalPath 'c:\NerdDinner' \

-ApplicationPool '.NET v4.5‘

Page 41: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/iis:windowsservercore-10.0.14393.693

SHELL ["powershell", "-command"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET; Install-WindowsFeature Web-Asp-Net45

RUN Remove-Website -Name 'Default Web Site'; \

mkdir c:\NerdDinner; \

New-Website -Name 'nerd-dinner' \

-Port 80 -PhysicalPath 'c:\NerdDinner' \

-ApplicationPool '.NET v4.5‘

EXPOSE 80

Page 42: Deploying windows containers with kubernetes

> type Dockerfile

FROM microsoft/iis:windowsservercore-10.0.14393.693

SHELL ["powershell", "-command"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET; Install-WindowsFeature Web-Asp-Net45

RUN Remove-Website -Name 'Default Web Site'; \

mkdir c:\NerdDinner; \

New-Website -Name 'nerd-dinner' \

-Port 80 -PhysicalPath 'c:\NerdDinner' \

-ApplicationPool '.NET v4.5‘

EXPOSE 80

COPY NerdDinner c:\NerdDinner

Page 43: Deploying windows containers with kubernetes

PS C:\> docker build –t nerddinner .

Page 44: Deploying windows containers with kubernetes

PS C:\> docker run -d -p 80:80 \

nerddinner

Page 45: Deploying windows containers with kubernetes

Container Orchestration

Page 46: Deploying windows containers with kubernetes

Kubernetes is an open-

source system for

automating deployment,

scaling, and management

of containerized

applications.

Page 47: Deploying windows containers with kubernetes

http://queue.acm.org/detail.cfm?id=2898444

Page 48: Deploying windows containers with kubernetes

Kubernetes

Page 49: Deploying windows containers with kubernetes

Automatic binpacking

Self-healing

Horizontal scaling

Service discovery and load balancing

Automated rollouts

Secret and configuration management

Batch execution

Role based access control

Auditing

API and Extension Hooks

Page 50: Deploying windows containers with kubernetes

http://www.ofbizian.com/2017/05/bet-on-cloud-native-ecosystem.html

Page 51: Deploying windows containers with kubernetes
Page 52: Deploying windows containers with kubernetes

Services Define Communication

• NodePort – Hardcoded Port

• ClusterIP – Cluster Assigned IP

• LoadBalancer – Public IP Assigned by Cloud

Page 53: Deploying windows containers with kubernetes

https://www.slideshare.net/weaveworks/orchestrating-microservices-with-kubernetes

Page 54: Deploying windows containers with kubernetes
Page 55: Deploying windows containers with kubernetes
Page 56: Deploying windows containers with kubernetes

Infrastructure as CodeapiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: frontend-web

spec:

replicas: 1

template:

metadata:

labels:

app: frontend

spec:

containers:

- name: http-server

image: katacoda/docker-http-server:latest

ports:

- containerPort: 80

Page 57: Deploying windows containers with kubernetes

Infrastructure as Code

apiVersion: v1

kind: Service

metadata:

name: frontend-svc

labels:

app: frontend

spec:

type: NodePort

ports:

- port: 80

nodePort: 30080

selector:

app: frontend

Page 58: Deploying windows containers with kubernetes

apiVersion: v1

kind: Deployment

metadata:

labels:

name: nerddinner

name: nerddinner

namespace: default

spec:

containers:

- image: benhall/nerddinner:v2.0

imagePullPolicy: Always

name: nerddinner

ports:

- containerPort: 80

nodeSelector:

beta.kubernetes.io/os: windows

Page 59: Deploying windows containers with kubernetes

Creating Kubernetes Cluster

Page 60: Deploying windows containers with kubernetes

htt

ps:

//tw

itte

r.co

m/k

ub

ern

etes

on

arm

Page 61: Deploying windows containers with kubernetes

Master === Linux OnlyNodes === Linux, Windows, Arm

Page 62: Deploying windows containers with kubernetes

Linux Containers on Windows

Page 63: Deploying windows containers with kubernetes

Kubeadmmaster$ kubeadm init

linuxnode01$ kubeadm join --discovery-token abcdef.1234567890abcdef 1.2.3.4:6443

windowsnode01$ .\kubeadm.exe join --discovery-token abcdef.1234567890abcdef 1.2.3.4:6443

Page 64: Deploying windows containers with kubernetes

kubectlmaster$ kubectl get nodes

NAME STATUS AGE VERSION

master Ready 9m v1.9.0

linuxnode01 Ready 9m v1.9.0

windowsnode01 Ready 9m v1.9.0

Page 65: Deploying windows containers with kubernetes
Page 66: Deploying windows containers with kubernetes

Creating AKS Service

Page 67: Deploying windows containers with kubernetes
Page 68: Deploying windows containers with kubernetes
Page 69: Deploying windows containers with kubernetes
Page 70: Deploying windows containers with kubernetes
Page 71: Deploying windows containers with kubernetes

$ kubectl expose deployments dotnetapp \

--port=80 --target-port=5000 \

--type=LoadBalancerservice "dotnetapp" exposed

$ kubectl get svcNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE

dotnetapp 10.0.47.252 <pending> 80:31078/TCP 8s

kubernetes 10.0.0.1 <none> 443/TCP 15m

Page 72: Deploying windows containers with kubernetes

$ kubectl expose deployments dotnetapp --port=80 --target-port=5000 --type=LoadBalancer

service "dotnetapp" exposed

$ kubectl get svc

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE

dotnetapp 10.0.47.252 <pending> 80:31078/TCP 8s

kubernetes 10.0.0.1 <none> 443/TCP 15m

wait for Kubernetes to configure Azure LB

$ kubectl get svc

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE

dotnetapp 10.0.47.252 40.118.100.56 80:31078/TCP 5m

curl 40.118.100.56

Page 73: Deploying windows containers with kubernetes

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

Page 74: Deploying windows containers with kubernetes

$ kubectl scale --replicas=3 deployment/dotnetappdeployment "dotnetapp" scaled

$ kubectl get deployments

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

dotnetapp 3 3 3 3 6m

$ kubectl get pods

NAME READY STATUS RESTARTS AGE

dotnetapp-2582115574-0l028 1/1 Running 0 1m

dotnetapp-2582115574-b59rg 1/1 Running 0 7m

dotnetapp-2582115574-hdbc6 1/1 Running 0 1m

Page 75: Deploying windows containers with kubernetes

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-0l028

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-hdbc6

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

Page 76: Deploying windows containers with kubernetes

Deploy new versions?

Page 77: Deploying windows containers with kubernetes

$ kubectl set image \

deployment/dotnetapp=katacoda/dotnet-example:v1.1> curl 40.118.100.56

[v1.1] Request processed by dotnetapp-2582115574-0l028

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-b59rg

> curl 40.118.100.56

Request processed by dotnetapp-2582115574-hdbc6

> curl 40.118.100.56

[v1.1] Request processed by dotnetapp-2582115574-b59rg

$ kubectl apply –f mydeployment.yaml

Page 78: Deploying windows containers with kubernetes

What about Private Registries?

Page 79: Deploying windows containers with kubernetes

> kubectl create secret docker-registry myregistrykey \

--docker-server=katacodademoreg1.azurecr.io \

--docker-username=katacodademoreg1 \

--docker-password=<REMOVE> \

--docker-email [email protected]

secret "myregistrykey" created.

Page 80: Deploying windows containers with kubernetes

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

labels:

run: dotnetapp

spec:

replicas: 6

strategy:

rollingUpdate:

maxSurge: 1

maxUnavailable: 1

type: RollingUpdate

template:

spec:

containers:

- image: katacodademoreg1.azurecr.io/katacoda/dotnet-example:v1.1

imagePullSecrets:

- name: myregistrykey

metadata:

labels:

run: dotnetapp

Page 81: Deploying windows containers with kubernetes

Storage

Page 82: Deploying windows containers with kubernetes
Page 83: Deploying windows containers with kubernetes

> cat sqlserver.yaml

apiVersion: v1

kind: Secret

metadata:

name: azure-secret

type: Opaque

data:

azurestorageaccountname: <name>

azurestorageaccountkey: <key>

---

apiVersion: v1

kind: Pod

metadata:

labels:

name: mssql

role: master

name: mssql

spec:

containers:

- env:

- name: ACCEPT_EULA

value: "Y"

- name: SA_PASSWORD

value: yourStrong133tPassword

image: microsoft/mssql-server-linux

volumeMounts:

- mountPath: /var/opt/mssql/data

name: azure

volumes:

- azureFile:

readOnly: false

secretName: azure-secret

shareName: k8stest

name: azure

Page 84: Deploying windows containers with kubernetes

apiVersion: apps/v1

kind: StatefulSet

metadata:

name: web

spec:

serviceName: "nginx"

replicas: 2

selector:

matchLabels:

app: nginx

template:

metadata:

labels:

app: nginx

spec:

containers:

- name: nginx

image: k8s.gcr.io/nginx-slim:0.8

ports:

- containerPort: 80

name: web

volumeMounts:

- name: www

mountPath: /usr/share/nginx/html

volumeClaimTemplates:

- metadata:

name: www

spec:

accessModes: [ "ReadWriteOnce" ]

resources:

requests:

storage: 1Gi

Page 85: Deploying windows containers with kubernetes

Kubernetes Operators

• An Operator represents human operational knowledge in software to reliably manage an application.

• https://coreos.com/operators/

Page 86: Deploying windows containers with kubernetes

OperatorsapiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: etcd-operator

spec:

replicas: 1

template:

metadata:

labels:

name: etcd-operator

spec:

containers:

- name: etcd-operator

image: quay.io/coreos/etcd-operator:v0.8.1

command:

- etcd-operator

env:

- name: MY_POD_NAMESPACE

valueFrom:

fieldRef:

https://raw.githubusercontent.com/coreos/etcd-operator/master/example/deployment.yaml

Page 87: Deploying windows containers with kubernetes

OperatorsapiVersion: "etcd.database.coreos.com/v1beta2"

kind: "EtcdCluster"

metadata:

name: "example-etcd-cluster"

spec:

size: 3

version: "3.2.13"

https://github.com/coreos/etcd-operator/blob/master/example/example-etcd-cluster.yaml

Page 88: Deploying windows containers with kubernetes

Is it still up?

Monitoring with Prometheus and Kubernetes

Page 89: Deploying windows containers with kubernetes

apiVersion: extensions/v1beta1

kind: DaemonSet

metadata:

name: node-exporter

spec:

template:

metadata:

labels:

app: node-exporter

name: node-exporter

spec:

containers:

- image: prom/node-exporter

name: node-exporter

ports:

- containerPort: 9100

hostPort: 9100

name: scrape

DaemonSet? When new nodes are deployed, automatically schedule

workload

Page 90: Deploying windows containers with kubernetes

What should we monitor?

Page 91: Deploying windows containers with kubernetes

apiVersion: v1

kind: Service

metadata:

annotations:

prometheus.io/scrape: 'true'

labels:

app: node-exporter

name: node-exporter

name: node-exporter

spec:

clusterIP: None

ports:

- name: scrape

port: 9100

protocol: TCP

selector:

app: node-exporter

type: ClusterIP

Page 92: Deploying windows containers with kubernetes
Page 93: Deploying windows containers with kubernetes
Page 94: Deploying windows containers with kubernetes
Page 95: Deploying windows containers with kubernetes
Page 96: Deploying windows containers with kubernetes
Page 97: Deploying windows containers with kubernetes

The Future?

Page 98: Deploying windows containers with kubernetes
Page 99: Deploying windows containers with kubernetes

SQL Server as a Container

Page 100: Deploying windows containers with kubernetes

Visual Studio as a Container?

Page 101: Deploying windows containers with kubernetes
Page 102: Deploying windows containers with kubernetes

Everything as a Container

Page 103: Deploying windows containers with kubernetes

Deploy Anywhere

Page 104: Deploying windows containers with kubernetes

www.katacoda.com

Page 105: Deploying windows containers with kubernetes
Page 106: Deploying windows containers with kubernetes
Page 107: Deploying windows containers with kubernetes
Page 108: Deploying windows containers with kubernetes
Page 109: Deploying windows containers with kubernetes

Online Kubernetes

Training for Companies

at Katacoda.com

@Ben_Hall

[email protected]

Blog.BenHall.me.uk

www.Katacoda.com