一、概述

搭建k8s集群时,需要访问google,下载相关镜像以及安装软件,非常麻烦。

正好阿里云提供了k8s的更新源,国内用户就可以直接使用了。

二、环境介绍

操作系统 主机名 IP地址 功能 配置
ubuntu-16.04.5-server-amd64 k8s-master 192.168.91.128 主节点 2核4G
ubuntu-16.04.5-server-amd64 k8s-node1 192.168.91.129 从节点 2核4G
ubuntu-16.04.5-server-amd64 k8s-node2 192.168.91.131 从节点 2核4G

 注意:请确保CPU至少2核,内存2G

三、安装前准备

主机名

确保3台主机的 /etc/hostname 已经修改为正确的主机名,修改后,请重启系统。

时间

务必保证3台服务器的时区是一样的,强制更改时区为上海,执行以下命令

ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
bash -c "echo 'Asia/Shanghai' > /etc/timezone"

安装ntpdate

apt-get install -y ntpdate

如果出现以下错误

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

执行2个命令解决

sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

使用阿里云的时间服务器更新

ntpdate ntp1.aliyun.com

3台服务器都执行一下,确保时间一致!

请确保防火墙都关闭了!

四、正式开始

禁用swap

所有主机

sudo sed -i '/swap/ s/^/#/' /etc/fstab
sudo swapoff -a

安装Docker

更新apt源,并添加https支持(所有主机)

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

使用utc源添加GPG Key (所有主机)

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add 

添加Docker-ce稳定版源地址(所有主机)

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

安装docker-ce(所有主机)

sudo apt-get update
sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu

安装kubelet,kubeadm,kubectl

添加apt key以及源(所有主机)

sudo apt update && sudo apt install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" >>/etc/apt/sources.list.d/kubernetes.list

安装(所有主机)

sudo apt update
sudo apt install -y kubelet=1.15.2-00 kubeadm=1.15.2-00 kubectl=1.15.2-00
sudo apt-mark hold kubelet=1.15.2-00 kubeadm=1.15.2-00 kubectl=1.15.2-00

安装kubernetes集群(仅master)

—image-repository 指定镜像源,指定为阿里云的源,这样就会避免在拉取镜像超时,如果没问题,过几分钟就能看到成功的日志输入

sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.2 --pod-network-cidr=192.169.0.0/16

输出:

[init] Using Kubernetes version: v1.15.2
[preflight] Running pre-flight checks[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.10.104]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.10.104 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.10.104 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 41.503569 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 767b6y.incfuyom78fl6j88
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxyYour Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configYou should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.10.104:6443 --token 767b6y.incfuyom78fl6j88 \--discovery-token-ca-cert-hash sha256:941807715378bcbd5bd1cbe244c4bdbf00dee4e45c3b0ff3555eea746607a672 

View Code

注意:出现以下警告信息:

 [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/

可以忽略,解决方法:

#修改或者创建
vim /etc/docker/daemon.json#添加如下内容 --这行就不要添加了这只是个注释(^_^)------qingfeng{"exec-opts": ["native.cgroupdriver=systemd"]
}

拷贝kubeconfig文件到家目录的.kube目录 (仅master)

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

安装网络插件,让pod之间通信(仅master)

kubectl apply -f http://mirror.faasx.com/k8s/calico/v3.3.2/rbac-kdd.yaml
kubectl apply -f http://mirror.faasx.com/k8s/calico/v3.3.2/calico.yaml

查看kube-system命名空间下的pod状态(仅master)

kubectl get pod -n kube-system

等待1分钟,效果如下:

NAME                                 READY   STATUS    RESTARTS   AGE
calico-node-fhzg9                    2/2     Running   0          79s
calico-node-kg5xf                    2/2     Running   0          79s
calico-node-wxfrc                    2/2     Running   0          79s
coredns-bccdc95cf-bbjmv              1/1     Running   0          3m32s
coredns-bccdc95cf-hd8d6              1/1     Running   0          3m32s
etcd-k8s-master                      1/1     Running   0          2m49s
kube-apiserver-k8s-master            1/1     Running   0          2m49s
kube-controller-manager-k8s-master   1/1     Running   0          2m32s
kube-proxy-5dsbp                     1/1     Running   0          101s
kube-proxy-5m544                     1/1     Running   0          104s
kube-proxy-gqblh                     1/1     Running   0          3m32s
kube-scheduler-k8s-master            1/1     Running   0          2m52s

加入node节点 (仅node)

拷贝kubeadm init输入的加入节点命令,在每个node上执行

kubeadm join 192.168.10.104:6443 --token 767b6y.incfuyom78fl6j88     --discovery-token-ca-cert-hash sha256:941807715378bcbd5bd1cbe244c4bdbf00dee4e45c3b0ff3555eea746607a672

