这里提供一个云原生快速入门部署K8S集群的方式,我使用的是三台虚拟机
Master:192.168.63.133
node1:192.168.63.134
node2:192.168.63.135
首先需要同时更新一下三个机器的repo为安装docker准备:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker并设置开机自启
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
配置docker镜像源daemon.json:
vim /etc/docker/daemon.json
{
"registry-mirrors": [
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://zeco842d.mirror.aliyuncs.com"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
在三台机器上配置k8s.repo
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装k8s,我这里使用的是1.14.3版本:
yum install -y kubelet-1.14.3 kubeadm-1.14.3 kubectl-1.14.3
到此为止,我们在三个机器上的基础环境设置已经完成,接下来我们配置Master以及Node节点
Master:
kubeadm config print init-defaults > kubeadm-config.yaml #生成配置文件模板
修改配置文件钟advertiseAddress为当前Master节点IP地址
vim kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta1
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.63.133
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: mysql1
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta1
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: ""
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.14.0
networking:
dnsDomain: cluster.local
podSubnet: ""
serviceSubnet: 10.96.0.0/12
scheduler: {}
拉取镜像源:
kubeadm config images pull --config kubeadm-config.yaml
初始化Master节点:
kubeadm init --config kubeadm-config.yaml
———————————————如果报错—————————————————————————
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`执行:echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
到这一步我们就成功了,可以看输出内容如下:
To start using your cluster, you need to run the follr:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/c
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
https://kubernetes.io/docs/concepts/cluster-adminis
Then you can join any number of worker nodes by runnich as root:
kubeadm join 192.168.63.133:6443 --token 0aiq9x.05epkn5wt5ycsny8 --discovery-token-ca-cert-hash sha256:04778f77f4b5016de529aadf20b815deda7918b2ad616190e4361fb13cad8364
此时我们可以输入:kubectl get node 查看当前节点状态,稍等一会就会变成Ready~~~
在Master节点执行上面的输出内容:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/c
sudo chown $(id -u):$(id -g) $HOME/.kube/config
在其他节点上执行:
kubeadm join 192.168.63.133:6443 --token 0aiq9x.05epkn5wt5ycsny8 --discovery-token-ca-cert-hash sha256:04778f77f4b5016de529aadf20b815deda7918b2ad616190e4361fb13cad8364
——————————————如果报错——————————————————————————
error execution phase preflight: couldn't validate the identity of the API Server: expected a 32 byte SHA-256 hash, found 21 bytes重新在master拉取新的token
kubeadm token create --print-join-command
到这里我们再次输入:kubectl get node 就可以查看到我们其他节点也已经加入集群啦~
大功告成,如果你的状态一直为NoReady,输入journalctl -f -u kubelet查看日志,我在这个过程钟出现报错
根据报错可以看到是网络插件出现了问题,我们执行
kubectl -n kube-system apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
flannel这是网络插件,问题解决~~~