百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Kubernetes安装记录 kubernetes组件介绍

suiw9 2024-10-31 16:03 23 浏览 0 评论

   主机	        IP地址	        系统
k8s-master 192.168.1.131 CentOS7 x64
k8s-node1 192.168.1.132 CentOS7 x64
k8s-node2 192.168.1.133 CentOS7 x64
k8s-node3 192.168.1.134 Centos8 X64

一、设置

1、关闭防火墙,分别在所有服务器上执行

[root@localhost ~]# systemctl stop firewalld 
[root@localhost ~]# systemctl disable firewalld

2、关闭selinux,分别在所有服务器上执行

[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@localhost ~]# setenforce 0

3、关闭swap,分别在所有服务器上执行

编辑etc/fstab 将/dev/mapper/centos-swap那一行注释掉或者删除掉

[root@localhost ~]# vim /etc/fstab
#/dev/mapper/centos-swap swap      swap    defaults        0 0

注:关闭swap之后需要重启系统

4、更新yum,分别在所有服务器上执行

[root@localhost ~]# yum update

5、修改Master主机名称

[root@k8s-master ~]# hostnamectl set-hostname k8s-master

6、修改主机名到IP地址的映射,只在Master机上执行

编辑/etc/hosts文件,在文件最后加入所有node的IP到主机名称的映射

[root@k8s-master ~]# vim /etc/hosts
192.168.1.131 k8s-master
192.168.1.132 k8s-node1
192.168.1.133 k8s-node2

二、安装Docker

1、移除旧的Docker,如果没有安装过,可以略过该步骤,在所有服务器执行

[root@localhost ~]# yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

2、安装Docker所需要的依赖包,在所有服务器执行

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

3、设置Docker的yum源,这里使用国内阿里的源,在所有服务器执行

