Kubernetes快速部署
安装要求
在开始之前,部署Kubernetes集群机器需要满⾜以下⼏个条件:
- ⾄少3台机器,操作系统 CentOS7+
- 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
- 集群中所有机器之间⽹络互通
- 可以访问外⽹,需要拉取镜像
- 禁⽌swap分区
####准备环境
角色 | ip |
---|---|
master | 192.168.31.140 |
node1 | 192.168.31.130 |
node2 | 192.168.31.138 |
master上
关闭防⽕墙:
[root@localhost ~]# systemctl disable --now firewalld
关闭selinux:
[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
关闭swap:
[root@localhost ~]# vim /etc/fstab
注释掉swap分区那一条
设置主机名:
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
node1上
//关闭防⽕墙:
[root@localhost ~]# systemctl disable --now firewalld
//关闭selinux:
[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
关闭swap:
[root@localhost ~]# vim /etc/fstab
注释掉swap分区那一条
设置主机名:
[root@localhost ~]# hostnamectl set-hostname node1.example.com
[root@localhost ~]# bash
node2上
关闭防⽕墙:
[root@localhost ~]# systemctl disable --now firewalld
关闭selinux:
[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
关闭swap:
[root@localhost ~]# vim /etc/fstab
注释掉swap分区那一条
设置主机名:
[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
#master上
添加hosts
[root@master ~]# cat >> /etc/hosts << EOF
> 192.168.31.140 master master.example.com
> 192.168.31.130 node1 node1.example.com
> 192.168.31.138 node2 node2.example.com
> EOF
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.31.140 master master.example.com
192.168.31.130 node1 node1.example.com
192.168.31.138 node2 node2.example.com
将桥接的IPv4流量传递到iptables的链
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@master ~]# sysctl --system
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-coredump.conf ...
…………
时间同步
[root@master ~]# yum -y install chrony
[root@master ~]# systemctl enable --now chronyd
//三台机都需要
免密认证
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
……
把密钥传出去
[root@master ~]# ssh-copy-id master
[root@master ~]# ssh-copy-id node1
[root@master ~]# ssh-copy-id node2
测试免密是否做成功
[root@master ~]# for host in master node1 node2;do ssh $host 'date';done
2021年 08月 24日 星期二 23:43:06 EDT
2021年 08月 24日 星期二 23:43:06 EDT
2021年 08月 24日 星期二 23:43:06 EDT
所有节点安装Docker/kubeadm/kubele
安装docker
每台主机都需要
[root@master ~]# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[root@master ~]# yum -y install docker-ce
[root@master ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@master ~]# cat > /etc/docker/daemon.json << EOF
> {
> "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
> "exec-opts": ["native.cgroupdriver=systemd"],
> "log-driver": "json-file",
> "log-opts": {
> "max-size": "100m"
> },
> "storage-driver": "overlay2"
> }
> EOF
添加kubernetes阿⾥云YUM软件源
[root@master ~]# cat /etc/yum.repos.d/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
安装kubeadm,kubelet和kubectl
也是需要在所有的主机上操作
[root@master ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0 //需要指定版本号,版本也不要用到最新的
[root@master ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.
部署Kubernetes Master
在192.168.31.140(Master)执⾏
[root@master ~]# kubeadm init \
> --apiserver-advertise-address=192.168.31.140 \
> --image-repository registry.aliyuncs.com/google_containers \
> --kubernetes-version v1.20.0 \
> --service-cidr=10.96.0.0/12 \
> --pod-network-cidr=10.244.0.0/16
第一次可能由于缺少软件包没有
[root@master ~]# yum -y install iproute-tc
安装这个 在执行一边
……
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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.31.140:6443 --token v93eje.r5ui8fxta7xa56af \
--discovery-token-ca-cert-hash sha256:8ba15791c8bf91b52b461ffa3d95b8590719b43e0bac981**这是后面加入进来的命令
由于默认拉取镜像地址k8s.gcr.io国内⽆法访问,这⾥指定阿⾥云镜像仓库地址。使⽤kubectl⼯具
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@master ~]# kubectl get nodes //查看有哪些节点
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 2m44s v1.20.0
安装Pod⽹络插件(CNI)//所有都要做
[root@master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
确保能够访问到quay.io这个registery
//每台主机上都要有,不然不识别
[root@master ~]# scp /etc/hosts node1:/etc/
hosts 100% 277 361.6KB/s 00:00
[root@master ~]# scp /etc/hosts node2:/etc/
hosts 100% 277 48.3KB/s 00:00
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master.example.com Ready control-plane,master 10m v1.20.0
node1.example.com Ready <none> 89s v1.20.0
node2.example.com Ready <none> 82s v1.20.0
在Kubernetes集群中创建⼀个pod,验证是否正常运⾏
[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
[root@master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-6799fc88d8-hbj7q 0/1 ContainerCreating 0 14s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19m
service/nginx NodePort 10.98.72.65 <none> 80:31040/TCP 5s
如果加不进去,可能是token过期了,需要重新生成,在用新产生的join就行
[root@master ~]# kubeadm token generate #生成token
89iyqj.gn056whesto7mkzc
[root@master ~]# kubeadm token create 89iyqj.gn056whesto7mkzc --print-join-command --ttl=0
kubeadm join 192.168.31.140:6443 --token 89iyqj.gn056whesto7mkzc --discovery-token-ca-cert-hash sha256:8ba15791c8bf91b52b461ffa3d95b8590719b43e0bac9816e18e00af1eeea9fe