K8S集群搭建及控制面板安装

K8S集群搭建及控制面板安装,第1张

K8S集群搭建及控制面板安装 环境准备

三台虚拟机

主机名配置IP地址master4G 4C192.168.0.200node12G 4C192.168.0.201node22G 4C192.168.0.202

注:没特意说明在那个节点执行意思就是全部都要

停止防火墙

systemctl disable firewalld
systemctl stop firewalld

禁用SELinux

# 永久 关闭
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 重启
reboot

#临时关闭
setenforce 0

关闭swap分区

# 永久
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 重启
reboot

# 临时关闭swap分区
swapoff -a

在master节点上添加hosts

cat >> /etc/hosts << EOF
192.168.0.200 master
192.168.0.201 node1
192.168.0.202 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
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOF
# 加载br_netfilter模块
modprobe br_netfilter
# 查看是否加载
lsmod | grep br_netfilter
# 生效
sysctl --system  

开启ipvs

yum -y install ipset ipvsadm
cat > /etc/sysconfig/modules/ipvs.modules < 

授权、运行、检查是否加载:

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

检查是否加载:

lsmod | grep -e ipvs -e nf_conntrack_ipv4

安装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.3.ce-3.el7

#启动docker并设置开机自启动
systemctl enable docker && systemctl start docker

设置Docker镜像加速:

# 创建docker配置目录
mkdir -p /etc/docker

# 创建镜像配置文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "exec-opts": ["native.cgroupdriver=systemd"],	
  "registry-mirrors": ["https://8wxxnmd4.mirror.aliyuncs.com"]
}
EOF
# 重启docker
systemctl daemon-reload
systemctl restart docker

配置kubeadm阿里云的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

由于版本更新频繁,这里指定版本号部署

yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0

为了实现Docker使用的cgroup drvier和kubelet使用的cgroup drver一致,建议修改"/etc/sysconfig/kubelet"文件的内容:

vim /etc/sysconfig/kubelet
# 修改
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"

设置为开机自启动即可,由于没有生成配置文件,集群初始化后自动启动:

systemctl enable kubelet
部署k8s的Master节点
# 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里需要指定阿里云镜像仓库地址
kubeadm init 
  --apiserver-advertise-address=192.168.0.200 
  --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

查看节点

[root@master ~]# kubectl get nodes
NAME     STATUS     ROLES    AGE     VERSION
master   NotReady   master   4m47s   v1.18.0
添加k8s的Node节点

在192.168.0.201和192.168.0.202上添加如下的命令:

# 向k8s集群中添加Node节点
kubeadm join 192.168.0.200:6443 --token axgy03.n8nbeak54a5dr1gm 
    --discovery-token-ca-cert-hash sha256:653055540c6cb5afe85dfc35a88d7634af2a57e86c602625edee3f7be2253068

默认的token有效期为24小时,当过期之后,该token就不能用了,这时可以使用如下的命令创建token:

# 生成一个新的token
kubeadm token create --print-join-command
# 生成一个永不过期的token
kubeadm token create --ttl 0
部署CNI网络插件

根据提示,在Master节点使用kubectl工具查看节点状态:

[root@master ~]# kubectl get nodes
NAME     STATUS     ROLES    AGE     VERSION
master   NotReady   master   5m50s   v1.18.0
node1    NotReady      13s     v1.18.0
node2    NotReady      13s     v1.18.0

在Master节点部署CNI网络插件(可能会失败,如果失败,请下载到本地,然后安装):

# 直接部署
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/documentation/kube-flannel.yml

# 下载后部署
wget https://raw.githubusercontent.com/coreos/flannel/master/documentation/kube-flannel.yml
[root@master k8s]# kubectl apply -f 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

查看部署CNI网络插件进度:

kubectl get pods -n kube-system

再次在Master节点使用kubectl工具查看节点状态:

kubectl get nodes

查看集群健康状态:

kubectl get cs

#查看集群信息
kubectl cluster-info

安装UI管理界面

在Mastr节点安装
下载recommended.yaml文件
项目开源地址 : https://github.com/kubernetes/dashboard

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.1/aio/deploy/recommended.yaml

修改yaml文件

新增type: NodePort 和 nodePort:31443,以便能实现非本机访问

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type: NodePort
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 31443
  selector:
    k8s-app: kubernetes-dashboard
kubectl apply -f recommended.yaml

创建一个admin-user

vim dashboard-adminuser.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
kubectl apply -f dashboard-adminuser.yaml

创建一个集群角色

vim dashboard-ClusterRoleBinding.yaml
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
kubectl apply -f dashboard-ClusterRoleBinding.yaml

获取token

kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

Name:         admin-user-token-fjr5w
Namespace:    kubernetes-dashboard
Labels:       
Annotations:  kubernetes.io/service-account.name: admin-user
              kubernetes.io/service-account.uid: 2889c0f4-2fbf-4005-ab06-c15873e89017

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  20 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IjQwcmFuMG9Gcll4b1BWVE5kQ3ZDZkhkN0RWdWFURk1xWFdfZHFsbjhrWHcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWZqcjV3Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIyODg5YzBmNC0yZmJmLTQwMDUtYWIwNi1jMTU4NzNlODkwMTciLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.l80AoTuM5g_rOOT4svNozuW8fYzgPT_Jo7jlx40LDoWOYx-uSoCsZN2OzYNafqanUBs5I0gC-7chAmS3w3G6PNUlKCi07ANySC2iPm22BFzFxMsTP8vnY64rAvUgP3nCjAGPY2LSJY5KoyUrVai0rjCJUFKVDX-aGA_2w7_hYJ1JmS0K8joeFf3UYzMghOqaPwiwlze6yc3be3oPuD0FJNTQ4zDFMi_QhoUUINLjFMYjDg2lweWzLlGBSCPq7g-lkfbUonPYYB9k2IMHdDbS6NyQt7JCIK0dpgR4tY8l28-PNqOkBTYJh9BI-9mlzPFiviGnmzkZ5M3R5GMoFbSJ6g

访问k8s集群UI

访问 https://you_k8s_server_ip:31443/

输入上边的token就可以登入了

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5703965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存