Kubernetes安装部署
服务器(腾讯云),系统:CentOs7.6
主机名 | IP | 配置 |
k8s-master | 172.17.0.4 | 2CPU4G |
k8s-node1 | 172.16.16.9 | 1CPU2G |
k8s-node2 | 172.16.16.6 | 1CPU2G |
设置
1.1修改主机名称
hostnamectl set-hostname k8s-master hostnamectl set-hostname k8s-node1 hostnamectl set-hostname k8s-node2
cat /etc/hostname #查看主机名称
1.2添加主机名与IP映射关系
cat <<EOF >>/etc/hosts 172.17.0.4 k8s-master 172.16.16.9 k8s-node1 172.16.16.6 k8s-node2 EOF
1.3关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1.4关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config setenforce 0
1.5关闭swap
vim /etc/fstab #编辑etc/fstab将swap那一行注释掉或者删除掉 /dev/mapper/centos-swap swap swap defaults 0 0
安装Docker
2.1yum更新到最新
sudo yum update
2.2移除旧的Docker版本
sudo 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.3安装Docker所需的依赖包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2.4设置Docker的yum的源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
或者使用阿里云下载源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.5安装Docker
yum list docker-ce --showduplicates | sort -r #查看所有版本
sudo yum install docker-ce #安装的是最新版
sudo yum install <FQPN> # 安装指定版本,例如:sudo yum install docker-ce-17.12.1.ce
systemctl enable docker && systemctl start docker
systemctl start docker.service #启用docker服务
systemctl enable docker.service #开机启动
docker --version #检查是否安装成功
CentOS 8可能出现错误:
centos8默认使用podman代替docker,所以需要containerd.io
可以在https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/edge/Packages/中选择版本安装
yum install -y https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el8.x86_64.rpm
或者
yum install -y https://download.docker.com/linux/fedora/33/x86_64/stable/Packages/containerd.io-1.4.3-3.1.fc33.x86_64.rpm
3安装Kubeadm&Kubelet
3.1添加阿里云Yum软件源
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
3.2安装Kubeadm&Kubelet&Kubectl
yum install -y kubelet-1.16.3 kubeadm-1.16.3 kubectl-1.16.3
systemctl enable kubelet
安装遇错误
Total 20 MB/s | 57 MB 00:00:02 Retrieving key from https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg Importing GPG key 0xA7317B0F: Userid : "Google Cloud Packages Automatic Signing Key <gc-team@google.com>" Fingerprint: d0bc 747f d8ca f711 7500 d6fa 3746 c208 a731 7b0f From : https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg Public key for b45a63e77d36fc7e1ef84f1cd2f7b84bccf650c8248191a37d20c69564d8b8df-kubeadm-1.16.3-0.x86_64.rpm is not installed Failing package is: kubeadm-1.16.3-0.x86_64 GPG Keys are configured as: https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
解决方法
yum install kubeadm-1.16.3-0.x86_64 --nogpgcheck
3.3部署Kubernetes Master
kubeadm init \ --apiserver-advertise-address=172.17.0.4 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.16.3 \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16
未修改主机名称时遇到的错误,修改主机名称就行了
nodeRegistration.name: Invalid value: "vm_0_4_centos": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*') To see the stack trace of this error execute with --v=5 or higher
然后使用kubectl命令,执行以下命令
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config kubectl get nodes
kubectl get nodes后可以看到
3.4部署Kubernetes Master
kubectl apply -f \ https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml OR kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
然后就能看到状态NotReady变成了Ready
Master节点部署结束了。
3.5加入Kubernetes Node
kubeadm token create #新的Token
kubeadm token create --ttl 0 #永久Token kubeadm token list #查看Token openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' #获取ca证书sha256编码hash值 kubeadm join 172.17.0.4:6443 --token gc0acd.5pfa3040fwdtoar9 --discovery-token-ca-cert-hash \ sha256:b55ab7a4520fcd3072e5a0bce72aaf2f71802ff32d242e1c1587e4db1630220b #加入命令
遇到错误:
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
解决方法:
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
遇到错误:
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1
解决方法
echo "1" > /proc/sys/net/ipv4/ip_forward
遇到错误:
https://172.17.0.4:6443/api/v1/namespaces/kube-public/configmaps/cluster-info: dial tcp 172.17.0.4:6443: connect: no route to host]
这是由于我的内网ip和docker0IP发生重复导致无法找到主机,解决方法:
vim /etc/docker/daemon.json
{
"bip":"172.31.0.1/16" }
//"registry-mirrors": ["https://d8b3zdiw.mirror.aliyuncs.com"]https://docker.mirrors.ustc.edu.cn
[root@k8s-node1 ~]# sudo systemctl daemon-reload
[root@k8s-node1 ~]# sudo systemctl restart docker
如果看到两个Node状态不是Ready,那么可能需要检查哪些Pod没有正常运行:
kubectl get pod --all-namespaces
4部署ASP.NET Core WebAPI到K8S
4.1创建deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: k8s-demo namespace: aspnetcore labels: name: k8s-demo spec: replicas: 2 selector: matchLabels: name: k8s-demo template: metadata: labels: name: k8s-demo spec: containers: - name: k8s-demo image: edisonsaonian/k8s-demo ports: - containerPort: 80 imagePullPolicy: Always --- kind: Service apiVersion: v1 metadata: name: k8s-demo namespace: aspnetcore spec: type: NodePort ports: - port: 80 targetPort: 80 selector: name: k8s-demo
需要注意的是,这里我们提前为要部署的ASP.NET Core WebAPI项目创建了一个namespace,叫做aspnetcore,因此这里写的namespace : aspnetcore。
kubectl create namespace aspnetcore
YAML文件各个节点的解释,可以通过下面这个命令去了解:
kubectl explain deployment.metadata
4.2通过Kubectl部署到K8S
kubectl create -f deployment.yaml
查看验证
kubectl get pods,svc -n aspnetcore
kubectl get pods --all-namespaces -o wide #查看分配节点
参考
<<kubeadm init 后master一直处于notready状态>>