用K3s快速搭建單機容器環境


K3s,一個輕量的K8s。

不多廢話,想詳細了解的話,官網地址:https://www.rancher.cn/k3s/

安裝

准備一台干凈的CentOS 7機器,把如下安裝腳本保存為k3s.sh:

##k3s.sh
#改國內yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#更新內核&軟件
yum update -y
#######K3s默認將使用containerd作為容器環境,如果想使用docker,請取消下列注釋。docker打包的鏡像可以直接導入containerd,containerd也能從docker鏡像倉庫拉鏡像。
#####安裝docker-ce
#yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
#yum install -y yum-utils
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#yum install -y docker-ce docker-ce-cli containerd.io
#systemctl enable docker
#systemctl start docker
#修改docker源
#cat << EOF > /etc/docker/daemon.json
#{
# "registry-mirrors":["https://3laho3y3.mirror.aliyuncs.com"]
#}
#EOF
#systemctl daemon-reload
#systemctl restart docker
#####安裝docker結束
#關firewalld防火牆
systemctl stop firewalld
systemctl disable firewalld
#安裝k3s,如果啟用docker作為容器環境,請使用注釋掉的那行替代這一行
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
#curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - --docker

shell執行

sh k8s.sh

安裝完成后,可以執行k3s check-config檢查主機是否滿足條件。若不滿足可調整內核參數后再執行。(根據情況問度娘)

如此簡單,k3s環境就部署好了。可以直接使用kubectl命令查看狀態。(實際上是k3s kubectl的別名)

測試

跑個測試的應用:

將下面內容保存為deploy.yaml

##deploy.yaml
#deployment
kind: Deployment
apiVersion: apps/v1
metadata:
  name: test
  namespace: default
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
        - name: test
          image: nginx:alpine
          ports:
            - name: tcp-80
              containerPort: 80
              protocol: TCP
          resources: {}
          imagePullPolicy: IfNotPresent
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  progressDeadlineSeconds: 600
---
#service
kind: Service
apiVersion: v1
metadata:
  name: test
  namespace: default
  labels:
    app: test
spec:
  ports:
    - name: http-80
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30010
  selector:
    app: test
  type: NodePort

shell執行

kubectl apply -f deploy.yaml

稍等片刻,使用你的機器,訪問http://{ip}:30010/

很好,看到nginx的歡迎頁面了。😊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM