Setup a Service - hqzhang/cloudtestbed GitHub Wiki

Centos Service

Location
/usr/lib/systemd/system/ | Unit files distributed with installed packages. Do not modify unit files in this location.
-- | --
/run/systemd/system/ | Unit files that are dynamically created at runtime. Changes in this directory are lost when rebooted.
/etc/systemd/system/ | Unit files created by systemctl enable and custome unit files created by system administrators.

Jenkins.service
[Unit]
Description=Jenkins Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/java -jar /usr/local/bin/jenkins.war
Restart=on-abort

[Install]
WantedBy=multi-user.target


K8s Data Model

There are three datamodel of k8s, we can use loki as example to know how to write it.
STATEFULSET ---loki
-----
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  annotations:
    openshift.io/host.generated: "true"
  labels:
    app: loki
  name: loki
  namespace: loki
spec:
  host: loki-loki.logs-dev-us-south-cluster-0334be1e13fdeb7b0e1e56eb02181f1f-0000.us-south.containers.appdomain.cloud
  port:
    targetPort: "3100"
  tls:
    termination: edge
  to:
    kind: Service
    name: loki
    weight: 100
  wildcardPolicy: None`
-----
apiVersion: v1
kind: Service
metadata:
  name: loki
  namespace: loki
  labels:
    app: loki
spec:
  type: ClusterIP
  ports:
    - port: 3100
      protocol: TCP
      name: http-metrics
      targetPort: http-metrics
  selector:
    app: loki
    release: loki
------
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: loki
  namespace: loki
  labels:
    app: loki
spec:
  podManagementPolicy: OrderedReady
  replicas: 2
  selector:
    matchLabels:
      app: loki
      release: loki
  serviceName: loki-headless
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: loki
        name: loki
        release: loki
      annotations:
        checksum/config: 1737e51aa17404a03935f4b8851b8688332b6fa460b7f2a579fd58f98475a03a
        prometheus.io/port: http-metrics
        prometheus.io/scrape: "true"
    spec:
      serviceAccountName: loki
      securityContext: null
      initContainers: []
      containers:
        - name: loki
          image: "grafana/loki:2.4.2"
          imagePullPolicy: IfNotPresent
          args:
            - "-config.file=/etc/loki/loki.yaml"
          volumeMounts:
            - name: config
              mountPath: /etc/loki
            - name: storage
              mountPath: "/data"
              subPath:
          ports:
            - name: http-metrics
              containerPort: 3100
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /ready
              port: http-metrics
            initialDelaySeconds: 45
          readinessProbe:
            httpGet:
              path: /ready
              port: http-metrics
            initialDelaySeconds: 45
          resources: {}
          securityContext:
            readOnlyRootFilesystem: true
          env: 
      nodeSelector: {}
      affinity: {}
      tolerations: []

How to setup service in Ubuntu

#config a service


#restart a service
systemctl restart jenkins-lts
service jenkins-lts restart

#How to setup service in MacOS

#config a service


#restart a service
launchctl load /usr/local/Cellar/jenkins-lts/2.235.1/homebrew.mxcl.jenkins-lts.plist 
brew services restart jenkins-lts