概述
简介
k8s 有很多种安装方式
本文使用kubeadm安装
软件环境
Centos: 7.3
Docker: 18.06.1-ce
Kubernetes: 1.16.2
实验服务器信息
服务器建议2核4G以上配置
master: 192.168.100.119
node01: 192.168.100.200
node02: 192.168.100.201
环境准备
所有节点都执行
修改主机名
管理节点
hostnamectl set-hostname master
其他节点
hostnamectl set-hostname node01
hostnamectl set-hostname node02
…
添加hosts解析
cat <>/etc/hosts
192.168.100.119 master
192.168.100.200 node1
192.168.100.201 node2
EOF
关闭防火墙、selinux和swap
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
配置内核参数,将流量转到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源
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
配置国内k8s源
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=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
配置docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
软件安装
docker&&kubectl安装
所有机器节点执行
安装docker
yum install -y docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker version
安装kubeadm、kubelet、kubectl
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
部署master节点
在master节点执行
初始化k8s集群
kubeadm init --kubernetes-version=1.16.2
–apiserver-advertise-address=192.168.100.119
–image-repository registry.aliyuncs.com/google_containers
–service-cidr=10.1.0.0/16
–pod-network-cidr=10.244.0.0/16
定义POD的网段为: 10.244.0.0/16, api server地址就是master本机IP地址。
这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址。
执行成功后会出现下面的信息:
kubeadm join 192.168.100.119:6443 --token kekvgu.nw1ndasdaoncomj6
–discovery-token-ca-cert-hash sha256:1s3chdadqwop08ca62f2dou983h40jnafa4d50e6634acfaa8291f28582codaihsdoinci
配置kubectl工具
mkdir -p /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config
kubectl get nodes
kubectl get cs
部署flannel网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
部署node节点
在所有node节点上执行
加入k8s集群
kubeadm join 192.168.100.119:6443 --token kekvgu.nw1ndasdaoncomj6
–discovery-token-ca-cert-hash sha256:1s3chdadqwop08ca62f2dou983h40jnafa4d50e6634acfaa8291f28582codaihsdoinci
此命令为集群初始化时(kubeadm init)返回结果中的内容。
集群状态检测
在master节点上执行
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 3d5h v1.14.3
node1 Ready 3d5h v1.14.3
node2 Ready 3d5h v1.14.3
部署Dashboard
在master上操作
创建Dashboard的yaml文件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
1
编辑kubernetes-dashboard.yaml文件
sed -i ‘s/k8s.gcr.io/registry.cn-hangzhou.aliyuncs.com/kuberneters/g’ kubernetes-dashboard.yaml
sed -i ‘/targetPort:/a nodePort: 30001n type: NodePort’ kubernetes-dashboard.yaml
部署Dashboard
kubectl create -f kubernetes-dashboard.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
查看访问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}’)
返回信息
Name: dashboard-admin-token-xr4rq
Namespace: kube-system
Labels:
Annotations: kubernetes.io/service-account.name: dashboard-admin
kubernetes.io/service-account.uid: 6b103593-ee1f-11e9-93b4-001a4ae62b23
Type: kubernetes.io/service-account-token
ca.crt: 1025 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4teHI0cnEiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNmIxMDM1OTMtZWUxZi0xMWU5LTkzYjQtMDAxYTRhZTYyYjIzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.WlETClsc1_AY5_b3stvMArjVCnZvt_pvIzd7bKjRQY14P7bWRciCCVk611Drcr7uPXl1oCvUUHvi6g3nwtec9sG7BRqWl5hPAMmzI9xZlr_N1FRkWL520mSIP0yHtXYBBVIkRheyXrOkz4VeGsHBCOv5jQLl9Mo-hHhIdMuTfKNSWmdmLv50xgi_tYdb9JokswIGjJ2oyzLyXqJ8nFvsMiKyfvwgQ8kixBeKusLx5oUroqJfRVVakYfxcqpxpD2ywe9qhoZC0xWlpx07YDogA5EB8cyClnJ-sgNE3emEMRNdxC_XyTxhJ1CnvpKo6_t16TFdGAz6QG-lgTLgZa5xGQ
浏览器打开
https://192.168.100.119:30001
再输入上面服务器返回的token
最后
以上就是调皮中心为你收集整理的k8s安装的全部内容,希望文章能够帮你解决k8s安装所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复