K8S集群部署


 ###############   K8S集群部署  ###############
### 版本

kubelet-1.18.0
kubeadm-1.18.0
kubectl-1.18.0
docker-18.06.1
calico-v3.20

### 机器

172.31.16.2 k8s-master
172.31.16.4 k8s-node1
172.31.16.5 k8s-node2


### 环境准备(所以机器执行)

关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld

关闭selinux:
$ sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
$ setenforce 0 # 临时

关闭swap:
$ swapoff -a # 临时
$ vim /etc/fstab # 永久

设置主机名:
$ hostnamectl set-hostname <hostname>

在master添加hosts:
$ cat >> /etc/hosts << EOF
172.31.16.2 k8s-master
172.31.16.4 k8s-node1
172.31.16.5 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

 

### Docker安装 (所以环境)

1、下载
$ 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
Docker version 18.06.1-ce, build e68fc7a

2、配置下镜像仓库地址:
# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF

### 安装kube三件套 (所以环境)

1、添加K8S 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

2、安装kubeadm,kubelet和kubectl
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

### 部署 K8S master
1、初始化
kubeadm init \
--apiserver-advertise-address=172.31.16.2 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.92.0.0/12 \
--pod-network-cidr=10.220.0.0/16 \
--ignore-preflight-errors=all

2、部署完成后获取node加入集群的命令以及token:(这里每个集群都不一样)
kubeadm join 172.31.16.2:6443 --token 733dim.91wwhdfgqn8c461r \
--discovery-token-ca-cert-hash sha256:6f4f75170f512fc2fb11f13637023fbb56734b301826bf53485a9e6e75b60f42

3、配置kubeconfig相关参数
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

### 加入 K8S node (node节点操作)
1、使用kubeadm加入集群
kubeadm join 172.31.16.2:6443 --token 733dim.91wwhdfgqn8c461r \
--discovery-token-ca-cert-hash sha256:6f4f75170f512fc2fb11f13637023fbb56734b301826bf53485a9e6e75b60f42

 

### 部署网络插件【CNI】

1、下载
curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O ###docker版本18与kubelet版本1.18是匹配V3.20 calico.yaml的

2、修改CALICO_IPV4POOL_CIDR
- name: CALICO_IPV4POOL_CIDR
value: "10.220.0.0/16"10.220.0.0/16前面初始化时候预留了网段)
3、启动
kubectl apply -f calico.yaml
kubectl get pods -n kube-system

### 验证集群运行情况
kubectl get node
### 附加 (未认证)
据说docker版本19.03.15与kubelet版本1.23.1是匹配的 wget https://docs.projectcalico.org/manifests/calico.yaml


############################### 可视化部署 Dashboard UI
1、下载
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml

2、修改并运行

vim recommended.yaml
spec:
ports:
- port: 443
targetPort: 8443
# 暴露的端口30080
nodePort: 30080 #添加大概40行左右
# 以NodePort方式暴露端口
type: NodePort #添加

kubectl apply -f recommended.yaml

3、访问
https://宿主机IP:30080 (要使用https)

4、创建访问账号
vi dash.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
# 对象的名称
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard

 


-----------------------------------
5、运行
kubectl apply -f dash.yaml

6、获取token(打开网页时候用到)
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"

posted @ 2022-10-18 19:56  博客威  阅读(40)  评论(0编辑  收藏  举报