基础环境

操作系统

  1. $ cat /etc/issue [22:34:09]
  2. Ubuntu 16.04.6 LTS \n \l

cpu

  1. $ lscpu [22:28:57]
  2. Architecture: x86_64
  3. CPU op-mode(s): 32-bit, 64-bit
  4. Byte Order: Little Endian
  5. CPU(s): 2

内存

  1. $ free -h [22:30:26]
  2. total used free shared buff/cache available
  3. Mem: 3.9G 293M 2.9G 4.0M 656M 3.3G
  4. Swap: 0B 0B 0B

docker

  1. $ sudo apt-get update
  2. $ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

安装

  1. sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - [22:37:06]
  2. add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

安装docker

  1. $ sudo apt-get update
  2. $ sudo apt-get install -y docker-ce docker-ce-cli containerd.io

查看docker 版本

  1. $ sudo docker version [22:39:21]
  2. Client: Docker Engine - Community
  3. Version: 19.03.12
  4. API version: 1.40
  5. Go version: go1.13.10
  6. Git commit: 48a66213fe
  7. Built: Mon Jun 22 15:45:49 2020
  8. OS/Arch: linux/amd64
  9. Experimental: false
  10. Server: Docker Engine - Community
  11. Engine:
  12. Version: 19.03.12
  13. API version: 1.40 (minimum version 1.12)
  14. Go version: go1.13.10
  15. Git commit: 48a66213fe
  16. Built: Mon Jun 22 15:44:20 2020
  17. OS/Arch: linux/amd64
  18. Experimental: false
  19. containerd:
  20. Version: 1.2.13
  21. GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
  22. runc:
  23. Version: 1.0.0-rc10
  24. GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
  25. docker-init:
  26. Version: 0.18.0
  27. GitCommit: fec3683

关闭

  1. $ sudo vi /etc/modules
  2. $ sudo vi /etc/sysctl.d/k8s.conf
  3. $ sudo swapoff -a [22:44:44]
  4. $ sudo sed -i 's/.*swap.*/#&/' /etc/fstab

切换

  1. sudo su root
  2. # curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
  3. #cat > /etc/apt/sources.list.d/aliyun-k8s.list <<EOF
  4. deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
  5. EOF
  6. # apt update
  7. # apt install -y kubeadm kubelet kubectl ipvsadm ipset

