Never too late to learn.

0%

Windows 10上安装Docker Desktop 并启用Kubernetes

windows 10上安装 Docker Desktop

环境

OS: Windows 10
终端:PowerShell

Docker overview

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

安装Docker Desktop

Docker Desktop 为windows提供社区版本,可以直接从Docker Hub上下载并安装。

安装完成之后可以跟着Quick Start, 完成一个简单的容器的下载,构建,运行流程。

右击系统托盘的docker图标,点击”About Docker Desktop”,可以获取各组件版本信息

docker_desktop.png

开启Kubernetes

不要直接在设置->Kubernetes中开启,否则会出现Kubernetes一直处于启动状态,无法结束,按照如下教程Docker Desktop for Mac/Windows 开启 Kubernetes

为 Docker daemon 配置镜像加速

Setting -> Docker Engine中修改配置文件

1
2
3
4
5
6
7
8
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": [],
"debug": true,
"experimental": true
}

docker_desktop1.png

下载Kubernetes所需镜像

预先从阿里云Docker镜像服务下载 Kubernetes 所需要的镜像, 并cd进去

1
2
git clone https://github.com/AliyunContainerService/k8s-for-docker-desktop.git
cd .\k8s-for-docker-desktop\

checkout 到Docker Desktop中Kubernetes对应版本的号

1
git checkout v1.15.5

在Windows上, 以管理员身份启动PowerShell, 运行Set-ExecutionPolicy RemoteSigned, 然后运行如下命令,启动下载Kubernetes所需镜像

1
.\load_images.ps1

镜像下载完成后,进入Setting, 开启 Kubernetes,并等待 Kubernetes 开始运行

docker_desktop2.png

配置 Kubernetes 控制台

部署Kubernetes dashboard, 并开启API Server访问代理

1
2
3
kubectl create -f kubernetes-dashboard.yaml

kubectl proxy

docker_desktop3.png

通过如下URL访问 Kubernetes dashboard

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/overview?namespace=default

登录dashboard

  • 生成访问令牌登录
    1
    2
    3
    $TOKEN=((kubectl -n kube-system describe secret default | Select-String "token:") -split " +")[1]
    kubectl config set-credentials docker-for-desktop --token="${TOKEN}"
    echo $TOKEN
  • 使用Kube文件登录
    1
    %UserProfile%\.kube\config

测试

官方教程

1
kubectl get pods -n kube-system

docker_desktop4.png

为确保Kubernetes正常启动和运行,创建一个名为pod.yaml的文本文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1

kind: Pod

metadata:

name: demo

spec:

containers:

- name: testpod

image: alpine:3.5

command: ["ping", "114.114.114.114"]

该文件用于创建一个pod,且该pod中仅有一个容器,在该容器中执行ping命令

在Powershell中执行如下命令创建pod

1
2
PS C:\Users\Kid\docker> kubectl apply -f pod.yaml
pod/demo created

查看pods状态和日志

1
2
3
4
5
6
7
8
9
10
11
PS C:\Users\Kid\docker> kubectl get pods
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 43s
PS C:\Users\Kid\docker> kubectl logs demo
PING 114.114.114.114 (114.114.114.114): 56 data bytes
64 bytes from 114.114.114.114: seq=0 ttl=37 time=17.616 ms
64 bytes from 114.114.114.114: seq=1 ttl=37 time=18.877 ms
64 bytes from 114.114.114.114: seq=2 ttl=37 time=15.548 ms
64 bytes from 114.114.114.114: seq=3 ttl=37 time=18.090 ms
64 bytes from 114.114.114.114: seq=4 ttl=37 time=48.770 ms
64 bytes from 114.114.114.114: seq=5 ttl=37 time=15.864 ms

删除pod

1
2
3
4
5
PS C:\Users\Kid\docker> kubectl delete pod demo
pod "demo" deleted
PS C:\Users\Kid\docker>
PS C:\Users\Kid\docker> kubectl get pods
No resources found.

部署到Kubernetes

官方教程

Kubernetes provides many tools for scaling, networking, securing and maintaining your containerized applications, above and beyond the abilities of containers themselves.

用Kubernetes YAML来描述apps

All containers in Kubernetes are scheduled as pods, which are groups of co-located containers that share some resources. Furthermore, in a realistic application we almost never create individual pods; instead, most of our workloads are scheduled as deployments, which are scalable groups of pods maintained automatically by Kubernetes. Lastly, all Kubernetes objects can and should be described in manifests called Kubernetes YAML files. These YAML files describe all the components and configurations of your Kubernetes app, and can be used to easily create and destroy your app in any Kubernetes environment.

创建一个bb.yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apiVersion: apps/v1
kind: Deployment
metadata:
name: bb-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
bb: web
template:
metadata:
labels:
bb: web
spec:
containers:
- name: bb-site
image: bulletinboard:1.0
---
apiVersion: v1
kind: Service
metadata:
name: bb-entrypoint
namespace: default
spec:
type: NodePort
selector:
bb: web
ports:
- port: 8080
targetPort: 8080
nodePort: 30001

deploy

bb.yaml同目录下,部署

1
kubectl apply -f bb.yaml

列出deployments(部署), pods(容器组),services(服务)

1
2
3
kubectl get deployments
kubectl get pods
kubectl get services

删除部署

1
kubectl delete -f bb.yaml

docker_desktop5.png

Coffee? ☕