环境准备

  • 操作系统:openEuler 22.03
  • Docker:18.09.0
  • Kubernetes:1.29.3
  • Harbor:2.10.0
  1. 临时关闭swap
swapoff -a
  1. 可以临时关闭selinux,减少额外配置
setenforce 0
  1. 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
  1. 修改hosts文件 方便查看域名映射
# 修改成本机ip、你自己想要配置的域名
echo "127.0.0.1	harbor.fxm.com" >> /etc/hosts
  1. 修改内核参数
modprobe br_netfilter

echo "modprobe br_netfilter" >> /etc/profile

tee /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

# 重新加载配置
sysctl -p /etc/sysctl.d/k8s.conf
  1. 配置k8s的yum源
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF

安装Docker

  • 安装
dnf install docker conntrack -y
  • 启动docker
systemctl start docker
  • 设置为开机启动
systemctl enable docker
  • 查看docker版本信息
docker -v
# 或使用
docker version
  • 修改docker的 /etc/docker/daemon.json文件
# 设置国内阿里云docker源,地址改为自己在阿里云容器镜像服务申请的地址即可。
# 设置cgroupdriver为systemd,这步很重要!!!

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xcjha0pw.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
  • 修改完成后 重启docker ,使docker与kubelet的cgroup 驱动一致
systemctl daemon-reload
systemctl restart docker

安装cri-docker

  • 下载
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.14/cri-dockerd-0.3.14.amd64.tgz

tar xzvf cri-dockerd-0.3.14.amd64.tgz
  • 生成systemd配置文件
[root@fxm  ~]# cd cri-dockerd/
[root@fxm  cri-dockerd]# mkdir systemd
[root@fxm  cri-dockerd]# cd systemd
[root@fxm systemd]# vi cri-docker.service
[root@fxm systemd]# vi cri-docker.socket
  • cri-docker.service
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
Requires=cri-docker.socket
 
[Service]
Type=notify
ExecStart=/usr/local/bin/cri-dockerd --container-runtime-endpoint fd://
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
 
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
 
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
 
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
 
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
Delegate=yes
KillMode=process
 
[Install]
WantedBy=multi-user.target
  • cri-docker.socket
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service
 
[Socket]
ListenStream=%t/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
 
[Install]
WantedBy=sockets.target

安装k8s

安装三大组件-kubeadm、kubelet、kubectl

kubeadm:用来初始化k8s集群的指令。
kubelet:在集群的每个节点上用来启动 Pod 和容器等。
kubectl:用来与k8s集群通信的命令行工具,查看、创建、更新和删除各种资源。
  • 安装kubeadmin、kubelet、kubernetes-master
sudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# 查看是否安装成功
kubelet --version
kubectl version
kubeadm version
  • 启动kubelet
systemctl daemon-reload
systemctl start kubelet
# 设置kubelet开机启动
systemctl enable kubelet
  • 安装Kubernetes
# apiserver-advertise-address 替换本机ip
kubeadm init --apiserver-advertise-address={ip} --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.29.3 --service-cidr=250.0.0.0/12 --pod-network-cidr=240.0.0.0/12
  • 按照输出,执行下面的命令
配置环境变量
# 非root用户请执行
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

# root用户直接执行
# 临时生效,重启后失效,不推荐。
$ export KUBECONFIG=/etc/kubernetes/admin.conf 
# 永久生效,执行kubeadm reset后再次init也无需再次执行这条命令
$ echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >>  ~/.bash_profile 
$ source ~/.bash_profile
  • 如果初始化失败需要重新初始化时,对集群进行重置
kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock
  • 部署网络插件kube-flannel
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml 

# 网络不好下载不下来也可以直接复制以下内容新建yml文件执行
---
kind: Namespace
apiVersion: v1
metadata:
  name: kube-flannel
  labels:
    pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni-plugin
       #image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
       #image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
       #image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply)
        image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: EVENT_QUEUE_DEPTH
          value: "5000"
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
        - name: xtables-lock
          mountPath: /run/xtables.lock
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni-plugin
        hostPath:
          path: /opt/cni/bin
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate
  • 安装kubernetes-cni
dnf install -y kubernetes-cni --allowerasing
  • 获取nodes状态信息
kubectl get nodes

安装harbor

  • 安装Docker Compose
dnf install -y docker-compose
  • 安装openssl
dnf install -y openssl
  • 下载harbor
# wget下载不了的话,下载后上传到服务器
wget https://github.com/goharbor/harbor/releases/download/v2.10.0/harbor-offline-installer-v2.10.0.tgz
  • 解压
tar -xzvf harbor-offline-installer-v2.10.0.tgz
#进入目录
cd ./harbor
  • 证书秘钥配置
# 创建文件夹
mkdir certs
# 生成秘钥
openssl genrsa -out ca.key 4096
# 以下harbor.fxm.com 改为[环境准备]-4-域名
openssl req -x509 -new -nodes -sha512 -days 3650 \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.fxm.com" \
 -key ca.key \
 -out ca.crt
# 生成服务器私钥
openssl genrsa -out harbor.fxm.com.key 4096

openssl req -sha512 -new \
 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.fxm.com" \
 -key harbor.fxm.com.key \
 -out harbor.fxm.com.csr
# 生成X509 v3的密钥文件
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=harbor.fxm.com
DNS.2=harbor.fxm.com
DNS.3=hostname
EOF
# 使用v3.ext文件生成你harbor主机密钥
openssl x509 -req -sha512 -days 3650 \
 -extfile v3.ext \
 -CA ca.crt -CAkey ca.key -CAcreateserial \
 -in harbor.fxm.com.csr \
 -out harbor.fxm.com.crt
# 创建 /data/cert目录为了给harbor.yml使用
mkdir -p /data/cert
# 将证书复制到/data/cert下
cp harbor.fxm.com.crt /data/cert/cp harbor.fxm.com.key /data/cert/
# 为docker生成对应的证书给它使用
openssl x509 -inform PEM -in harbor.fxm.com.crt -out harbor.fxm.com.cert
# 在docker所在机器创建对应目录
mkdir -p /etc/docker/certs.d/harbor.fxm.com/
# 复制证书到目录下
cp harbor.fxm.com.cert /etc/docker/certs.d/harbor.fxm.com/
cp harbor.fxm.com.key /etc/docker/certs.d/harbor.fxm.com/
cp ca.crt /etc/docker/certs.d/harbor.fxm.com/
  • harbor.yml配置
cp harbor.yml.tmpl harbor.yml
vi harbor.yml
# 修改参数
hostname:harbor.fxm.com # 修改域名
# 证书位置修改
certificate: 证书路径修改为/harbor/certs/ca.crt
private_key: 秘钥路径修改为/harbor/certs/ca.key
  • 执行prepare命令生成配置文件
./prepare
  • harbor安装
./install.sh
  • harbor服务查看
docker-compose -f docker-compose.yml ps
  • docker登陆harbor
docker login harbor.fxm.com -u admin -p Harbor12345
Logo

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

更多推荐