我是靠谱客的博主 体贴绿草,最近开发中收集的这篇文章主要介绍kubernetes(K8s)自动化安装一键部署一、每个节点先执行公共脚本 二、部署Master节点,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

环境:我的机器配置:

IP地址

节点角色

CPU

Memory

Hostname

磁盘

192.168.25.150

master

>=2c

>=2G

master

sda、sdb

192.168.25.151

worker

>=2c

>=2G

node1

sda、sdb

192.168.25.152

worker

>=2c

>=2G

node2

sda、sdb

PS:机器内存必须大于2G  否则报错

一、每个节点先执行公共脚本

每个节点先设置名称:

192.168.25.150:

hostnamectl set-hostname master

192.168.25.151:

hostnamectl set-hostname node1

192.168.25.152:

hostnamectl set-hostname node2

下面是一键脚本(注意hosts里面要写每个node的ip)

可以写到一个文件里面批量执行

cat <<EOF >>/etc/hosts
 
192.168.182.133 master
 
192.168.182.134 node1
 
EOF

systemctl stop firewalld
 
systemctl disable firewalld
 
setenforce 0
 
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
 
swapoff -a
 
sed -i 's/.*swap.*/#&/' /etc/fstab

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 wget
 
mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
 
yum clean all && yum makecache

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
 
[kubernetes]
 
name=Kubernetes
 
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
 
enabled=1
 
gpgcheck=1
 
repo_gpgcheck=1
 
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
 
EOF
 
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
 
yum install -y docker-ce-18.06.1.ce-3.el7
 
systemctl enable docker && systemctl start docker
 
docker –version
 
docker version 18.06.1-ce, build e68fc7a
 
yum install -y kubelet kubeadm kubectl
 
systemctl enable kubelet

 


 

二、部署Master节点

1kubeadm 执行安装(注意前面yum安装的版本  在最后安装完成后有log打印  版本千万要对上  不然重来)

kubeadm init --kubernetes-version=1.15.0 --apiserver-advertise-address=192.168.25.150 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

Ps:注意前一步yum安装的 kubelet kubeadm kubectl 的版本

集群初始化成功后返回如下信息:

kubeadm join 192.168.25.150:6443 --token v0h9d5.y6ryogyngkjv8463 --discovery-token-ca-cert-hash sha256:ec13b19b3b97fa8cf308e9a7e744a639f1ab4bc124b692931934ad6b9cf747f2

记录生成的最后部分内容,此内容需要在其它节点加入Kubernetes集群时执行。

2、配置kubectl

mkdir -p /root/.kube

cp /etc/kubernetes/admin.conf /root/.kube/config

kubectl get nodes

kubectl get cs

3.部署flannel网络

kubectl apply -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

 

#到这里 可以先让node加入集群

----------------------------

4、部署Dashboard

// 获取yaml文件

wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

修改下载的kubernetes-dashboard.yaml文件

service模块配置暴露k8s集群外部访问端口,配置如下:

# ------------------- Dashboard Service ------------------- #

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  type: NodePort
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001
  selector:
    k8s-app: kubernetes-dashboard

修改与文件镜像

       image: cuiyf/kubernetes-dashboard-amd64:v1.10.1

启动Dashboard

kubectl create -f kubernetes-dashboard.yaml

使用令牌登录(需要创建能够访问 Dashboard 的用户)

account.yaml 配置如下:

# Create Service Account
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
---
# Create ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system

创建用户:

[root@master yaml]# kubectl create -f account.yaml

// 检查状态

kubectl get deployment kubernetes-dashboard -n kube-system

kubectl get pods -n kube-system -o wide

kubectl get services -n kube-system

netstat -ntlp|grep 30001

在Firefox浏览器输入Dashboard访问地址:https://192.168.25.150:30001(Chrome玩不了)

// 查看访问Dashboard的认证令牌

kubectl create serviceaccount  dashboard-admin -n kube-system

kubectl create clusterrolebinding  dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin

kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

#下面是登陆dashboard的令牌

eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tZ3NxaHEiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMmM0OGJiYWEtYWI4YS00MzA0LWFhYTktZDk2YzY2ODVhMDlkIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.b6fu2KxfJi7vTR7rUL5qOY_M3HJuCMaaAdcFioLqF9AoBg8bdy1OgGcv2PCNa-06KVip1pOz8-EsiNE81wI2pELfLu47eCTtqfLQSi_MF1LYm5jD5JLO0Cym5Vohf_cTi0DCOJZnO4TbWAxAxYb5xw6_TySbnOOvyrB6CVR_IWUZukLquHoF1s7Xg3km5NU8XOP973IOYpGnYGkXHT9aMHEUOfWxKfQ8LRgIvlvs2fwxeY7UNxajUVKlIEYNLP-zxU1FB7ZHySMUj-t6wMXU-91LY7BOEASnu8ad2PUs5a5BZA7CtriMXmJGGkStvlaW5Isc7kIKyoEgQTXLH4enTQ

三、部署Node节点

#只需加入集群即可

kubeadm join 192.168.25.150:6443 --token v0h9d5.y6ryogyngkjv8463 --discovery-token-ca-cert-hash sha256:ec13b19b3b97fa8cf308e9a7e744a639f1ab4bc124b692931934ad6b9cf747f2

安装完成:

master节点   kubectl get nodes查看即可


  PS:

  • CPU最少2H 内存最少2G  不然kubeadm会报错
  • 如果部署失败,可以执行kubeadm reset清理环境
  • 确定集群至少有一台cluster,并且可以正常通信,不然kubeadm会报错

最后

以上就是体贴绿草为你收集整理的kubernetes(K8s)自动化安装一键部署一、每个节点先执行公共脚本 二、部署Master节点的全部内容,希望文章能够帮你解决kubernetes(K8s)自动化安装一键部署一、每个节点先执行公共脚本 二、部署Master节点所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部