在Ubuntu 18.04 中安装和使用 mircok8s

本文实验在ubunt18.04 Server中安装 microk8s,体验单机版的kubernetes。

安装 microk8s

  1. 查看可用的microk8s版本信息,在命令行执行:
1
snap info microk8s

系统输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
name:      microk8s
summary: Kubernetes for workstations and appliances
publisher: Canonical✓
contact: https://github.com/ubuntu/microk8s
license: Apache-2.0
description: |
MicroK8s is a small, fast, secure, single node Kubernetes that installs on just about any Linux
box. Use it for offline development, prototyping, testing, or use it on a VM as a small, cheap,
reliable k8s for CI/CD. It's also a great k8s for appliances - develop your IoT apps for k8s and
deploy them to MicroK8s on your boxes.
snap-id: EaXqgt1lyCaxKaQCU349mlodBkDCXRcg
channels:
stable: v1.15.2 2019-08-05 (743) 192MB classic
candidate: v1.15.3 2019-08-20 (778) 171MB classic
beta: v1.15.3 2019-08-20 (778) 171MB classic
edge: v1.15.3 2019-09-04 (826) 171MB classic
1.16/stable: –
1.16/candidate: –
1.16/beta: v1.16.0-beta.1 2019-08-23 (799) 173MB classic
1.16/edge: v1.16.0-alpha.3 2019-08-23 (798) 173MB classic
1.15/stable: v1.15.2 2019-08-06 (745) 192MB classic
...

可以看到,当前的版本 1.15.2.

  1. 安装

执行命令:

1
sudo snap install microk8s --classic

系统输出:

1
microk8s v1.15.2 from Canonical✓ installed

表示安装成功。

查看 microk8s 的状态

执行:

1
microk8s.status

系统显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
microk8s is running
addons:
knative: disabled
jaeger: disabled
fluentd: disabled
gpu: disabled
storage: disabled
registry: disabled
rbac: disabled
ingress: disabled
dns: disabled
metrics-server: disabled
linkerd: disabled
prometheus: disabled
istio: disabled
dashboard: disabled

可以看到,所有的 addon 现在都是停止的。

开启必要的服务

我们首先开启 dashboard 和 dns 服务, 执行:

1
microk8s.enable dashboard

系统显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Applying manifest
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
service/monitoring-grafana created
service/monitoring-influxdb created
service/heapster created
deployment.extensions/monitoring-influxdb-grafana-v4 created
serviceaccount/heapster created
clusterrolebinding.rbac.authorization.k8s.io/heapster created
configmap/heapster-config created
configmap/eventer-config created
deployment.extensions/heapster-v1.5.2 created

If RBAC is not enabled access the dashboard using the default token retrieved with:

token=$(microk8s.kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
microk8s.kubectl -n kube-system describe secret $token

In an RBAC enabled setup (microk8s.enable RBAC) you need to create a user with restricted
permissions as shown in https://github.com/kubernetes/dashboard/wiki/Creating-sample-user

接着执行:

1
microk8s.enable dns

系统显示:

1
2
3
4
5
6
7
8
9
10
11
Enabling DNS
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
clusterrole.rbac.authorization.k8s.io/coredns created
clusterrolebinding.rbac.authorization.k8s.io/coredns created
Restarting kubelet
[sudo] password for stu:
DNS is enabled

在执行过程中会提示需要管理员用户的密码。

获取当前所有的命名空间

执行:

1
microk8s.kubectl get all --all-namespaces

系统显示:

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
NAMESPACE     NAME                                                  READY   STATUS              RESTARTS   AGE
kube-system pod/coredns-f7867546d-r62q2 0/1 ContainerCreating 0 3m3s
kube-system pod/heapster-v1.5.2-6b794f77c8-hwwfx 0/4 ContainerCreating 0 5m43s
kube-system pod/kubernetes-dashboard-7d75c474bb-2jxpm 0/1 ContainerCreating 0 5m45s
kube-system pod/monitoring-influxdb-grafana-v4-6b6954958c-g6g9q 0/2 ContainerCreating 0 5m44s


NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 3h2m
kube-system service/heapster ClusterIP 10.152.183.69 <none> 80/TCP 5m45s
kube-system service/kube-dns ClusterIP 10.152.183.10 <none> 53/UDP,53/TCP,9153/TCP 3m3s
kube-system service/kubernetes-dashboard ClusterIP 10.152.183.215 <none> 443/TCP 5m46s
kube-system service/monitoring-grafana ClusterIP 10.152.183.123 <none> 80/TCP 5m46s
kube-system service/monitoring-influxdb ClusterIP 10.152.183.66 <none> 8083/TCP,8086/TCP 5m46s


NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system deployment.apps/coredns 0/1 1 0 3m3s
kube-system deployment.apps/heapster-v1.5.2 0/1 1 0 5m44s
kube-system deployment.apps/kubernetes-dashboard 0/1 1 0 5m46s
kube-system deployment.apps/monitoring-influxdb-grafana-v4 0/1 1 0 5m45s

NAMESPACE NAME DESIRED CURRENT READY AGE
kube-system replicaset.apps/coredns-f7867546d 1 1 0 3m3s
kube-system replicaset.apps/heapster-v1.5.2-6b794f77c8 1 1 0 5m44s
kube-system replicaset.apps/kubernetes-dashboard-7d75c474bb 1 1 0 5m45s
kube-system replicaset.apps/monitoring-influxdb-grafana-v4-6b6954958c 1 1 0 5m45s

本文标题:在Ubuntu 18.04 中安装和使用 mircok8s

文章作者:Morning Star

发布时间:2019年09月05日 - 11:09

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/docker/microk8s-on-ubuntu1804/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。