首页 > 系统相关 >Centos7.9部署单节点K8S环境

Centos7.9部署单节点K8S环境

时间:2024-06-10 21:55:57浏览次数:28  
标签:redhat nginx Centos7.9 localhost docker K8S root 节点 dir

Centos7.9部署单节点K8S环境

通过Centos extras镜像源安装K8S环境,优点是方便快捷,缺点是版本较低,安装后的版本为1.5.2。

1. 准备工作

  1. 关闭selinux
[root@localhost ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

  1. 关闭防火墙
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

2. 安装kubernetes和etcd

  1. yum下载安装

yum install etcd kubernetes -y

如果提示docker组件冲突,需要卸载现有的docker组件:

...
Error: docker-ce-cli conflicts with 2:docker-1.13.1-210.git7d71120.el7.centos.x86_64
Error: docker-ce conflicts with 2:docker-1.13.1-210.git7d71120.el7.centos.x86_64
 You could try using --skip-broken to work around the problem
...
# 卸载环境现有的docker环境
yum remove docker*`
  1. 修改kube-apiserver配置文件
[root@localhost ~]# cat /etc/kubernetes/apiserver
###
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"    # #127.0.0.1改成0.0.0.0

# The port on the local server to listen on.
# KUBE_API_PORT="--port=8080"

# Port minions listen on
# KUBELET_PORT="--kubelet-port=10250"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota"   # 修改策略

# Add your own!
KUBE_API_ARGS=""
  1. 启动配置服务

启动服务:

systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy

配置服务自启动:

systemctl enable etcd
systemctl enable docker
systemctl enable kube-apiserver
systemctl enable kube-controller-manager
systemctl enable kube-scheduler
systemctl enable kubelet
systemctl enable kube-proxy

查看环境信息:

# 查看k8s版本,使用该种方式部署的k8s版本较低
[root@localhost ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
# 查看节点列表
[root@localhost ~]# kubectl get nodes
NAME        STATUS    AGE
127.0.0.1   Ready     3h

3. 部署示例应用

创建两个yaml文件,分别用于部署nginx的deployment和service。

[root@localhost ~]# cat nginx-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
  labels:
    web: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15
        ports:
        - containerPort: 80
# 创建pod
[root@localhost ~] kubectl create -f nginx-deploy.yaml
[root@localhost ~]# cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-demo
spec:
  type: NodePort
  ports:
   - port: 80
     nodePort: 30080
  selector:
    app: nginx
# 创建sevice
[root@localhost ~] kubectl create -f nginx-svc.yaml

# 查看创建的k8s资源
[root@localhost ~]# kubectl get pod
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-3856710913-4wcvd   1/1       Running   1          1h
nginx-deployment-3856710913-nmf32   1/1       Running   1          1h
nginx-deployment-3856710913-v0mcz   1/1       Running   1          1h
[root@localhost ~]# kubectl get svc
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1       <none>        443/TCP        3h
nginx-demo   10.254.146.200   <nodes>       80:30080/TCP   1h

访问测试,使用浏览器或者curl命令访问nginx:

[root@localhost ~]# curl http://192.168.226.133:30080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

测试访问pod成功。

4. 部署遇到的问题记录

4.1 docker服务无法启动--Error starting daemon: layer does not exist

docker服务无法启动,查看docker服务报错Error starting daemon: layer does not exist

解决办法:

使用下面的脚本清空/var/lib/docker,脚本内容如下:

#!/bin/sh
set -e

dir="$1"

if [ -z "$dir" ]; then
	{
		echo 'This script is for destroying old /var/lib/docker directories more safely than'
		echo '  "rm -rf", which can cause data loss or other serious issues.'
		echo
		echo "usage: $0 directory"
		echo "   ie: $0 /var/lib/docker"
	} >&2
	exit 1
fi

if [ "$(id -u)" != 0 ]; then
	echo >&2 "error: $0 must be run as root"
	exit 1
fi

if [ ! -d "$dir" ]; then
	echo >&2 "error: $dir is not a directory"
	exit 1
fi

dir="$(readlink -f "$dir")"

echo
echo "Nuking $dir ..."
echo '  (if this is wrong, press Ctrl+C NOW!)'
echo

( set -x; sleep 10 )
echo

dir_in_dir() {
	inner="$1"
	outer="$2"
	[ "${inner#$outer}" != "$inner" ]
}

# let's start by unmounting any submounts in $dir
#   (like -v /home:... for example - DON'T DELETE MY HOME DIRECTORY BRU!)
for mount in $(awk '{ print $5 }' /proc/self/mountinfo); do
	mount="$(readlink -f "$mount" || true)"
	if dir_in_dir "$mount" "$dir"; then
		( set -x; umount -f "$mount" )
	fi
done

# now, let's go destroy individual btrfs subvolumes, if any exist
if command -v btrfs > /dev/null 2>&1; then
	root="$(df "$dir" | awk 'NR>1 { print $NF }')"
	root="${root#/}" # if root is "/", we want it to become ""
	for subvol in $(btrfs subvolume list -o "$root/" 2>/dev/null | awk -F' path ' '{ print $2 }' | sort -r); do
		subvolDir="$root/$subvol"
		if dir_in_dir "$subvolDir" "$dir"; then
			( set -x; btrfs subvolume delete "$subvolDir" )
		fi
	done
fi

# finally, DESTROY ALL THINGS
( set -x; rm -rf "$dir" )

将脚本保持为docker-recovery.sh,运行命令sh docker-recovery.sh /var/lib/docker进行修复。

4.2 拉取pod-infrastructure镜像失败

部署创建pod时,会从红帽的官方镜像仓库拉取基础容器pod,报错如下:

Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.

details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""

解决办法:

  1. node节点,包括master节点执行如下操作

查看/etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt 是一个软链接,但是链接过去后并没有真实的/etc/rhsm。使用yum进行安装:

[root@localhost ~]# ls -alh /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt
lrwxrwxrwx. 1 root root 27 Jun 10 05:34 /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt -> /etc/rhsm/ca/redhat-uep.pem
[root@localhost ~]# yum install *rhsm* -y
  1. 下载安装证书
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
[root@localhost ~]# rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem
  1. 拉取镜像
[root@localhost ~]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest                                                                                                                                                                                                    Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ...
latest: Pulling from registry.access.redhat.com/rhel7/pod-infrastructure
26e5ed6899db: Pull complete
66dbe984a319: Pull complete
9138e7863e08: Pull complete
Digest: sha256:47db25d46e39f338142553f899cedf6b0ad9f04c6c387a94b6b0964b7d1b7678
Status: Downloaded newer image for registry.access.redhat.com/rhel7/pod-infrastructure:latest

标签:redhat,nginx,Centos7.9,localhost,docker,K8S,root,节点,dir
From: https://www.cnblogs.com/lldhsds/p/18241094

相关文章

  • K8S部署ECK采集日志
    1.部署nfs1.安装nfs#所有节点安装yuminstall-ynfs-utils在master节点创建nfs共享目录mkdir-pv/data/kubernetes编写配置文件cat>/etc/exports<<'EOF'/data/kubernetes*(rw,no_root_squash)EOFmaster节点启动nfssystemctlenable--nowrpcbindnfs2.使用......
  • Aws EC2,kubeadm方式安装kubernetes(k8s)
    版本docker版本:20.10.25k8s版本(kubeadm,kubelet和kubectl):1.20.10-0初始化#禁用SELinuxsudosetenforce0sudosed-i's/^SELINUX=enforcing$/SELINUX=permissive/'/etc/selinux/config#关闭防火墙sudosystemctlstopfirewalldsudosystemctldisablefirewal......
  • MacOS 安装k8s
    安装前准备确保本地已经安装并启动好了DockerDesktop拉取k8s镜像(如果本地网络好可以正常拉取到k8s官方镜像,可以跳过这一步)克隆git仓库到本地gitclonehttps://github.com/gotok8s/k8s-docker-desktop-for-mac.git进入项目目录,执行./load_images.sh等待所有镜像拉取完成......
  • 在centos7.9下编译安装nginx1.16.1带fancyindex
    在centos7.9下编译安装nginx1.16.1带fancyindex文章目录前言一、安装环境centos7.9/nginx1.16.1/ngx-fancyindex-0.4.4二、需要达到的效果1.默认效果2.安装主题效果三、nginx编译安装1.安装依赖工具2.创建目录并下载Nginx及其模块3.运行编译与安装4.配置环境变......
  • k8s概述
    目录一、什么是Kubernetes1、官网链接2、概述3、特点4、功能二、Kubernetes架构1、架构图2、核心组件2.1、控制平面组件(ControlPlaneComponents)2.1.1、kube-apiserver2.1.2、etcd2.1.3、kube-scheduler2.1.4、kube-controller-manager2.2、Node组件2.2.1、kubelet2.2.2、kube-pr......
  • Q20 LeetCode19 删除链表倒数第N个节点
    1.创建虚拟头结点ListNodepre=newListNode(0,head);2.创建快慢节点3.快节点fastIndex先往后移N个位置4.快慢slowIndex节点一直往后移,直至快节点到链表末尾5.删除慢节点后面的节点 1classSolution{2publicListNoderemoveNthFromEnd(ListNodehead,intn){......
  • k8s_安装dns_metalLB_dashboard_metrics合集
    部署DashboardDashboard是官方提供的一个UI,可用于基本管理K8s资源。#在master节点执行#wget\https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.0/aio/deploy/recommended.yamlvirecommended.yaml增加nodePort:30001和type:NodePort............
  • Q19 LeetCode24 两两交换链表节点
    1.注意节点交换顺序,以防节点丢失2.ListNodedummy=newListNode(0,head);定义空指针,并指向head节点 好语句3.还是虚拟头结点好用 1classSolution{2publicListNodeswapPairs(ListNodehead){3ListNodedummy=newListNode(0,head);4......
  • 代码随想录第4天 | 24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 0
    题目:24.两两交换链表中的节点思路:设置虚拟头结点,双指针+临时指针,(感觉也能递归,未尝试)时间复杂度:O(n)空间复杂度:O(1)坑:1.又忘了else{}和return2.试图访问空指针,多个条件的顺序问题及"&&""||"问题,cur->next要写在cur->next->next前面/***Definitionforsingly-linked......
  • AcWing 33:链表中倒数第k个节点 ← 尾插法
    【题目来源】https://www.acwing.com/problem/content/32/【题目描述】输入一个链表,输出该链表中倒数第k个结点。注意:  ●k>=1;  ●如果k大于链表长度,则返回NULL;【数据范围】链表长度[0,30]。【输入样例】输入:链表:1->2->3->4->5,k=2【输出样例】输出:4......