操作系统:CentOS 7.5
硬件配置:4C8G50G
一共两台机器
2.1. 修改主机名
hostnamectl set-hostname knative-master
hostnamectl set-hostname knative-node1
2.2. 关闭防火墙
systemctl disable firewalld
systemctl stop firewalld
2.3. 禁用SELinux
setenforce 0
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
2.4. 关闭Swap
swapoff -a
sed -i '/.*swap.*/ s/^[a-Z]/#/' /etc/fstab
2.5. 将桥接的IPv4流量传递到iptables链【CentOS必须做这一步】
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
sysctl --system
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum install docker-ce-18.06.3.ce-3.el7 -y
systemctl enable docker && systemctl start docker
docker -v
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 --disableexcludes=kubernetes -y
systemctl enable kubelet
5.1. 生成集群配置文件
mkdir /opt/k8s && cd /opt/k8s
kubeadm config print init-defaults > init.default.yaml
advertiseAddress 填写Master的IP地址
imageRepository 拉取镜像的仓库地址
kubernetesVersion K8S的版本
serviceSubnet 指定Pod的地址范围
完整内容
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 10.168.41.129
bindPort: 6443
nodeRegistration:
taints:
- effect: PreferNoSchedule
key: node-role.kubernetes.io/master
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
imageRepository: registry.aliyuncs.com/google_containers
kubernetesVersion: v1.16.2
networking:
podSubnet: 10.244.0.0/16
执行命令
kubeadm config images pull --config=init.default.yaml
5.2. 运行kubeadm init安装Master
kubeadm init \
--apiserver-advertise-address=10.168.41.129 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.16.2 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
--service-cidr #表示service分配的IP段,也就是cluster ip所在的网段
--pod-network-cidr #表示pod ip所在的网段
5.3. 使kubectl命令可以正常使用
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
如果其他节点需要用kubectl命令,需要把Master上的/etc/kubernetes/admin.conf发送过去,执行同样的操作即可
5.4. 允许master节点部署pod
由于我只有两台机器,让master也可以部署pod
kubectl taint nodes --all node-role.kubernetes.io/master-
6.1. 获取Master上的token
kubeadm token list
如果token过期,可用下面的命令生成永久token
kubeadm token create --ttl 0
6.2. 创建join-config.yaml
mkdir /opt/k8s && cd /opt/k8s
token和 tlsBootstrapToken为上一步获取的token
cat > join-config.yaml << EOF
apiVersion: kubeadm.k8s.io/v1beta2
kind: JoinConfiguration
discovery:
bootstrapToken:
apiServerEndpoint: 10.168.41.129:6443
token: iyfkbf.2rol062xun1ndsed
unsafeSkipCAVerification: true
tlsBootstrapToken: iyfkbf.2rol062xun1ndsed
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: knative-node1
EOF
6.3. 加入集群
如果node不能访问外网,需要把Master上的kube-proxy、pause镜像导入过去
kubeadm join --config=join-config.yaml
wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml --no-check-certificate
#镜像拉取比较慢,可以先手动拉取
docker pull quay.io/coreos/flannel:v0.11.0-amd64
kubectl apply -f kube-flannel.yml
如果node不能访问外网,需要把Master上的flannel镜像导入过去
kubectl get nodes
kubectl get pod -n kube-system
kubectl create deployment nginx --image=nginx #会去初始化指定的镜像仓库拉取nginx镜像,需要等待
kubectl expose deployment nginx --port=80 --type=NodePort #声明内部通信端口为80,外部以节点IP加端口访问
kubectl get pods,svc -o wide #查看详细信息
在集群内部可以通过10.244.0.8访问Nginx,在集群外部可以通过任意节点物理机IP:30527端口访问Nginx,如10.168.41.129:30527