k8S
部署
1.三台节点
master : 144
node1 : 145
node2 : 146
2.系统初始化
关闭防火墙
yum -y install wget
systemctl stop firewalld
systemctl disable firewalld
关闭 selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
关闭 swap:
#关闭 swap:
swapoff -a # 临时
vim /etc/fstab #
cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
设置主机名 分别设置
hostnamectl set-hostname k8s-master #master节点
hostnamectl set-hostname k8s-node1 #node节点
hostnamectl set-hostname k8s-node2 #node节点
在 master 添加 hosts
cat >> /etc/hosts << EOF
192.168.92.138 k8s-master
192.168.92.145 k8s-node1
192.168.92.146 k8s-node2
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 ntpdate -y
ntpdate time.windows.com
3.安装
所有节点安装 Docker/kubeadm/kubelet
安装 Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
设置仓库地址
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
添加 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 install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
部署Kubernetes Master(主节点执行)
#在192.168.92.144(Master)执行。 address:主机ip repository:阿里云仓库 cidr:ip,不冲突就行,没有别的要求
#kubeadm reset
kubeadm init --apiserver-advertise-address=192.168.92.144 \
--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
使用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 get nodes
加入Kubernetes Node(丛节点进行)
#这一段是虚拟机上考下来的
kubeadm join 192.168.92.138:6443 --token if89zy.ijlfug0watesdieo \
--discovery-token-ca-cert-hash sha256:9c41336a59cef683f2dffc99a94dd450793244bdb7f68a3eb25051e005fe519e
#重新创建token
kubeadm token create --print-join-command
部署CNI网络插件
#wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#主节点执行
kubectl apply -f ./kube-flannel.yml
kubectl get pods -n kube-system
#设置节点role名称
kubectl label node k8s-node1 node-role.kubernetes.io/worker=worker
kubectl label node k8s-node2 node-role.kubernetes.io/worker=worker
#查看节点
kubectl get nodes
测试kubernetes集群
#拉取镜像
kubectl create deployment nginx --image=nginx
#保楼端口
kubectl expose deployment nginx --port=80 --type=NodePort
#查看镜像是否running
kubectl get pod
#查看提供服务的端口
[root@k8s-master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-f89759699-4kqwg 1/1 Running 0 5m28s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29m
service/nginx NodePort 10.106.160.166 <none> 80:30027/TCP 5m24s
#测试 访问node节点的ip+端口
http://192.168.92.146:30027