技术流ken

运维拯救世界

课堂记录:k8s控制器job、cj及service讲解(三)–技术流ken

控制器讲解job

 

第一步:编写job的yml文件

apiVersion: batch/v1
kind: Job
metadata:
  name: job
spec:
  template:
    spec:
      containers:
      - name: job
        image: busybox
        command: ["echo","Hello World"]
        imagePullPolicy: IfNotPresent
      restartPolicy: Never

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f job.yml 

 

第三步:查看job

[root@ken1 ~]# kubectl get job
NAME   COMPLETIONS   DURATION   AGE
job    1/1           2s         22s

 

第四步:查看日志输出

[root@ken1 ~]# kubectl logs job-twctr 
Hello World

 

 

演示:如果job失败会发生什么?

会发现会自动生成很多的pod

为什么会出现这样的情况那?

因为在job中期待完成数是1.但是因为job内部有错误,无法完成相应的任务,而且重启策略是Never,表示容器从来不会重启,所以系统为了完成工作,就会一直开启新的容器,试图去完成自己的工作。

 

 

如果重启策略为OnFailure的话,pod会一直重启!

 

 

注意:

restartPolicy 表示pod重启策略

 

Always: 这个是默认值,pod退出就会自动重启

Never: 这个表示容器退出不会重启

OnFailure: 表示容器再失败退出的时候才会重启,容器正常退出不会重启

 

 

imagePullPolicy 表示镜像拉取的策略

 

Always: 这个是默认值,表示每次执行yml文件都会去拉取dockerhub中的镜像

Never: 总是不拉取镜像

IfNotPresent: 本地不存在才会去拉取镜像

 

 

cronjob演示

 

第一步:编写cj的yml文件

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cj
spec:
  schedule: "* * * * *"
  jobTemplate:
    metadata:
     name: job
    spec:
      template:
        metadata:
            name: pod
        spec:
          containers:
          - name: busybox
            image: busybox
            command: ["echo","hello"]
           # args:
           #  - /bin/sh
           #  - c
           #  - echo "hello wordld"
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f cj.yml 

 

第三步:查看计划任务

[root@ken1 ~]# kubectl get cj
NAME   SCHEDULE    SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cj     * * * * *   False     0        24s             33s

 

第四步:查看job

[root@ken1 ~]# kubectl get job
NAME            COMPLETIONS   DURATION   AGE
cj-1566353280   1/1           3s         39s

 

第五步:查看pod

[root@ken1 ~]# kubectl get po
NAME                  READY   STATUS      RESTARTS   AGE
cj-1566353280-m7gpj   0/1     Completed   0          79s
cj-1566353340-j6fc4   0/1     Completed   0          19s

 

第六步:查看pod日志输出

[root@ken1 ~]# kubectl logs cj-1566353340-j6fc4
hello

 

 

第二版:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cj
spec:
  schedule: "* * * * *"
  jobTemplate:
    metadata:
     name: job
    spec:
      template:
        metadata:
            name: pod
        spec:
          restartPolicy: OnFailure
          containers:
          - name: busybox
            imagePullPolicy: IfNotPresent
            image: busybox
           # command: ["echo","hello"]
            args:
             - /bin/sh
             - c
             - echo "hello wordld"

 

 

 

演示service

 

第一步:编写部署nginx的yml文件

[root@ken1 ~]# cat nginx-svc.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: OnFailure
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-svc.yml 

 

第三步:查看标签

[root@ken1 ~]# kubectl get po --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-5c6666d499-hpg65   1/1     Running   0          37s   pod-template-hash=5c6666d499,run=nginx
nginx-5c6666d499-s9txs   1/1     Running   0          37s   pod-template-hash=5c6666d499,run=nginx

 

 

第四步:外部访问nginx服务

无法访问!

 

编写svc

 

第一步:编写svc的yaml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:运行svcyml文件

[root@ken1 ~]# kubectl apply -f svc.yml

 

第三步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   5d17h
my-svc       ClusterIP   10.106.124.174   <none>        80/TCP    19s

 

第四步:查看svc详细信息

[root@ken1 ~]# kubectl describe svc my-svc
Name:              my-svc
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"my-svc","namespace":"default"},"spec":{"ports":[{"port":80,"targe...
Selector:          run=nginx
Type:              ClusterIP
IP:                10.106.124.174
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.13:80,10.244.2.79:80
Session Affinity:  None
Events:            <none>

 

 

补充:在deploy当中的pod中暴露端口

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80

 

 

 

yml文件合并

 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

 

DNS访问

 

第一步:启动busyboxpod

[root@ken1 ~]# kubectl run busybox -it --rm --image=busybox /bin/sh

 

第二步:通过名称访问svc

/ # wget my-svc:80
Connecting to my-svc:80 (10.99.81.124:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved
/ # cat index.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body 

 

 

注意:

1.通过名称访问,只能是在同一名称空间的!

2.如果不是在同一名称空间,需要指定namespace

 

 

第一步:修改yml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
  namespace: kube-system
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-svc
  namespace: kube-system
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:查看

[root@ken1 ~]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   5d17h
my-svc     ClusterIP   10.100.60.114   <none>        80/TCP                   2m6s

 

第三步:访问

[root@ken1 ~]# kubectl run busybox -it --rm --image=busybox /bin/sh
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.
/ # wget my-svc:80
wget: bad address 'my-svc:80'

 

第四步:指定名称空间进行访问

/ # wget my-svc.kube-system:80
Connecting to my-svc.kube-system:80 (10.100.60.114:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved

 

第五步:解决办法也可以在kube-system名称空间进行运行busybox

[root@ken1 ~]# kubectl run busybox1 -it --rm --image=busybox -n kube-system /bin/sh 
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.
/ # wget my-svc:80
Connecting to my-svc:80 (10.100.60.114:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved

 

 

外网如何访问pod?

 

 

外部如何访问service?

 

  1. clusterIP
  2. NodePort
  3. LoadBalance
  4. ExternalName

 

第一步:编写svc的yml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  type: NodePort
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-svc.yml 

 

第三步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d17h
my-svc       NodePort    10.96.194.243   <none>        80:32247/TCP   4s

 

第四步:查看宿主机端口

[root@ken1 ~]# ss -tnl | grep 32247
LISTEN     0      128         :::32247                   :::*                  

 

第五步:外部访问

可以访问!

 

固定宿主机端口

端口需要在30000-32767

第一步:修改svc的yml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  type: NodePort
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port
     nodePort: 30007

 

第二步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        5d17h
my-svc       NodePort    10.107.106.116   <none>        80:30007/TCP   43s

 

第三步:外部访问

http://192.168.64.11:30007/

 

几个端口表示的含义

port: 表示svc的端口

targetPort: pod的端口

nodePort: 需要映射到宿主机的端口

 

 

发表评论

电子邮件地址不会被公开。