基础环境
操作系统
$ cat /etc/issue [22:34:09]
Ubuntu 16.04.6 LTS \n \l
cpu
$ lscpu [22:28:57]
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
内存
$ free -h [22:30:26]
total used free shared buff/cache available
Mem: 3.9G 293M 2.9G 4.0M 656M 3.3G
Swap: 0B 0B 0B
docker
$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
安装
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - [22:37:06]
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
安装docker
$ sudo apt-get update
$ sudo apt-get install -y docker-ce docker-ce-cli containerd.io
查看docker 版本
$ sudo docker version [22:39:21]
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:49 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:44:20 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
关闭
$ sudo vi /etc/modules
$ sudo vi /etc/sysctl.d/k8s.conf
$ sudo swapoff -a [22:44:44]
$ sudo sed -i 's/.*swap.*/#&/' /etc/fstab
切换
sudo su root
# curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#cat > /etc/apt/sources.list.d/aliyun-k8s.list <<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
# apt update
# apt install -y kubeadm kubelet kubectl ipvsadm ipset
安装
# kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
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]
[init] Using Kubernetes version: v1.19.3
[preflight] Running pre-flight checks
[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/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[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]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [izbp1dmodzug132aiko240z localhost] and IPs [10.111.118.193 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [izbp1dmodzug132aiko240z localhost] and IPs [10.111.118.193 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[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
[apiclient] All control plane components are healthy after 18.502143 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node izbp1dmodzug132aiko240z as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node izbp1dmodzug132aiko240z as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: o0runy.uikvs46vtfxidq0s
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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.111.118.193:6443 --token o0runy.uikvs46vtfxidq0s \
--discovery-token-ca-cert-hash sha256:0fc36ea0a82cca6d155ef893f4646ecba10e67b355a54a9cfbca27fbea1a210b
配置文件
# mkdir -p $HOME/.kube
# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# chown $(id -u):$(id -g) $HOME/.kube/config
验证集群信息
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
izbp1dmodzug132aiko240z NotReady master 2m26s v1.19.3
# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6d56c8448f-4krmb 0/1 Pending 0 2m25s
coredns-6d56c8448f-9svcn 0/1 Pending 0 2m25s
etcd-izbp1dmodzug132aiko240z 1/1 Running 0 2m34s
kube-apiserver-izbp1dmodzug132aiko240z 1/1 Running 0 2m35s
kube-controller-manager-izbp1dmodzug132aiko240z 1/1 Running 0 2m35s
kube-proxy-hntxx 1/1 Running 0 2m25s
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
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
hostNetwork: true
priorityClassName: system-node-critical
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
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
执行解压命令
docker load < flanneld-v0.13.0-amd64.docker
去除master
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
izbp1dmodzug132aiko240z Ready master 19m v1.19.3
# kubectl taint nodes izbp1dmodzug132aiko240z node-role.kubernetes.io/master-
或者
$ kubectl taint nodes --all node-role.kubernetes.io/master-
在“node-role.kubernetes.io/master”这个键后面加上了一个短横线“-”,这个格式就意味着移除所有以“node-role.kubernetes.io/master”为键的 Taint。
Hello
kubectl create deployment nginx --image=nginx
kubectl expose deploy nginx --type=NodePort --port=8080 --target-port=80 --name=nginx-svc
pod
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-xnd4t 1/1 Running 0 28s
svc
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc NodePort 10.111.49.102 <none> 8080:30232/TCP 82s
执行
# curl 10.111.49.102:8080
.....
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
....
问题
The connection to the server 127.0.0.1:32768 was refused - did you specify the right host or port?