首页 > 技术文章 > k8s环境搭建

faith007 2022-02-26 15:16 原文

一、安装vmware

1、在windows上安装虚拟机,在虚拟机上虚拟出3个linux系统,我这里分别是master,node1,node2,确保三个linux虚拟机之间可以互相访问

二、对安装之后的操作系统进行初始化

1、关闭防火墙,三台机器全关闭
systemctl stop firewalld
2、关闭selinux,三台机器全关闭
sed -i 's/enforcing/disabled/' /etc/selinux/config
3、关闭swap ,三台机器全关闭
swapoff -a
4、设置主机名
hostnamectl set-hostname 主机名,三个主机名分别设置成master,node1和node2
5、在master添加hosts
[root@localhost etc]# cat hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.117.128 master 192.168.117.129 node1 192.168.117.130 node2
6、将桥接的ipv4的流量传递到iptables的链,三台机器设置一样
cat > /etc/sysctl.d/k8s.conf <<EOF net.bridge.bridge‐nf‐call‐ip6tables= 1 net.bridge.bridge‐nf‐call‐iptables= 1 EOF
设置完成后要生效,执行sysctl --system
7、设置时间同步
yum install ntpdate -y
ntpdate time.windows.com

三、安装docker、kubeadm、kubectl等

1、安装docker

安装依赖包:
yum install -y yum-utils
设置镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker服务
yum install docker-ce docker-ce-cli containerd.io -y
启动docker服务
systemctl start docker

2、添加阿里云软件源,三台机器全部添加

cat > /etc/yum.repos.d/kubernetes.repo <<EOF [kubernetes] name=Kubernetes baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF

3、安装kubeadm、kubelet和kubectl,三台机器上安装

yum install -y kubeadm-1.19.0 kubelet-1.19.0 kubectl-1.19.0
systemctl enable kubelet

四、在master节点上部署kubetnets

kubeadm init --kubernetes-version=1.19.0 --apiserver-advertise-address=192.168.117.128 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16

当看到successfully就表示init成功了

接着执行命令:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
在node节点中执行命令:
kubeadm join 192.168.117.128:6443 --token lhuydx.c2lk64q5k5sg4fbt \ --discovery-token-ca-cert-hash sha256:325b4eec0c85e88418dd4ee0e4b0e95608cfbeedbd7607e83cc0b34eb16bd6cc
执行完命令后,在master节点中执行命令查看是否假如

五、部署CNI网络插件

在master机器上执行命令:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果出现拒绝,则在host添加如下即可
185.199.108.133 raw.githubusercontent.com
执行上述命令后,在master执行:
kubectl get pods -n kube-system
下图表示启动成功

六,集群验证

在kubenetes集群中创建一个pod
1、创建nginx
kubectl create deployment nginx --image=nginx
2、暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
3、查看端口
kubectl get pod,svc
4、任意一个node节点的ip加上端口访问

推荐阅读