目录

一、准备工作

二、环境配置

三、安装docker

四、安装k8s



一、准备工作

1、在vmware中安装,创建三台centos服务器

主机 ip 配置

master 192.168.42.132 4核,4g内存,20g硬盘

node1 192.168.42.129 2核,2g内存,20g硬盘

node2 192.168.42.130 2核,2g内存,20g硬盘

二、环境配置

1、配置hosts配置,分别在master和node节点进行配置

vim /etc/hosts
192.168.42.132 master
192.168.42.129 node1
192.168.42.130 node2

 2、配置ssh免密登录(master节点)生成密钥,再复制到所有节点

执行ssh-keygen命令后,一直回车完成密玥生成

在master节点执行ssh-copy-id命令复制到所有节点
ssh-copy-id master
ssh-copy-id node1
ssh-copy-id node2
#测试连接
ssh node1

3、关闭swap分区(所有节点执行
swapoff -a  # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久
 #检查是否关闭
free -m

4、关闭防火墙(所有节点执行
防火墙禁止开机自启
systemctl disable firewalld
停止防火墙
systemctl stop firewalld

5、# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
setenforce 0  # 临时

6、#配置网卡允许检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
# 将桥接的IPv4流量传递到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  # 生效
7、挂载镜像文件(在最后添加镜像部署路径)
vim  /etc/fstab
/dev/sr0  /mnt iso9660 defaults 0 0

8、配置集群时间同步(所有节点执行

#安装日期插件

yum install -y ntp ntpdate

启动服务
systemctl start ntpd

设置开机自启
systemctl enable ntpd

8、配置k8s和docker的yum源(所有节点执行

下载阿里云k8s的yum 源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

配置阿里云docker的yum源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

配置阿里云k8s的yum源
vim /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

更新yum配置
yum clean all
yum  makecache
yum  repolist 

三、安装docker

1、如果环境有安装docker请先卸载docker(所有节点执行

yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate

2、安装docker(所有节点执行)
yum install docker-ce-20.10.9-3.el7 docker-ce-cli-20.10.9-3.el7 docker-compose-plugin containerd.io
#启动修改docker配置文件支持systemctl
vim /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

启动服务
systemctl start docker
停止服务 
systemctl stop docker
重启服务
systemctl  restart docker
更新服务配置
systemctl  daemon-reload docker
设置开机自启
systemctl enable docker 
查看配置是否生效
docker info|grep Cgroup

查看安装版本信息
docker --version

四、安装k8s

1、安装三大组件-kubeadm、kubelet、kubectl(所有节点执行注意组件版本和docker的版本具有依赖性)

kubeadm:用来初始化k8s集群的指令。

kubelet:在集群的每个节点上用来启动 Pod 和容器等。

kubectl:用来与k8s集群通信的命令行工具,查看、创建、更新和删除各种资源。
 

安装组件
yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17

添加配置文件kubelet的cgroup, 编辑/etc/sysconfig/kubelet,添加如下配置:
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
查看版本信息
kubectl version
设置开机自启
systemctl enable kubelet
启动kubelet服务
systemctl start kubelet
检查docker和kubelet服务是否启动成功
systemctl  status docker

systemctl  status kubelet(注意kubelet启动后的状态不是一直是running的状态,需要进行多次确认,看kubelet服务是否有running的状态,出现一次即可 )

 五、下载镜像初始化节点


1、查看需要的k8s镜像版本
kubeadm config images list


2、拉取k8s集群部署依赖镜像版本
方式一直接通过命令拉取
kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
方式二编写shell脚本通过docker拉取镜像(脚本编写好需要添加执行权限,并执行脚本
vim  pull-k8s-v-1.23.0.sh
#!/bin/bash
# download images from aliyun
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.23.17
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.23.17
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.23.17
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.23.17
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.6-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.6

3、确认镜像是否拉取成功
docker images(集群部署好了所以多一些镜像)

 


4、初始化k8s集群(master节点)

kubeadm init  --apiserver-advertise-address=192.168.42.132   --image-repository registry.aliyuncs.com/google_containers  --kubernetes-version v1.23.17 --service-cidr=10.96.0.0/12  --pod-network-cidr=10.244.0.0/16   --ignore-preflight-errors=swap
成功界面
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.42.132:6443 --token khtnp8.7tmgibf078ixg4gt \

--discovery-token-ca-cert-hash sha256:8c1d562205b0f37f7eb7a3723962e2108e48ffe9d6bf61d5f6dd3004000bcb64

 5、在master节点执行以下初始化成功提示的命令
  mkdir -p $HOME/.kube

  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

  sudo chown $(id -u):$(id -g) $HOME/.kube/config


 6、加入节点(所有node节点)

kubeadm join 192.168.42.132:6443 --token khtnp8.7tmgibf078ixg4gt \

--discovery-token-ca-cert-hash sha256:8c1d562205b0f37f7eb7a3723962e2108e48ffe9d6bf61d5f6dd3004000bcb64
7、安装网络插件
下载calico.yaml
curl https://docs.projectcalico.org/v3.15/manifests/calico.yaml -O
执行命令部署
kubectl apply -f calico.yaml
插件是否部署成功
kubectl get pods --all-namespaces -o wide | grep ca

8、检查集群部署是否起来
kubectl get  nodes

 


六、查看k8s集群状态,完成部署

 (注意这个是已经部署完成并安装了可视化界面和nginxpod的状态


七、部署Jenkins

1.部署Jenkins服务器
登录jenkens官网获取yum部署安装包
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
yum -y update(如果获取有问题尝试更新安装软件)
#查看JDK版本信息(需要jdk-11以上的版本)
java -version
java version "1.8.0_221" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 1.8.0_221-LTS-207)
Java HotSpot(TM) 64-Bit Server VM (build 1.8.0_221-LTS-207, mixed mode, sharing)
如果不是jdk-11以上的需要升级jdk版本
#查看JDK信息
rpm -qa|grep java
# 进行删除
rpm -e --nodeps java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64
rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.412.b08-1.el7_9.x86_64
创建jdk部署目录
mkdir -p /devconfig/java
官网下载jdk版本jdk-17_linux-x64_bin.tar.gz上传服务器
MV jdk-17_linux-x64_bin.tar.gz  /devconfig/java
进行解压缩
tar -zxvf jdk-17_linux-x64_bin.tar.gz
#修改配置文件,在最后增加以下配置
vim /etc/profile
# set java environment
JAVA_HOME=/devconfig/java/jdk-17.0.11
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
#生效配置
source /etc/profile
#再次查看JDK版本信息
java -version
java version "17.0.11" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 17.0.11+7-LTS-207)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.11+7-LTS-207, mixed mode, sharing)
#升级完成部署安装jenkins
yum install jenkins
#启动jenkens(注意直接启动会报错,需要修改部分配置文件)
systemctl start  jenkins
#修改配置文件,找到jdk配置位置开启指引路径后保存,同时可以修改对应的访问端口号(根据部署的自己的jdk路径填写)
vim /usr/lib/systemd/system/jenkins.service
 50 # The Java home directory. When left empty, JENKINS_JAVA_CMD and PATH are consulted.
 51 #Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
 52 Environment="JAVA_HOME=/devconfig/java/jdk-17.0.11"
  #修改端口
  # To be able to listen on privileged ports (port numbers less than 1024),
 68 # add the CAP_NET_BIND_SERVICE capability to the AmbientCapabilities
 69 # directive below.
 70 Environment="JENKINS_PORT=8080"
#systemctl更新配置文件
systemctl daemon-reload
#通过systemctl启动Jenkins服务
systemctl start jenkins.service
#检查Jenkins是否运行
systemctl status jenkins.service
 jenkins.service - Jenkins Continuous Integration Server
   Loaded: loaded (/usr/lib/systemd/system/jenkins.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2024-07-16 16:51:28 CST; 11min ago
 Main PID: 39395 (java)
    Tasks: 49
   Memory: 228.2M
   CGroup: /system.slice/jenkins.service
           └─39395 /devconfig/java/jdk-17.0.11/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=%C/jenkins/war --httpPort=8080

7月 16 16:51:27 node3 jenkins[39395]: 2024-07-16 08:51:27.903+0000 [id=37]        INFO        jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
7月 16 16:51:27 node3 jenkins[39395]: 2024-07-16 08:51:27.914+0000 [id=37]        INFO        jenkins.InitReactorRunner$1#onAttained: Started all plugins
7月 16 16:51:27 node3 jenkins[39395]: 2024-07-16 08:51:27.922+0000 [id=37]        INFO        jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.492+0000 [id=32]        INFO        jenkins.InitReactorRunner$1#onAttained: System config loaded
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.494+0000 [id=32]        INFO        jenkins.InitReactorRunner$1#onAttained: System config adapted
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.496+0000 [id=32]        INFO        jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.499+0000 [id=32]        INFO        jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.776+0000 [id=33]        INFO        jenkins.InitReactorRunner$1#onAttained: Completed initialization
7月 16 16:51:28 node3 jenkins[39395]: 2024-07-16 08:51:28.797+0000 [id=25]        INFO        hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
7月 16 16:51:28 node3 systemd[1]: Started Jenkins Continuous Integration Server.
完成部署
#访问Jenkins服务一直在加载中修改以下配置文件中的地址
#把https://updates.jenkins.io/update-center.json
#换成https://mirror.xmission.com/jenkins/updates/update-center.json
vim /var/lib/jenkins/hudson.model.UpdateCenter.xml
?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>https://mirror.xmission.com/jenkins/updates/update-center.json</url>
  </site>
</sites>


 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