主机名 | CPU | 内存 |
---|---|---|
k8smaster | 2核 | 4G |
k8snode1 | 2核 | 4G |
k8snode2 | 2核 | 4G |
cat << EOF > /etc/hosts
192.168.240.130 k8smaster
192.168.240.132 k8snode1
192.168.240.133 k8snode2
EOF
hostnamectl set-hostname k8smaster
hostnamectl set-hostname k8snode1
hostnamectl set-hostname k8snode2
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#让配置文件生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
systemctl disable firewalld && systemctl stop firewalld
yum install ntpdate -y
ntpdate time.windows.com
systemctl stop docker
yum remove -y docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
yum -y install gcc
yum -y install gcc-c++
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce docker-ce-cli containerd.io
systemctl enable docker && systemctl start docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
cat <<EOF > kubernetes.repo
[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
mv kubernetes.repo /etc/yum.repos.d/
yum remove -y kubelet kubeadm kubectl && yum install -y kubelet-1.16.2 kubectl-1.16.2 kubeadm-1.16.2 --disableexcludes=kubernetes
systemctl enable kubelet
mkdir k8s-install && cd k8s-install
kubeadm config print init-defaults > kubeadm.yaml
#修改生成好的配置文件镜像与版本
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.169.240.130
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8smaster
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers #修改镜像源
kind: ClusterConfiguration
kubernetesVersion: v1.16.2 #修改版本
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 #pod网络
serviceSubnet: 10.96.0.0/12
scheduler: {}
kubeadm config images pull --config kubeadm.yaml
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 192.168.240.130:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:1877573f37b7fd0bc9d8e38cf8d68f17f51e64a55606f7b657f3cdba79469f16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看集群节点状态
kubectl get nodes
此时只有master节点是NotReady状态
kubeadm join 192.168.240.130:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:1877573f37b7fd0bc9d8e38cf8d68f17f51e64a55606f7b657f3cdba79469f16
再次查看master节点信息
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
编辑kube-flannel.yml
改成虚拟机的网卡名称
docker pull quay.io/coreos/flannel:v0.11.0-amd64
kubectl create -f kube-flannel.yml
kubectl get pods -A