部署k8s集群

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 部署k8s集群

三台:
cd /etc/yum.repos.d
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

cat << EOF >> /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl -p

cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubelet kubeadm kubectl

systemctl enable kubelet

master:
cd
kubeadm config print init-defaults > init-config.yaml

vim init-config.yaml
修改以下内容:
第12行:192.168.1.10
第32行:registry.aliyuncs.com/google_containers
第37行:192.168.2.0/24

scp /root/init-config.yaml 192.168.1.11:/root/
scp /root/init-config.yaml 192.168.1.12:/root/

阿里云加速docker拉取镜像(否则下载镜像时,会很慢会卡顿):

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://aq63ygn3.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

kubeadm config images list --config init-config.yaml

kubeadm config images pull --config=init-config.yaml

kubeadm init --config=init-config.yaml

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

node1和node2复制上面显示的内容并运行:
例如:
kubeadm join 192.168.1.10:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash xxx4

可能遇到的错误:
ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables 设置错误导致kubeadm安装k8s失败
echo "1">/proc/sys/net/bridge/bridge-nf-call-iptables
echo "1">/proc/sys/net/bridge/bridge-nf-call-ip6tables

部署 Calico 网络插件
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl get pods -n kube-system

删除步骤:

删除K8s对象

kubectl delete -f calico.yaml
检查所有节点上的网络,看看是否存在Tunl0

ip addr show
如果有Tunl0,将其删除

modprobe -r ipip
移除Calico配置文件

ls /etc/cni/net.d/
看看是否存在Calico相关的文件和目录,如:10-calico.conflist, calico-kubeconfig, calico-tls,如果有将其移除。

这时候整个Calico移除成功。

执行以下命令使确认所有正在运行 Pod 与 Node
kubectl get nodes

kubectl get pod --all-namespaces

创建服务
kubectl create ns policy-demo

kubectl run --namespace=policy-demo nginx --replicas=2 --image=nginx

vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: policy-demo
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:

    - name: nginx
      image: nginx
      ports: 
      - containerPort: 80

kubectl apply -f nginx-deployment.yaml

kubectl expose --namespace=policy-demo deployment nginx --port=80

kubectl get all -n policy-demo

kubectl run --namespace=policy-demo access --rm -ti --image busybox
/bin/sh

wget -q nginx -O -
<!DOCTYPE html>







Welcome to nginx!


If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.


For online documentation and support please refer to
nginx.org.

Commercial support is available at
nginx.com.



Thank you for using nginx.



kubectl create ns adv-policy-demo

vim nginx-dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: adv-policy-demo
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:

    - name: nginx
      image: nginx
      ports: 
      - containerPort: 80

kubectl apply -f nginx-dep.yaml
kubectl expose --namespace=adv-policy-demo deployment nginx --port=80
kubectl run --namespace=adv-policy-demo access --rm -ti --image busybox
/bin/sh

wget -q --timeout=5 nginx -O -

拒绝所有入口流量:
vim k8s.deny-all-input.yaml
添加内容:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: adv-policy-demo
spec:
podSelector:
matchLabels: {}
policyTypes:

  • Ingress

kubectl create -f k8s.deny-all-input.yaml

kubectl run --namespace=adv-policy-demo access --rm -ti --image busybox
/bin/sh

wget -q --timeout=5 nginx -O -

允许进入nginx的流量
vim k8s.allow-input.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: access-nginx
namespace: adv-policy-demo
spec:
podSelector:
matchLabels:
app: nginx
ingress:

- from:
  - podSelector:
      matchLabels: {}

kubectl create -f k8s.allow-input.yaml

kubectl run --namespace=adv-policy-demo access --rm -ti --image busybox
/bin/sh

wget -q --timeout=5 nginx -O -

K8集群重新初始化:

master:
kubeadm reset
kubeadm init --config init-config.yaml --upload-certs

node1和node2(恢复快照重新安装软件):
cd /etc/yum.repo
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

cat << EOF >> /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl -p

cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubelet kubeadm kubectl

systemctl enable kubelet

node1和node2重新复制上面显示的内容并运行:
例如:
kubeadm join 192.168.1.11:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:xxx

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

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
10天前
|
canal Kubernetes Docker
基于Kubernetes v1.25.0和Docker部署高可用集群(03部分)
基于Kubernetes v1.25.0和Docker部署高可用集群(03部分)
|
5天前
|
Kubernetes 安全 数据安全/隐私保护
Kubernetes(K8S) 集群安全机制
Kubernetes(K8S) 集群安全机制
14 2
|
11天前
|
Kubernetes Ubuntu Linux
基于Kubernetes v1.25.0和Docker部署高可用集群(02部分)
基于Kubernetes v1.25.0和Docker部署高可用集群(02部分)
|
5天前
|
Kubernetes 负载均衡 应用服务中间件
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- Ingress-Ngnix
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- Ingress-Ngnix
14 0
|
5天前
|
存储 Kubernetes 调度
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- 持久化存储(NFS网络存储)
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- 持久化存储(NFS网络存储)
18 0
|
5天前
|
存储 Kubernetes API
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- 持久化存储(PV&PVC)
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- 持久化存储(PV&PVC)
12 0
|
5天前
|
Kubernetes Java Docker
Kubernetes(K8S) Deployment 拉取阿里云镜像部署
Kubernetes(K8S) Deployment 拉取阿里云镜像部署
13 0
|
5天前
|
Kubernetes Docker 容器
使用 Kubeadm 部署 Kubernetes(K8S) 安装--附K8S架构图
使用 Kubeadm 部署 Kubernetes(K8S) 安装--附K8S架构图
45 0
|
5天前
|
Kubernetes 应用服务中间件 API
Ingress:K8S集群进出流量的总管
【8月更文挑战第14天】创建Ingress时可通过`kubectl create`命令指定所属的Ingress Class及路由规则。部署Ingress Controller时需定制配置文件以适应特定环境需求。
|
Kubernetes 开发者 微服务
简化Kubernetes应用部署工具-Helm之Hook
微服务和容器化给复杂应用部署与管理带来了极大的挑战。Helm是目前Kubernetes服务编排领域的唯一开源子项目,做为Kubernetes应用的一个包管理工具,可理解为Kubernetes的apt-get / yum,由Deis 公司发起,该公司已经被微软收购。
1606 0