文章目录

  • 1 Kubernetes快速部署
    • 1.1 Kubernetes安装要求
    • 1.2 安装步骤
    • 1.3 准备环境
    • 1.4 所有节点安装Docker/kubeadm/kubelet
      • 1.4.1 安装Docker
      • 1.4.2 添加kubernetes阿里云YUM软件源
      • 1.4.3 安装kubeadm,kubelet和kubectl
    • 1.5 部署Kubernetes Master
    • 1.6 安装Pod网络插件(CNI)
    • 1.7 加入Kubernetes Node
  • 2 测试kubernetes集群

1 Kubernetes快速部署

kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具
这个工具能通过两条指令完成一个kubernetes集群的部署

# 创建一个 Master 节点
$ kubeadm init# 将一个 Node 节点加入到当前集群中
$ kubeadm join <Master节点的IP和端口>

1.1 Kubernetes安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件
至少3台机器,操作系统 CentOS7+

  • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
  • 集群中所有机器之间网络互通
  • 可以访问外网,需要拉取镜像
  • 禁止swap分区

1.2 安装步骤

  • 在所有节点上安装Docker和kubeadm
  • 部署Kubernetes Master
  • 部署容器网络插件
  • 部署 Kubernetes Node,将节点加入Kubernetes集群中
  • 部署Dashboard Web页面,可视化查看Kubernetes资源

1.3 准备环境

主机 IP
master 192.168.25.146
node1 192.168.25.147
node2 192.168.25.148

部署

//修改主机名
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@master ~]# hostname
master.example.com[root@node ~]# hostnamectl set-hostname node1.example.com
[root@node ~]# bash
[root@node1 ~]# hostname
node1.example.com[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
[root@node2 ~]# hostname
node2.example.com#需在三台主机上操作步骤
//关闭防火墙和selinux
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config//关闭swap分区
# vim /etc/fstab
注释掉swap分区//添加hosts
[root@master ~]# cat >> /etc/hosts << EOF
192.168.25.146 master master.example.com
192.168.25.147 node1 node1.example.com
192.168.25.148 node2 node2.example.com
EOF//将桥接的IPv4流量传递到iptables的链(在master上做)
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF[root@master ~]# sysctl --system     # 生效//配置时间同步
[root@master ~]# yum -y install chrony
[root@master ~]# vi /etc/chrony.conf
pool time1.aliyun.com iburst[root@master ~]# systemctl enable --now chronyd[root@master ~]# for i in master node1 node2 ;do ssh $i 'date' ;done
2021年 12月 18日 星期六 04:59:41 EST
2021年 12月 18日 星期六 04:59:42 EST
2021年 12月 18日 星期六 04:59:42 EST//免密认证(只需要在master上做)
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3DYIG2AYzaeUUWN8asgfqScnXoRyn+tYGOexy92qhD4 root@master.example.com
The key's randomart image is:
+---[RSA 3072]----+
|  .=+=+          |
|  ..=oo..        |
|   o =o+         |
|  . * *= o       |
|   o.*+oS +      |
|    +*Bo . .     |
|   .o*=.         |
|   .E=.o .       |
|    oo=.o..      |
+----[SHA256]-----+[root@master ~]# ssh-copy-id master
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@master's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'master'"
and check to make sure that only the key(s) you wanted were added.[root@master ~]# ssh-copy-id node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'node1'"
and check to make sure that only the key(s) you wanted were added.[root@master ~]# ssh-copy-id node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node2's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'node2'"
and check to make sure that only the key(s) you wanted were added.// 重启三台主机,使上面的一些配置生效
[root@master ~]# reboot

1.4 所有节点安装Docker/kubeadm/kubelet

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。

1.4.1 安装Docker

[root@master ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[root@master ~]# yum -y install docker-ce
[root@master ~]# systemctl enable --now docker[root@master ~]# systemctl status docker
● docker.service - Docker Application Container EngineLoaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disab>Active: active (running) since Sat 2021-12-18 05:10:41 EST; 9s agoDocs: https://docs.docker.comMain PID: 7143 (dockerd)Tasks: 9Memory: 36.3MCGroup: /system.slice/docker.service└─7143 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.840348434-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.865784971-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.865832834-0>
12月 18 05:10:40 master.example.com dockerd[7143]: time="2021-12-18T05:10:40.866033860-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.040791665-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.093668762-0>
12月 18 05:10:41 master.example.com dockerd[7143]: time="2021-12-18T05:10:41.109693941-0>
lines 1-17[root@master ~]# docker --version
Docker version 20.10.12, build e91ed57[root@master ~]# cat > /etc/docker/daemon.json << EOF
{"registry-mirrors": ["https://xj3hc284.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "100m"},"storage-driver": "overlay2"
}
EOF

1.4.2 添加kubernetes阿里云YUM软件源

[root@master ~]# 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

1.4.3 安装kubeadm,kubelet和kubectl

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

[root@node2 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@master ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

1.5 部署Kubernetes Master

在192.168.25.146(Master)执行

[root@master ~]# kubeadm init \
--apiserver-advertise-address=192.168.25.146 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16// 记录下面的这些东西到一个文件中,后面会用到kubeadm join 192.168.25.146:6443 --token 481ink.zzl4pouej6vq82bs \--discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031 

由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址

[root@master ~]# echo ‘export KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@master ~]# source /etc/profile.d/k8s.sh

1.6 安装Pod网络插件(CNI)

//使用这条命令直接安装
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml//我这里是把文件下载下来安装
[root@master ~]# ls
anaconda-ks.cfg  kube-flannel.yml[root@master ~]# kubectl apply -f /root/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

确保能够访问到quay.io这个registery

1.7 加入Kubernetes Node

在192.168.25.147、192.168.25.148上(Node)执行,执行刚刚保存的命令
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令

[root@node1 ~]#  kubeadm join 192.168.25.146:6443 --token 481ink.zzl4pouej6vq82bs     --discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031 [root@node2 ~]#  kubeadm join 192.168.25.146:6443 --token 481ink.zzl4pouej6vq82bs     --discovery-token-ca-cert-hash sha256:94dc5e83208b662f4a9f7f133a1da3ed6052ead72922de5aee750ab498d44031 

2 测试kubernetes集群

在Kubernetes集群中创建一个pod,验证是否正常运行

[root@master ~]# kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.example.com   Ready    control-plane,master   15m     v1.20.0
node1.example.com    Ready    <none>                 5m58s   v1.20.0
node2.example.com    Ready    <none>                 5m53s   v1.20.0[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed[root@master ~]# kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-sk9zs   1/1     Running   0          4m56sNAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        21m
service/nginx        NodePort    10.110.176.8   <none>        80:32279/TCP   4m36s//访问在pod中运行的容器
[root@master ~]# curl 10.110.176.8
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

在浏览器上访问

Kubernetes部署相关推荐

  1. 项目部署时网关怎么回事_使用Kubernetes部署聊天网关(或技术按预期运行时)...

    项目部署时网关怎么回事 by Richard Li 理查德·李(Richard Li) 使用Kubernetes部署聊天网关(或技术按预期运行时) (Using Kubernetes to deplo ...

  2. Kubernetes部署(三):CA证书制作

    相关内容: Kubernetes部署(一):架构及功能说明 Kubernetes部署(二):系统环境初始化 Kubernetes部署(三):CA证书制作 Kubernetes部署(四):ETCD集群部 ...

  3. 用Kubernetes部署超级账本Fabric的区块链即服务(1)

    用Kubernetes部署超级账本Fabric的区块链即服务(1) 2017年08月13日 00:00:00 阅读数:937 题图摄于旧金山市区:云海中的 Twin Peaks 不久前,我们发表了如何 ...

  4. k8s部署jar包_使用Kubernetes部署Springboot或Nginx的详细教程

    1 前言 经过<Maven一键部署Springboot到Docker仓库,为自动化做准备>,Springboot的Docker镜像已经准备好,也能在Docker上成功运行了,是时候放上Ku ...

  5. 容器化单页面应用中Nginx反向代理与Kubernetes部署

    在<容器化单页面应用中RESTful API的访问>一文中,我介绍了一个在容器化环境中单页面应用访问后端服务的完整案例.这里我将继续使用这个案例,介绍一下容器化单页面应用部署的另一个场景: ...

  6. Kubernetes 部署 Ingress 控制器 Traefik v2.1

    目录[-] . 一.Traefik 简介 . 二.Kubernetes 部署 Traefik . 1.创建 CRD 资源 . 2.创建 RBAC 权限 . 3.创建 Traefik 配置文件 . 4. ...

  7. Kubernetes 部署 Mysql 8.0 数据库(单节点)

    作者:超级小豆丁 http://www.mydlq.club/article/75 目录 简介 Mysql 参数配置 创建 ConfigMap 存储 Mysql 配置文件 通过 Kubectl 工具部 ...

  8. 三种主流的Kubernetes部署方式

    [译者的话]本文分析介绍了三种主流的Kubernetes部署方式,为广大Kubernetes的使用者提供了很好的参考借鉴. [烧脑式Kubernetes实战训练营]本次培训理论结合实践,主要包括:Ku ...

  9. kubernetes部署nfs持久存储(静态和动态)

    kubernetes部署nfs持久存储(静态和动态) NFS简介 NFS是网络文件系统Network File System的缩写,NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地的文 ...

  10. Kubernetes生产实践系列之二十二:Service Mesh之在Kubernetes部署Linkerd2进行service mesh

    一.前言 service mesh是为复杂微服务系统提供基础设施服务的方法,支持Kubernetes的service mesh实现包括文章<kubernetes系列之十八:使用helm安装ist ...

最新文章

  1. 人月神话阅读笔记07
  2. 开发日记-20190725 关键词 读书笔记《Linux 系统管理技术手册(第二版)》DAY 15
  3. python同时对文件进行读写操作-Python实现的读取文件内容并写入其他文件操作示例...
  4. linux系统怎么清理磁盘空间,LINUX系统怎么使用命令清理磁盘空间?
  5. 使用C语言将一个数组首尾互换后输出,函数实现体会地址传递
  6. Android开发之WebView加载自定义scheme报错net::ERR_UNKNOWN_URL_SCHEME(附带源码标题下面可点击下载)
  7. android输入时背景颜色,Button根据EditText输入状态改变背景颜色
  8. nginx 配置文件的匹配规则
  9. 理论塔板数 matlab,matlab作图法计算精馏理论板数
  10. 18. 爱吃皮蛋的小明(斐波那契数列)
  11. java inputstream编码格式_纯文本-FileInputStream的编码与解码方式
  12. php幼儿园管理系源码,先启幼儿园在线管理系统
  13. php网页错误404,php出现404错误页面的解决方法
  14. flash基础动画制作
  15. oracle学习札记72
  16. 微信小程序switch组件尺寸控制
  17. 观大数据有感_观《大数据时代》有感
  18. 【机器学习】基于生成对抗网络的黑白图片上色
  19. 为防止用户自定义Bixby按键,三星先给T-Mobile用户的S8发送更新
  20. 网络信息传播中图像识别技术的意义

热门文章

  1. 你好,女神节有个给你的惊喜请查收
  2. 长角牛网络监听 arp欺骗
  3. TYPEC 转HDMI VGA+PD+ USB3.0 HUB+TF/CF/SD+RJ45等多功能扩展坞方案设计资料|TYPE-C转HDMI转VGA带PD USB3.0多功能拓展器方案介绍
  4. python判断用户名密码是否正确_python实现用户名密码校验
  5. usb-cam(1)安装
  6. 自定义notepad++ --添加插件SourceCookifier
  7. 双鱼林 android 服务器,双鱼林安卓Android代码生成器下载_双鱼林安卓Android代码生成器官方下载-太平洋下载中心...
  8. Hive2.x、HiveServer、HiveServer2简述及Beeline使用
  9. 千年鸿蒙盼尔来兮,古言爱情誓言
  10. 运维工程师必会的109个Linux命令