安装

  1. # kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
  2. W1027 22:51:15.001922 5101 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
  3. [init] Using Kubernetes version: v1.19.3
  4. [preflight] Running pre-flight checks
  5. [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
  6. [preflight] Pulling images required for setting up a Kubernetes cluster
  7. [preflight] This might take a minute or two, depending on the speed of your internet connection
  8. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  9. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  10. [certs] Generating "ca" certificate and key
  11. [certs] Generating "apiserver" certificate and key
  12. [certs] apiserver serving cert is signed for DNS names [izbp1dmodzug132aiko240z kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.111.118.193]
  13. [certs] Generating "apiserver-kubelet-client" certificate and key
  14. [certs] Generating "front-proxy-ca" certificate and key
  15. [certs] Generating "front-proxy-client" certificate and key
  16. [certs] Generating "etcd/ca" certificate and key
  17. [certs] Generating "etcd/server" certificate and key
  18. [certs] etcd/server serving cert is signed for DNS names [izbp1dmodzug132aiko240z localhost] and IPs [10.111.118.193 127.0.0.1 ::1]
  19. [certs] Generating "etcd/peer" certificate and key
  20. [certs] etcd/peer serving cert is signed for DNS names [izbp1dmodzug132aiko240z localhost] and IPs [10.111.118.193 127.0.0.1 ::1]
  21. [certs] Generating "etcd/healthcheck-client" certificate and key
  22. [certs] Generating "apiserver-etcd-client" certificate and key
  23. [certs] Generating "sa" key and public key
  24. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  25. [kubeconfig] Writing "admin.conf" kubeconfig file
  26. [kubeconfig] Writing "kubelet.conf" kubeconfig file
  27. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  28. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  29. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  30. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  31. [kubelet-start] Starting the kubelet
  32. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  33. [control-plane] Creating static Pod manifest for "kube-apiserver"
  34. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  35. [control-plane] Creating static Pod manifest for "kube-scheduler"
  36. [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
  37. [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
  38. [apiclient] All control plane components are healthy after 18.502143 seconds
  39. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  40. [kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
  41. [upload-certs] Skipping phase. Please see --upload-certs
  42. [mark-control-plane] Marking the node izbp1dmodzug132aiko240z as control-plane by adding the label "node-role.kubernetes.io/master=''"
  43. [mark-control-plane] Marking the node izbp1dmodzug132aiko240z as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  44. [bootstrap-token] Using token: o0runy.uikvs46vtfxidq0s
  45. [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
  46. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
  47. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
  48. [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  49. [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  50. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
  51. [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
  52. [addons] Applied essential addon: CoreDNS
  53. [addons] Applied essential addon: kube-proxy
  54. Your Kubernetes control-plane has initialized successfully!
  55. To start using your cluster, you need to run the following as a regular user:
  56. mkdir -p $HOME/.kube
  57. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  58. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  59. You should now deploy a pod network to the cluster.
  60. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  61. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  62. Then you can join any number of worker nodes by running the following on each as root:
  63. kubeadm join 10.111.118.193:6443 --token o0runy.uikvs46vtfxidq0s \
  64. --discovery-token-ca-cert-hash sha256:0fc36ea0a82cca6d155ef893f4646ecba10e67b355a54a9cfbca27fbea1a210b

配置文件

  1. # mkdir -p $HOME/.kube
  2. # cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. # chown $(id -u):$(id -g) $HOME/.kube/config

验证集群信息

  1. # kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. izbp1dmodzug132aiko240z NotReady master 2m26s v1.19.3
  4. # kubectl get pod -n kube-system
  5. NAME READY STATUS RESTARTS AGE
  6. coredns-6d56c8448f-4krmb 0/1 Pending 0 2m25s
  7. coredns-6d56c8448f-9svcn 0/1 Pending 0 2m25s
  8. etcd-izbp1dmodzug132aiko240z 1/1 Running 0 2m34s
  9. kube-apiserver-izbp1dmodzug132aiko240z 1/1 Running 0 2m35s
  10. kube-controller-manager-izbp1dmodzug132aiko240z 1/1 Running 0 2m35s
  11. kube-proxy-hntxx 1/1 Running 0 2m25s
  12. kube-scheduler-izbp1dmodzug132aiko240z 1/1 Running 0 2m35s

CoreDNS、kube-controller-manager 等依赖于网络的 Pod 都处于 Pending 状态,即调度失败。这当然是符合预期的:因为这个 Master 节点的网络尚未就绪。

flannel 网络插件安装

查看版本
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

  1. apiVersion: apps/v1
  2. kind: DaemonSet
  3. metadata:
  4. name: kube-flannel-ds
  5. namespace: kube-system
  6. labels:
  7. tier: node
  8. app: flannel
  9. spec:
  10. selector:
  11. matchLabels:
  12. app: flannel
  13. template:
  14. metadata:
  15. labels:
  16. tier: node
  17. app: flannel
  18. spec:
  19. affinity:
  20. nodeAffinity:
  21. requiredDuringSchedulingIgnoredDuringExecution:
  22. nodeSelectorTerms:
  23. - matchExpressions:
  24. - key: kubernetes.io/os
  25. operator: In
  26. values:
  27. - linux
  28. hostNetwork: true
  29. priorityClassName: system-node-critical
  30. tolerations:
  31. - operator: Exists
  32. effect: NoSchedule
  33. serviceAccountName: flannel
  34. initContainers:
  35. - name: install-cni
  36. image: quay.io/coreos/flannel:v0.13.0

可能存在quay.io/coreos/flannel:v0.13.0 拉取失败的情况 可以使用 https://github.com/coreos/flannel/releases
的flanneld-v0.13.0-amd64.docker

图片.png
执行解压命令

  1. docker load < flanneld-v0.13.0-amd64.docker

去除master

  1. # kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. izbp1dmodzug132aiko240z Ready master 19m v1.19.3
  4. # kubectl taint nodes izbp1dmodzug132aiko240z node-role.kubernetes.io/master-

或者

  1. $ kubectl taint nodes --all node-role.kubernetes.io/master-

在“node-role.kubernetes.io/master”这个键后面加上了一个短横线“-”,这个格式就意味着移除所有以“node-role.kubernetes.io/master”为键的 Taint。

Hello

  1. kubectl create deployment nginx --image=nginx
  2. kubectl expose deploy nginx --type=NodePort --port=8080 --target-port=80 --name=nginx-svc

pod

  1. kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-6799fc88d8-xnd4t 1/1 Running 0 28s

svc

  1. # kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. nginx-svc NodePort 10.111.49.102 <none> 8080:30232/TCP 82s

执行

  1. # curl 10.111.49.102:8080
  2. .....
  3. <h1>Welcome to nginx!</h1>
  4. <p>If you see this page, the nginx web server is successfully installed and
  5. working. Further configuration is required.</p>
  6. ....

问题

  1. The connection to the server 127.0.0.1:32768 was refused - did you specify the right host or port?