机器准备
IP | CPU | memory | role |
---|---|---|---|
172.168.20.135 | 4x | 16G | node1 |
172.168.20.136 | 4x | 32G | node2 |
172.168.20.137 | 4x | 8G | master |
基础环境
关闭、禁用防火墙
systemctl stop firewalld
systemctl disable firewalld
禁用selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
关闭swap分区
swapoff -a
vim /etc/fstab--->注释#/dev/mapper/centos-swap swap swap defaults 0 0
free -m
vim /etc/sysctl.conf--->添加vm.swappiness=0
sysctl -p
free -m检查swap是否为0
设置主机名
hostnamectl set-hostname k8smaster
master修改hosts文件
只在master执行:
cat >> /etc/hosts << EOF
172.168.20.137 k8smaster
172.168.20.135 k8snode1
172.168.20.136 k8snode2
EOF
将桥接的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
时间同步
yum install -y ntpdate
ntpdate ntp.aliyun.com
安装docker
添加docker-ce的yum源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum clean all
yum makecache
下载、启动
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
修改docker镜像源
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://yqsuaswv.mirror.aliyuncs.com"]
}
EOF
重启
systemctl restart docker
kubernetes安装
添加阿里云yum源
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
安装kubeadm,kubelet,kubectl
yum -y install kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet #设置开机启动
master init
设置master:初始化apiserver,controller等等组件(初始化完成后,可以使用docker images查看所有组件)
kubeadm init \
--apiserver-advertise-address=172.168.20.137 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
时间较长,耐心等待......
依据提示执行命令
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
将node添加入集群
(如果是加入过master的node,需要重新配置master,需要先执行kubeadm reset
)
node节点中添加到master中(以下命令来自于master init后的提示,重新安装master时直接复制提示最后几行即可):
kubeadm join 172.168.20.137:6443 --token sdham3.1wfuxhn1m46ecs37 \
--discovery-token-ca-cert-hash sha256:aecb57a29ea2649b9ce070ccbed3446612358280296051915258e39820cd83ef
安装网络插件CNI
master安装pod网络插件CNI:(国内无法访问raw.githubusercontent.com,修改/etc/hosts文件,添加199.232.68.133 raw.githubusercontent.com)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看集群状态
查看所有组件,是否都是running状态,如果不是,等待docker组件pull(这里比较耗时)。(可以使用kubectl get pods -n kube-system命令,查看所有docker组件拉取情况)
没问题后,查看kubectl get nods,全部都是ready状态,表示集群的搭建成功
测试
测试kubernetes集群,部署一个nginx,并开放80端口
kubectl create deployment nginx --image=nginx
kubectl get pods
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
创建secret
kubectl create secret docker-registry registry-secret --docker-server=registry.cn-beijing.aliyuncs.com --docker-username=qiu_yq --docker-password=yishan@006892 --docker-email=[email protected] -n default
--docker-server: 仓库地址
--docker-username: 仓库登陆账号
--docker-password: 仓库登陆密码
--docker-email: 邮件地址(选填)
-n 命名空间
导出yaml文件
kubectl create deployment nginx --image=nginx -o yaml --dry-run > my-deployment.yaml
创建deployment
kubectl create -f deployment.yaml