[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

4、安装Docker

[root@localhost ~]# yum install docker-ce
[root@localhost ~]# systemctl start docker.service  #启用docker服务
[root@localhost ~]# systemctl enable docker.service  #开机启动
[root@localhost ~]# docker --version  #检查是否安装成功,能看到安装的Docker版本

三、安装kubernetes

1、设置Kubernetes的安装源为啊里的源

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

2、安装kubeadm、kubelet、kubectl

[root@localhost ~]# yum install -y kubelet kubeadm kubectl --nogpgcheck
[root@localhost ~]# systemctl enable kubelet

3、部署Kubernetes Master

[root@k8s-master ~]# kubeadm init \
--apiserver-advertise-address=192.168.1.131 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16

安装完成后,会显示Master信息,主要记录下token,后面在加入node时要使用该token

Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You 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.1.131:6443 --token 20iavg.1ghcijtyzp7eg8aj \
    --discovery-token-ca-cert-hash sha256:b6b97a55eb7c46f42c9d4ce3829a6b8225feab1b328046ee1e935213e4b550c1 

然后使用kubectl命令,执行以下命令

[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# kubectl get nodes 

此时能看到下面的信息,说明安装成功

NAME                    STATUS     ROLES                  AGE   VERSION
localhost.localdomain   NotReady   control-plane,master   66s   v1.20.0

4、部署Kubernetes Master 下载kube-flannel.yml文件时可能需要翻墙,也可以在其它电脑上先翻墙下载到本地,再传到安装的服务器上去,使用本地文件安装

[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 
#或者使用本地的文件 
[root@k8s-master ~]# kubectl apply -f kube-flannel.yml 

这里需要等待一会,然后查看Master部署结果,看到master变成Ready时说明安装正确

[root@k8s-master ~]# kubectl get nodes 
NAME                    STATUS   ROLES                  AGE   VERSION
localhost.localdomain   Ready    control-plane,master   17h   v1.20.0

看到Ready说明安装成功,下面安装界面查看工具

四、安装dashboard

1、获取yaml文件,需要先下载到本地后修改几个参数,获取文件过程中可能需要翻墙

编辑yaml文件的第39行,加入type和端口

[root@k8s-master ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.5/aio/deploy/recommended.yaml 
[root@k8s-master ~]# vim recommended.yaml +39
spec:
  type: NodePort #默认情况下,service的类型是cluster IP,需更改为NodePort的方式
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001 #访问的服务端口
[root@k8s-master ~]# kubectl apply -f recommended.yaml 
[root@k8s-master ~]# kubectl get pod -n kubernetes-dashboard #等待两个变成Running,需要等待一会
NAME                                         READY   STATUS    RESTARTS   AGE
dashboard-metrics-scraper-79c5968bdc-mnddd   1/1     Running   0          16h
kubernetes-dashboard-6f65cb5c64-cxh77        1/1     Running   0          16h
[root@k8s-master ~]# kubectl get svc -n kubernetes-dashboard #查看服务启动情况
NAME                        TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)         AGE
dashboard-metrics-scraper   ClusterIP   10.1.20.211   <none>        8000/TCP        16h
kubernetes-dashboard        NodePort    10.1.77.173   <none>        443:30001/TCP   16h

2、客户端访问测试 用浏览器打开https://192.168.1.131:30001,需要点击高级继续继续访问,选择Token登陆,然后回到服务器的控制台,添加Token相关信息

[root@k8s-master ~]# kubectl create serviceaccount dashboard-admin -n kube-system 
[root@k8s-master ~]# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
[root@k8s-master ~]# kubectl get secrets -n kube-system | grep dashboard
dashboard-admin-token-4cz88                      kubernetes.io/service-account-token   3      46m
[root@localhost ~]# kubectl describe secrets -n kube-system dashboard-admin-token-4cz88
Name:         dashboard-admin-token-4cz88
Namespace:    kube-system
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: dashboard-admin
              kubernetes.io/service-account.uid: 9eeed30f-a6c6-40ae-9459-e280d6c9345a

Type:  kubernetes.io/service-account-token

Data
====
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IkdhaDhpVTVaMVVyUGxORnExNGRvLW9hS0g4aVFPSXh1RE9CV3V0QXRSS1UifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tNGN6ODgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiOWVlZWQzMGYtYTZjNi00MGFlLTk0NTktZTI4MGQ2YzkzNDVhIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.jQx4LNA-1iBhDH7z6kiZ5dgvNcA42K0ez1F2oP7i-QT0_JHzagTzQfSZIxRiGMNq92EnRIiNNYp0AVhq23SrgRlXU3bzgwFO5kKXVX8E7jCCcrlHgT7VcqK4wMwkpdv5O7bSvvQIig1cmWa1uk68FBsmviH4cY4GczN2_qBBR2-Rxqj0l-i3B8qp6mCl21QlXnBt76Y87UV_ilXq0iiau59GrkcrdI6g51K35Y9JtVbCoW9r52E-vTf1uCfRiEoXuvdspGyy-NmLWPBS03Qrno6-BGVDa8GdKrNsQup-PFHVYNvfzLYlBBWvcgrAyMJE-O-7pXvY_yJ267rgHlPiAg
ca.crt:     1066 bytes
namespace:  11 bytes

拷贝上面的token后面的内容到刚刚的浏览器token中,就可以登陆了。

五、安装Node并加入Kubernetes集群

1、关闭防火墙
2、关闭selinux
3、关闭swap
4、更新yum
5、修改主机名称,Kubernetes集群内名称要唯一
6、在Kubernetes的Master主机上加入该节点的名称到IP地址映射

[root@k8s-master ~]# vim /etc/hosts #在文件最后加入IP地址 名称
192.168.1.134 k8s-node3

7、执行第二步(安装Docker) 8、执行第三步(安装Kubernetes)中的1、2小节 9、将安装好的Kubernetes节点加入到集群上去,执行下面的命令

[root@k8s-node2 ~]# kubeadm join 192.168.1.131:6443 --token 20iavg.1ghcijtyzp7eg8aj     --discovery-token-ca-cert-hash sha256:b6b97a55eb7c46f42c9d4ce3829a6b8225feab1b328046ee1e935213e4b550c1
[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/
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.0. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

其中join后面的IP为Kubernetes的Master的IP地址,--toeken为安装Master生成的token值,在Master主执行“kubectl get nodes”查看刚刚加入的节点,如下:

[root@k8s-master ~]# kubectl get nodes
NAME                    STATUS   ROLES                  AGE     VERSION
k8s-node1               Ready    <none>                 47m     v1.20.0
k8s-node2               Ready    <none>                 42m     v1.20.0
k8s-node3               Ready    <none>                 6m30s   v1.20.0
localhost.localdomain   Ready    control-plane,master   88m     v1.20.0

表示所有节点已经加载完成

相关推荐

俄罗斯的 HTTPS 也要被废了?(俄罗斯网站关闭)

发布该推文的ScottHelme是一名黑客,SecurityHeaders和ReportUri的创始人、Pluralsight作者、BBC常驻黑客。他表示,CAs现在似乎正在停止为俄罗斯域名颁发...

如何强制所有流量使用 HTTPS一网上用户

如何强制所有流量使用HTTPS一网上用户使用.htaccess强制流量到https的最常见方法可能是使用.htaccess重定向请求。.htaccess是一个简单的文本文件,简称为“.h...

https和http的区别(https和http有何区别)

“HTTPS和HTTP都是数据传输的应用层协议,区别在于HTTPS比HTTP安全”。区别在哪里,我们接着往下看:...

快码住!带你十分钟搞懂HTTP与HTTPS协议及请求的区别

什么是协议?网络协议是计算机之间为了实现网络通信从而达成的一种“约定”或“规则”,正是因为这个“规则”的存在,不同厂商的生产设备、及不同操作系统组成的计算机之间,才可以实现通信。简单来说,计算机与网络...

简述HTTPS工作原理(简述https原理,以及与http的区别)

https是在http协议的基础上加了一层SSL(由网景公司开发),加密由ssl实现,它的目的是为用户提供对网站服务器的身份认证(需要CA),以至于保护交换数据的隐私和完整性,原理如图示。1、客户端发...

21、HTTPS 有几次握手和挥手?HTTPS 的原理什么是(高薪 常问)

HTTPS是3次握手和4次挥手,和HTTP是一样的。HTTPS的原理...

一次安全可靠的通信——HTTPS原理

为什么HTTPS协议就比HTTP安全呢?一次安全可靠的通信应该包含什么东西呢,这篇文章我会尝试讲清楚这些细节。Alice与Bob的通信...

为什么有的网站没有使用https(为什么有的网站点不开)

有的网站没有使用HTTPS的原因可能涉及多个方面,以下是.com、.top域名的一些见解:服务器性能限制:HTTPS使用公钥加密和私钥解密技术,这要求服务器具备足够的计算能力来处理加解密操作。如果服务...

HTTPS是什么?加密原理和证书。SSL/TLS握手过程

秘钥的产生过程非对称加密...

图解HTTPS「转」(图解http 完整版 彩色版 pdf)

我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取。所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议。...

HTTP 和 HTTPS 有何不同?一文带你全面了解

随着互联网时代的高速发展,Web服务器和客户端之间的安全通信需求也越来越高。HTTP和HTTPS是两种广泛使用的Web通信协议。本文将介绍HTTP和HTTPS的区别,并探讨为什么HTTPS已成为We...

HTTP与HTTPS的区别,详细介绍(http与https有什么区别)

HTTP与HTTPS介绍超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的...

一文让你轻松掌握 HTTPS(https详解)

一文让你轻松掌握HTTPS原文作者:UC国际研发泽原写在最前:欢迎你来到“UC国际技术”公众号,我们将为大家提供与客户端、服务端、算法、测试、数据、前端等相关的高质量技术文章,不限于原创与翻译。...

如何在Spring Boot应用程序上启用HTTPS?

HTTPS是HTTP的安全版本,旨在提供传输层安全性(TLS)[安全套接字层(SSL)的后继产品],这是地址栏中的挂锁图标,用于在Web服务器和浏览器之间建立加密连接。HTTPS加密每个数据包以安全方...

一文彻底搞明白Http以及Https(http0)

早期以信息发布为主的Web1.0时代,HTTP已可以满足绝大部分需要。证书费用、服务器的计算资源都比较昂贵,作为HTTP安全扩展的HTTPS,通常只应用在登录、交易等少数环境中。但随着越来越多的重要...

取消回复欢迎 发表评论: