k3s service building

Posted by dymon on Sat, 21 Mar 2020 15:32:51 +0100

There is a server at home, usually used as its own development environment, running some small programs. It is planned to upgrade the server and use k3s to manage common development tools, which is convenient for your own use. Because I only have one server, there is no cluster in this article.

Quick install command

## "-- Docker" uses Docker as a container < defau lt use containerd >
## "-- no deploy tracefik" means that the tracefik service is not used
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--docker --no-deploy traefik" sh -

Please refer to the following method to shut down the traifik service after startup. After that, we will use ingress as the direction agent service.

Turn off the default traifik service

## Enter k3s default startup orchestration directory
cd /var/lib/rancher/k3s/server/manifests

## Rename the traefik choreography to prevent restart after shutdown
mv traefik.yaml traefik.yaml_back

## View the running status of reverse agent service (traifik)
kubectl get svc traefik --namespace kube-system -w

kubectl describe svc traefik --namespace kube-system | grep Ingress | awk '{print $3}'

//Prompt: it indicates that there is no traifik service in the current cluster, otherwise it needs to be deleted manually
Error from server (NotFound): services "traefik" not found

## Delete traifik manually
kubectl delete -f traefik.yaml

## Modify the configuration file of K3S service
vim /etc/systemd/system/multi-user.target.wants/k3s.service

## Modify the value of ExecStart to:
ExecStart=/usr/local/bin/k3s server --docker --no-deploy traefik

View startup node

### sudo kubectl get nodes

root@MiWiFi-R1CM-srv:~# sudo kubectl get nodes
NAME              STATUS   ROLES    AGE    VERSION
miwifi-r1cm-srv   Ready    master   3d7h   v1.17.3+k3s1

View basic service startup status

### kubectl -n kube-system get pods
root@MiWiFi-R1CM-srv:~# kubectl -n kube-system get pods
NAME                                      READY   STATUS    RESTARTS   AGE
local-path-provisioner-58fb86bdfd-l8msw   1/1     Running   2          3d7h
metrics-server-6d684c7b5-k8r9n            1/1     Running   1          3d7h
coredns-d798c9dd-67mmc                    1/1     Running   1          3d7h

Uninstall k3s

## Necessary for initial installation
sh /usr/local/bin/k3s-uninstall.sh

Install ingress (reverse agent)


You need to install the ingress controller first. When the service is started, it will automatically listen to port 80 and port 443.

Then start tomcat

apiVersion: apps/v1
kind: Deployment
  namespace: kube-public
  name: tomcat-service
    app: tomcat-service
  replicas: 1
      app: tomcat-service
        app: tomcat-service
        - name: tomcat-service
          image: tomcat
            - containerPort: 8080
apiVersion: v1
kind: Service
  namespace: kube-public
  name: tomcat-service
    - port: 8080
      protocol: TCP
    app: tomcat-service

Add reverse proxy:

apiVersion: extensions/v1beta1
kind: Ingress
  namespace: kube-public
  name: ingress-nginx
    - host: a1.free-js.com
          - backend:
              serviceName: tomcat-service
              servicePort: 8080

View service startup

kubectl get svc --all-namespaces

NAMESPACE       NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
default         kubernetes       ClusterIP       <none>        443/TCP                      3d9h
kube-system     kube-dns         ClusterIP      <none>        53/UDP,53/TCP,9153/TCP       3d9h
kube-system     metrics-server   ClusterIP    <none>        443/TCP                      3d9h
ingress-nginx   ingress-nginx    NodePort   <none>        80:30729/TCP,443:30338/TCP   4h47m
kube-public     tomcat-service   ClusterIP   <none>        8080/TCP                     169m

Modify the local hosts file to add domain name resolution. Convert address to server physical IP

## Test whether the reverse agent is successful. If a message "tomat" is prompted, it indicates that the agent is correct.
curl http://a1.free-js.com

So far, k3s has been started normally, and some commonly used services will be arranged later.

Topics: Operation & Maintenance Tomcat Docker Nginx curl