查看集群状态(仅master)

root@k8s-master:~# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   44m   v1.15.2
k8s-node1    Ready    <none>   42m   v1.15.2
k8s-node2    Ready    <none>   42m   v1.15.2

命令补全

apt-get install bash-completionsource <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
source  ~/.bashrc

四、部署应用

这里以flask为例:

vim flask.yaml

内容如下:

apiVersion: extensions/v1beta1
kind: Deployment
metadata: name: flaskapp-1
spec: replicas: 1template: metadata: labels: name: flaskapp-1spec: containers: - name: flaskapp-1image: jcdemo/flaskappports:- containerPort: 5000---
apiVersion: v1
kind: Service
metadata:name: flaskapp-1labels:name: flaskapp-1
spec:type: NodePortports:- port: 5000name: flaskapp-porttargetPort: 5000protocol: TCPnodePort: 30005selector:name: flaskapp-1

启动应用

kubectl apply -f flask.yaml

查看应用状态

root@k8s-master:~# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
flaskapp-1-59698bc97d-ssqv8   1/1     Running   0          43m   192.169.2.3   k8s-node2   <none>           <none>

以上信息可以说明,这个pod运行在k8s-node2这台主机上面

使用curl访问pod ip的服务

root@k8s-master:~# curl 192.169.2.3:5000
<html><head><title>Docker + Flask Demo</title></head><body><table><tr><td> Start Time </td> <td>2019-Aug-07 12:30:38</td> </tr><tr><td> Hostname </td> <td>flaskapp-1-59698bc97d-ssqv8</td> </tr><tr><td> Local Address </td> <td>192.169.2.3</td> </tr><tr><td> Remote Address </td> <td>192.168.10.120</td> </tr><tr><td> Server Hit </td> <td>3</td> </tr></table></body></html>root@k8s-master:~# 

查看svc端口

root@k8s-master:~# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
flaskapp-1   NodePort    10.107.108.46   <none>        5000:30005/TCP   45m
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          53m

直接网页访问k8s-node2的30005端口

http://192.168.10.119:30005/

效果如下:

五、部署dashboard可视化插件

概述

在 Kubernetes Dashboard 中可以查看集群中应用的运行状态,也能够创建和修改各种 Kubernetes 资源,比如 Deployment、Job、DaemonSet 等。用户可以 Scale Up/Down Deployment、执行 Rolling Update、重启某个 Pod 或者通过向导部署新的应用。Dashboard 能显示集群中各种资源的状态以及日志信息。

可以说,Kubernetes Dashboard 提供了 kubectl 的绝大部分功能,大家可以根据情况进行选择。

github地址:

https://github.com/kubernetes/dashboard

安装

Kubernetes 默认没有部署 Dashboard,可通过如下命令安装:

kubectl apply -f http://mirror.faasx.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

查看service

root@k8s-master:~# kubectl --namespace=kube-system get deployment kubernetes-dashboard
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-dashboard   1/1     1            1           5m23s
root@k8s-master:~# kubectl --namespace=kube-system get service kubernetes-dashboard
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes-dashboard   ClusterIP   10.100.111.103   <none>        443/TCP   5m28s

查看pod

确保状态是Running

root@k8s-master:~# kubectl get pod --namespace=kube-system -o wide | grep dashboard
kubernetes-dashboard-8594bd9565-t78bj   1/1     Running   0          8m41s   192.169.2.7      k8s-node2    <none>           <none>

允许外部访问

注意:会占用终端

kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'

通过浏览器访问

注意:192.168.10.104为master ip

http://192.168.10.104:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/.

效果如下:

配置登录权限

Dashboard 支持 Kubeconfig 和 Token 两种认证方式,为了简化配置,我们通过配置文件 dashboard-admin.yaml 为 Dashboard 默认用户赋予 admin 权限。

vim dashboard-admin.yml

内容如下:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:name: kubernetes-dashboardlabels: k8s-app: kubernetes-dashboard
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:
- kind: ServiceAccountname: kubernetes-dashboardnamespace: kube-system

执行kubectl apply使之生效

kubectl apply -f dashboard-admin.yml

现在直接点击登录页面的跳过,就可以进入 Dashboard 了,效果如下:

关于dashboard界面结构介绍,请参考链接:

https://www.cnblogs.com/kenken2018/p/10340157.html

本文参考链接:

https://www.toutiao.com/i6703112655323791884

https://www.cnblogs.com/busigulang/p/10736040.html

https://www.cnblogs.com/qingfeng2010/p/10540832.html

https://www.cnblogs.com/kenken2018/p/10340157.html

