前置条件

安装环境 Ubuntu 18.04 , 将系统镜像源修改未国内镜像源

关闭 swap

  1. $ sudo swapoff -a
  2. $ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  3. # 验证
  4. $ free -m
  5. total used free shared buff/cache available
  6. Mem: 7895 213 5396 1 2284 7399
  7. Swap: 0 0 0

安装 docker 和 k8s

docker 安装脚本

  1. sudo apt-get update
  2. sudo apt-get install \
  3. apt-transport-https \
  4. ca-certificates \
  5. curl \
  6. gnupg-agent \
  7. software-properties-common
  8. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  9. sudo apt-key fingerprint 0EBFCD88
  10. sudo add-apt-repository \
  11. "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  12. $(lsb_release -cs) \
  13. stable"
  14. sudo apt-get update
  15. sudo apt-get install docker-ce docker-ce-cli containerd.io

k8s 安装脚本

  1. curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
  2. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  3. deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
  4. EOF
  5. sudo apt-get update
  6. sudo apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated

关闭防火墙

  1. # 查看状态
  2. $ sudo ufw status
  3. # 关闭
  4. $ sudo ufw disable

启动 K8S

创建 kubeadm.yam 配置文件

  1. apiVersion: kubeadm.k8s.io/v1beta2
  2. kind: ClusterConfiguration
  3. controllerManager:
  4. extraArgs:
  5. horizontal-pod-autoscaler-use-rest-clients: "true"
  6. horizontal-pod-autoscaler-sync-period: "10s"
  7. node-monitor-grace-period: "10s"
  8. apiServer:
  9. extraArgs:
  10. runtime-config: "api/all=true"
  11. imageRepository: "registry.cn-hangzhou.aliyuncs.com/google_containers"
  12. kubernetesVersion: "v1.18.0"
  1. $ kubeadm init --config kubeadm.yaml
  2. # 记录成功信息,如下
  3. Your Kubernetes control-plane has initialized successfully!
  4. To start using your cluster, you need to run the following as a regular user:
  5. mkdir -p $HOME/.kube
  6. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  7. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  8. You should now deploy a pod network to the cluster.
  9. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  10. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  11. Then you can join any number of worker nodes by running the following on each as root:
  12. kubeadm join 10.10.0.166:6443 --token 33opf5.3juftd3l3ujabyfb \
  13. --discovery-token-ca-cert-hash sha256:ad3acac72239eaba001b616e5cc3049c95e8682c4d97ca3d5376896b0cda0473
  1. $ mkdir -p $HOME/.kube
  2. $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. $ sudo chown $(id -u):$(id -g) $HOME/.kube/config

部署网络插件

  1. $ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

安装 dashboard

  1. $ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml

recommended.yaml 添加一个type,指定端口类型为 NodePort,这样外界可以通过地址 nodeIP:nodePort 访问

ubuntu18.04部署流程 - 图1

  1. $ kubectl apply -f recommended.yaml
  2. # 查看所有 pod svc 状态,过程较慢,请稍等
  3. $ kubectl get pods --all-namespaces
  4. kube-system coredns-546565776c-f2wjn 1/1 Running 0 17h
  5. kube-system coredns-546565776c-wczfh 1/1 Running 0 17h
  6. kube-system etcd-k8s 1/1 Running 0 17h
  7. kube-system kube-apiserver-k8s 1/1 Running 0 17h
  8. kube-system kube-controller-manager-k8s 1/1 Running 0 17h
  9. kube-system kube-proxy-7ghhg 1/1 Running 0 17h
  10. kube-system kube-proxy-whrx9 1/1 Running 0 17h
  11. kube-system kube-scheduler-k8s 1/1 Running 0 17h
  12. kube-system weave-net-c7gsv 2/2 Running 0 17h
  13. kube-system weave-net-jtbs6 2/2 Running 0 17h
  14. kubernetes-dashboard dashboard-metrics-scraper-6b4884c9d5-ln7fk 1/1 Running 0 39s
  15. kubernetes-dashboard kubernetes-dashboard-7f99b75bf4-bpzkm 1/1 Running 0 39s
  16. # 查看端口使用
  17. $ kubectl get pods,svc -A
  18. NAMESPACE NAME READY STATUS RESTARTS AGE
  19. kube-system pod/coredns-546565776c-f2wjn 1/1 Running 0 17h
  20. kube-system pod/coredns-546565776c-wczfh 1/1 Running 0 17h
  21. kube-system pod/etcd-k8s 1/1 Running 0 17h
  22. kube-system pod/kube-apiserver-k8s 1/1 Running 0 17h
  23. kube-system pod/kube-controller-manager-k8s 1/1 Running 0 17h
  24. kube-system pod/kube-proxy-7ghhg 1/1 Running 0 17h
  25. kube-system pod/kube-proxy-whrx9 1/1 Running 0 17h
  26. kube-system pod/kube-scheduler-k8s 1/1 Running 0 17h
  27. kube-system pod/weave-net-c7gsv 2/2 Running 0 17h
  28. kube-system pod/weave-net-jtbs6 2/2 Running 0 17h
  29. kubernetes-dashboard pod/dashboard-metrics-scraper-6b4884c9d5-ln7fk 1/1 Running 0 4m45s
  30. kubernetes-dashboard pod/kubernetes-dashboard-7f99b75bf4-bpzkm 1/1 Running 0 4m45s
  31. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  32. default service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17h
  33. kube-system service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 17h
  34. kubernetes-dashboard service/dashboard-metrics-scraper ClusterIP 10.109.186.136 <none> 8000/TCP 4m45s
  35. kubernetes-dashboard service/kubernetes-dashboard NodePort 10.102.7.137 <none> 443:31225/TCP 4m45s

open url https://10.10.0.166:31225

ubuntu18.04部署流程 - 图2

登陆界面需要验证,建admin的权限,得到token

新建 kubernetes-dashboard-admin.rbac.yam l文件

  1. ---
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. labels:
  6. k8s-app: kubernetes-dashboard
  7. name: kubernetes-dashboard-admin
  8. namespace: kube-system
  9. ---
  10. apiVersion: rbac.authorization.k8s.io/v1beta1
  11. kind: ClusterRoleBinding
  12. metadata:
  13. name: kubernetes-dashboard-admin
  14. labels:
  15. k8s-app: kubernetes-dashboard
  16. roleRef:
  17. apiGroup: rbac.authorization.k8s.io
  18. kind: ClusterRole
  19. name: cluster-admin
  20. subjects:
  21. - kind: ServiceAccount
  22. name: kubernetes-dashboard-admin
  23. namespace: kube-system
  1. # 创建
  2. $ kubectl create -f kubernetes-dashboard-admin.rbac.yaml
  3. # 使用命令得到token的name
  4. $ kubectl -n kube-system get secret | grep kubernetes-dashboard-admin
  5. kubernetes-dashboard-admin-token-xn87t kubernetes.io/service-account-token 3 15s
  6. # 查看 token
  7. $ kubectl describe -n kube-system secret/kubernetes-dashboard-admin-token-jvm8w
  8. Name: kubernetes-dashboard-admin-token-r8vxg
  9. Namespace: kube-system
  10. Labels: <none>
  11. Annotations: kubernetes.io/service-account.name: kubernetes-dashboard-admin
  12. kubernetes.io/service-account.uid: a8207a93-0688-4000-bde4-810c6e50fade
  13. Type: kubernetes.io/service-account-token
  14. Data
  15. ====
  16. token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjhJOGt5TGdzenRTZEhjcEZTZnFtRGhyalJwQTBBVUM0MlRocVhINGpJTGMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbi10b2tlbi1yOHZ4ZyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImE4MjA3YTkzLTA2ODgtNDAwMC1iZGU0LTgxMGM2ZTUwZmFkZSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZC1hZG1pbiJ9.DlhZfTb_PolIwLNoKsWtl88bFo1RDA1tExUHnqI8MJzuvw99wBXiuGJi4xR02OH_pfpX8wrGGXmLopoS007MZv6zyTt7h9TRgSSfbtJKwQkJoizEdKLWi_eGyXnnRWcjdAmI_i_upi0seXpoWhPN6MuEPtul1vRMbCQBTdf0-dEM1iSs0YN1VlDnXve8EcbHqz4zoRvwWvGIJ0PkKij905cmZziVxp-BqJP6urLfNtmWo2ZmRXngKSIplAY3D4pI7q2GIfexNWOhG7wIdvopg4L2O4ortO1MBVXVdxQ55I0YgFCNiNhkDAQW986ICsfNC67Y86bJRBfqe1RDg3fcfw
  17. ca.crt: 1025 bytes
  18. namespace: 11 bytes

将 token 在页面上输入,网页显示如下

ubuntu18.04部署流程 - 图3

部署容器存储插件(未验证)

  1. $ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/common.yaml
  2. $ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/operator.yaml
  3. $ kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/cluster.yaml