K8S系列-通过YAML文件/命令创建一个Nginx服务(pod)

36 篇文章 2 订阅
5 篇文章 0 订阅

开始

在完成K8S集群部署之后,我们可以小试牛刀,先创建一个简单应用进行测试

通过YAML创建

K8S支持创建YAML文件,我们可以通过K8S自动创建的YAML内容进行编辑,创建YAML内容方法如下:

kubectl run myapp --image=nginx --dry-run -o yaml

执行结果:

root@debian:~# kubectl run myapp --image=nginx --dry-run -o yaml
W0609 17:30:23.266011   62036 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: myapp
  name: myapp
spec:
  containers:
  - image: nginx
    name: myapp
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

然后把反馈的内容复制到一个yaml文件进行修改即可,例如:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

将修改完的内容输入到一个文件之后,使用apply -f进行一个应用即可创建,过程如下:

root@master:~# cat nginx.yaml 
apiVersion: v1
kind: Pod
metadata:
        name: nginx
        labels:
                app: nginx
spec:
        containers:
        - name: nginx
          image: nginx
          ports:
          - containerPort: 80
root@master:~# kubectl get pod
No resources found in default namespace.
root@master:~# kubectl apply -f nginx.yaml 
pod/nginx created
root@master:~# kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          2s
root@master:~# kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          10s
root@master:~# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          28s
root@master:~# 

可以看到,此时这个Nginx的Pod就起来了

通过终端命令创建

同样的,kubectl也提供了类似docker run的一个运行方法,例如:

kubectl run --image=nginx:alpine nginx-app --port=80

执行效果

root@master:~# kubectl run --image=nginx nginx-app --port=80
pod/nginx-app created
root@master:~# kubectl get pod
NAME        READY   STATUS              RESTARTS   AGE
nginx-app   0/1     ContainerCreating   0          8s
root@master:~# kubectl get pod
NAME        READY   STATUS              RESTARTS   AGE
nginx-app   0/1     ContainerCreating   0          14s
root@master:~# kubectl logs nginx-app 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/06/17 01:20:21 [notice] 1#1: using the "epoll" event method
2021/06/17 01:20:21 [notice] 1#1: nginx/1.21.0
2021/06/17 01:20:21 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/06/17 01:20:21 [notice] 1#1: OS: Linux 5.4.0-74-generic
2021/06/17 01:20:21 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/06/17 01:20:21 [notice] 1#1: start worker processes
2021/06/17 01:20:21 [notice] 1#1: start worker process 33
2021/06/17 01:20:21 [notice] 1#1: start worker process 34
2021/06/17 01:20:21 [notice] 1#1: start worker process 35
2021/06/17 01:20:21 [notice] 1#1: start worker process 36
root@master:~# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
nginx-app   1/1     Running   0          48s
root@master:~# 
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐公交也用券

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值