ubuntu 使用阿里云镜像源快速搭建kubernetes 1.15.2集群相关推荐

  1. 使用国内的镜像源搭建 kubernetes(k8s)集群

    概述 老话说的好:努力学习,提高自己,让自己知道的比别人多,了解的别人多. 言归正传,之前我们聊了 Docker,随着业务的不断扩大,Docker 容器不断增多,物理机也不断增多,此时我们会发现,登录 ...

  2. 使用Kubeadm搭建Kubernetes(1.12.2)集群

    Kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,在2018年将进入GA状态,说明离生产环境中使用的距离越来 ...

  3. 搭建Kubernetes(k8s)集群(cka考试环境)

    搭建Kubernetes(k8s)集群 基础介绍 containerd简介 Docker vs containerd K8S为什么要放弃使用Docker作为容器运行时,而使用containerd呢? ...

  4. ubuntu更换阿里云镜像源操作步骤

    过程较为复杂,能清楚自己改了镜像源,避免了忘记或备份等 修改下载(镜像源)源 输入: sudo nano /etc/apt/sources.list 2.将所有deb 后的网址http://..... ...

  5. Kubeadm 快速搭建 k8s v1.24.1 集群(openEuler 22.03 LTS)

    kubeadm 简介 kubeadm 是 Kubernetes(以下简称 k8s)官方提供的用于快速安装部署 k8s 集群的工具,伴随 k8s 每个版本的发布都会同步更新,kubeadm 会对集群配置 ...

  6. 在滴滴云快速搭建自己的简易服务集群(入门版)

    引言 万物互联的时代,各行各业都或多或少的接入线上,作为开发人员,我们有了小而美的产品或服务方面的想法,就把它她做出来放到线上,让它发展壮大. 我以滴滴云为例,教你一步一步的搭建自己的服务器集群,包括 ...

  7. 快速搭建redis单机版和redis集群版

    单机版 第一步:需要安装redis所需的C语言环境,若虚拟机联网,则执行 yum install gcc-c++ 第二步:redis的源码包上传到linux系统 第三步:解压缩redis   tar ...

  8. ubuntu18.04搭建 kubernetes(k8s)集群

    ♦♦♦这里介绍使用kubeadm安装k8s集群,因为这种搭建方式需要访问grc.io(https://cloud.google.com/container-registry/)(国内被墙了)下载对应的 ...

  9. mac使用虚拟机(VirtualBox+centos7)搭建kubernetes(K8S)集群

    文章目录 说明 一.环境准备 1. 配置主机网络 2. 配置磁盘空间 3. 安装虚拟机配置网络 4.设置Linux环境(三台均需要设置) 二.安装docker kubeadm kubelet kube ...

最新文章

  1. python值函数名的使用以及闭包,迭代器详解
  2. 神经网络与深度学习——TensorFlow2.0实战(笔记)(二)(开发环境介绍)
  3. inux中限制用户进程CPU和内存占用率
  4. Normalizing Flows Tutorial(标准化流教程)第一部分
  5. js模仿块级作用域(js没有块级作用域私有作用域)
  6. css选择器([class*= icon-], [class^=icon-] 的区别)
  7. 改进的脱机手写体汉字细化算法
  8. STM32标准库与HAL库中的Mode和Pull设置
  9. 启动Tomcat时报错,一大堆的
  10. 面试题:关于搭建测试环境 (一)
  11. Invalid bound statement (not found): com.xingyu.demo.mapper.UserMapper.update错误
  12. 空间触觉学习法 啥都能同理可证!真的是有手就行
  13. #Paper Reading# Stochastic Optimization of Sorting Networks via Continuous Relaxations
  14. 软考知识点梳理--项目评估
  15. pdf转txt java_pdf转换txt怎么操作?pdf文件可以转换成txt文件吗?
  16. 【计算机网络】——通信协议综述(网络协议、网络分层、ifconfige命令行、DHCP与PXE)
  17. SecureFX之激活教程
  18. 苹果4s怎么越狱_iPhone11/iPhoneXs iOS13.3越狱来啦
  19. CHIL-SQL-快速参考
  20. oracle中强制走索引,Oracle数据库强制索引

热门文章

  1. linux查看串口波特率
  2. 国海证券分析报告(0607)
  3. i9 12900HX和i9 12900HK有什么区别
  4. 关于码云的一些基本知识_网络攻防前期知识储备 - osc_ojkfylwq的个人空间 - OSCHINA - 中文开源技术交流社区...
  5. 矩阵和稀疏矩阵相互转化(数组方式实现)
  6. 解决Configuration is faulty. Check the Issues view for details. Error while building/deploying project
  7. LVS中使用toa模块遇到的坑
  8. excel连接mysql速度太慢_Excel 一句话让程序运行速度提高了几百倍(recordset的CursorLocation属性)...
  9. 《FLUENT 14.0超级学习手册》——3.3 ANSYS ICEM CFD 14.0的应用
  10. steam上快速游戏_如何将Steam游戏移动到另一个驱动器上