侧边栏壁纸
  • 累计撰写 23 篇文章
  • 累计创建 7 个标签
  • 累计收到 0 条评论
标签搜索

目 录CONTENT

文章目录

搭载blog之安装k3s(一)

爱喝酸梅汤的小猫咪
2023-05-30 / 0 评论 / 0 点赞 / 590 阅读 / 922 字

搭载blog之安装k3s(一)

要求:单点安装搭载k3s,并用k3s安装nginx

1.安装k3s

介绍:轻量级的 Kubernetes 发行版,内核机制还是和 k8s 一样,安装简单,占用资源少,只需要512M内存就可以运行

  • 官网安装
$ curl -sfL https://get.k3s.io | sh -
$ curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_KUBECONFIG_MODE="644" sh -
  • 查验成功
$ kubectl get nodes

16697104591828

  • 补全命令
$ ku    

如果失败了,需要安装bash-completion项目

$ apt-get update && apt-get install -y bash-completion

再次执行source <(kubectl completion bash)
尝试成功

# kubectl + tab键,如果没用可以多按几次tab
root@JamesWuService:~# kubectl 
alpha          auth           cordon         diff           get            patch          run            version
annotate       autoscale      cp             drain          help           plugin         scale          wait
api-resources  certificate    create         edit           kustomize      port-forward   set            
api-versions   cluster-info   debug          exec           label          proxy          taint          
apply          completion     delete         explain        logs           replace        top            
attach         config         describe       expose         options        rollout        uncordon     
  • 设置别名
    查看/usr/local/bin 命令

16697756756765

可以让永久生效,比如将

1.打开文件
$ vi .bashrc
2.编辑添加
alias kaf='kubectl apply -f'
alias kgn='kubectl get namespace'
alias kgp='kubectl get pods -o wide'
alias kgs='kubectl get -o wide svc'

3.保存
$ source .bashrc
2.安装nginx

安装nginx,并通过外网ip和域名访问

  • 内网部署nginx
    创建namespace,deployment和service
#创建namesspace
apiVersion: v1 
kind: Namespace  
metadata:
  name: ns-test  
  labels:
    name: label-test  

---
#创建pod/deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ns-test
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

---
#创建service
apiVersion: v1
kind: Service
metadata:
  namespace: ns-test
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

查看部署情况

$ kgp -n ns-test
NAME                               READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
nginx-deployment-965685897-7zb2s   1/1     Running   0          51m   10.42.0.9    jameswuservice   <none>           <none>
nginx-deployment-965685897-cm4sd   1/1     Running   0          51m   10.42.0.10   jameswuservice   <none>           <none>

curl 10.42.0.10 或者 curl 10.42.0.9
根据ip 可访问 curl ip 可看到hello nginx html

  • 设置node端口,外网访问
    修改service
apiVersion: v1
kind: Service
metadata:
  namespace: ns-test
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 32000

执行更新

$ kaf nginx-yaml.yaml

通过外网ip访问 http://{{外网ip}}:32000

  • 配置域名访问
    创建nginx-ingress.yaml
创建 nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-nginx
  namespace: ns-test
spec:
  rules:
  - host: "jamesyt.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: nginx-service #和上面的service 名字必须对应
            port:
              number: 80
  - host: "www.jamesyt.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: nginx-service #和上面的service 名字必须对应
            port:
              number: 80

访问域名:http://jamesyt.com/

16697884293096

3.卸载k3s

在卸载,namespace 出现Terminating 状态

原因是有service报错
kubectl get apiservice
删除掉false的service
kubectl delete apiservice xxx
或者进入namespace 进行编写
kubectl edit ns xxx

卸载k3s

$ /usr/local/bin/k3s-uninstall.sh
4.使用阿里安装k3s
  • 安装
curl –sfL \
     https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | \
     INSTALL_K3S_MIRROR=cn K3S_KUBECONFIG_MODE=644 sh -s - \
     --system-default-registry "registry.cn-hangzhou.aliyuncs.com" \
     --write-kubeconfig ~/.kube/config \
     --write-kubeconfig-mode 666 \
     --disable traefik
  • 配置 containerd 的 mirror
cat > /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  docker.io:
    endpoint:
      - "http://hub-mirror.c.163.com"
      - "https://docker.mirrors.ustc.edu.cn"
      - "https://registry.docker-cn.com"
EOF

systemctl restart k3s
  • 报错1
    Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get “http://localhost:8080/version”: dial tcp 127.0.0.1:8080: connect: connection refused

解决方法
编辑/etc/profile

vim /etc/profile
追加内容:

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
刷新配置

source /etc/profile

  • 常用的别名
    直接编写
# vim ~/.bash_aliases
alias k='kubectl'
alias kg='kubectl get'
alias kgp='kubectl get pods'
alias kaf='kubectl apply -f'
alias kd='kubectl delete'
alias kdf='kubectl delete -f'
alias kdes='kubectl describe'

0
  • 0

评论区