ubuntu20.04安装k8s

环境初始化

#关闭swap
swapoff -a
rm -f /swap.img
vim /etc/fstab
# /swap.img

#开启ip转发
vim /etc/sysctl.conf
net.ipv4.ip_forward=1

#查看状态
sysctl -p

安装docker

step1:安装依赖

sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

step 2: 安装GPG证书

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

Step 3: 写入软件源信息

sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

Step 4: 更新并安装Docker-ce

sudo apt-get -y update
apt install -y docker-ce

安装docker-compose

apt install -y docker-compose

将docker设置为开机自启

systemctl enable docker

安装k8s

添加证书

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

添加apt源

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF  

apt-get update

查看可安装版本

apt-cache madison kubelet

安装指定版本

apt-get install -y kubelet=1.18.4-00 kubeadm=1.18.4-00 kubectl=1.18.4-00

设置开机启动

sudo systemctl enable kubelet && sudo systemctl start kubelet

查看所需镜像

kubeadm config images list --kubernetes-version=v1.18.4

k8s.gcr.io/kube-apiserver:v1.18.4
k8s.gcr.io/kube-controller-manager:v1.18.4
k8s.gcr.io/kube-scheduler:v1.18.4
k8s.gcr.io/kube-proxy:v1.18.4
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7

从新地址下载镜像

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7

为镜像重新打tag

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.4 k8s.gcr.io/kube-apiserver:v1.18.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.4 k8s.gcr.io/kube-controller-manager:v1.18.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.4 k8s.gcr.io/kube-scheduler:v1.18.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.4 k8s.gcr.io/kube-proxy:v1.18.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 k8s.gcr.io/coredns:1.6.7

初始化master节点

kubeadm init --kubernetes-version=v1.18.4 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

初始化完成之后会出现提示,Your Kubernetes control-plane has initialized successfully!,之后将后面的内容保存到一个文件中,先不添加节点检查集群状态是否正常。

kubectl get componentstatus

添加网络组件

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

检查状态

kubectl get pods -n kube-system -l app=flannel

检查节点是否准备完成

kubectl get nodes

为node的角色打标签

kubectl label node node01 node-role.kubernetes.io/node=node

部署Prometheus+Grafana

#获取相关文件
git clone https://gitee.com/liugpwwwroot/k8s-prometheus-grafana.git
#部署Node-exporter组件
kubectl create -f k8s-prometheus-grafana/node-exporter.yaml    
#部署prometheus组件
kubectl create -f  k8s-prometheus-grafana/prometheus/rbac-setup.yaml
kubectl create -f  k8s-prometheus-grafana/prometheus/configmap.yaml 
kubectl create -f  k8s-prometheus-grafana/prometheus/prometheus.deploy.yml
kubectl create -f  k8s-prometheus-grafana/prometheus/prometheus.svc.yml
#部署grafana组件
kubectl create -f   k8s-prometheus-grafana/grafana/grafana-deploy.yaml
kubectl create -f   k8s-prometheus-grafana/grafana/grafana-svc.yaml
kubectl create -f   k8s-prometheus-grafana/grafana/grafana-ing.yaml
#查看相应服务
kubectl get svc -n kube-system    

之后暴露端口进行访问即可

如果单机master跑prometheus和grafana的话可能会因为调度问题导致处于pending状态,这时候需要修改允许master部署pod

kubectl taint nodes --all node-role.kubernetes.io/master-

你可能感兴趣的:(k8s,运维,kubernetes,ubuntu,docker)