前置条件
安装环境 Ubuntu 18.04 , 将系统镜像源修改未国内镜像源
关闭 swap
$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 验证
$ free -m
total used free shared buff/cache available
Mem: 7895 213 5396 1 2284 7399
Swap: 0 0 0
安装 docker 和 k8s
docker 安装脚本
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
k8s 安装脚本
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated
关闭防火墙
# 查看状态
$ sudo ufw status
# 关闭
$ sudo ufw disable
启动 K8S
创建 kubeadm.yam 配置文件
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
controllerManager:
extraArgs:
horizontal-pod-autoscaler-use-rest-clients: "true"
horizontal-pod-autoscaler-sync-period: "10s"
node-monitor-grace-period: "10s"
apiServer:
extraArgs:
runtime-config: "api/all=true"
imageRepository: "registry.cn-hangzhou.aliyuncs.com/google_containers"
kubernetesVersion: "v1.18.0"
$ kubeadm init --config kubeadm.yaml
# 记录成功信息,如下
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 10.10.0.166:6443 --token 33opf5.3juftd3l3ujabyfb \
--discovery-token-ca-cert-hash sha256:ad3acac72239eaba001b616e5cc3049c95e8682c4d97ca3d5376896b0cda0473
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
部署网络插件
$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
安装 dashboard
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
recommended.yaml 添加一个type,指定端口类型为 NodePort,这样外界可以通过地址 nodeIP:nodePort 访问
$ kubectl apply -f recommended.yaml
# 查看所有 pod svc 状态,过程较慢,请稍等
$ kubectl get pods --all-namespaces
kube-system coredns-546565776c-f2wjn 1/1 Running 0 17h
kube-system coredns-546565776c-wczfh 1/1 Running 0 17h
kube-system etcd-k8s 1/1 Running 0 17h
kube-system kube-apiserver-k8s 1/1 Running 0 17h
kube-system kube-controller-manager-k8s 1/1 Running 0 17h
kube-system kube-proxy-7ghhg 1/1 Running 0 17h
kube-system kube-proxy-whrx9 1/1 Running 0 17h
kube-system kube-scheduler-k8s 1/1 Running 0 17h
kube-system weave-net-c7gsv 2/2 Running 0 17h
kube-system weave-net-jtbs6 2/2 Running 0 17h
kubernetes-dashboard dashboard-metrics-scraper-6b4884c9d5-ln7fk 1/1 Running 0 39s
kubernetes-dashboard kubernetes-dashboard-7f99b75bf4-bpzkm 1/1 Running 0 39s
# 查看端口使用
$ kubectl get pods,svc -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/coredns-546565776c-f2wjn 1/1 Running 0 17h
kube-system pod/coredns-546565776c-wczfh 1/1 Running 0 17h
kube-system pod/etcd-k8s 1/1 Running 0 17h
kube-system pod/kube-apiserver-k8s 1/1 Running 0 17h
kube-system pod/kube-controller-manager-k8s 1/1 Running 0 17h
kube-system pod/kube-proxy-7ghhg 1/1 Running 0 17h
kube-system pod/kube-proxy-whrx9 1/1 Running 0 17h
kube-system pod/kube-scheduler-k8s 1/1 Running 0 17h
kube-system pod/weave-net-c7gsv 2/2 Running 0 17h
kube-system pod/weave-net-jtbs6 2/2 Running 0 17h
kubernetes-dashboard pod/dashboard-metrics-scraper-6b4884c9d5-ln7fk 1/1 Running 0 4m45s
kubernetes-dashboard pod/kubernetes-dashboard-7f99b75bf4-bpzkm 1/1 Running 0 4m45s
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17h
kube-system service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 17h
kubernetes-dashboard service/dashboard-metrics-scraper ClusterIP 10.109.186.136 <none> 8000/TCP 4m45s
kubernetes-dashboard service/kubernetes-dashboard NodePort 10.102.7.137 <none> 443:31225/TCP 4m45s
open url https://10.10.0.166:31225
登陆界面需要验证,建admin的权限,得到token
新建 kubernetes-dashboard-admin.rbac.yam l文件
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-admin
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard-admin
labels:
k8s-app: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard-admin
namespace: kube-system
# 创建
$ kubectl create -f kubernetes-dashboard-admin.rbac.yaml
# 使用命令得到token的name
$ kubectl -n kube-system get secret | grep kubernetes-dashboard-admin
kubernetes-dashboard-admin-token-xn87t kubernetes.io/service-account-token 3 15s
# 查看 token
$ kubectl describe -n kube-system secret/kubernetes-dashboard-admin-token-jvm8w
Name: kubernetes-dashboard-admin-token-r8vxg
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: kubernetes-dashboard-admin
kubernetes.io/service-account.uid: a8207a93-0688-4000-bde4-810c6e50fade
Type: kubernetes.io/service-account-token
Data
====
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjhJOGt5TGdzenRTZEhjcEZTZnFtRGhyalJwQTBBVUM0MlRocVhINGpJTGMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbi10b2tlbi1yOHZ4ZyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImE4MjA3YTkzLTA2ODgtNDAwMC1iZGU0LTgxMGM2ZTUwZmFkZSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbiJ9.DlhZfTb_PolIwLNoKsWtl88bFo1RDA1tExUHnqI8MJzuvw99wBXiuGJi4xR02OH_pfpX8wrGGXmLopoS007MZv6zyTt7h9TRgSSfbtJKwQkJoizEdKLWi_eGyXnnRWcjdAmI_i_upi0seXpoWhPN6MuEPtul1vRMbCQBTdf0-dEM1iSs0YN1VlDnXve8EcbHqz4zoRvwWvGIJ0PkKij905cmZziVxp-BqJP6urLfNtmWo2ZmRXngKSIplAY3D4pI7q2GIfexNWOhG7wIdvopg4L2O4ortO1MBVXVdxQ55I0YgFCNiNhkDAQW986ICsfNC67Y86bJRBfqe1RDg3fcfw
ca.crt: 1025 bytes
namespace: 11 bytes
将 token 在页面上输入,网页显示如下
部署容器存储插件(未验证)
$ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/common.yaml
$ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/operator.yaml
$ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/cluster.yaml